Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
kosegor committed Nov 7, 2023
1 parent 7779fd6 commit a6cd510
Show file tree
Hide file tree
Showing 7 changed files with 1,820 additions and 189 deletions.
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
}

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(
"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

0 comments on commit a6cd510

Please sign in to comment.