Skip to content

Commit

Permalink
add AddNewSymbolToBandOracleRequest to support vault
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNhann committed Sep 26, 2024
1 parent 892f919 commit 22834ab
Show file tree
Hide file tree
Showing 13 changed files with 710 additions and 664 deletions.
39 changes: 33 additions & 6 deletions proto/reserve/oracle/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ message GenesisState {
// params defines all the parameters of the module.
Params params = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
BandParams band_params = 8 [ (gogoproto.nullable) = false ];
repeated BandPriceState band_price_states = 2;
repeated BandOracleRequest band_oracle_requests = 3;
uint64 band_latest_client_id = 4;
repeated CalldataRecord calldata_records = 5;
uint64 band_latest_request_id = 11;
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 {
Expand Down Expand Up @@ -59,6 +60,32 @@ message BandOracleRequest {
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;
Expand Down
12 changes: 0 additions & 12 deletions proto/reserve/oracle/proposal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ message UpdateBandParamsProposal {
BandParams band_params = 3 [ (gogoproto.nullable) = false ];
}

message AuthorizeBandOracleRequestProposal {
option (amino.name) = "oracle/AuthorizeBandOracleRequestProposal";
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";

string title = 1;
string description = 2;
BandOracleRequest request = 3 [ (gogoproto.nullable) = false ];
}

message UpdateBandOracleRequestProposal {
option (amino.name) = "oracle/UpdateBandOracleRequestProposal";
option (gogoproto.equal) = false;
Expand Down
135 changes: 0 additions & 135 deletions x/oracle/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func GetTxCmd() *cobra.Command {

cmd.AddCommand(
NewRequestBandRatesTxCmd(),
NewAuthorizeBandOracleRequestProposalTxCmd(),
NewUpdateBandOracleRequestProposalTxCmd(),
NewDeleteBandOracleRequestProposalTxCmd(),
)
Expand Down Expand Up @@ -87,61 +86,6 @@ func NewRequestBandRatesTxCmd() *cobra.Command {
return cmd
}

func NewAuthorizeBandOracleRequestProposalTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "authorize-band-oracle-request-proposal [flags]",
Args: cobra.ExactArgs(1),
Short: "Submit a proposal to authorize a Band Oracle IBC Request.",
Long: `Submit a proposal to authorize a Band Oracle IBC Request.
Example:
$ %s tx oracle authorize-band-oracle-request-proposal 23 --symbols "BTC,ETH,USDT,USDC" --requested-validator-count 4 --sufficient-validator-count 3 --min-source-count 3 --prepare-gas 20000 --fee-limit "1000uband" --execute-gas 400000 --from mykey
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

content, err := authorizeBandOracleRequestProposalArgsToContent(cmd, args)
if err != nil {
return err
}

from := clientCtx.GetFromAddress()

depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}

msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().StringSlice(flagSymbols, []string{}, "Symbols used in calling the oracle script")
cmd.Flags().Uint64(flagPrepareGas, 50000, "Prepare gas used in fee counting for prepare request")
cmd.Flags().Uint64(flagExecuteGas, 300000, "Execute gas used in fee counting for execute request")
cmd.Flags().String(flagFeeLimit, "", "the maximum tokens that will be paid to all data source providers")
cmd.Flags().Uint64(flagRequestedValidatorCount, 4, "Requested Validator Count")
cmd.Flags().Uint64(flagSufficientValidatorCount, 10, "Sufficient Validator Count")
cmd.Flags().Uint64(flagMinSourceCount, 3, "Min Source Count")
cmd.Flags().String(govcli.FlagTitle, "", "title of proposal")
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal")
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")

flags.AddTxFlagsToCmd(cmd)
return cmd
}

func NewUpdateBandOracleRequestProposalTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-band-oracle-request-proposal 1 37 [flags]",
Expand Down Expand Up @@ -245,85 +189,6 @@ func NewDeleteBandOracleRequestProposalTxCmd() *cobra.Command {
return cmd
}

func authorizeBandOracleRequestProposalArgsToContent(
cmd *cobra.Command,
args []string,
) (govtypes.Content, error) {
title, err := cmd.Flags().GetString(govcli.FlagTitle)
if err != nil {
return nil, err
}

description, err := cmd.Flags().GetString(govcli.FlagDescription)
if err != nil {
return nil, err
}

int64OracleScriptID, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return nil, err
}

askCount, err := cmd.Flags().GetUint64(flagRequestedValidatorCount)
if err != nil {
return nil, err
}

minCount, err := cmd.Flags().GetUint64(flagSufficientValidatorCount)
if err != nil {
return nil, err
}

minSourceCount, err := cmd.Flags().GetUint64(flagMinSourceCount)
if err != nil {
return nil, err
}

symbols, err := cmd.Flags().GetStringSlice(flagSymbols)
if err != nil {
return nil, err
}

prepareGas, err := cmd.Flags().GetUint64(flagPrepareGas)
if err != nil {
return nil, err
}

executeGas, err := cmd.Flags().GetUint64(flagExecuteGas)
if err != nil {
return nil, err
}

coinStr, err := cmd.Flags().GetString(flagFeeLimit)
if err != nil {
return nil, err
}

feeLimit, err := sdk.ParseCoinsNormalized(coinStr)
if err != nil {
return nil, err
}

content := &types.AuthorizeBandOracleRequestProposal{
Title: title,
Description: description,
Request: types.BandOracleRequest{
OracleScriptId: int64OracleScriptID,
Symbols: symbols,
AskCount: askCount,
MinCount: minCount,
FeeLimit: feeLimit,
PrepareGas: prepareGas,
ExecuteGas: executeGas,
MinSourceCount: minSourceCount,
},
}
if err := content.ValidateBasic(); err != nil {
return nil, err
}
return content, nil
}

func updateBandOracleRequestProposalArgsToContent(
cmd *cobra.Command,
args []string,
Expand Down
58 changes: 58 additions & 0 deletions x/oracle/keeper/band_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ func (k Keeper) GetBandParams(ctx sdk.Context) types.BandParams {
return bandParams
}

// SetBandOracleRequestParams sets the Band Oracle request params in the state
func (k Keeper) SetBandOracleRequestParams(ctx sdk.Context, bandOracleRequestParams types.BandOracleRequestParams) error{
bz := k.cdc.MustMarshal(&bandOracleRequestParams)
store := k.storeService.OpenKVStore(ctx)
return store.Set(types.BandOracleRequestParamsKey, bz)
}

// GetBandParams gets the Band params stored in the state
func (k Keeper) GetBandOracleRequestParams(ctx sdk.Context) types.BandOracleRequestParams {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.BandParamsKey)

if err != nil {
return types.DefaultGenesis().BandOracleRequestParams
}

if bz == nil {
return types.DefaultGenesis().BandOracleRequestParams
}

var bandOracleRequestParams types.BandOracleRequestParams
k.cdc.MustUnmarshal(bz, &bandOracleRequestParams)
return bandOracleRequestParams
}

// SetBandCallData sets the Band IBC oracle request call data
func (k Keeper) SetBandCallDataRecord(ctx sdk.Context, record *types.CalldataRecord) error {
bz := k.cdc.MustMarshal(record)
Expand Down Expand Up @@ -112,6 +137,7 @@ func (k Keeper) GetBandLatestRequestID(ctx sdk.Context) uint64 {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.LatestRequestIDKey)
if err != nil {
// TODO: should we return 0 here?
return 0
}
if bz == nil {
Expand Down Expand Up @@ -215,6 +241,38 @@ func (k *Keeper) GetAllBandPriceStates(ctx sdk.Context) []*types.BandPriceState
return priceStates
}

// AddNewSymbolToBandOracleRequest adds a new symbol to the bandOracle request
func (k Keeper) AddNewSymbolToBandOracleRequest(ctx sdk.Context, symbol string, oracleScriptId int64) error{
allBandOracleRequests := k.GetAllBandOracleRequests(ctx)
// check if new symbol's oracle script id is existing
for _, req := range allBandOracleRequests {
if req.OracleScriptId == oracleScriptId {
req.Symbols = append(req.Symbols, symbol)
k.SetBandOracleRequest(ctx, *req)

Check failure on line 251 in x/oracle/keeper/band_oracle.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `k.SetBandOracleRequest` is not checked (errcheck)
return nil
}
}

bandOracleRequestParams := k.GetBandOracleRequestParams(ctx)
requestID := k.GetBandLatestRequestID(ctx) + 1
newBandOracleRequest := types.BandOracleRequest{
RequestId: requestID,
OracleScriptId: oracleScriptId,
Symbols: []string{symbol},
AskCount: bandOracleRequestParams.AskCount,
MinCount: bandOracleRequestParams.MinCount,
FeeLimit: bandOracleRequestParams.FeeLimit,
PrepareGas: bandOracleRequestParams.PrepareGas,
ExecuteGas: bandOracleRequestParams.ExecuteGas,
MinSourceCount: bandOracleRequestParams.MinSourceCount,
}

k.SetBandOracleRequest(ctx, newBandOracleRequest)

Check failure on line 270 in x/oracle/keeper/band_oracle.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `k.SetBandOracleRequest` is not checked (errcheck)

k.SetBandLatestRequestID(ctx, requestID)

Check failure on line 272 in x/oracle/keeper/band_oracle.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `k.SetBandLatestRequestID` is not checked (errcheck)
return nil
}

// GetPrice fetches band ibc prices for a given pair in math.LegacyDec
func (k *Keeper) GetPrice(ctx sdk.Context, base, quote string) *math.LegacyDec {
// query ref by using GetBandPriceState
Expand Down
17 changes: 10 additions & 7 deletions x/oracle/module/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
if genState.BandLatestRequestId != 0 {
k.SetBandLatestRequestID(ctx, genState.BandLatestRequestId)
}

k.SetBandOracleRequestParams(ctx, genState.BandOracleRequestParams)
}

// ExportGenesis returns the module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
return &types.GenesisState{
Params: k.GetParams(ctx),
BandParams: k.GetBandParams(ctx),
BandPriceStates: k.GetAllBandPriceStates(ctx),
BandOracleRequests: k.GetAllBandOracleRequests(ctx),
BandLatestClientId: k.GetBandLatestClientID(ctx),
CalldataRecords: k.GetAllBandCalldataRecords(ctx),
BandLatestRequestId: k.GetBandLatestRequestID(ctx),
Params: k.GetParams(ctx),
BandParams: k.GetBandParams(ctx),
BandPriceStates: k.GetAllBandPriceStates(ctx),
BandOracleRequests: k.GetAllBandOracleRequests(ctx),
BandLatestClientId: k.GetBandLatestClientID(ctx),
CalldataRecords: k.GetAllBandCalldataRecords(ctx),
BandLatestRequestId: k.GetBandLatestRequestID(ctx),
BandOracleRequestParams: k.GetBandOracleRequestParams(ctx),
}
}
16 changes: 0 additions & 16 deletions x/oracle/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ func NewOracleProposalHandler(k keeper.Keeper) govtypes.Handler {
switch c := content.(type) {
case *types.UpdateBandParamsProposal:
return handleUpdateBandParamsProposal(ctx, k, c)
case *types.AuthorizeBandOracleRequestProposal:
return handleAuthorizeBandOracleRequestProposal(ctx, k, c)
case *types.UpdateBandOracleRequestProposal:
return handleUpdateBandOracleRequestProposal(ctx, k, c)

Expand Down Expand Up @@ -49,20 +47,6 @@ func handleUpdateBandParamsProposal(ctx sdk.Context, k keeper.Keeper, p *types.U
return nil
}

func handleAuthorizeBandOracleRequestProposal(ctx sdk.Context, k keeper.Keeper, p *types.AuthorizeBandOracleRequestProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
}

requestID := k.GetBandLatestRequestID(ctx) + 1
p.Request.RequestId = requestID

k.SetBandOracleRequest(ctx, p.Request)

k.SetBandLatestRequestID(ctx, requestID)
return nil
}

func handleUpdateBandOracleRequestProposal(ctx sdk.Context, k keeper.Keeper, p *types.UpdateBandOracleRequestProposal) error {
if err := p.ValidateBasic(); err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions x/oracle/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgRequestBandRates{}, "oracle/MsgRequestBandRates", nil)

cdc.RegisterConcrete(&UpdateBandParamsProposal{}, "oracle/UpdateBandParamsProposal", nil)
cdc.RegisterConcrete(&AuthorizeBandOracleRequestProposal{}, "oracle/AuthorizeBandOracleRequestProposal", nil)
cdc.RegisterConcrete(&UpdateBandOracleRequestProposal{}, "oracle/UpdateBandOracleRequestProposal", nil)
cdc.RegisterConcrete(&DeleteBandOracleRequestProposal{}, "oracle/DeleteBandOracleRequestProposal", nil)
}
Expand All @@ -29,7 +28,6 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {

registry.RegisterImplementations((*govtypes.Content)(nil),
&UpdateBandParamsProposal{},
&AuthorizeBandOracleRequestProposal{},
&UpdateBandOracleRequestProposal{},
&DeleteBandOracleRequestProposal{},
)
Expand Down
1 change: 1 addition & 0 deletions x/oracle/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func DefaultGenesis() *GenesisState {
// this line is used by starport scaffolding # genesis/types/default
Params: DefaultParams(),
BandParams: DefaultBandParams(),
BandOracleRequestParams: DefaultBandOracelRequestParams(),
}
}

Expand Down
Loading

0 comments on commit 22834ab

Please sign in to comment.