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

test: fixing meToken e2e #2255

Merged
merged 3 commits into from
Sep 21, 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
77 changes: 58 additions & 19 deletions tests/e2e/e2e_metoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/umee-network/umee/v6/app"
"github.com/umee-network/umee/v6/tests/grpc"
Expand All @@ -15,7 +16,6 @@ import (
)

func (s *E2ETest) TestMetokenSwapAndRedeem() {
var prices []metoken.IndexPrices
var index metoken.Index
valAddr, err := s.Chain.Validators[0].KeyInfo.GetAddress()
s.Require().NoError(err)
Expand All @@ -40,7 +40,7 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() {
err = grpc.MetokenRegistryUpdate(s.Umee, []metoken.Index{meUSD}, nil)
s.Require().NoError(err)

prices = s.checkMetokenBalance(meUSD.Denom, expectedBalance)
s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom)
},
)

Expand All @@ -57,6 +57,7 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() {
amountToReserves := assetSettings.ReservePortion.MulInt(amountToSwap).TruncateInt()
amountToLeverage := amountToSwap.Sub(amountToReserves)

prices := s.getPrices(mocks.MeUSDDenom)
usdtPrice, err := prices[0].PriceByBaseDenom(mocks.USDTBaseDenom)
s.Require().NoError(err)
returned := usdtPrice.SwapRate.MulInt(amountToSwap).TruncateInt()
Expand All @@ -71,7 +72,7 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() {
usdtBalance.Leveraged = usdtBalance.Leveraged.Add(amountToLeverage)
expectedBalance.SetAssetBalance(usdtBalance)

prices = s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance)
s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom)
},
)

Expand All @@ -86,25 +87,24 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() {
"not enough",
)

prices = s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance)
s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom)
},
)

s.Run(
"redeem_50meUSD_success", func() {
s.T().Skip("test never succeeds, need to be updated")

prices := s.getPrices(mocks.MeUSDDenom)
fiftyMeUSD := sdk.NewCoin(mocks.MeUSDDenom, sdkmath.NewInt(50_000000))

s.executeRedeemSuccess(valAddr.String(), fiftyMeUSD, mocks.USDTBaseDenom)

usdtPrice, err := prices[0].PriceByBaseDenom(mocks.USDTBaseDenom)
usdtToRedeem, err := prices[0].RedeemRate(fiftyMeUSD, mocks.USDTBaseDenom)
s.Require().NoError(err)
usdtToRedeem := usdtPrice.RedeemRate.MulInt(fiftyMeUSD.Amount).TruncateInt()
fee := index.Fee.MinFee.MulInt(usdtToRedeem).TruncateInt()

assetSettings, i := index.AcceptedAsset(mocks.USDTBaseDenom)
s.Require().True(i >= 0)

amountFromReserves := assetSettings.ReservePortion.MulInt(usdtToRedeem).TruncateInt()
amountFromLeverage := usdtToRedeem.Sub(amountFromReserves)

Expand All @@ -116,37 +116,76 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() {
usdtBalance.Leveraged = usdtBalance.Leveraged.Sub(amountFromLeverage)
expectedBalance.SetAssetBalance(usdtBalance)

_ = s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance)
s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom)
},
)
}

func (s *E2ETest) checkMetokenBalance(denom string, expectedBalance metoken.IndexBalances) []metoken.IndexPrices {
var prices []metoken.IndexPrices
func (s *E2ETest) checkMetokenBalance(valAddr, denom string) {
s.Require().Eventually(
func() bool {
resp, err := s.QueryMetokenBalances(denom)
if err != nil {
return false
}

var exist bool
for _, balance := range resp.IndexBalances {
if balance.MetokenSupply.Denom == expectedBalance.MetokenSupply.Denom {
exist = true
s.Require().Equal(expectedBalance, balance)
break
coins, err := s.QueryUmeeAllBalances(s.UmeeREST(), authtypes.NewModuleAddress(metoken.ModuleName).String())
if err != nil {
return false
}

for _, coin := range coins {
var exist bool
for _, balance := range resp.IndexBalances[0].AssetBalances {
if balance.Denom == coin.Denom {
exist = true
expectedBalance := balance.Interest.Add(balance.Fees).Add(balance.Reserved)
s.Require().Equal(coin.Amount, expectedBalance)
continue
}

if "u/"+balance.Denom == coin.Denom {
exist = true
s.Require().Equal(coin.Amount, balance.Leveraged)
continue
}
}
s.Require().True(exist)
}

coins, err = s.QueryUmeeAllBalances(s.UmeeREST(), valAddr)
if err != nil {
return false
}

for _, coin := range coins {
if coin.Denom == mocks.MeUSDDenom {
s.Require().Equal(coin.Amount, resp.IndexBalances[0].MetokenSupply.Amount)
}
}

s.Require().True(exist)
prices = resp.Prices
return true
},
30*time.Second,
500*time.Millisecond,
)
}

func (s *E2ETest) getPrices(denom string) []metoken.IndexPrices {
var prices []metoken.IndexPrices
s.Require().Eventually(
func() bool {
resp, err := s.QueryMetokenPrices(denom)
if err != nil {
return false
}

prices = resp.Prices
return true
},
30*time.Second,
500*time.Millisecond,
)
return prices
}

Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/setup/metoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func (s *E2ETestSuite) QueryMetokenIndexes(denom string) (metoken.QueryIndexesRe
return resp, s.QueryREST(endpoint, &resp)
}

func (s *E2ETestSuite) QueryMetokenPrices(denom string) (metoken.QueryIndexPricesResponse, error) {
endpoint := fmt.Sprintf("%s/umee/metoken/v1/index_prices?metoken_denom=%s", s.UmeeREST(), denom)
var resp metoken.QueryIndexPricesResponse

return resp, s.QueryREST(endpoint, &resp)
}

func (s *E2ETestSuite) TxMetokenSwap(umeeAddr string, asset sdk.Coin, meTokenDenom string) error {
req := &metoken.MsgSwap{
User: umeeAddr,
Expand Down
2 changes: 1 addition & 1 deletion x/metoken/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (q Querier) getPrices(k Keeper, meTokenDenom string) ([]metoken.IndexPrices
return nil, err
}

prices[i] = ip.QueryExport()
prices[i] = ip
}

return prices, nil
Expand Down
17 changes: 17 additions & 0 deletions x/metoken/keeper/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (k Keeper) Prices(index metoken.Index) (metoken.IndexPrices, error) {
if err != nil {
return indexPrices, err
}

indexPrices.SetPrice(
metoken.AssetPrice{
BaseDenom: aa.Denom,
Expand Down Expand Up @@ -76,6 +77,22 @@ func (k Keeper) Prices(index metoken.Index) (metoken.IndexPrices, error) {
indexPrices.Price = meTokenPrice
}

for i := 0; i < len(indexPrices.Assets); i++ {
asset := indexPrices.Assets[i]
swapRate, err := metoken.Rate(asset.Price, indexPrices.Price, asset.Exponent, indexPrices.Exponent)
if err != nil {
return indexPrices, err
}

redeemRate, err := metoken.Rate(indexPrices.Price, asset.Price, indexPrices.Exponent, asset.Exponent)
if err != nil {
return indexPrices, err
}

indexPrices.Assets[i].SwapRate = swapRate
indexPrices.Assets[i].RedeemRate = redeemRate
}

return indexPrices, nil
}

Expand Down
39 changes: 9 additions & 30 deletions x/metoken/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ func (ip IndexPrices) SwapRate(from sdk.Coin) (sdkmath.Int, error) {
return sdkmath.Int{}, err
}

exchangeRate := fromPrice.Price.Quo(ip.Price)

exponentFactor, err := ExponentFactor(fromPrice.Exponent, ip.Exponent)
if err != nil {
return sdkmath.Int{}, err
}

return exchangeRate.MulInt(from.Amount).Mul(exponentFactor).TruncateInt(), nil
return fromPrice.SwapRate.MulInt(from.Amount).TruncateInt(), nil
}

// RedeemRate converts meToken to an asset applying exchange_rate and normalizing the exponent.
Expand All @@ -67,32 +60,18 @@ func (ip IndexPrices) RedeemRate(from sdk.Coin, to string) (sdkmath.Int, error)
return sdkmath.Int{}, err
}

exchangeRate := ip.Price.Quo(toPrice.Price)

exponentFactor, err := ExponentFactor(ip.Exponent, toPrice.Exponent)
if err != nil {
return sdkmath.Int{}, err
}

return exchangeRate.MulInt(from.Amount).Mul(exponentFactor).TruncateInt(), nil
return toPrice.RedeemRate.MulInt(from.Amount).TruncateInt(), nil
}

// QueryExport completes the structure with missing data for the query.
func (ip IndexPrices) QueryExport() IndexPrices {
assets := make([]AssetPrice, len(ip.Assets))
for i := 0; i < len(ip.Assets); i++ {
asset := ip.Assets[i]
asset.SwapRate = asset.Price.Quo(ip.Price)
asset.RedeemRate = ip.Price.Quo(asset.Price)
assets[i] = asset
}
func Rate(fromPrice, toPrice sdk.Dec, fromExponent, toExponent uint32) (sdk.Dec, error) {
exchangeRate := fromPrice.Quo(toPrice)

return IndexPrices{
Denom: ip.Denom,
Price: ip.Price,
Exponent: ip.Exponent,
Assets: assets,
exponentFactor, err := ExponentFactor(fromExponent, toExponent)
if err != nil {
return sdk.Dec{}, err
}

return exchangeRate.Mul(exponentFactor), nil
}

// ExponentFactor calculates the factor to multiply by which the assets with different exponents.
Expand Down
Loading