Skip to content

Commit

Permalink
[Bug]: Fix calculate discount and add more tests (#570)
Browse files Browse the repository at this point in the history
* add calculate discount test and bug fix

* get asset by denom
  • Loading branch information
amityadav0 authored Jun 17, 2024
1 parent 85e9ff7 commit 62d5ec8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
16 changes: 10 additions & 6 deletions x/tier/keeper/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion x/tier/keeper/portfolio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions x/tier/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 62d5ec8

Please sign in to comment.