Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: oracle query #73

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.21-alpine AS go-builder
#ARG arch=x86_64

# See https://github.com/initia-labs/initiavm/releases
ENV LIBINITIAVM_VERSION=v0.2.0-beta.1
ENV LIBINITIAVM_VERSION=v0.2.0-beta.2

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
Expand Down
5 changes: 3 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,16 @@ func NewInitiaApp(
appCodec,
runtime.NewKVStoreService(keys[movetypes.StoreKey]),
app.AccountKeeper,
app.DistrKeeper,
app.BankKeeper,
app.OracleKeeper,
// app.NftTransferKeeper,
app.BaseApp.MsgServiceRouter(),
moveConfig,
// staking feature
app.BankKeeper,
app.DistrKeeper,
app.StakingKeeper,
app.RewardKeeper,
app.DistrKeeper,
authtypes.FeeCollectorName,
authorityAddr,
ac, vc,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/hashicorp/go-metrics v0.5.2
github.com/initia-labs/OPinit v0.2.0-beta.1
// we also need to update `LIBINITIAVM_VERSION` of images/private/Dockerfile#5
github.com/initia-labs/initiavm v0.2.0-beta.1
github.com/initia-labs/initiavm v0.2.0-beta.2
github.com/novifinancial/serde-reflection/serde-generate/runtime/golang v0.0.0-20220519162058-e5cd3c3b3f3a
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -73,6 +73,7 @@ require (
github.com/99designs/keyring v1.2.1 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/aptos-labs/serde-reflection/serde-generate/runtime/golang v0.0.0-20231213012317-73b6bbf74833 // indirect
github.com/aws/aws-sdk-go v1.44.224 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/aptos-labs/serde-reflection/serde-generate/runtime/golang v0.0.0-20231213012317-73b6bbf74833 h1:/OtHsUoaVegyyGQ9/Ycm5vrC3xTTyj4+N/HsJ0wwAGA=
github.com/aptos-labs/serde-reflection/serde-generate/runtime/golang v0.0.0-20231213012317-73b6bbf74833/go.mod h1:zUz5maIUAS+tl1lC+lJR7HUf0vVnHk6W7bCvN+DzjME=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -723,8 +725,8 @@ github.com/initia-labs/OPinit v0.2.0-beta.1 h1:98LhxGZ3ZOcqziSGJgcasq6Wh4tp1KqHk
github.com/initia-labs/OPinit v0.2.0-beta.1/go.mod h1:1dn1iseG1S4wfXUtnb4mUkraJdL9/lWBQPpI+HbVzL0=
github.com/initia-labs/cosmos-sdk v0.0.0-20240123082052-77e8b246064a h1:wD9ynlE7z9u3YxP/D9fEvbQ1CCD+ojqb6o+zrMdr2gY=
github.com/initia-labs/cosmos-sdk v0.0.0-20240123082052-77e8b246064a/go.mod h1:tlrkY1sntOt1q0OX/rqF0zRJtmXNoffAS6VFTcky+w8=
github.com/initia-labs/initiavm v0.2.0-beta.1 h1:IEJrJu38qXSdcoWNqaj9BiXos/WswnjzmJDxnK7snI8=
github.com/initia-labs/initiavm v0.2.0-beta.1/go.mod h1:6LNXn8eRv2jBzvM1gyQMgA1GRzZJh65VGVKCYDtbh18=
github.com/initia-labs/initiavm v0.2.0-beta.2 h1:aSUZok4fQAWVJF9RA5mkF2mrdZq1gtqDllCHgBPjV58=
github.com/initia-labs/initiavm v0.2.0-beta.2/go.mod h1:KTdC3W2wgeOiL0YYIu6MEvK4j8envSpLjH14sMKMD6k=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
2 changes: 1 addition & 1 deletion images/private/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.21-alpine AS go-builder
#ARG arch=x86_64

# See https://github.com/initia-labs/initiavm/releases
ARG LIBINITIAVM_VERSION=v0.2.0-beta.1
ARG LIBINITIAVM_VERSION=v0.2.0-beta.2

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
Expand Down
21 changes: 18 additions & 3 deletions x/bank/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ import (
rewardkeeper "github.com/initia-labs/initia/x/reward/keeper"
rewardtypes "github.com/initia-labs/initia/x/reward/types"
"github.com/initia-labs/initia/x/slashing"

"github.com/initia-labs/initiavm/precompile"

"github.com/skip-mev/slinky/x/oracle"
oraclekeeper "github.com/skip-mev/slinky/x/oracle/keeper"
oracletypes "github.com/skip-mev/slinky/x/oracle/types"
)

var ModuleBasics = module.NewBasicManager(
Expand All @@ -76,6 +81,7 @@ var ModuleBasics = module.NewBasicManager(
gov.AppModuleBasic{},
slashing.AppModuleBasic{},
move.AppModuleBasic{},
oracle.AppModuleBasic{},
)

// Bond denom should be set for staking test
Expand Down Expand Up @@ -203,6 +209,7 @@ type TestKeepers struct {
BankKeeper bankkeeper.Keeper
GovKeeper govkeeper.Keeper
MoveKeeper movekeeper.Keeper
OracleKeeper oraclekeeper.Keeper
EncodingConfig initiaappparams.EncodingConfig
Faucet *TestFaucet
MultiStore storetypes.CommitMultiStore
Expand Down Expand Up @@ -244,7 +251,7 @@ func _createTestInput(
keys := storetypes.NewKVStoreKeys(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
rewardtypes.StoreKey, distributiontypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, movetypes.StoreKey,
govtypes.StoreKey, movetypes.StoreKey, oracletypes.StoreKey,
)
ms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
for _, v := range keys {
Expand Down Expand Up @@ -353,17 +360,24 @@ func _createTestInput(

accountKeeper.GetModuleAccount(ctx, movetypes.MoveStakingModuleName)

oracleKeeper := oraclekeeper.NewKeeper(
runtime.NewKVStoreService(keys[oracletypes.StoreKey]),
appCodec,
authtypes.NewModuleAddress(govtypes.ModuleName),
)

*moveKeeper = *movekeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[movetypes.StoreKey]),
accountKeeper,
distKeeper,
bankKeeper,
oracleKeeper,
nil,
moveConfig,
bankKeeper,
distKeeper,
stakingKeeper,
rewardKeeper,
distKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
ac, vc,
Expand Down Expand Up @@ -415,6 +429,7 @@ func _createTestInput(
MoveKeeper: *moveKeeper,
BankKeeper: bankKeeper,
GovKeeper: *govKeeper,
OracleKeeper: oracleKeeper,
EncodingConfig: encodingConfig,
Faucet: faucet,
MultiStore: ms,
Expand Down
7 changes: 4 additions & 3 deletions x/distribution/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,14 @@ func _createTestInput(
appCodec,
runtime.NewKVStoreService(keys[movetypes.StoreKey]),
accountKeeper,
distKeeper,
nil,
moveConfig,
bankKeeper,
nil, // oracle keeper unused
nil, // msg router unused
moveConfig,
distKeeper,
stakingKeeper,
rewardKeeper,
distKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
ac, vc,
Expand Down
7 changes: 4 additions & 3 deletions x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,14 @@ func _createTestInput(
appCodec,
runtime.NewKVStoreService(keys[movetypes.StoreKey]),
accountKeeper,
distKeeper,
nil,
moveConfig,
bankKeeper,
nil, // oracle keeper unused
nil, // msg router unused
moveConfig,
distKeeper,
stakingKeeper,
rewardKeeper,
distKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
ac, vc,
Expand Down
42 changes: 42 additions & 0 deletions x/move/keeper/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package keeper

import (
"bytes"
"context"
"errors"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -10,6 +12,8 @@
"github.com/initia-labs/initia/x/move/types"
vmapi "github.com/initia-labs/initiavm/api"
vmtypes "github.com/initia-labs/initiavm/types"

oracletypes "github.com/skip-mev/slinky/x/oracle/types"
)

var _ vmapi.GoAPI = &GoApi{}
Expand Down Expand Up @@ -93,3 +97,41 @@
sdkCtx := sdk.UnwrapSDKContext(api.ctx)
return uint64(sdkCtx.BlockTime().Unix()) + uint64(unbondingTime)
}

func (api GoApi) GetPrice(pairId string) ([]byte, uint64, uint64, error) {
cp, err := oracletypes.CurrencyPairFromString(pairId)
if err != nil {
return nil, 0, 0, err
}

Check warning on line 105 in x/move/keeper/api.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/api.go#L104-L105

Added lines #L104 - L105 were not covered by tests

sdkCtx := sdk.UnwrapSDKContext(api.ctx)
price, err := api.oracleKeeper.GetPriceForCurrencyPair(sdkCtx, cp)
if err != nil {
return nil, 0, 0, err
}

Check warning on line 111 in x/move/keeper/api.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/api.go#L110-L111

Added lines #L110 - L111 were not covered by tests

priceBz, err := getLittleEndianBytes(price.Price)
if err != nil {
return nil, 0, 0, err
}

Check warning on line 116 in x/move/keeper/api.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/api.go#L115-L116

Added lines #L115 - L116 were not covered by tests

return priceBz, uint64(price.BlockTimestamp.Unix()), uint64(cp.Decimals()), nil
}

// convert math.Int to little endian bytes
// with u256 size assertion.
func getLittleEndianBytes(num math.Int) ([]byte, error) {
b := num.BigInt().Bytes()
for i := 0; i < len(b)/2; i++ {
b[i], b[len(b)-i-1] = b[len(b)-i-1], b[i]
}

diff := 32 - len(b)
if diff > 0 {
b = append(b, bytes.Repeat([]byte{0}, diff)...)
} else if diff < 0 {
return nil, errors.New("exceed u256 range")
}

Check warning on line 134 in x/move/keeper/api.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/api.go#L133-L134

Added lines #L133 - L134 were not covered by tests

return b, nil
}
35 changes: 35 additions & 0 deletions x/move/keeper/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"fmt"
"testing"
"time"

Expand All @@ -14,6 +15,8 @@ import (
stakingkeeper "github.com/initia-labs/initia/x/mstaking/keeper"

vmtypes "github.com/initia-labs/initiavm/types"

oracletypes "github.com/skip-mev/slinky/x/oracle/types"
)

func Test_GetAccountInfo(t *testing.T) {
Expand Down Expand Up @@ -174,3 +177,35 @@ func Test_UnbondTimestamp(t *testing.T) {
resTimestamp := api.UnbondTimestamp()
require.Equal(t, uint64(now.Unix()+60*60*24*7), resTimestamp)
}

func Test_GetPrice(t *testing.T) {
ctx, input := createDefaultTestInput(t)

pairId := "BITCOIN/USD"
cp, err := oracletypes.CurrencyPairFromString(pairId)
require.NoError(t, err)

price := math.NewInt(111111).MulRaw(1_000_000_000).MulRaw(1_000_000_000).MulRaw(1_000_000_000)
now := time.Now()

err = input.OracleKeeper.SetPriceForCurrencyPair(ctx, cp, oracletypes.QuotePrice{
Price: price,
BlockTimestamp: now,
BlockHeight: 100,
})
require.NoError(t, err)

pairIdArg, err := vmtypes.SerializeString(pairId)
require.NoError(t, err)

res, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"oracle",
"get_price",
[]vmtypes.TypeTag{},
[][]byte{pairIdArg},
)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("[\"%s\",\"%d\",\"%d\"]", price.String(), now.Unix(), cp.Decimals()), res)
}
20 changes: 17 additions & 3 deletions x/move/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ import (
vmapi "github.com/initia-labs/initiavm/api"
"github.com/initia-labs/initiavm/precompile"
vmtypes "github.com/initia-labs/initiavm/types"

"github.com/skip-mev/slinky/x/oracle"
oraclekeeper "github.com/skip-mev/slinky/x/oracle/keeper"
oracletypes "github.com/skip-mev/slinky/x/oracle/types"
)

var ModuleBasics = module.NewBasicManager(
Expand All @@ -86,6 +90,7 @@ var ModuleBasics = module.NewBasicManager(
gov.AppModuleBasic{},
slashing.AppModuleBasic{},
move.AppModuleBasic{},
oracle.AppModuleBasic{},
)

// Bond denom should be set for staking test
Expand Down Expand Up @@ -223,6 +228,7 @@ type TestKeepers struct {
BankKeeper bankkeeper.Keeper
GovKeeper govkeeper.Keeper
MoveKeeper movekeeper.Keeper
OracleKeeper oraclekeeper.Keeper
EncodingConfig initiaappparams.EncodingConfig
Faucet *TestFaucet
MultiStore storetypes.CommitMultiStore
Expand Down Expand Up @@ -263,7 +269,7 @@ func _createTestInput(
keys := storetypes.NewKVStoreKeys(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
rewardtypes.StoreKey, distributiontypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, authzkeeper.StoreKey, movetypes.StoreKey,
govtypes.StoreKey, authzkeeper.StoreKey, movetypes.StoreKey, oracletypes.StoreKey,
)
ms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
for _, v := range keys {
Expand Down Expand Up @@ -373,17 +379,24 @@ func _createTestInput(

accountKeeper.GetModuleAccount(ctx, movetypes.MoveStakingModuleName)

oracleKeeper := oraclekeeper.NewKeeper(
runtime.NewKVStoreService(keys[oracletypes.StoreKey]),
appCodec,
authtypes.NewModuleAddress(govtypes.ModuleName),
)

*moveKeeper = *movekeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[movetypes.StoreKey]),
accountKeeper,
distKeeper,
bankKeeper,
oracleKeeper,
TestMsgRouter{},
moveConfig,
bankKeeper,
distKeeper,
stakingKeeper,
rewardKeeper,
distKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
ac, vc,
Expand Down Expand Up @@ -444,6 +457,7 @@ func _createTestInput(
MoveKeeper: *moveKeeper,
BankKeeper: bankKeeper,
GovKeeper: *govKeeper,
OracleKeeper: oracleKeeper,
// NftTransferKeeper: nftTransferKeeper,
EncodingConfig: encodingConfig,
Faucet: faucet,
Expand Down
Loading
Loading