From e3ddf4df58be57c345022aabbf05e75882a7b3eb Mon Sep 17 00:00:00 2001 From: Egor Kostetskiy Date: Tue, 19 Sep 2023 17:19:14 -0300 Subject: [PATCH 1/3] test1 --- tests/e2e/e2e_metoken_test.go | 68 +++++++++++++++++++++++++++------- tests/e2e/setup/metoken.go | 7 ++++ x/metoken/keeper/grpc_query.go | 2 +- x/metoken/keeper/price.go | 17 +++++++++ x/metoken/price.go | 41 ++++++-------------- 5 files changed, 92 insertions(+), 43 deletions(-) diff --git a/tests/e2e/e2e_metoken_test.go b/tests/e2e/e2e_metoken_test.go index 48eb8a4345..4c0c6d4471 100644 --- a/tests/e2e/e2e_metoken_test.go +++ b/tests/e2e/e2e_metoken_test.go @@ -1,13 +1,13 @@ package e2e import ( + "fmt" "strings" "time" sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/umee-network/umee/v6/app" "github.com/umee-network/umee/v6/tests/grpc" ltypes "github.com/umee-network/umee/v6/x/leverage/types" "github.com/umee-network/umee/v6/x/metoken" @@ -15,15 +15,14 @@ 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) expectedBalance := mocks.EmptyUSDIndexBalances(mocks.MeUSDDenom) - if app.Experimental { - s.T().Skip("Skipping tests for experimental module x/metoken") - } + //if !app.Experimental { + // s.T().Skip("Skipping tests for experimental module x/metoken") + //} s.Run( "create_stable_index", func() { @@ -40,7 +39,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(meUSD.Denom, expectedBalance) }, ) @@ -57,6 +56,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() @@ -71,7 +71,7 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { usdtBalance.Leveraged = usdtBalance.Leveraged.Add(amountToLeverage) expectedBalance.SetAssetBalance(usdtBalance) - prices = s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) + s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) }, ) @@ -86,19 +86,22 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { "not enough", ) - prices = s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) + s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) }, ) 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) + fmt.Printf("fiftyMeUSD: %s\n", fiftyMeUSD.String()) + fmt.Printf("usdtPrice: %s\n", usdtPrice.Price.String()) + fmt.Printf("usdtPrice.RedeemRate: %s\n", usdtPrice.RedeemRate.String()) s.Require().NoError(err) usdtToRedeem := usdtPrice.RedeemRate.MulInt(fiftyMeUSD.Amount).TruncateInt() fee := index.Fee.MinFee.MulInt(usdtToRedeem).TruncateInt() @@ -116,13 +119,12 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { usdtBalance.Leveraged = usdtBalance.Leveraged.Sub(amountFromLeverage) expectedBalance.SetAssetBalance(usdtBalance) - _ = s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) + s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) }, ) } -func (s *E2ETest) checkMetokenBalance(denom string, expectedBalance metoken.IndexBalances) []metoken.IndexPrices { - var prices []metoken.IndexPrices +func (s *E2ETest) checkMetokenBalance(denom string, expectedBalance metoken.IndexBalances) { s.Require().Eventually( func() bool { resp, err := s.QueryMetokenBalances(denom) @@ -133,6 +135,31 @@ func (s *E2ETest) checkMetokenBalance(denom string, expectedBalance metoken.Inde var exist bool for _, balance := range resp.IndexBalances { if balance.MetokenSupply.Denom == expectedBalance.MetokenSupply.Denom { + fmt.Printf("expected[0].Fees: %s\n", expectedBalance.AssetBalances[0].Fees) + fmt.Printf("expected[0].Reserved: %s\n", expectedBalance.AssetBalances[0].Reserved) + fmt.Printf("expected[0].Leveraged: %s\n", expectedBalance.AssetBalances[0].Leveraged) + fmt.Printf("expected[0].Interest: %s\n", expectedBalance.AssetBalances[0].Interest) + fmt.Printf("expected[1].Fees: %s\n", expectedBalance.AssetBalances[1].Fees) + fmt.Printf("expected[1].Reserved: %s\n", expectedBalance.AssetBalances[1].Reserved) + fmt.Printf("expected[1].Leveraged: %s\n", expectedBalance.AssetBalances[1].Leveraged) + fmt.Printf("expected[1].Interest: %s\n", expectedBalance.AssetBalances[1].Interest) + fmt.Printf("expected[2].Fees: %s\n", expectedBalance.AssetBalances[2].Fees) + fmt.Printf("expected[2].Reserved: %s\n", expectedBalance.AssetBalances[2].Reserved) + fmt.Printf("expected[2].Leveraged: %s\n", expectedBalance.AssetBalances[2].Leveraged) + fmt.Printf("expected[2].Interest: %s\n", expectedBalance.AssetBalances[2].Interest) + fmt.Printf("----------------------\n") + fmt.Printf("balance[0].Fees: %s\n", balance.AssetBalances[0].Fees) + fmt.Printf("balance[0].Reserved: %s\n", balance.AssetBalances[0].Reserved) + fmt.Printf("balance[0].Leveraged: %s\n", balance.AssetBalances[0].Leveraged) + fmt.Printf("balance[0].Interest: %s\n", balance.AssetBalances[0].Interest) + fmt.Printf("balance[1].Fees: %s\n", balance.AssetBalances[1].Fees) + fmt.Printf("balance[1].Reserved: %s\n", balance.AssetBalances[1].Reserved) + fmt.Printf("balance[1].Leveraged: %s\n", balance.AssetBalances[1].Leveraged) + fmt.Printf("balance[1].Interest: %s\n", balance.AssetBalances[1].Interest) + fmt.Printf("balance[2].Fees: %s\n", balance.AssetBalances[2].Fees) + fmt.Printf("balance[2].Reserved: %s\n", balance.AssetBalances[2].Reserved) + fmt.Printf("balance[2].Leveraged: %s\n", balance.AssetBalances[2].Leveraged) + fmt.Printf("balance[2].Interest: %s\n", balance.AssetBalances[2].Interest) exist = true s.Require().Equal(expectedBalance, balance) break @@ -140,13 +167,28 @@ func (s *E2ETest) checkMetokenBalance(denom string, expectedBalance metoken.Inde } 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 } diff --git a/tests/e2e/setup/metoken.go b/tests/e2e/setup/metoken.go index 97027f0779..4ab5564819 100644 --- a/tests/e2e/setup/metoken.go +++ b/tests/e2e/setup/metoken.go @@ -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, diff --git a/x/metoken/keeper/grpc_query.go b/x/metoken/keeper/grpc_query.go index 0c096443d9..7c4a9a8732 100644 --- a/x/metoken/keeper/grpc_query.go +++ b/x/metoken/keeper/grpc_query.go @@ -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 diff --git a/x/metoken/keeper/price.go b/x/metoken/keeper/price.go index 16ec739d93..33cf972cd0 100644 --- a/x/metoken/keeper/price.go +++ b/x/metoken/keeper/price.go @@ -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, @@ -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 } diff --git a/x/metoken/price.go b/x/metoken/price.go index 32419f5939..42f41235c6 100644 --- a/x/metoken/price.go +++ b/x/metoken/price.go @@ -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. @@ -67,32 +60,22 @@ func (ip IndexPrices) RedeemRate(from sdk.Coin, to string) (sdkmath.Int, error) return sdkmath.Int{}, err } - exchangeRate := ip.Price.Quo(toPrice.Price) + fmt.Printf("from.Amount: %s\n", from.Amount.String()) + fmt.Printf("toPrice: %s\n", toPrice.Price.String()) + fmt.Printf("toPrice.RedeemRate: %s\n", toPrice.RedeemRate.String()) - 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. From a864175a4e045686e6a94939f32492237d2294f8 Mon Sep 17 00:00:00 2001 From: Egor Kostetskiy Date: Wed, 20 Sep 2023 18:30:53 -0300 Subject: [PATCH 2/3] metoken e2e stabillized --- tests/e2e/e2e_metoken_test.go | 91 +++++++++++++++++------------------ 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/tests/e2e/e2e_metoken_test.go b/tests/e2e/e2e_metoken_test.go index 4c0c6d4471..69b8b44e0c 100644 --- a/tests/e2e/e2e_metoken_test.go +++ b/tests/e2e/e2e_metoken_test.go @@ -1,13 +1,14 @@ package e2e import ( - "fmt" "strings" "time" 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" ltypes "github.com/umee-network/umee/v6/x/leverage/types" "github.com/umee-network/umee/v6/x/metoken" @@ -20,9 +21,9 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { s.Require().NoError(err) expectedBalance := mocks.EmptyUSDIndexBalances(mocks.MeUSDDenom) - //if !app.Experimental { - // s.T().Skip("Skipping tests for experimental module x/metoken") - //} + if app.Experimental { + s.T().Skip("Skipping tests for experimental module x/metoken") + } s.Run( "create_stable_index", func() { @@ -39,7 +40,7 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { err = grpc.MetokenRegistryUpdate(s.Umee, []metoken.Index{meUSD}, nil) s.Require().NoError(err) - s.checkMetokenBalance(meUSD.Denom, expectedBalance) + s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom) }, ) @@ -71,7 +72,7 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { usdtBalance.Leveraged = usdtBalance.Leveraged.Add(amountToLeverage) expectedBalance.SetAssetBalance(usdtBalance) - s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) + s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom) }, ) @@ -86,28 +87,24 @@ func (s *E2ETest) TestMetokenSwapAndRedeem() { "not enough", ) - s.checkMetokenBalance(mocks.MeUSDDenom, expectedBalance) + s.checkMetokenBalance(valAddr.String(), mocks.MeUSDDenom) }, ) s.Run( "redeem_50meUSD_success", func() { 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) - fmt.Printf("fiftyMeUSD: %s\n", fiftyMeUSD.String()) - fmt.Printf("usdtPrice: %s\n", usdtPrice.Price.String()) - fmt.Printf("usdtPrice.RedeemRate: %s\n", usdtPrice.RedeemRate.String()) + 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) @@ -119,12 +116,12 @@ 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) { +func (s *E2ETest) checkMetokenBalance(valAddr, denom string) { s.Require().Eventually( func() bool { resp, err := s.QueryMetokenBalances(denom) @@ -132,41 +129,41 @@ func (s *E2ETest) checkMetokenBalance(denom string, expectedBalance metoken.Inde return false } - var exist bool - for _, balance := range resp.IndexBalances { - if balance.MetokenSupply.Denom == expectedBalance.MetokenSupply.Denom { - fmt.Printf("expected[0].Fees: %s\n", expectedBalance.AssetBalances[0].Fees) - fmt.Printf("expected[0].Reserved: %s\n", expectedBalance.AssetBalances[0].Reserved) - fmt.Printf("expected[0].Leveraged: %s\n", expectedBalance.AssetBalances[0].Leveraged) - fmt.Printf("expected[0].Interest: %s\n", expectedBalance.AssetBalances[0].Interest) - fmt.Printf("expected[1].Fees: %s\n", expectedBalance.AssetBalances[1].Fees) - fmt.Printf("expected[1].Reserved: %s\n", expectedBalance.AssetBalances[1].Reserved) - fmt.Printf("expected[1].Leveraged: %s\n", expectedBalance.AssetBalances[1].Leveraged) - fmt.Printf("expected[1].Interest: %s\n", expectedBalance.AssetBalances[1].Interest) - fmt.Printf("expected[2].Fees: %s\n", expectedBalance.AssetBalances[2].Fees) - fmt.Printf("expected[2].Reserved: %s\n", expectedBalance.AssetBalances[2].Reserved) - fmt.Printf("expected[2].Leveraged: %s\n", expectedBalance.AssetBalances[2].Leveraged) - fmt.Printf("expected[2].Interest: %s\n", expectedBalance.AssetBalances[2].Interest) - fmt.Printf("----------------------\n") - fmt.Printf("balance[0].Fees: %s\n", balance.AssetBalances[0].Fees) - fmt.Printf("balance[0].Reserved: %s\n", balance.AssetBalances[0].Reserved) - fmt.Printf("balance[0].Leveraged: %s\n", balance.AssetBalances[0].Leveraged) - fmt.Printf("balance[0].Interest: %s\n", balance.AssetBalances[0].Interest) - fmt.Printf("balance[1].Fees: %s\n", balance.AssetBalances[1].Fees) - fmt.Printf("balance[1].Reserved: %s\n", balance.AssetBalances[1].Reserved) - fmt.Printf("balance[1].Leveraged: %s\n", balance.AssetBalances[1].Leveraged) - fmt.Printf("balance[1].Interest: %s\n", balance.AssetBalances[1].Interest) - fmt.Printf("balance[2].Fees: %s\n", balance.AssetBalances[2].Fees) - fmt.Printf("balance[2].Reserved: %s\n", balance.AssetBalances[2].Reserved) - fmt.Printf("balance[2].Leveraged: %s\n", balance.AssetBalances[2].Leveraged) - fmt.Printf("balance[2].Interest: %s\n", balance.AssetBalances[2].Interest) - 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) return true }, 30*time.Second, From 707849d33d870f6bfa80f0641deab13b40353cdb Mon Sep 17 00:00:00 2001 From: Egor Kostetskiy Date: Wed, 20 Sep 2023 18:42:02 -0300 Subject: [PATCH 3/3] rollback print --- x/metoken/price.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/x/metoken/price.go b/x/metoken/price.go index 42f41235c6..3c4003b9e9 100644 --- a/x/metoken/price.go +++ b/x/metoken/price.go @@ -60,10 +60,6 @@ func (ip IndexPrices) RedeemRate(from sdk.Coin, to string) (sdkmath.Int, error) return sdkmath.Int{}, err } - fmt.Printf("from.Amount: %s\n", from.Amount.String()) - fmt.Printf("toPrice: %s\n", toPrice.Price.String()) - fmt.Printf("toPrice.RedeemRate: %s\n", toPrice.RedeemRate.String()) - return toPrice.RedeemRate.MulInt(from.Amount).TruncateInt(), nil }