From 62d5ec8e644c0fe7d2d0a3f9863b819821489947 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Mon, 17 Jun 2024 08:08:27 +0530 Subject: [PATCH] [Bug]: Fix calculate discount and add more tests (#570) * add calculate discount test and bug fix * get asset by denom --- x/tier/keeper/portfolio.go | 16 ++++++++++------ x/tier/keeper/portfolio_test.go | 26 +++++++++++++++++++++++++- x/tier/types/expected_keepers.go | 2 ++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/x/tier/keeper/portfolio.go b/x/tier/keeper/portfolio.go index 9ed1301e2..0d41f3dd7 100644 --- a/x/tier/keeper/portfolio.go +++ b/x/tier/keeper/portfolio.go @@ -30,7 +30,7 @@ func (k Keeper) RetreiveAllPortfolio(ctx sdk.Context, user string) { totalValue := sdk.NewDec(0) for _, balance := range balances { tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, balance.Denom) - asset, found := k.assetProfileKeeper.GetEntry(ctx, balance.Denom) + asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, balance.Denom) if !found { continue } @@ -45,7 +45,7 @@ func (k Keeper) RetreiveAllPortfolio(ctx sdk.Context, user string) { if err1 == nil { for _, balance := range estaking.Total { tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, balance.Denom) - asset, found := k.assetProfileKeeper.GetEntry(ctx, balance.Denom) + asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, balance.Denom) if !found { continue } @@ -57,7 +57,7 @@ func (k Keeper) RetreiveAllPortfolio(ctx sdk.Context, user string) { if err2 == nil { for _, balance := range masterchef.TotalRewards { tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, balance.Denom) - asset, found := k.assetProfileKeeper.GetEntry(ctx, balance.Denom) + asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, balance.Denom) if !found { continue } @@ -70,7 +70,7 @@ func (k Keeper) RetreiveAllPortfolio(ctx sdk.Context, user string) { perpetuals, _, err := k.perpetual.GetMTPsForAddress(ctx, sender, &query.PageRequest{}) if err == nil { for _, perpetual := range perpetuals { - asset, found := k.assetProfileKeeper.GetEntry(ctx, perpetual.GetTradingAsset()) + asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, perpetual.GetTradingAsset()) if !found { continue } @@ -98,7 +98,7 @@ func (k Keeper) RetreiveAllPortfolio(ctx sdk.Context, user string) { totalValue = totalValue.Add(amount.Mul(info.LpTokenPrice)) } else { tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, commitment.Denom) - asset, found := k.assetProfileKeeper.GetEntry(ctx, commitment.Denom) + asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, commitment.Denom) if !found { continue } @@ -111,7 +111,7 @@ func (k Keeper) RetreiveAllPortfolio(ctx sdk.Context, user string) { delegations := k.stakingKeeper.GetAllDelegatorDelegations(ctx, sender) bondDenom := k.stakingKeeper.BondDenom(ctx) tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, bondDenom) - asset, found := k.assetProfileKeeper.GetEntry(ctx, bondDenom) + asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, bondDenom) if found { for _, delegation := range delegations { amount := delegation.Shares.Quo(Pow10(asset.Decimals)) @@ -181,6 +181,10 @@ func (k Keeper) GetMembershipTier(ctx sdk.Context, user string) (total_portfoili } } + if minTotal.Equal(sdk.NewDec(math.MaxInt64)) { + return sdk.NewDec(0), "bronze", 0 + } + // TODO: Make tier discount and minimum balance configurable if minTotal.GTE(sdk.NewDec(500000)) { return minTotal, "platinum", 30 diff --git a/x/tier/keeper/portfolio_test.go b/x/tier/keeper/portfolio_test.go index 3067800a9..b4cb3315a 100644 --- a/x/tier/keeper/portfolio_test.go +++ b/x/tier/keeper/portfolio_test.go @@ -178,6 +178,29 @@ func TestGetPortfolioAmm(t *testing.T) { require.Equal(t, portfolio, sdk.NewDec(100100)) } +func TestPortfolioGetDiscount(t *testing.T) { + keeper, ctx := keepertest.MembershiptierKeeper(t) + items := make([]types.Portfolio, 10) + for j := 0; j < 8; j++ { + ctx = ctx.WithBlockTime(ctx.BlockTime().AddDate(0, 0, 1)) + for i := range items { + items[i].Creator = strconv.Itoa(i) + items[i].Portfolio = sdk.NewDec(400000) + + keeper.SetPortfolio(ctx, keeper.GetDateFromBlock(ctx.BlockTime()), items[i].Creator, items[i]) + } + } + + items[9].Portfolio = sdk.NewDec(500) + keeper.SetPortfolio(ctx, keeper.GetDateFromBlock(ctx.BlockTime()), items[9].Creator, items[9]) + + _, _, discount := keeper.GetMembershipTier(ctx, items[0].Creator) + require.Equal(t, discount, uint64(20)) + + _, _, discount = keeper.GetMembershipTier(ctx, items[9].Creator) + require.Equal(t, discount, uint64(0)) +} + func TestGetPortfolioPerpetual(t *testing.T) { app := simapp.InitElysTestApp(true) ctx := app.BaseApp.NewContext(true, tmproto.Header{}) @@ -286,7 +309,8 @@ func SetupCoinPrices(ctx sdk.Context, oracle oraclekeeper.Keeper, assetProfiler Display: "ATOM", Decimal: 6, }) - assetProfiler.SetEntry(ctx, profiletypes.Entry{BaseDenom: ptypes.Elys}) + assetProfiler.SetEntry(ctx, profiletypes.Entry{BaseDenom: ptypes.Elys, Denom: ptypes.Elys}) + assetProfiler.SetEntry(ctx, profiletypes.Entry{BaseDenom: ptypes.BaseCurrency, Denom: ptypes.BaseCurrency}) oracle.SetPrice(ctx, oracletypes.Price{ Asset: "USDC", diff --git a/x/tier/types/expected_keepers.go b/x/tier/types/expected_keepers.go index 0373616d0..5900a0ae1 100644 --- a/x/tier/types/expected_keepers.go +++ b/x/tier/types/expected_keepers.go @@ -51,6 +51,8 @@ type AssetProfileKeeper interface { GetEntry(ctx sdk.Context, baseDenom string) (val assetprofiletypes.Entry, found bool) // GetUsdcDenom returns USDC denom GetUsdcDenom(ctx sdk.Context) (string, bool) + // GetEntryByDenom returns a entry from its denom value + GetEntryByDenom(ctx sdk.Context, denom string) (val assetprofiletypes.Entry, found bool) } type AmmKeeper interface {