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

Testfix #389

Merged
merged 4 commits into from
Aug 11, 2022
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 x/asset/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
7 changes: 5 additions & 2 deletions x/asset/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
35 changes: 0 additions & 35 deletions x/asset/keeper/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
7 changes: 0 additions & 7 deletions x/asset/keeper/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.GetAssetsForOracle(*ctx)
s.Require().Equal(len(results), 2)
}

func (s *KeeperTestSuite) TestAddPair() {

assetKeeper, ctx := &s.assetKeeper, &s.ctx
Expand Down
17 changes: 17 additions & 0 deletions x/asset/types/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion x/asset/types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 0 additions & 5 deletions x/asset/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var (
PairsVaultIDKey = []byte{0x05}

AssetKeyPrefix = []byte{0x11}
AssetForOracleKeyPrefix = []byte{0x12}
PairKeyPrefix = []byte{0x14}
AppKeyPrefix = []byte{0x15}
PairsVaultKeyPrefix = []byte{0x16}
Expand Down Expand Up @@ -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)...)
}
2 changes: 1 addition & 1 deletion x/bandoracle/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions x/bandoracle/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions x/bandoracle/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion x/esm/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/esm/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions x/esm/keeper/esm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 15 additions & 13 deletions x/market/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
k.SetMarketForAsset(ctx, asset.Id, asset.Name)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
rate, _ := k.GetRates(ctx, asset.Name)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
scriptID := k.GetFetchPriceMsg(ctx).OracleScriptID

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
var (
market = types.Market{
Symbol: asset.Name,
ScriptID: scriptID,
Rates: rate,
}
)
k.SetMarket(ctx, market)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/market/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions x/market/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 19 additions & 11 deletions x/market/keeper/oracle.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -101,20 +102,23 @@ func (k Keeper) SetRates(ctx sdk.Context, _ string) {
data, _ := k.bandoraclekeeper.GetFetchPriceResult(ctx, bandoraclemoduletypes.OracleRequestID(id))

var sym []string
assets := k.GetAssetsForOracle(ctx)
rateSliceLength := len(data.Rates)
if rateSliceLength >= len(assets) {
for i, asset := range assets {
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])
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)
}
}
}
}
Expand Down Expand Up @@ -165,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
}