Skip to content

Commit

Permalink
Merge pull request DongCoNY#9 from onomyprotocol/nhan/oracle_module
Browse files Browse the repository at this point in the history
[Feature] Add `Oracle module`
  • Loading branch information
vuong177 authored Oct 7, 2024
2 parents 79e9025 + df4c2be commit c86c35d
Show file tree
Hide file tree
Showing 85 changed files with 19,269 additions and 550 deletions.
Binary file added .DS_Store
Binary file not shown.
24 changes: 22 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
ibctestingtypes "github.com/cosmos/ibc-go/v8/testing/types"

oraclemodulekeeper "github.com/onomyprotocol/reserve/x/oracle/keeper"

Expand Down Expand Up @@ -141,6 +142,8 @@ type App struct {
ScopedIBCTransferKeeper capabilitykeeper.ScopedKeeper
ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper
ScopedKeepers map[string]capabilitykeeper.ScopedKeeper
// ScopedOracleKeeper capabilitykeeper.ScopedKeeper

OracleKeeper oraclemodulekeeper.Keeper
VaultsKeeper vaultskeeper.Keeper
Expand Down Expand Up @@ -213,7 +216,7 @@ func New(
baseAppOptions ...func(*baseapp.BaseApp),
) (*App, error) {
var (
app = &App{}
app = &App{ScopedKeepers: make(map[string]capabilitykeeper.ScopedKeeper)}
appBuilder *runtime.AppBuilder

// merge the AppConfig and other configuration in one config
Expand Down Expand Up @@ -438,7 +441,12 @@ func (app *App) GetIBCKeeper() *ibckeeper.Keeper {

// GetCapabilityScopedKeeper returns the capability scoped keeper.
func (app *App) GetCapabilityScopedKeeper(moduleName string) capabilitykeeper.ScopedKeeper {
return app.CapabilityKeeper.ScopeToModule(moduleName)
sk, ok := app.ScopedKeepers[moduleName]
if !ok {
sk = app.CapabilityKeeper.ScopeToModule(moduleName)
app.ScopedKeepers[moduleName] = sk
}
return sk
}

// SimulationManager implements the SimulationApp interface.
Expand All @@ -459,6 +467,18 @@ func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig
docs.RegisterOpenAPIService(Name, apiSvr.Router)
}

func (app *App) GetBaseApp() *baseapp.BaseApp { return app.BaseApp }

func (app *App) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
return app.ScopedIBCKeeper
}

func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper {
return app.StakingKeeper
}

func (app *App) GetTxConfig() client.TxConfig { return app.txConfig }

// GetMaccPerms returns a copy of the module account permissions
//
// NOTE: This is solely to be used for testing purposes.
Expand Down
15 changes: 12 additions & 3 deletions app/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import (
solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"

// this line is used by starport scaffolding # ibc/app/import
oraclemodule "github.com/onomyprotocol/reserve/x/oracle/module"
oracle "github.com/onomyprotocol/reserve/x/oracle"
oraclemoduletypes "github.com/onomyprotocol/reserve/x/oracle/types"
oraclemodule "github.com/onomyprotocol/reserve/x/oracle/module"
)

// registerIBCModules register IBC keepers and non dependency inject modules.
Expand Down Expand Up @@ -93,7 +93,8 @@ func (app *App) registerIBCModules() error {
// by granting the governance module the right to execute the message.
// See: https://docs.cosmos.network/main/modules/gov#proposal-messages
govRouter := govv1beta1.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govv1beta1.ProposalHandler)
govRouter.AddRoute(govtypes.RouterKey, govv1beta1.ProposalHandler).
AddRoute(oraclemoduletypes.RouterKey, oracle.NewOracleProposalHandler(app.OracleKeeper))

app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
app.appCodec, app.GetKey(ibcfeetypes.StoreKey),
Expand Down Expand Up @@ -161,6 +162,12 @@ func (app *App) registerIBCModules() error {
AddRoute(icahosttypes.SubModuleName, icaHostIBCModule)

oracleIBCModule := ibcfee.NewIBCMiddleware(oraclemodule.NewIBCModule(app.OracleKeeper), app.IBCFeeKeeper)
// oracleStack, err := app.registerOracleModule()
// if err != nil {
// return err
// }

// ibcRouter.AddRoute(oraclemoduletypes.ModuleName, oracleStack)
ibcRouter.AddRoute(oraclemoduletypes.ModuleName, oracleIBCModule)
// this line is used by starport scaffolding # ibc/app/module

Expand Down Expand Up @@ -199,6 +206,8 @@ func RegisterIBC(registry cdctypes.InterfaceRegistry) map[string]appmodule.AppMo
capabilitytypes.ModuleName: capability.AppModule{},
ibctm.ModuleName: ibctm.AppModule{},
solomachine.ModuleName: solomachine.AppModule{},

// oraclemoduletypes.ModuleName: oraclemodule.AppModule{},
}

for name, m := range modules {
Expand Down
56 changes: 56 additions & 0 deletions app/oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package app

// import (
// storetypes "cosmossdk.io/store/types"
// oraclekeeper "github.com/onomyprotocol/reserve/x/oracle/keeper"
// oraclemodule "github.com/onomyprotocol/reserve/x/oracle/module"
// oracletypes "github.com/onomyprotocol/reserve/x/oracle/types"
// "github.com/cosmos/cosmos-sdk/runtime"
// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
// ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
// porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
// )

// // registerOracleModule register Oracle keepers and non dependency inject modules.
// func (app *App) registerOracleModule() (porttypes.IBCModule, error) {
// // set up non depinject support modules store keys
// if err := app.RegisterStores(
// storetypes.NewKVStoreKey(oracletypes.StoreKey),
// ); err != nil {
// panic(err)
// }

// // register the key tables for legacy param subspaces
// app.ParamsKeeper.Subspace(oracletypes.ModuleName).WithKeyTable(oracletypes.ParamKeyTable())
// // add capability keeper and ScopeToModule for oracle ibc module
// scopedOralceKeeper := app.CapabilityKeeper.ScopeToModule(oracletypes.ModuleName)

// app.OracleKeeper = oraclekeeper.NewKeeper(
// app.AppCodec(),
// runtime.NewKVStoreService(app.GetKey(oracletypes.StoreKey)),
// app.Logger(),
// authtypes.NewModuleAddress(oracletypes.ModuleName).String(),
// app.GetIBCKeeper,
// scopedOralceKeeper,
// )

// // register IBC modules
// if err := app.RegisterModules(
// oraclemodule.NewAppModule(
// app.AppCodec(),
// app.OracleKeeper,
// app.AccountKeeper,
// app.BankKeeper,
// )); err != nil {
// return nil, err
// }

// app.ScopedOracleKeeper = scopedOralceKeeper

// // Create fee enabled ibc stack for oracel
// var oracleStack porttypes.IBCModule
// oracleStack = oraclemodule.NewIBCModule(app.OracleKeeper)
// oracleStack = ibcfee.NewIBCMiddleware(oracleStack, app.IBCFeeKeeper)

// return oracleStack, nil
// }
5 changes: 5 additions & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"encoding/json"
"testing"
"os"

abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -209,3 +210,7 @@ func initAccountWithCoins(app *App, ctx sdk.Context, addr sdk.AccAddress, coins
panic(err)
}
}

func Cleanup(app *App) { // release cosmwasm instance cache lock
_ = os.RemoveAll(DefaultNodeHome) // remove default dir, if it was overridden during test Setup, it's a responsibility of the sender to remove the folder
}
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.1.0
cosmossdk.io/x/nft v0.1.0
cosmossdk.io/x/tx v0.13.1
cosmossdk.io/x/upgrade v0.1.1
github.com/bufbuild/buf v1.30.0
github.com/cometbft/cometbft v0.38.6
Expand All @@ -37,6 +38,9 @@ require (
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand All @@ -46,6 +50,7 @@ require (
google.golang.org/grpc v1.64.1
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand All @@ -56,7 +61,6 @@ require (
cloud.google.com/go/storage v1.38.0 // indirect
connectrpc.com/connect v1.15.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
cosmossdk.io/x/tx v0.13.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -190,7 +194,6 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/profile v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
Expand All @@ -208,7 +211,6 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand Down Expand Up @@ -245,7 +247,6 @@ require (
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.3.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down
30 changes: 30 additions & 0 deletions proto/reserve/oracle/events.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
syntax = "proto3";
package reserve.oracle;

import "gogoproto/gogo.proto";

option go_package = "github.com/onomyprotocol/reserve/x/oracle/types";

message EventBandAckSuccess {
string ack_result = 1;
int64 client_id = 2;
}

message EventBandAckError {
string ack_error = 1;
int64 client_id = 2;
}

message EventBandResponseTimeout { int64 client_id = 1; }

message SetBandPriceEvent {
string relayer = 1;
repeated string symbols = 2;
repeated string prices = 3 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
uint64 resolve_time = 4;
uint64 request_id = 5;
int64 client_id = 6;
}
108 changes: 107 additions & 1 deletion proto/reserve/oracle/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package reserve.oracle;
import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "reserve/oracle/params.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/onomyprotocol/reserve/x/oracle/types";

Expand All @@ -14,5 +15,110 @@ message GenesisState {
// params defines all the parameters of the module.
Params params = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
string port_id = 2;
BandParams band_params = 2 [ (gogoproto.nullable) = false ];
repeated BandPriceState band_price_states = 3;
repeated BandOracleRequest band_oracle_requests = 4;
uint64 band_latest_client_id = 5;
repeated CalldataRecord calldata_records = 6;
uint64 band_latest_request_id = 7;
BandOracleRequestParams band_oracle_request_params = 8 [ (gogoproto.nullable) = false ];
}

message BandOracleRequest {
// Unique Identifier for band ibc oracle request
uint64 request_id = 1;

// OracleScriptID is the unique identifier of the oracle script to be
// executed.
int64 oracle_script_id = 2;

// Symbols is the list of symbols to prepare in the calldata
repeated string symbols = 3;

// AskCount is the number of validators that are requested to respond to this
// oracle request. Higher value means more security, at a higher gas cost.
uint64 ask_count = 4;

// MinCount is the minimum number of validators necessary for the request to
// proceed to the execution phase. Higher value means more security, at the
// cost of liveness.
uint64 min_count = 5;

// FeeLimit is the maximum tokens that will be paid to all data source
// providers.
repeated cosmos.base.v1beta1.Coin fee_limit = 6 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];

// PrepareGas is amount of gas to pay to prepare raw requests
uint64 prepare_gas = 7;
// ExecuteGas is amount of gas to reserve for executing
uint64 execute_gas = 8;
// MinSourceCount is the minimum number of data sources that must be used by
// each validator
uint64 min_source_count = 9;
}

message BandOracleRequestParams {
// AskCount is the number of validators that are requested to respond to this
// oracle request. Higher value means more security, at a higher gas cost.
uint64 ask_count = 1;

// MinCount is the minimum number of validators necessary for the request to
// proceed to the execution phase. Higher value means more security, at the
// cost of liveness.
uint64 min_count = 2;

// FeeLimit is the maximum tokens that will be paid to all data source
// providers.
repeated cosmos.base.v1beta1.Coin fee_limit = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];

// PrepareGas is amount of gas to pay to prepare raw requests
uint64 prepare_gas = 4;
// ExecuteGas is amount of gas to reserve for executing
uint64 execute_gas = 5;
// MinSourceCount is the minimum number of data sources that must be used by
// each validator
uint64 min_source_count = 6;
}

message BandParams {
// block request interval to send Band IBC prices
int64 ibc_request_interval = 2;
// band IBC source channel
string ibc_source_channel = 3;
// band IBC version
string ibc_version = 4;
// band IBC portID
string ibc_port_id = 5;
// legacy oracle scheme ids
repeated int64 legacy_oracle_ids = 6;
}

message BandPriceState {
string symbol = 1;
string rate = 2 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
uint64 resolve_time = 3;
uint64 request_ID = 4;
PriceState price_state = 5 [ (gogoproto.nullable) = false ];
}

message PriceState {
string price = 1 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
int64 timestamp = 2;
}

message CalldataRecord {
uint64 client_id = 1;
bytes calldata = 2;
}
Loading

0 comments on commit c86c35d

Please sign in to comment.