Skip to content
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

feat: feed oracle with meTokens price every endBlock #2316

Merged
merged 24 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x/metoken/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ type LeverageKeeper interface {
// OracleKeeper interface for price feed.
type OracleKeeper interface {
AllMedianPrices(ctx sdk.Context) otypes.Prices
SetExchangeRateWithEvent(ctx sdk.Context, denom string, rate sdk.Dec)
}
4 changes: 4 additions & 0 deletions x/metoken/keeper/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (o Oracle) AllMedianPrices(_ sdk.Context) otypes.Prices {
return o.prices
}

func (o Oracle) SetExchangeRateWithEvent(_ sdk.Context, _ string, _ sdk.Dec) {
return
}
kosegor marked this conversation as resolved.
Show resolved Hide resolved

func NewOracleMock() Oracle {
return Oracle{prices: mocks.ValidPrices()}
}
Expand Down
21 changes: 21 additions & 0 deletions x/metoken/keeper/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ func (k Keeper) Prices(index metoken.Index) (metoken.IndexPrices, error) {
return indexPrices, nil
}

// SetPricesToOracle of every registered index.
func (k Keeper) SetPricesToOracle() error {
indexes := k.GetAllRegisteredIndexes()
for _, index := range indexes {
iPrice, err := k.Prices(index)
if err != nil {
k.Logger().Debug(
kosegor marked this conversation as resolved.
Show resolved Hide resolved
"setting price to oracle: couldn't calculate the price",
"denom", index.Denom,
"error", err.Error(),
"block_time", k.ctx.BlockTime(),
)
continue
}

k.oracleKeeper.SetExchangeRateWithEvent(*k.ctx, index.Denom, iPrice.Price)
}

return nil
}

// latestPrice from the list of medians, based on the block number.
func latestPrice(prices otypes.Prices, symbolDenom string) (sdk.Dec, error) {
latestPrice := otypes.Price{}
Expand Down
7 changes: 7 additions & 0 deletions x/metoken/keeper/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func TestIndexPrices_Convert(t *testing.T) {
)
}

func TestIndexPrices_SetPricesToOracle(t *testing.T) {
o := NewOracleMock()
l := NewLeverageMock()
k := initMeUSDKeeper(t, nil, l, o)
require.NoError(t, k.SetPricesToOracle())
}

func meUSDIndexPricesAdjustedToBalance(t *testing.T, balance metoken.IndexBalances) metoken.IndexPrices {
usdtSupply, i := balance.AssetBalance(mocks.USDTBaseDenom)
require.True(t, i >= 0)
Expand Down
Loading
Loading