From 482cf72a3d738d66431f3896b9b9dd6e68e6608d Mon Sep 17 00:00:00 2001 From: matthiasmatt Date: Thu, 30 Nov 2023 14:58:55 +0100 Subject: [PATCH] tests: add more tests for msg server for DnR --- x/perp/v2/keeper/msg_server.go | 8 +++ x/perp/v2/keeper/msg_server_test.go | 88 ++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/x/perp/v2/keeper/msg_server.go b/x/perp/v2/keeper/msg_server.go index ae2e9d35c..133275bf8 100644 --- a/x/perp/v2/keeper/msg_server.go +++ b/x/perp/v2/keeper/msg_server.go @@ -159,6 +159,10 @@ func (m msgServer) ChangeCollateralDenom( } func (m msgServer) AllocateEpochRebates(ctx context.Context, msg *types.MsgAllocateEpochRebates) (*types.MsgAllocateEpochRebatesResponse, error) { + if msg == nil { + return nil, common.ErrNilMsg() + } + sender, err := sdk.AccAddressFromBech32(msg.Sender) if err != nil { return nil, err @@ -172,6 +176,10 @@ func (m msgServer) AllocateEpochRebates(ctx context.Context, msg *types.MsgAlloc } func (m msgServer) WithdrawEpochRebates(ctx context.Context, msg *types.MsgWithdrawEpochRebates) (*types.MsgWithdrawEpochRebatesResponse, error) { + if msg == nil { + return nil, common.ErrNilMsg() + } + sender, err := sdk.AccAddressFromBech32(msg.Sender) if err != nil { return nil, err diff --git a/x/perp/v2/keeper/msg_server_test.go b/x/perp/v2/keeper/msg_server_test.go index 96813846b..e76c0a3b7 100644 --- a/x/perp/v2/keeper/msg_server_test.go +++ b/x/perp/v2/keeper/msg_server_test.go @@ -7,6 +7,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + inflationtypes "github.com/NibiruChain/nibiru/x/inflation/types" + "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" @@ -143,7 +145,7 @@ func TestMsgServerAddMargin(t *testing.T) { ), BalanceEqual(alice, types.TestingCollateralDenomNUSD, sdk.NewInt(98)), ), - TC("partial close"). + TC("msg server close"). Given( CreateCustomMarket(pair, WithEnabled(true)), FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(types.TestingCollateralDenomNUSD, 100))), @@ -167,6 +169,30 @@ func TestMsgServerAddMargin(t *testing.T) { ), BalanceEqual(alice, types.TestingCollateralDenomNUSD, sdk.NewInt(98)), ), + TC("partial close"). + Given( + CreateCustomMarket(pair, WithEnabled(true)), + FundAccount(alice, sdk.NewCoins(sdk.NewInt64Coin(types.TestingCollateralDenomNUSD, 100))), + MarketOrder(alice, pair, types.Direction_LONG, sdk.OneInt(), sdk.OneDec(), sdk.ZeroDec()), + MoveToNextBlock(), + ). + When( + MsgServerPartialClosePosition(alice, pair, sdk.MustNewDecFromStr("0.5")), + ). + Then( + PositionShouldBeEqual(alice, pair, + Position_PositionShouldBeEqualTo(types.Position{ + TraderAddress: alice.String(), + Pair: pair, + Size_: sdk.MustNewDecFromStr("0.499999999999"), + Margin: sdk.NewDec(1), + OpenNotional: sdk.MustNewDecFromStr("0.499999999999250000"), + LatestCumulativePremiumFraction: sdk.ZeroDec(), + LastUpdatedBlockNumber: 2, + }), + ), + BalanceEqual(alice, types.TestingCollateralDenomNUSD, sdk.NewInt(99)), + ), } NewTestSuite(t).WithTestCases(tests...).Run() @@ -340,7 +366,6 @@ func TestFailMsgServer(t *testing.T) { }) require.ErrorContains(t, err, "spendable balance is smaller than 1luna") } - func TestMsgChangeCollateralDenom(t *testing.T) { app, ctx := testapp.NewNibiruTestAppAndContext() @@ -348,7 +373,10 @@ func TestMsgChangeCollateralDenom(t *testing.T) { msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2) - _, err := msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{ + _, err := msgServer.ChangeCollateralDenom(ctx, nil) + require.ErrorContains(t, err, "nil msg") + + _, err = msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{ Sender: sender, NewDenom: "luna", }) @@ -412,3 +440,57 @@ func TestMsgServerSettlePosition(t *testing.T) { NewTestSuite(t).WithTestCases(tests...).Run() } + +func TestAllocateEpochRebates(t *testing.T) { + app, ctx := testapp.NewNibiruTestAppAndContext() + + sender := testutil.AccAddress().String() + + msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2) + + _, err := msgServer.AllocateEpochRebates(ctx, nil) + require.ErrorContains(t, err, "nil msg") + + _, err = msgServer.AllocateEpochRebates(ctx, &types.MsgAllocateEpochRebates{}) + require.ErrorContains(t, err, "empty address string is not allowed") + + _, err = msgServer.AllocateEpochRebates(ctx, &types.MsgAllocateEpochRebates{ + Sender: sender, + Rebates: sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100))), + }) + require.ErrorContains(t, err, "insufficient funds") + + app.BankKeeper.MintCoins(ctx, inflationtypes.ModuleName, sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100)))) + app.BankKeeper.SendCoinsFromModuleToAccount(ctx, inflationtypes.ModuleName, sdk.MustAccAddressFromBech32(sender), sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100)))) + + _, err = msgServer.AllocateEpochRebates(ctx, &types.MsgAllocateEpochRebates{ + Sender: sender, + Rebates: sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100))), + }) + require.NoError(t, err) + + // Withdraw rebates + _, err = msgServer.WithdrawEpochRebates(ctx, nil) + require.ErrorContains(t, err, "nil msg") + + _, err = msgServer.WithdrawEpochRebates(ctx, &types.MsgWithdrawEpochRebates{}) + require.ErrorContains(t, err, "empty address string is not allowed") + + _, err = msgServer.WithdrawEpochRebates(ctx, &types.MsgWithdrawEpochRebates{ + Sender: sender, + Epochs: []uint64{1}, + }) + require.ErrorContains(t, err, "collections: not found") + + currentEpoch, err := app.PerpKeeperV2.DnREpoch.Get(ctx) + require.NoError(t, err) + + require.NoError(t, app.PerpKeeperV2.StartNewEpoch(ctx, currentEpoch+1)) + require.NoError(t, app.PerpKeeperV2.StartNewEpoch(ctx, currentEpoch+2)) + + _, err = msgServer.WithdrawEpochRebates(ctx, &types.MsgWithdrawEpochRebates{ + Sender: sender, + Epochs: []uint64{1}, + }) + require.NoError(t, err) +}