Skip to content

Commit

Permalink
refactor(x/oracle): handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pyncz committed Nov 19, 2024
1 parent 2015aea commit d65f5bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
24 changes: 14 additions & 10 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ func TestExportInitGenesis(t *testing.T) {
ValAddrs[i] = sdk.ValAddress(vals[i].Address)
}

keepers.OracleKeeper.Params.Set(ctx, types.DefaultParams())
keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1])
keepers.OracleKeeper.ExchangeRates.Set(ctx, "pair1:pair2", types.DatedPrice{ExchangeRate: math.LegacyNewDec(123), CreationHeight: 0, CreationTime: 0})
keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{123}, ValAddrs[0], uint64(2)))
keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "foo", ExchangeRate: math.LegacyNewDec(123)}}, ValAddrs[0]))
keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair1:pair1")
keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair2:pair2")
keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], 10)
keepers.OracleKeeper.Rewards.Set(ctx, 0, types.Rewards{
require.NoError(t, keepers.OracleKeeper.Params.Set(ctx, types.DefaultParams()))
require.NoError(t, keepers.OracleKeeper.FeederDelegations.Set(ctx, ValAddrs[0], AccAddrs[1]))
require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, "pair1:pair2", types.DatedPrice{
ExchangeRate: math.LegacyNewDec(123),
CreationHeight: 0,
CreationTime: 0,
}))
require.NoError(t, keepers.OracleKeeper.Prevotes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{123}, ValAddrs[0], uint64(2))))
require.NoError(t, keepers.OracleKeeper.Votes.Set(ctx, ValAddrs[0], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "foo", ExchangeRate: math.LegacyNewDec(123)}}, ValAddrs[0])))
require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair1:pair1"))
require.NoError(t, keepers.OracleKeeper.WhitelistedPairs.Set(ctx, "pair2:pair2"))
require.NoError(t, keepers.OracleKeeper.MissCounters.Set(ctx, ValAddrs[0], 10))
require.NoError(t, keepers.OracleKeeper.Rewards.Set(ctx, 0, types.Rewards{
Id: 0,
VotePeriods: 100,
Coins: sdk.NewCoins(sdk.NewInt64Coin("test", 1000)),
})
}))
genesis := oracle.ExportGenesis(ctx, keepers.OracleKeeper)

chain = e2eTesting.NewTestChain(t, 2)
Expand Down
12 changes: 8 additions & 4 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,23 @@ func (k Keeper) GetExchangeRate(ctx sdk.Context, pair asset.Pair) (price math.Le

// SetPrice sets the price for a pair as well as the price snapshot.
func (k Keeper) SetPrice(ctx sdk.Context, pair asset.Pair, price math.LegacyDec) {
k.ExchangeRates.Set(ctx, pair, types.DatedPrice{
if err := k.ExchangeRates.Set(ctx, pair, types.DatedPrice{
ExchangeRate: price,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
}); err != nil {
ctx.Logger().Error("failed to set DatedPrice", "pair", pair, "error", err)
}

key := collections.Join(pair, ctx.BlockTime())
timestampMs := ctx.BlockTime().UnixMilli()
k.PriceSnapshots.Set(ctx, key, types.PriceSnapshot{
if err := k.PriceSnapshots.Set(ctx, key, types.PriceSnapshot{
Pair: pair,
Price: price,
TimestampMs: timestampMs,
})
}); err != nil {
ctx.Logger().Error("failed to set PriceSnapshot", "pair", pair, "error", err)
}
if err := ctx.EventManager().EmitTypedEvent(&types.EventPriceUpdate{
Pair: pair.String(),
Price: price,
Expand Down
27 changes: 15 additions & 12 deletions x/oracle/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func TestQueryExchangeRate(t *testing.T) {
querier := keeper.NewQuerier(keepers.OracleKeeper)

rate := math.LegacyNewDec(1700)
keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{
require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{
ExchangeRate: rate,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
}))

// empty request
_, err := querier.ExchangeRate(ctx, nil)
Expand Down Expand Up @@ -90,16 +90,16 @@ func TestQueryExchangeRates(t *testing.T) {
querier := keeper.NewQuerier(keepers.OracleKeeper)

rate := math.LegacyNewDec(1700)
keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{
require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{
ExchangeRate: rate,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{
}))
require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{
ExchangeRate: rate,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
}))

res, err := querier.ExchangeRates(ctx, &types.QueryExchangeRatesRequest{})
require.NoError(t, err)
Expand Down Expand Up @@ -231,21 +231,24 @@ func TestQueryActives(t *testing.T) {
queryClient := keeper.NewQuerier(keepers.OracleKeeper)

rate := math.LegacyNewDec(1700)
keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{

require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), types.DatedPrice{
ExchangeRate: rate,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD), types.DatedPrice{
}))

require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD), types.DatedPrice{
ExchangeRate: rate,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{
}))

require.NoError(t, keepers.OracleKeeper.ExchangeRates.Set(ctx, asset.Registry.Pair(denoms.ETH, denoms.NUSD), types.DatedPrice{
ExchangeRate: rate,
CreationHeight: ctx.BlockHeight(),
CreationTime: uint64(ctx.BlockTime().UnixMilli()),
})
}))

res, err := queryClient.Actives(ctx, &types.QueryActivesRequest{})
require.NoError(t, err)
Expand Down

0 comments on commit d65f5bb

Please sign in to comment.