From 4b401f833f7c35446960a5709b72d41bd4b0ff27 Mon Sep 17 00:00:00 2001 From: Chandragupta Singh Date: Thu, 11 Aug 2022 17:15:16 +0530 Subject: [PATCH 1/4] update asset fixed, kv store removed --- x/asset/client/cli/flags.go | 2 +- x/asset/client/cli/tx.go | 7 +++++-- x/asset/keeper/asset.go | 35 --------------------------------- x/asset/keeper/asset_test.go | 2 +- x/asset/types/asset.go | 17 ++++++++++++++++ x/asset/types/gov.go | 2 +- x/asset/types/keys.go | 5 ----- x/bandoracle/expected/keeper.go | 2 +- x/bandoracle/keeper/alias.go | 4 ++-- x/bandoracle/keeper/oracle.go | 6 ++++-- x/esm/expected/keeper.go | 2 +- x/esm/keeper/alias.go | 4 ++-- x/esm/keeper/esm.go | 12 ++++++----- x/market/abci.go | 28 ++++++++++++++------------ x/market/expected/keeper.go | 2 +- x/market/keeper/alias.go | 4 ++-- x/market/keeper/oracle.go | 22 +++++++++++---------- 17 files changed, 72 insertions(+), 84 deletions(-) diff --git a/x/asset/client/cli/flags.go b/x/asset/client/cli/flags.go index ce7f1cfe5..68c71310c 100644 --- a/x/asset/client/cli/flags.go +++ b/x/asset/client/cli/flags.go @@ -10,7 +10,7 @@ const ( flagName = "name" flagDenom = "denom" flagDecimals = "decimals" - flagAssetOraclePrice = "isOraclePriceRequired" + flagAssetOraclePrice = "is-oracle-price-required" FlagAddAssetMappingFile = "add-asset-mapping-file" FlagAddAssetsMappingFile = "add-assets-file" ) diff --git a/x/asset/client/cli/tx.go b/x/asset/client/cli/tx.go index 7e2f6003c..12c52c68b 100644 --- a/x/asset/client/cli/tx.go +++ b/x/asset/client/cli/tx.go @@ -142,11 +142,13 @@ func NewCmdSubmitUpdateAssetProposal() *cobra.Command { return err } - assetOraclePrice, err := cmd.Flags().GetBool(flagAssetOraclePrice) + assetOraclePrice, err := cmd.Flags().GetString(flagAssetOraclePrice) if err != nil { return err } + newAssetOraclePrice := ParseBoolFromString(assetOraclePrice) + title, err := cmd.Flags().GetString(cli.FlagTitle) if err != nil { return err @@ -164,7 +166,7 @@ func NewCmdSubmitUpdateAssetProposal() *cobra.Command { Name: name, Denom: denom, Decimals: decimals, - IsOraclePriceRequired: assetOraclePrice, + IsOraclePriceRequired: newAssetOraclePrice, } depositStr, err := cmd.Flags().GetString(cli.FlagDeposit) @@ -197,6 +199,7 @@ func NewCmdSubmitUpdateAssetProposal() *cobra.Command { cmd.Flags().String(flagName, "", "name") cmd.Flags().String(flagDenom, "", "denomination") cmd.Flags().Int64(flagDecimals, -1, "decimals") + cmd.Flags().String(flagAssetOraclePrice, "", "is-oracle-price-required") _ = cmd.MarkFlagRequired(cli.FlagTitle) _ = cmd.MarkFlagRequired(cli.FlagDescription) diff --git a/x/asset/keeper/asset.go b/x/asset/keeper/asset.go index 6176b66ce..932ccb2cc 100644 --- a/x/asset/keeper/asset.go +++ b/x/asset/keeper/asset.go @@ -177,9 +177,6 @@ func (k Keeper) AddAssetRecords(ctx sdk.Context, msg types.Asset) error { k.SetAssetID(ctx, asset.Id) k.SetAsset(ctx, asset) k.SetAssetForDenom(ctx, asset.Denom, asset.Id) - if msg.IsOraclePriceRequired { - k.SetAssetForOracle(ctx, asset) - } return nil } @@ -244,35 +241,3 @@ func (k Keeper) AddPairsRecords(ctx sdk.Context, msg types.Pair) error { return nil } - -func (k Keeper) SetAssetForOracle(ctx sdk.Context, asset types.Asset) { - var ( - store = k.Store(ctx) - key = types.AssetForOracleKey(asset.Id) - value = k.cdc.MustMarshal(&asset) - ) - - store.Set(key, value) -} - -func (k Keeper) GetAssetsForOracle(ctx sdk.Context) (assets []types.Asset) { - var ( - store = k.Store(ctx) - iter = sdk.KVStorePrefixIterator(store, types.AssetForOracleKeyPrefix) - ) - - defer func(iter sdk.Iterator) { - err := iter.Close() - if err != nil { - return - } - }(iter) - - for ; iter.Valid(); iter.Next() { - var asset types.Asset - k.cdc.MustUnmarshal(iter.Value(), &asset) - assets = append(assets, asset) - } - - return assets -} diff --git a/x/asset/keeper/asset_test.go b/x/asset/keeper/asset_test.go index df050461d..031fe8436 100644 --- a/x/asset/keeper/asset_test.go +++ b/x/asset/keeper/asset_test.go @@ -331,7 +331,7 @@ func (s *KeeperTestSuite) TestQueryAssets() { func (s *KeeperTestSuite) TestGetAssetsForOracle() { s.TestAddAssetRecords() assetKeeper, ctx := &s.assetKeeper, &s.ctx - results := assetKeeper.GetAssetsForOracle(*ctx) + results := assetKeeper.GetAssets(*ctx) s.Require().Equal(len(results), 2) } diff --git a/x/asset/types/asset.go b/x/asset/types/asset.go index ae096d5a7..a6e537b0c 100644 --- a/x/asset/types/asset.go +++ b/x/asset/types/asset.go @@ -27,3 +27,20 @@ func (m *Asset) Validate() error { return nil } + +func (m *Asset) UpdateValidate() error { + + if len(m.Name) > MaxAssetNameLength { + return fmt.Errorf("name length cannot be greater than %d", MaxAssetNameLength) + } + if m.Denom != "" { + if err := sdk.ValidateDenom(m.Denom); err != nil { + return errors.Wrapf(err, "invalid denom %s", m.Denom) + } + } + if m.Decimals < 0 { + return fmt.Errorf("decimals cannot be less than zero") + } + + return nil +} diff --git a/x/asset/types/gov.go b/x/asset/types/gov.go index b5f6a12e9..f0f4f5056 100644 --- a/x/asset/types/gov.go +++ b/x/asset/types/gov.go @@ -100,7 +100,7 @@ func (p *UpdateAssetProposal) ValidateBasic() error { } asset := p.Asset - if err := asset.Validate(); err != nil { + if err := asset.UpdateValidate(); err != nil { return err } diff --git a/x/asset/types/keys.go b/x/asset/types/keys.go index 3f19ffd09..4d929c76a 100644 --- a/x/asset/types/keys.go +++ b/x/asset/types/keys.go @@ -19,7 +19,6 @@ var ( PairsVaultIDKey = []byte{0x05} AssetKeyPrefix = []byte{0x11} - AssetForOracleKeyPrefix = []byte{0x12} PairKeyPrefix = []byte{0x14} AppKeyPrefix = []byte{0x15} PairsVaultKeyPrefix = []byte{0x16} @@ -60,7 +59,3 @@ func GenesisForApp(appID uint64) []byte { func PairKey(id uint64) []byte { return append(PairKeyPrefix, sdk.Uint64ToBigEndian(id)...) } - -func AssetForOracleKey(id uint64) []byte { - return append(AssetForOracleKeyPrefix, sdk.Uint64ToBigEndian(id)...) -} diff --git a/x/bandoracle/expected/keeper.go b/x/bandoracle/expected/keeper.go index 194611597..56e3e438e 100644 --- a/x/bandoracle/expected/keeper.go +++ b/x/bandoracle/expected/keeper.go @@ -11,7 +11,7 @@ import ( type MarketKeeper interface{} type AssetKeeper interface { - GetAssetsForOracle(ctx sdk.Context, id uint64) (assettypes.Asset, bool) + GetAssets(ctx sdk.Context, id uint64) (assettypes.Asset, bool) } type ChannelKeeper interface { diff --git a/x/bandoracle/keeper/alias.go b/x/bandoracle/keeper/alias.go index 45a3fcc37..937d84809 100644 --- a/x/bandoracle/keeper/alias.go +++ b/x/bandoracle/keeper/alias.go @@ -5,6 +5,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func (k Keeper) GetAssetsForOracle(ctx sdk.Context) (assets []types.Asset) { - return k.assetKeeper.GetAssetsForOracle(ctx) +func (k Keeper) GetAssets(ctx sdk.Context) (assets []types.Asset) { + return k.assetKeeper.GetAssets(ctx) } diff --git a/x/bandoracle/keeper/oracle.go b/x/bandoracle/keeper/oracle.go index dd7e216fd..5bb62b21d 100644 --- a/x/bandoracle/keeper/oracle.go +++ b/x/bandoracle/keeper/oracle.go @@ -68,9 +68,11 @@ func (k Keeper) FetchPrice(ctx sdk.Context, msg types.MsgFetchPriceData) (*types } var symbol []string - assets := k.GetAssetsForOracle(ctx) + assets := k.GetAssets(ctx) for _, asset := range assets { - symbol = append(symbol, asset.Name) + if asset.IsOraclePriceRequired { + symbol = append(symbol, asset.Name) + } } encodedCallData := obi.MustEncode(types.FetchPriceCallData{Symbols: symbol, Multiplier: 1000000}) diff --git a/x/esm/expected/keeper.go b/x/esm/expected/keeper.go index fd2f73ef6..0e152ec90 100644 --- a/x/esm/expected/keeper.go +++ b/x/esm/expected/keeper.go @@ -12,7 +12,7 @@ type AssetKeeper interface { GetAsset(ctx sdk.Context, id uint64) (assettypes.Asset, bool) GetPair(ctx sdk.Context, id uint64) (assettypes.Pair, bool) GetApps(ctx sdk.Context) (apps []assettypes.AppData, found bool) - GetAssetsForOracle(ctx sdk.Context) (assets []assettypes.Asset) + GetAssets(ctx sdk.Context) (assets []assettypes.Asset) GetPairsVault(ctx sdk.Context, id uint64) (pairs assettypes.ExtendedPairVault, found bool) GetAssetForDenom(ctx sdk.Context, denom string) (asset assettypes.Asset, found bool) } diff --git a/x/esm/keeper/alias.go b/x/esm/keeper/alias.go index b54281dd7..ad8fabf3b 100644 --- a/x/esm/keeper/alias.go +++ b/x/esm/keeper/alias.go @@ -19,8 +19,8 @@ func (k Keeper) GetApps(ctx sdk.Context) (apps []assettypes.AppData, found bool) return k.asset.GetApps(ctx) } -func (k Keeper) GetAssetsForOracle(ctx sdk.Context) (assets []assettypes.Asset) { - return k.asset.GetAssetsForOracle(ctx) +func (k Keeper) GetAssets(ctx sdk.Context) (assets []assettypes.Asset) { + return k.asset.GetAssets(ctx) } func (k Keeper) GetPriceForAsset(ctx sdk.Context, id uint64) (uint64, bool) { diff --git a/x/esm/keeper/esm.go b/x/esm/keeper/esm.go index 5a978343c..ac6b81c06 100644 --- a/x/esm/keeper/esm.go +++ b/x/esm/keeper/esm.go @@ -735,13 +735,15 @@ func (k Keeper) GetAllAppToAmtValue(ctx sdk.Context) (appToAmountValue []types.A } func (k Keeper) SnapshotOfPrices(ctx sdk.Context, esmStatus types.ESMStatus) error { - assets := k.GetAssetsForOracle(ctx) + assets := k.GetAssets(ctx) for _, a := range assets { - price, found := k.GetPriceForAsset(ctx, a.Id) - if !found { - continue + if a.IsOraclePriceRequired { + price, found := k.GetPriceForAsset(ctx, a.Id) + if !found { + continue + } + k.SetSnapshotOfPrices(ctx, esmStatus.AppId, a.Id, price) } - k.SetSnapshotOfPrices(ctx, esmStatus.AppId, a.Id, price) } esmStatus.SnapshotStatus = true k.SetESMStatus(ctx, esmStatus) diff --git a/x/market/abci.go b/x/market/abci.go index 937f38509..56010c7d8 100644 --- a/x/market/abci.go +++ b/x/market/abci.go @@ -16,20 +16,22 @@ func BeginBlocker(ctx sdk.Context, _ abci.RequestBeginBlock, k keeper.Keeper) { block := k.GetLastBlockheight(ctx) if block != types.Int64Zero { if ctx.BlockHeight()%types.Int64Twenty-types.Int64One == types.Int64Zero && ctx.BlockHeight() > block+types.Int64TwentyOne { - assets := k.GetAssetsForOracle(ctx) + assets := k.GetAssets(ctx) for _, asset := range assets { - k.SetRates(ctx, asset.Name) - k.SetMarketForAsset(ctx, asset.Id, asset.Name) - rate, _ := k.GetRates(ctx, asset.Name) - scriptID := k.GetFetchPriceMsg(ctx).OracleScriptID - var ( - market = types.Market{ - Symbol: asset.Name, - ScriptID: scriptID, - Rates: rate, - } - ) - k.SetMarket(ctx, market) + if asset.IsOraclePriceRequired { + k.SetRates(ctx, asset.Name) + k.SetMarketForAsset(ctx, asset.Id, asset.Name) + rate, _ := k.GetRates(ctx, asset.Name) + scriptID := k.GetFetchPriceMsg(ctx).OracleScriptID + var ( + market = types.Market{ + Symbol: asset.Name, + ScriptID: scriptID, + Rates: rate, + } + ) + k.SetMarket(ctx, market) + } } } } diff --git a/x/market/expected/keeper.go b/x/market/expected/keeper.go index 01de27b53..aebc5e649 100644 --- a/x/market/expected/keeper.go +++ b/x/market/expected/keeper.go @@ -27,7 +27,7 @@ type ScopedKeeper interface { } type AssetKeeper interface { - GetAssetsForOracle(ctx sdk.Context, id uint64) (assettypes.Asset, bool) + GetAssets(ctx sdk.Context, id uint64) (assettypes.Asset, bool) GetPair(ctx sdk.Context, id uint64) (assettypes.Pair, bool) } diff --git a/x/market/keeper/alias.go b/x/market/keeper/alias.go index e07844b76..b0b2eb5bf 100644 --- a/x/market/keeper/alias.go +++ b/x/market/keeper/alias.go @@ -19,8 +19,8 @@ func (k Keeper) HasAsset(ctx sdk.Context, id uint64) bool { return k.assetKeeper.HasAsset(ctx, id) } -func (k Keeper) GetAssetsForOracle(ctx sdk.Context) (assets []types.Asset) { - return k.assetKeeper.GetAssetsForOracle(ctx) +func (k Keeper) GetAssets(ctx sdk.Context) (assets []types.Asset) { + return k.assetKeeper.GetAssets(ctx) } func (k Keeper) GetLastFetchPriceID(ctx sdk.Context) int64 { diff --git a/x/market/keeper/oracle.go b/x/market/keeper/oracle.go index 605cf2017..ca06adf38 100644 --- a/x/market/keeper/oracle.go +++ b/x/market/keeper/oracle.go @@ -101,19 +101,21 @@ func (k Keeper) SetRates(ctx sdk.Context, _ string) { data, _ := k.bandoraclekeeper.GetFetchPriceResult(ctx, bandoraclemoduletypes.OracleRequestID(id)) var sym []string - assets := k.GetAssetsForOracle(ctx) + assets := k.GetAssets(ctx) rateSliceLength := len(data.Rates) if rateSliceLength >= len(assets) { for i, asset := range assets { - sym = append(sym, asset.Name) - store := k.Store(ctx) - key := types.PriceForMarketKey(sym[i]) - - if data.Rates[i] != 0 { - value, _ := k.cdc.Marshal(&protobuftypes.UInt64Value{ - Value: data.Rates[i], - }) - store.Set(key, value) + if asset.IsOraclePriceRequired { + sym = append(sym, asset.Name) + store := k.Store(ctx) + key := types.PriceForMarketKey(sym[i]) + + if data.Rates[i] != 0 { + value, _ := k.cdc.Marshal(&protobuftypes.UInt64Value{ + Value: data.Rates[i], + }) + store.Set(key, value) + } } } } From f75682f544684a4e7c42acf4b197549add2bde3e Mon Sep 17 00:00:00 2001 From: Pratik Date: Thu, 11 Aug 2022 19:02:37 +0530 Subject: [PATCH 2/4] updating oracle.go --- x/market/keeper/oracle.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/x/market/keeper/oracle.go b/x/market/keeper/oracle.go index ca06adf38..43ea87944 100644 --- a/x/market/keeper/oracle.go +++ b/x/market/keeper/oracle.go @@ -1,6 +1,7 @@ package keeper import ( + assetTypes "github.com/comdex-official/comdex/x/asset/types" bandoraclemoduletypes "github.com/comdex-official/comdex/x/bandoracle/types" sdk "github.com/cosmos/cosmos-sdk/types" protobuftypes "github.com/gogo/protobuf/types" @@ -101,21 +102,24 @@ func (k Keeper) SetRates(ctx sdk.Context, _ string) { data, _ := k.bandoraclekeeper.GetFetchPriceResult(ctx, bandoraclemoduletypes.OracleRequestID(id)) var sym []string - assets := k.GetAssets(ctx) - rateSliceLength := len(data.Rates) - if rateSliceLength >= len(assets) { - for i, asset := range assets { - if asset.IsOraclePriceRequired { - sym = append(sym, asset.Name) - store := k.Store(ctx) - key := types.PriceForMarketKey(sym[i]) - - if data.Rates[i] != 0 { - value, _ := k.cdc.Marshal(&protobuftypes.UInt64Value{ - Value: data.Rates[i], - }) - store.Set(key, value) - } + allAssets := k.GetAssets(ctx) + var assets []assetTypes.Asset + for _, a := range allAssets { + if a.IsOraclePriceRequired { + assets = append(assets, a) + } + } + for i, asset := range assets { + if asset.IsOraclePriceRequired { + sym = append(sym, asset.Name) + store := k.Store(ctx) + key := types.PriceForMarketKey(sym[i]) + + if data.Rates[i] != 0 { + value, _ := k.cdc.Marshal(&protobuftypes.UInt64Value{ + Value: data.Rates[i], + }) + store.Set(key, value) } } } From d000eb2ce33ccb5c433e49eabd9991e0af024748 Mon Sep 17 00:00:00 2001 From: Pratik Date: Thu, 11 Aug 2022 20:27:26 +0530 Subject: [PATCH 3/4] error handling for oracle prices --- x/market/keeper/oracle.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/x/market/keeper/oracle.go b/x/market/keeper/oracle.go index 43ea87944..3bbf0aa51 100644 --- a/x/market/keeper/oracle.go +++ b/x/market/keeper/oracle.go @@ -114,13 +114,11 @@ func (k Keeper) SetRates(ctx sdk.Context, _ string) { sym = append(sym, asset.Name) store := k.Store(ctx) key := types.PriceForMarketKey(sym[i]) + value, _ := k.cdc.Marshal(&protobuftypes.UInt64Value{ + Value: data.Rates[i], + }) + store.Set(key, value) - if data.Rates[i] != 0 { - value, _ := k.cdc.Marshal(&protobuftypes.UInt64Value{ - Value: data.Rates[i], - }) - store.Set(key, value) - } } } } @@ -171,5 +169,9 @@ func (k Keeper) GetPriceForAsset(ctx sdk.Context, id uint64) (uint64, bool) { return 0, false } - return k.GetPriceForMarket(ctx, market.Symbol) + rates, found := k.GetPriceForMarket(ctx, market.Symbol) + if !found || rates == 0 { + return 0, false + } + return rates, found } From 8151e65328282c2e6ab71701e38b8f3976bd04a3 Mon Sep 17 00:00:00 2001 From: Chandragupta Singh Date: Thu, 11 Aug 2022 20:34:32 +0530 Subject: [PATCH 4/4] testcase removed --- x/asset/keeper/asset_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/x/asset/keeper/asset_test.go b/x/asset/keeper/asset_test.go index 031fe8436..2dccfdb8c 100644 --- a/x/asset/keeper/asset_test.go +++ b/x/asset/keeper/asset_test.go @@ -328,13 +328,6 @@ func (s *KeeperTestSuite) TestQueryAssets() { s.Require().Equal(len(assets), 5) } -func (s *KeeperTestSuite) TestGetAssetsForOracle() { - s.TestAddAssetRecords() - assetKeeper, ctx := &s.assetKeeper, &s.ctx - results := assetKeeper.GetAssets(*ctx) - s.Require().Equal(len(results), 2) -} - func (s *KeeperTestSuite) TestAddPair() { assetKeeper, ctx := &s.assetKeeper, &s.ctx