Skip to content

Commit

Permalink
resolve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Apr 26, 2024
1 parent 022ff96 commit 2a97537
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
20 changes: 8 additions & 12 deletions x/masterchef/keeper/hooks_masterchef.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import (
stablestaketypes "github.com/elys-network/elys/x/stablestake/types"
)

func (k Keeper) GetPoolTotalSupply(ctx sdk.Context, poolId uint64) sdk.Int {
func (k Keeper) GetPoolTotalCommit(ctx sdk.Context, poolId uint64) sdk.Int {
shareDenom := ammtypes.GetPoolShareDenom(poolId)
if poolId == stablestaketypes.PoolId {
params := k.cmk.GetParams(ctx)
return params.TotalCommitted.AmountOf(stablestaketypes.GetShareDenom())
shareDenom = stablestaketypes.GetShareDenom()
}

pool, found := k.amm.GetPool(ctx, poolId)
if !found {
return sdk.ZeroInt()
}

return pool.TotalShares.Amount
params := k.cmk.GetParams(ctx)
return params.TotalCommitted.AmountOf(shareDenom)
}

func (k Keeper) GetPoolBalance(ctx sdk.Context, poolId uint64, user string) sdk.Int {
Expand All @@ -44,13 +40,13 @@ func (k Keeper) UpdateAccPerShare(ctx sdk.Context, poolId uint64, rewardDenom st
}
}

supply := k.GetPoolTotalSupply(ctx, poolId)
if supply.IsZero() {
totalCommit := k.GetPoolTotalCommit(ctx, poolId)
if totalCommit.IsZero() {
return
}
poolRewardInfo.PoolAccRewardPerShare = poolRewardInfo.PoolAccRewardPerShare.Add(
math.LegacyNewDecFromInt(amount.Mul(ammtypes.OneShare)).
Quo(math.LegacyNewDecFromInt(supply)),
Quo(math.LegacyNewDecFromInt(totalCommit)),
)
poolRewardInfo.LastUpdatedBlock = uint64(ctx.BlockHeight())
k.SetPoolRewardInfo(ctx, poolRewardInfo)
Expand Down
20 changes: 10 additions & 10 deletions x/masterchef/keeper/hooks_masterchef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ func TestHookMasterchef(t *testing.T) {

// new user join pool with same shares
share := ammtypes.InitPoolSharesSupply.Mul(math.NewIntWithDecimal(1, 5))
t.Log(mk.GetPoolTotalSupply(ctx, pools[0].PoolId))
require.Equal(t, mk.GetPoolTotalSupply(ctx, pools[0].PoolId), share)
require.Equal(t, mk.GetPoolBalance(ctx, pools[0].PoolId, addr[0].String()), share)
t.Log(mk.GetPoolTotalCommit(ctx, pools[0].PoolId))
require.Equal(t, mk.GetPoolTotalCommit(ctx, pools[0].PoolId).String(), "10002000000000000000000000")
require.Equal(t, mk.GetPoolBalance(ctx, pools[0].PoolId, addr[0].String()).String(), "10000000000000000000000000")
_, _, err = amm.JoinPoolNoSwap(ctx, addr[1], pools[0].PoolId, share, sdk.NewCoins(sdk.NewCoin(ptypes.Elys, sdk.NewInt(10000000)), sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(10000000))))
require.NoError(t, err)
require.Equal(t, mk.GetPoolTotalSupply(ctx, pools[0].PoolId), share.MulRaw(2))
require.Equal(t, mk.GetPoolTotalCommit(ctx, pools[0].PoolId).String(), "20002000000000000000000000")
require.Equal(t, mk.GetPoolBalance(ctx, pools[0].PoolId, addr[1].String()), share)

atomToken := sdk.NewCoins(sdk.NewCoin("uatom", math.NewIntWithDecimal(100000000, 6)))
Expand Down Expand Up @@ -220,12 +220,12 @@ func TestHookMasterchef(t *testing.T) {
User: addr[0].String(),
})
require.NoError(t, err)
require.Equal(t, res.TotalRewards[0].Amount, math.NewIntWithDecimal(100, 6).Mul(math.NewInt(99)).Quo(math.NewInt(2)))
require.Equal(t, res.TotalRewards[0].Amount.String(), "4949505049")
res, err = mk.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
User: addr[1].String(),
})
require.NoError(t, err)
require.Equal(t, res.TotalRewards[0].Amount, math.NewIntWithDecimal(100, 6).Mul(math.NewInt(99)).Quo(math.NewInt(2)))
require.Equal(t, res.TotalRewards[0].Amount.String(), "4949505049")

// check rewards claimed
_, err = masterchefSrv.ClaimRewards(sdk.WrapSDKContext(ctx), &types.MsgClaimRewards{
Expand All @@ -240,7 +240,7 @@ func TestHookMasterchef(t *testing.T) {
require.NoError(t, err)

atomAmount := app.BankKeeper.GetBalance(ctx, addr[1], "uatom")
require.Equal(t, atomAmount.Amount, math.NewIntWithDecimal(100, 6).Mul(math.NewInt(99)).Quo(math.NewInt(2)))
require.Equal(t, atomAmount.Amount.String(), "4949505049")

// no pending rewards
res, err = mk.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
Expand Down Expand Up @@ -272,12 +272,12 @@ func TestHookMasterchef(t *testing.T) {
User: addr[0].String(),
})
require.NoError(t, err)
require.Equal(t, res.TotalRewards[0].String(), "6666666666uatom")
require.Equal(t, res.TotalRewards[0].String(), "3999680025uatom")
res, err = mk.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
User: addr[1].String(),
})
require.NoError(t, err)
require.Equal(t, res.TotalRewards[0].String(), "3333333333uatom")
require.Equal(t, res.TotalRewards[0].String(), "1999840012uatom")

// check rewards claimed
_, err = masterchefSrv.ClaimRewards(sdk.WrapSDKContext(ctx), &types.MsgClaimRewards{
Expand All @@ -292,7 +292,7 @@ func TestHookMasterchef(t *testing.T) {
require.NoError(t, err)

atomAmount = app.BankKeeper.GetBalance(ctx, addr[1], "uatom")
require.Equal(t, atomAmount.String(), "8283333333uatom")
require.Equal(t, atomAmount.String(), "6949345061uatom")

// no pending rewards
res, err = mk.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
Expand Down

0 comments on commit 2a97537

Please sign in to comment.