Skip to content

Commit

Permalink
refactor: x/oracle: make GetExchangeRateBase more efficient (#866) (#872
Browse files Browse the repository at this point in the history
)

(cherry picked from commit d919765)

Co-authored-by: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com>
  • Loading branch information
mergify[bot] and adamewozniak authored May 5, 2022
1 parent e9c4a1e commit f62261e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

- [866](https://github.com/umee-network/umee/pull/866) Make the x/oracle keeper's GetExchangeRateBase method more efficient.

## [v2.0.1](https://github.com/umee-network/umee/releases/tag/v2.0.1) - 2022-04-25

### Features
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (s *IntegrationTestSuite) TestBallot_OrganizeBallotByDenom() {
s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec())
s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec())
claimMap := make(map[string]types.Claim)

// Empty Map
Expand Down
12 changes: 6 additions & 6 deletions x/oracle/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import (
)

func (s *IntegrationTestSuite) TestQuerier_ActiveExchangeRates() {
s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec())
s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec())
res, err := s.queryClient.ActiveExchangeRates(s.ctx.Context(), &types.QueryActiveExchangeRatesRequest{})
s.Require().NoError(err)
s.Require().Equal([]string{exchangeRate}, res.ActiveRates)
s.Require().Equal([]string{displayDenom}, res.ActiveRates)
}

func (s *IntegrationTestSuite) TestQuerier_ExchangeRates() {
s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec())
s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec())
res, err := s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{})
s.Require().NoError(err)
s.Require().Equal(sdk.DecCoins{
sdk.NewDecCoinFromDec(exchangeRate, sdk.OneDec()),
sdk.NewDecCoinFromDec(displayDenom, sdk.OneDec()),
}, res.ExchangeRates)

res, err = s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{
Denom: exchangeRate,
Denom: displayDenom,
})
s.Require().NoError(err)
s.Require().Equal(sdk.DecCoins{
sdk.NewDecCoinFromDec(exchangeRate, sdk.OneDec()),
sdk.NewDecCoinFromDec(displayDenom, sdk.OneDec()),
}, res.ExchangeRates)
}

Expand Down
13 changes: 4 additions & 9 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ func (k Keeper) GetExchangeRate(ctx sdk.Context, symbol string) (sdk.Dec, error)
// in the base denom (e.g. ATOM -> uatom)
func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, error) {
var symbol string

var exponent uint64
// Translate the base denom -> symbol
params := k.GetParams(ctx)
for _, listDenom := range params.AcceptList {
if listDenom.BaseDenom == denom {
symbol = listDenom.SymbolDenom
exponent = uint64(listDenom.Exponent)
break
}
}
Expand All @@ -107,14 +108,8 @@ func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, err
return sdk.ZeroDec(), err
}

for _, acceptedDenom := range params.AcceptList {
if denom == acceptedDenom.BaseDenom {
powerReduction := ten.Power(uint64(acceptedDenom.Exponent))
return exchangeRate.Quo(powerReduction), nil
}
}

return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, denom)
powerReduction := ten.Power(exponent)
return exchangeRate.Quo(powerReduction), nil
}

// SetExchangeRate sets the consensus exchange rate of USD denominated in the
Expand Down
48 changes: 36 additions & 12 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
)

const (
exchangeRate string = umeeapp.DisplayDenom
displayDenom string = umeeapp.DisplayDenom
bondDenom string = umeeapp.BondDenom
)

type IntegrationTestSuite struct {
Expand Down Expand Up @@ -177,7 +178,7 @@ func (s *IntegrationTestSuite) TestAggregateExchangeRateVote() {

var tuples types.ExchangeRateTuples
tuples = append(tuples, types.ExchangeRateTuple{
Denom: exchangeRate,
Denom: displayDenom,
ExchangeRate: sdk.ZeroDec(),
})

Expand Down Expand Up @@ -205,8 +206,8 @@ func (s *IntegrationTestSuite) TestAggregateExchangeRateVoteError() {

func (s *IntegrationTestSuite) TestSetExchangeRateWithEvent() {
app, ctx := s.app, s.ctx
app.OracleKeeper.SetExchangeRateWithEvent(ctx, exchangeRate, sdk.OneDec())
rate, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate)
app.OracleKeeper.SetExchangeRateWithEvent(ctx, displayDenom, sdk.OneDec())
rate, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom)
s.Require().NoError(err)
s.Require().Equal(rate, sdk.OneDec())
}
Expand All @@ -221,30 +222,53 @@ func (s *IntegrationTestSuite) TestGetExchangeRate_InvalidDenom() {
func (s *IntegrationTestSuite) TestGetExchangeRate_NotSet() {
app, ctx := s.app, s.ctx

_, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate)
_, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom)
s.Require().Error(err)
}

func (s *IntegrationTestSuite) TestGetExchangeRate_Valid() {
app, ctx := s.app, s.ctx

app.OracleKeeper.SetExchangeRate(ctx, exchangeRate, sdk.OneDec())
rate, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate)
app.OracleKeeper.SetExchangeRate(ctx, displayDenom, sdk.OneDec())
rate, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom)
s.Require().NoError(err)
s.Require().Equal(rate, sdk.OneDec())

app.OracleKeeper.SetExchangeRate(ctx, strings.ToLower(exchangeRate), sdk.OneDec())
rate, err = app.OracleKeeper.GetExchangeRate(ctx, exchangeRate)
app.OracleKeeper.SetExchangeRate(ctx, strings.ToLower(displayDenom), sdk.OneDec())
rate, err = app.OracleKeeper.GetExchangeRate(ctx, displayDenom)
s.Require().NoError(err)
s.Require().Equal(rate, sdk.OneDec())
}

func (s *IntegrationTestSuite) TestGetExchangeRateBase() {
oracleParams := s.app.OracleKeeper.GetParams(s.ctx)

var exponent uint64
for _, denom := range oracleParams.AcceptList {
if denom.BaseDenom == bondDenom {
exponent = uint64(denom.Exponent)
}
}

power := sdk.MustNewDecFromStr("10").Power(exponent)

s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec())
rate, err := s.app.OracleKeeper.GetExchangeRateBase(s.ctx, bondDenom)
s.Require().NoError(err)
s.Require().Equal(rate.Mul(power), sdk.OneDec())

s.app.OracleKeeper.SetExchangeRate(s.ctx, strings.ToLower(displayDenom), sdk.OneDec())
rate, err = s.app.OracleKeeper.GetExchangeRateBase(s.ctx, bondDenom)
s.Require().NoError(err)
s.Require().Equal(rate.Mul(power), sdk.OneDec())
}

func (s *IntegrationTestSuite) TestDeleteExchangeRate() {
app, ctx := s.app, s.ctx

app.OracleKeeper.SetExchangeRate(ctx, exchangeRate, sdk.OneDec())
app.OracleKeeper.DeleteExchangeRate(ctx, exchangeRate)
_, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate)
app.OracleKeeper.SetExchangeRate(ctx, displayDenom, sdk.OneDec())
app.OracleKeeper.DeleteExchangeRate(ctx, displayDenom)
_, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom)
s.Require().Error(err)
}

Expand Down

0 comments on commit f62261e

Please sign in to comment.