Skip to content

Commit

Permalink
tests: add more tests for msg server for DnR
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Nov 30, 2023
1 parent 2d59c9d commit 482cf72
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
8 changes: 8 additions & 0 deletions x/perp/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
88 changes: 85 additions & 3 deletions x/perp/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))),
Expand All @@ -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()
Expand Down Expand Up @@ -340,15 +366,17 @@ func TestFailMsgServer(t *testing.T) {
})
require.ErrorContains(t, err, "spendable balance is smaller than 1luna")
}

func TestMsgChangeCollateralDenom(t *testing.T) {
app, ctx := testapp.NewNibiruTestAppAndContext()

sender := testutil.AccAddress().String()

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",
})
Expand Down Expand Up @@ -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)
}

0 comments on commit 482cf72

Please sign in to comment.