Skip to content

Commit

Permalink
E2E: Increase masterchef coverage (#976)
Browse files Browse the repository at this point in the history
* add more tests

* query tests

* add more tests

* remove as we will switch to autocli

* add msg server tests

* add tests
  • Loading branch information
amityadav0 authored Nov 21, 2024
1 parent 79379d5 commit d974e53
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 38 deletions.
97 changes: 97 additions & 0 deletions x/masterchef/client/cli/query_aprs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cli_test

import (
"bytes"
"context"
"fmt"
"io"

sdkmath "cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/gogoproto/proto"
"github.com/elys-network/elys/x/masterchef/client/cli"
"github.com/elys-network/elys/x/masterchef/types"
)

func (s *CLITestSuite) TestAprs() {
cmd := cli.CmdAprs()
cmd.SetOutput(io.Discard)

testCases := []struct {
name string
ctxGen func() client.Context
args []string
expectResult proto.Message
expectErr bool
}{
{
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryAprsResponse{
UsdcAprUsdc: sdkmath.LegacyOneDec(),
EdenAprUsdc: sdkmath.LegacyOneDec(),
UsdcAprEdenb: sdkmath.LegacyOneDec(),
EdenAprEdenb: sdkmath.LegacyOneDec(),
})
c := clitestutil.NewMockCometRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QueryAprsResponse{
UsdcAprUsdc: sdkmath.LegacyOneDec(),
EdenAprUsdc: sdkmath.LegacyOneDec(),
UsdcAprEdenb: sdkmath.LegacyOneDec(),
EdenAprEdenb: sdkmath.LegacyOneDec(),
},
false,
},
{
"invalid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QueryAprsResponse{})
c := clitestutil.NewMockCometRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
"-1",
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QueryAprsResponse{},
true,
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
var outBuf bytes.Buffer

clientCtx := tc.ctxGen().WithOutput(&outBuf)
ctx := svrcmd.CreateExecuteContext(context.Background())

cmd.SetContext(ctx)
cmd.SetArgs(tc.args)

s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))

err := cmd.Execute()
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(s.encCfg.Codec.UnmarshalJSON(outBuf.Bytes(), tc.expectResult))
s.Require().NoError(err)
}
})
}
}
39 changes: 39 additions & 0 deletions x/masterchef/client/cli/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli_test

import (
"io"
"testing"

rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/bank"
)

type CLITestSuite struct {
suite.Suite

kr keyring.Keyring
encCfg testutilmod.TestEncodingConfig
baseCtx client.Context
}

func TestMigrateTestSuite(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}

func (s *CLITestSuite) SetupSuite() {
s.encCfg = testutilmod.MakeTestEncodingConfig(bank.AppModuleBasic{})
s.kr = keyring.NewInMemory(s.encCfg.Codec)
s.baseCtx = client.Context{}.
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard)
}
8 changes: 4 additions & 4 deletions x/masterchef/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (k Keeper) UpdateLPRewards(ctx sdk.Context) error {
}

// Calculate new Eden for this pool
newEdenAllocatedForPool := poolShareEdenEnable.MulInt(lpsEdenAmount)
newEdenAllocatedForPool := math.LegacyZeroDec()

// Maximum eden APR - 30% by default
poolMaxEdenAmount := params.MaxEdenRewardAprLps.
Expand All @@ -234,6 +234,7 @@ func (k Keeper) UpdateLPRewards(ctx sdk.Context) error {

// Use min amount (eden allocation from tokenomics and max apr based eden amount)
if pool.EnableEdenRewards {
newEdenAllocatedForPool = poolShareEdenEnable.MulInt(lpsEdenAmount)
newEdenAllocatedForPool = math.LegacyMinDec(newEdenAllocatedForPool, poolMaxEdenAmount)
if newEdenAllocatedForPool.IsPositive() {
err = k.commitmentKeeper.MintCoins(ctx, types.ModuleName, sdk.Coins{sdk.NewCoin(ptypes.Eden, newEdenAllocatedForPool.TruncateInt())})
Expand Down Expand Up @@ -266,9 +267,6 @@ func (k Keeper) UpdateLPRewards(ctx sdk.Context) error {

// Track pool rewards accumulation
edenReward := newEdenAllocatedForPool
if !pool.EnableEdenRewards {
edenReward = math.LegacyZeroDec()
}

k.AddPoolRewardsAccum(
ctx,
Expand Down Expand Up @@ -582,6 +580,8 @@ func (k Keeper) InitPoolParams(ctx sdk.Context, poolId uint64) bool {
ExternalIncentiveApr: math.LegacyZeroDec(),
// external reward denoms on the pool
ExternalRewardDenoms: []string{},
// enable eden reward on the pool
EnableEdenRewards: false,
}
k.SetPoolInfo(ctx, poolInfo)
}
Expand Down
30 changes: 30 additions & 0 deletions x/masterchef/keeper/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
"time"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -380,4 +382,32 @@ func (suite *MasterchefKeeperTestSuite) TestExternalRewardsDistribution() {
suite.Require().True(found)
suite.Require().Equal(rewardInfo.RewardDenom, externalIncentive2.RewardDenom)
suite.Require().Equal(rewardInfo.PoolAccRewardPerShare, sdkmath.LegacyMustNewDecFromStr("0.000099900099900099"))

// Get Tvl for non-existent pool
res := suite.app.MasterchefKeeper.GetPoolTVL(suite.ctx, 1000)
suite.Require().Equal(res, sdkmath.LegacyZeroDec())

// increase timestamp
suite.ctx = suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(time.Hour))
suite.app.MasterchefKeeper.ProcessExternalRewardsDistribution(suite.ctx)

// set current block to last block
suite.ctx = suite.ctx.WithBlockHeight(externalIncentive.ToBlock)

suite.app.MasterchefKeeper.ProcessExternalRewardsDistribution(suite.ctx)

// should delete incentives
listInc := suite.app.MasterchefKeeper.GetAllExternalIncentives(suite.ctx)
suite.Require().Equal(len(listInc), 0)
}

func (suite *MasterchefKeeperTestSuite) TestInitialParams() {
suite.ResetSuite(true)

res := suite.app.MasterchefKeeper.InitPoolParams(suite.ctx, 1)
suite.Require().Equal(res, true)

poolInfo, found := suite.app.MasterchefKeeper.GetPoolInfo(suite.ctx, 1)
suite.Require().Equal(found, true)
suite.Require().Equal(poolInfo.PoolId, uint64(1))
}
14 changes: 12 additions & 2 deletions x/masterchef/keeper/external_incentive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ func (suite *MasterchefKeeperTestSuite) TestUSDCExternalIncentive() {
})
suite.Require().NoError(err)

info, _ := suite.app.MasterchefKeeper.GetPoolInfo(suite.ctx, pools[0].PoolId)
info.EnableEdenRewards = true
suite.app.MasterchefKeeper.SetPoolInfo(suite.ctx, info)

// Fill in pool revenue wallet
revenueAddress1 := ammtypes.NewPoolRevenueAddress(1)
suite.MintTokenToAddress(revenueAddress1, math.NewInt(100000), ptypes.BaseCurrency)
Expand All @@ -192,12 +196,14 @@ func (suite *MasterchefKeeperTestSuite) TestUSDCExternalIncentive() {
User: addr[0].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "4949545046")
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "49")
suite.Require().Equal(res.TotalRewards[1].Amount.String(), "4949545046")
res, err = suite.app.MasterchefKeeper.UserPendingReward(suite.ctx, &types.QueryUserPendingRewardRequest{
User: addr[1].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "4949545046")
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "49")
suite.Require().Equal(res.TotalRewards[1].Amount.String(), "4949545046")

prevUSDCBal := suite.app.BankKeeper.GetBalance(suite.ctx, addr[1], ptypes.BaseCurrency)

Expand Down Expand Up @@ -228,4 +234,8 @@ func (suite *MasterchefKeeperTestSuite) TestUSDCExternalIncentive() {
})
suite.Require().NoError(err)
suite.Require().Len(res.TotalRewards, 0)

// Eden should not be claimable
curEdenBal := suite.app.BankKeeper.GetBalance(suite.ctx, addr[1], ptypes.Eden)
suite.Require().Equal(curEdenBal.Amount.String(), "0")
}
7 changes: 0 additions & 7 deletions x/masterchef/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package keeper

import (
"cosmossdk.io/core/store"
"fmt"

"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
estakingkeeper "github.com/elys-network/elys/x/estaking/keeper"

"github.com/elys-network/elys/x/masterchef/types"
Expand Down Expand Up @@ -68,7 +65,3 @@ func NewKeeper(
authority: authority,
}
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
Loading

0 comments on commit d974e53

Please sign in to comment.