-
Notifications
You must be signed in to change notification settings - Fork 173
feat: Add keeper methods for historacle prices and medians #1548
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
8b6ee7d
Add keeper methods for historacle prices and medians
rbajollari b52ecac
Merge branch 'main' into ryan/historacle-keeper
rbajollari 38bbb07
Add HistoraclePricing test
rbajollari d5951c0
Merge branch 'main' into ryan/historacle-keeper
rbajollari 7c1d213
fix sim-non-determinism
rbajollari b8e0987
get rid of getstampperiodhistoricprices
rbajollari 00245a7
Update x/oracle/keeper/params.go
rbajollari 515d608
Update x/oracle/keeper/params.go
rbajollari b2ce838
Update x/oracle/keeper/historic_price.go
rbajollari cf36377
Update x/oracle/keeper/historic_price.go
rbajollari bb7879d
Update x/oracle/keeper/historic_price.go
rbajollari 629e188
Update x/oracle/keeper/historic_price.go
rbajollari 943a300
Update x/oracle/types/params.go
rbajollari 790e150
Update x/oracle/keeper/historic_price.go
rbajollari fcb811d
PR suggestions
rbajollari cc0c293
Merge branch 'main' into ryan/historacle-keeper
rbajollari ea36bef
Merge branch 'main' into ryan/historacle-keeper
adamewozniak 6abbd31
Merge branch 'main' into ryan/historacle-keeper
rbajollari af8c1e6
Add median deviation keeper
rbajollari b9f974d
gofmt
rbajollari b719546
Update proto with MedianPeriod and leaner HistoricPrice type
rbajollari b38c8bb
Merge branch 'main' into ryan/historacle-keeper
adamewozniak 7514900
Add delete methods for median and median deviation
rbajollari 676b37d
Merge branch 'main' into ryan/historacle-keeper
rbajollari 6d6a69d
Update proto/umee/oracle/v1/oracle.proto
rbajollari c49a4f8
Update proto/umee/oracle/v1/oracle.proto
rbajollari e8262d4
Update x/oracle/keeper/historic_price.go
rbajollari 8b84251
Update x/oracle/keeper/historic_price.go
rbajollari 54b133e
Make historic price getter private and create error types for medians
rbajollari 7a4dcf9
Merge branch 'main' into ryan/historacle-keeper
rbajollari 85781b9
Add method for appending denom and block to key
rbajollari fe75545
Merge branch 'main' into ryan/historacle-keeper
rbajollari fbb76be
Empty-Commit
rbajollari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "sort" | ||
| "strings" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
|
||
| "github.com/umee-network/umee/v3/x/oracle/types" | ||
| ) | ||
|
|
||
| // median returns the median of a list of historic prices. | ||
| func median(prices []types.HistoricPrice) sdk.Dec { | ||
| lenPrices := len(prices) | ||
| if lenPrices == 0 { | ||
| return sdk.ZeroDec() | ||
| } | ||
|
|
||
| sort.Slice(prices, func(i, j int) bool { | ||
| return prices[i].ExchangeRates.ExchangeRate.BigInt(). | ||
| Cmp(prices[j].ExchangeRates.ExchangeRate.BigInt()) > 0 | ||
| }) | ||
|
|
||
| var medianPrice sdk.Dec | ||
| if lenPrices%2 == 0 { | ||
rbajollari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| medianPrice = prices[lenPrices/2-1].ExchangeRates.ExchangeRate. | ||
| Add(prices[lenPrices/2].ExchangeRates.ExchangeRate). | ||
| QuoInt64(2) | ||
| } else { | ||
| medianPrice = prices[lenPrices/2].ExchangeRates.ExchangeRate | ||
| } | ||
|
|
||
| return medianPrice | ||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // GetMedian returns the median price of a given denom. | ||
| func (k Keeper) GetMedian( | ||
| ctx sdk.Context, | ||
| denom string, | ||
| ) (sdk.Dec, error) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| denom = strings.ToUpper(denom) | ||
| bz := store.Get(types.GetMedianKey(denom)) | ||
| if bz == nil { | ||
| return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, denom) | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| decProto := sdk.DecProto{} | ||
| k.cdc.MustUnmarshal(bz, &decProto) | ||
|
|
||
| return decProto.Dec, nil | ||
| } | ||
|
|
||
| // setMedian uses all the historic prices of given denom to set the | ||
| // the median price of that denom in the store. | ||
| func (k Keeper) setMedian( | ||
| ctx sdk.Context, | ||
| denom string, | ||
| ) { | ||
| store := ctx.KVStore(k.storeKey) | ||
|
|
||
| historicPrices := k.GetHistoricPrices(ctx, denom) | ||
|
|
||
| median := median(historicPrices) | ||
|
|
||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: median}) | ||
| denom = strings.ToUpper(denom) | ||
| store.Set(types.GetMedianKey(denom), bz) | ||
| } | ||
|
|
||
| // GetHistoricPrice returns the historic price of a denom at a given | ||
| // block. | ||
| func (k Keeper) GetHistoricPrice( | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ctx sdk.Context, | ||
| denom string, | ||
| blockNum uint64, | ||
| ) (types.HistoricPrice, error) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| denom = strings.ToUpper(denom) | ||
| bz := store.Get(types.GetHistoricPriceKey(denom, blockNum)) | ||
| if bz == nil { | ||
| return types.HistoricPrice{}, sdkerrors.Wrap(types.ErrUnknownDenom, denom) | ||
| } | ||
|
|
||
| var historicPrice types.HistoricPrice | ||
| k.cdc.MustUnmarshal(bz, &historicPrice) | ||
|
|
||
| return historicPrice, nil | ||
| } | ||
|
|
||
| // GetHistoricPrices returns all the historic prices of a given denom. | ||
| func (k Keeper) GetHistoricPrices( | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ctx sdk.Context, | ||
| denom string, | ||
| ) []types.HistoricPrice { | ||
adamewozniak marked this conversation as resolved.
Show resolved
Hide resolved
adamewozniak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| denom = strings.ToUpper(denom) | ||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| historicPrices := []types.HistoricPrice{} | ||
|
|
||
| k.IterateHistoricPrices(ctx, denom, func(exchangeRate sdk.Dec, blockNum uint64) bool { | ||
| historicPrices = append(historicPrices, types.HistoricPrice{ | ||
| ExchangeRates: types.ExchangeRateTuple{ | ||
| Denom: denom, | ||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ExchangeRate: exchangeRate, | ||
| }, | ||
| BlockNum: blockNum, | ||
| }) | ||
|
|
||
| return false | ||
| }) | ||
|
|
||
| return historicPrices | ||
| } | ||
|
|
||
| // IterateHistoricPrices iterates over historic prices of a given | ||
| // denom in the store. | ||
rbajollari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func (k Keeper) IterateHistoricPrices( | ||
| ctx sdk.Context, | ||
| denom string, | ||
| handler func(sdk.Dec, uint64) bool, | ||
| ) { | ||
| store := ctx.KVStore(k.storeKey) | ||
|
|
||
| iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixHistoricPrice) | ||
| defer iter.Close() | ||
|
|
||
| for ; iter.Valid(); iter.Next() { | ||
| key := iter.Key() | ||
| historicPriceDenom := string(key[len(types.KeyPrefixExchangeRate) : len(key)-9]) | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if historicPriceDenom != denom { | ||
| continue | ||
| } | ||
| historicPrice := types.HistoricPrice{} | ||
|
|
||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| k.cdc.MustUnmarshal(iter.Value(), &historicPrice) | ||
| if handler(historicPrice.ExchangeRates.ExchangeRate, historicPrice.BlockNum) { | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // AddHistoricPrice adds the historic price of a denom at the current | ||
| // block height when called to the store. Afterwards it will call | ||
| // setMedian to update the median price of the denom in the store. | ||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func (k Keeper) AddHistoricPrice( | ||
| ctx sdk.Context, | ||
| denom string, | ||
| exchangeRate sdk.Dec, | ||
| ) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| exchangeRateTuple := types.ExchangeRateTuple{ | ||
| Denom: denom, | ||
rbajollari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ExchangeRate: exchangeRate, | ||
| } | ||
|
|
||
| block := uint64(ctx.BlockHeight()) | ||
| bz := k.cdc.MustMarshal(&types.HistoricPrice{ | ||
| ExchangeRates: exchangeRateTuple, | ||
| BlockNum: block, | ||
| }) | ||
| denom = strings.ToUpper(denom) | ||
| store.Set(types.GetHistoricPriceKey(denom, block), bz) | ||
|
|
||
| // update median after adding new historic price | ||
| k.setMedian(ctx, denom) | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // DeleteHistoricPrice deletes the historic price of a denom at a | ||
| // given block. | ||
| func (k Keeper) DeleteHistoricPrice( | ||
| ctx sdk.Context, | ||
| denom string, | ||
| blockNum uint64, | ||
| ) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| store.Delete(types.GetHistoricPriceKey(denom, blockNum)) | ||
|
|
||
| // update median after deleting historic price | ||
| k.setMedian(ctx, denom) | ||
| } | ||
|
|
||
| func (k Keeper) ClearHistoricPrices(ctx sdk.Context) { | ||
adamewozniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| store := ctx.KVStore(k.storeKey) | ||
| iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixHistoricPrice) | ||
| defer iter.Close() | ||
| for ; iter.Valid(); iter.Next() { | ||
| store.Delete(iter.Key()) | ||
| } | ||
|
|
||
| // clear medians as well | ||
| k.clearMedians(ctx) | ||
| } | ||
adamewozniak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func (k Keeper) clearMedians(ctx sdk.Context) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixMedian) | ||
| defer iter.Close() | ||
| for ; iter.Valid(); iter.Next() { | ||
| store.Delete(iter.Key()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package keeper_test | ||
|
|
||
| import ( | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
|
||
| "github.com/umee-network/umee/v3/x/oracle/types" | ||
| ) | ||
|
|
||
| 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()) | ||
|
|
||
| app.OracleKeeper.AddHistoricPrice(ctx, displayDenom, rate) | ||
| historicPrice, err := app.OracleKeeper.GetHistoricPrice(ctx, displayDenom, uint64(ctx.BlockHeight())) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(historicPrice, types.HistoricPrice{ | ||
| ExchangeRates: types.ExchangeRateTuple{ | ||
| Denom: displayDenom, | ||
| ExchangeRate: sdk.OneDec(), | ||
| }, | ||
| BlockNum: uint64(ctx.BlockHeight()), | ||
| }) | ||
|
|
||
| // add multiple historic prices to store | ||
| exchangeRates := []string{"1.2", "1.1", "1.4"} | ||
| for _, exchangeRate := range exchangeRates { | ||
| // update blockheight | ||
| ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) | ||
|
|
||
| // update exchange rate in store before updating historic price | ||
| newRate := sdk.OneDec().Mul(sdk.MustNewDecFromStr(exchangeRate)) | ||
| app.OracleKeeper.SetExchangeRate(ctx, displayDenom, newRate) | ||
| rate, err = app.OracleKeeper.GetExchangeRate(ctx, displayDenom) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(rate, newRate) | ||
|
|
||
| app.OracleKeeper.AddHistoricPrice(ctx, displayDenom, rate) | ||
| historicPrice, err = app.OracleKeeper.GetHistoricPrice(ctx, displayDenom, uint64(ctx.BlockHeight())) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(historicPrice, types.HistoricPrice{ | ||
| ExchangeRates: types.ExchangeRateTuple{ | ||
| Denom: displayDenom, | ||
| ExchangeRate: newRate, | ||
| }, | ||
| BlockNum: uint64(ctx.BlockHeight()), | ||
| }) | ||
| } | ||
|
|
||
| // check all historic prices were set | ||
| historicPrices := app.OracleKeeper.GetHistoricPrices(ctx, displayDenom) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(len(historicPrices), 4) | ||
|
|
||
| // check median was set | ||
| median, err := app.OracleKeeper.GetMedian(ctx, displayDenom) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(median, sdk.MustNewDecFromStr("1.15")) | ||
|
|
||
| // delete first historic price and check median was updated | ||
| app.OracleKeeper.DeleteHistoricPrice(ctx, displayDenom, uint64(ctx.BlockHeight()-3)) | ||
| s.Require().NoError(err) | ||
|
|
||
| median, err = app.OracleKeeper.GetMedian(ctx, displayDenom) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(median, sdk.MustNewDecFromStr("1.2")) | ||
|
|
||
| historicPrices = app.OracleKeeper.GetHistoricPrices(ctx, displayDenom) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(len(historicPrices), 3) | ||
|
|
||
| // check historic prices and medians get cleared | ||
| app.OracleKeeper.ClearHistoricPrices(ctx) | ||
| s.Require().NoError(err) | ||
|
|
||
| historicPrices = app.OracleKeeper.GetHistoricPrices(ctx, displayDenom) | ||
| s.Require().NoError(err) | ||
| s.Require().Equal(len(historicPrices), 0) | ||
|
|
||
| median, err = app.OracleKeeper.GetMedian(ctx, displayDenom) | ||
| s.Require().EqualError(err, sdkerrors.Wrap(types.ErrUnknownDenom, displayDenom).Error()) | ||
| s.Require().Equal(median, sdk.ZeroDec()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.