Skip to content

Commit

Permalink
chore(oracle)!: SetExchangeRateWithEvent (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba authored Nov 13, 2023
1 parent bf9c050 commit 78c26a1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func CalcPrices(ctx sdk.Context, params types.Params, k keeper.Keeper) error {
return err
}
// save the exchange rate to store with denom and timestamp
k.SetExchangeRateWithEvent(ctx, denom, exchangeRate)
k.SetExchangeRate(ctx, denom, exchangeRate)

if k.IsPeriodLastBlock(ctx, params.HistoricStampPeriod) {
k.AddHistoricPrice(ctx, denom, exchangeRate)
Expand Down
14 changes: 5 additions & 9 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,23 @@ func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, err
}

// SetExchangeRateWithTimestamp sets the consensus exchange rate of USD denominated in the
// denom asset to the store with a timestamp specified instead of using ctx
// denom asset to the store with a timestamp specified instead of using ctx.
// NOTE: must not be used outside of genesis import.
func (k Keeper) SetExchangeRateWithTimestamp(ctx sdk.Context, denom string, rate sdk.Dec, t time.Time) {
key := types.KeyExchangeRate(denom)
val := types.ExchangeRate{Rate: rate, Timestamp: t}
err := store.SetValue[*types.ExchangeRate](ctx.KVStore(k.storeKey), key, &val, "exchange_rate")
util.Panic(err)
}

// SetExchangeRate sets the consensus exchange rate of USD denominated in the
// denom asset to the store using timestamp from ctx
func (k Keeper) SetExchangeRate(ctx sdk.Context, denom string, rate sdk.Dec) {
k.SetExchangeRateWithTimestamp(ctx, denom, rate, ctx.BlockTime())
}

// SetExchangeRateWithEvent sets an consensus
// SetExchangeRate sets an consensus
// exchange rate to the store with ABCI event
func (k Keeper) SetExchangeRateWithEvent(ctx sdk.Context, denom string, rate sdk.Dec) {
func (k Keeper) SetExchangeRate(ctx sdk.Context, denom string, rate sdk.Dec) {
k.SetExchangeRateWithTimestamp(ctx, denom, rate, ctx.BlockTime())
sdkutil.Emit(&ctx, &types.EventSetFxRate{
Denom: denom, Rate: rate,
})

}

// IterateExchangeRates iterates over all USD rates in the store.
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ func (s *IntegrationTestSuite) TestAggregateExchangeRateVoteError() {
s.Require().Errorf(err, types.ErrNoAggregateVote.Error())
}

func (s *IntegrationTestSuite) TestSetExchangeRateWithEvent() {
func (s *IntegrationTestSuite) TestSetExchangeRate() {
v := sdk.OneDec()
s.app.OracleKeeper.SetExchangeRateWithEvent(s.ctx, displayDenom, v)
s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, v)
rate, err := s.app.OracleKeeper.GetExchangeRate(s.ctx, displayDenom)
s.Require().NoError(err)
s.Require().Equal(rate, types.ExchangeRate{Rate: v, Timestamp: s.ctx.BlockTime()})
Expand Down

0 comments on commit 78c26a1

Please sign in to comment.