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 all commits
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
16 changes: 16 additions & 0 deletions tests/e2e/e2e_metoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() {
)
}

func (s *E2ETest) checkMetokenPriceInOracle(denom string) {
s.Require().Eventually(
func() bool {
exchangeRates, err := s.QueryExchangeRate(s.UmeeREST(), denom)
if err != nil {
return false
}
if exchangeRates.AmountOf(denom).IsZero() {
return false
}
return true
},
2*time.Minute, 12*time.Second, "fetching metoken price",
)
}

func (s *E2ETest) checkMetokenBalance(valAddr, denom string) {
s.Require().Eventually(
func() bool {
Expand Down
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
SetExchangeRate(ctx sdk.Context, denom string, rate sdk.Dec)
}
7 changes: 7 additions & 0 deletions x/metoken/keeper/intest/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func initTestSuite(t *testing.T, registry []metoken.Index, balances []metoken.In
AllMedianPrices(gomock.Any()).
Return(mocks.ValidPrices()).
AnyTimes()
oracleMock.EXPECT().SetExchangeRate(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

kb := keeper.NewKeeperBuilder(
app.AppCodec(),
Expand Down Expand Up @@ -108,6 +109,12 @@ func initTestSuite(t *testing.T, registry []metoken.Index, balances []metoken.In
mocks.ValidToken(mocks.ETHBaseDenom, mocks.ETHSymbolDenom, 18),
),
)
require.NoError(
app.LeverageKeeper.SetTokenSettings(
ctx,
mocks.ValidToken(mocks.MeUSDDenom, mocks.MeUSDDenom, 6),
),
)

return &KeeperTestSuite{
ctx: ctx,
Expand Down
30 changes: 30 additions & 0 deletions x/metoken/keeper/intest/price_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package intest

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/umee-network/umee/v6/util/checkers"
"github.com/umee-network/umee/v6/x/metoken"
"github.com/umee-network/umee/v6/x/metoken/mocks"
)

func TestPrice_SetPricesToOracle(t *testing.T) {
index := mocks.StableIndex(mocks.MeUSDDenom)

s := initTestSuite(t, nil, nil)
msgServer, ctx, app := s.msgServer, s.ctx, s.app

_, err := msgServer.GovUpdateRegistry(
ctx, &metoken.MsgGovUpdateRegistry{
Authority: checkers.GovModuleAddr,
AddIndex: []metoken.Index{index},
UpdateIndex: nil,
},
)
require := require.New(t)
require.NoError(err)

err = app.MetokenKeeperB.Keeper(&ctx).SetPricesToOracle()
require.NoError(err)
}
3 changes: 3 additions & 0 deletions x/metoken/keeper/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func (o Oracle) AllMedianPrices(_ sdk.Context) otypes.Prices {
return o.prices
}

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

func NewOracleMock() Oracle {
return Oracle{prices: mocks.ValidPrices()}
}
Expand Down
32 changes: 32 additions & 0 deletions x/metoken/keeper/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ 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().Error(
"setting price to oracle: couldn't calculate the price",
"denom", index.Denom,
"error", err.Error(),
"block_time", k.ctx.BlockTime(),
)
continue
}

indexToken, err := k.leverageKeeper.GetTokenSettings(*k.ctx, index.Denom)
if err != nil {
k.Logger().Error(
"setting price to oracle: couldn't get token settings",
"denom", index.Denom,
"error", err.Error(),
"block_time", k.ctx.BlockTime(),
)
continue
}

k.oracleKeeper.SetExchangeRate(*k.ctx, indexToken.SymbolDenom, 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
14 changes: 13 additions & 1 deletion x/metoken/mocks/keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/metoken/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
func EndBlocker(k keeper.Keeper) []abci.ValidatorUpdate {
util.Panic(k.ClaimLeverageInterest())
util.Panic(k.RebalanceReserves())
util.Panic(k.SetPricesToOracle())

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
return []abci.ValidatorUpdate{}
}
6 changes: 6 additions & 0 deletions x/oracle/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/umee-network/umee/v6/x/metoken"

leveragetypes "github.com/umee-network/umee/v6/x/leverage/types"
"github.com/umee-network/umee/v6/x/oracle/types"
Expand Down Expand Up @@ -29,6 +30,11 @@ func (h Hooks) AfterTokenRegistered(ctx sdk.Context, token leveragetypes.Token)
return
}

// Metokens shouldn't be part of oracle accept list. Every index informs its price to oracle each endBlock.
if metoken.IsMeToken(token.BaseDenom) {
return
}

acceptList := h.k.AcceptList(ctx)

var tokenExists bool
Expand Down
Loading