Skip to content

Commit

Permalink
feat: Add WithinMedianDeviation keeper method (#1613)
Browse files Browse the repository at this point in the history
* Add WithinMedianDeviation keeper method

* Fix comment

* Update x/oracle/keeper/historic_price.go

Co-authored-by: Robert Zaremba <robert@zaremba.ch>

* Cleanup WithinMedianDeviation method

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
  • Loading branch information
rbajollari and robert-zaremba authored Nov 22, 2022
1 parent d86ccf3 commit c55960c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
20 changes: 20 additions & 0 deletions x/oracle/keeper/historic_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ func (k Keeper) GetMedianDeviation(
return decProto.Dec, nil
}

// WithinMedianDeviation returns whether or not a given price of a given
// denom is within the Standard Deviation around the Median.
func (k Keeper) WithinMedianDeviation(
ctx sdk.Context,
denom string,
price sdk.Dec,
) (bool, error) {
median, err := k.GetMedian(ctx, denom)
if err != nil {
return false, err
}

medianDeviation, err := k.GetMedianDeviation(ctx, denom)
if err != nil {
return false, err
}

return price.Sub(median).Abs().LTE(medianDeviation), nil
}

// setMedianDeviation sets a given denom's standard deviation around
// its median price in the last prune period since the current block.
func (k Keeper) calcAndSetMedianDeviation(
Expand Down
20 changes: 14 additions & 6 deletions x/oracle/keeper/historic_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
func (s *IntegrationTestSuite) TestSetHistoraclePricing() {
app, ctx := s.app, s.ctx

// set exchange rate in store before adding a historic price
app.OracleKeeper.SetExchangeRate(ctx, displayDenom, sdk.OneDec())
rate, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom)
s.Require().NoError(err)
s.Require().Equal(rate, sdk.OneDec())

// add multiple historic prices to store
exchangeRates := []string{"1.0", "1.2", "1.1", "1.4"}
for _, exchangeRate := range exchangeRates {
Expand All @@ -35,6 +29,16 @@ func (s *IntegrationTestSuite) TestSetHistoraclePricing() {
s.Require().NoError(err)
s.Require().Equal(medianDeviation, sdk.MustNewDecFromStr("0.0225"))

// check prices are within the median deviation
price1 := sdk.MustNewDecFromStr("1.13")
price2 := sdk.MustNewDecFromStr("1.12")
result, err := app.OracleKeeper.WithinMedianDeviation(ctx, displayDenom, price1)
s.Require().Equal(result, true)
s.Require().NoError(err)
result, err = app.OracleKeeper.WithinMedianDeviation(ctx, displayDenom, price2)
s.Require().Equal(result, false)
s.Require().NoError(err)

// delete first historic price, median, and median standard deviation
app.OracleKeeper.DeleteHistoricPrice(ctx, displayDenom, uint64(ctx.BlockHeight()-3))
app.OracleKeeper.DeleteMedian(ctx, displayDenom)
Expand All @@ -47,4 +51,8 @@ func (s *IntegrationTestSuite) TestSetHistoraclePricing() {
medianDeviation, err = app.OracleKeeper.GetMedianDeviation(ctx, displayDenom)
s.Require().Error(err, sdkerrors.Wrap(types.ErrUnknownDenom, displayDenom))
s.Require().Equal(median, sdk.ZeroDec())

result, err = app.OracleKeeper.WithinMedianDeviation(ctx, displayDenom, price1)
s.Require().Error(err, sdkerrors.Wrap(types.ErrUnknownDenom, displayDenom))
s.Require().Equal(result, false)
}

0 comments on commit c55960c

Please sign in to comment.