Skip to content

Commit

Permalink
test1
Browse files Browse the repository at this point in the history
  • Loading branch information
kosegor committed Sep 19, 2023
1 parent bfd312a commit e3ddf4d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.
68 changes: 55 additions & 13 deletions tests/e2e/e2e_metoken_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
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"
"github.com/umee-network/umee/v6/x/metoken/mocks"
)

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() {
Expand All @@ -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)
},
)

Expand All @@ -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()
Expand All @@ -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)
},
)

Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -133,20 +135,60 @@ 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
}
}

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
41 changes: 12 additions & 29 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,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.
Expand Down

0 comments on commit e3ddf4d

Please sign in to comment.