Skip to content

Commit

Permalink
test(perp-keeper): add more tests for msg server for DnR (#1686)
Browse files Browse the repository at this point in the history
* tests: add more tests for msg server for DnR

* chore: changelog

* refactor: fix linter errors

---------

Co-authored-by: Unique Divine <[email protected]>
Co-authored-by: Unique-Divine <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2023
1 parent 31f78b0 commit de0675a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1652](https://github.com/NibiruChain/nibiru/pull/1652) - test: refactors cli.network suites with 'Integration' to use common function
* [#1659](https://github.com/NibiruChain/nibiru/pull/1659) - refactor(oracle): curate oracle default whitelist
* [#1679](https://github.com/NibiruChain/nibiru/pull/1679) - test(perp): add more tests for perp module msg server
* [#1686](https://github.com/NibiruChain/nibiru/pull/1686) - test(perp): add more tests for perp module msg server for DnR

### Dependencies
- Bump `github.com/prometheus/client_golang` from 1.16.0 to 1.17.0 ([#1605](https://github.com/NibiruChain/nibiru/pull/1605))
Expand Down
3 changes: 1 addition & 2 deletions x/perp/v2/integration/action/dnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ func FundDnREpoch(amt sdk.Coins) action.Action {
return fundDnREpoch{amt}
}

type startNewDnRepochAction struct {
}
type startNewDnRepochAction struct{}

func (s startNewDnRepochAction) Do(app *app.NibiruApp, ctx sdk.Context) (outCtx sdk.Context, err error, isMandatory bool) {
currentEpoch, err := app.PerpKeeperV2.DnREpoch.Get(ctx)
Expand Down
18 changes: 12 additions & 6 deletions x/perp/v2/keeper/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,28 +296,34 @@ func TestAdmin_ChangeCollateralDenom(t *testing.T) {
}{
{name: "happy: normal denom", newDenom: "nusd", sender: adminSender, wantErr: ""},

{name: "happy: token factory denom",
{
name: "happy: token factory denom",
newDenom: tftypes.TFDenom{
Creator: testutil.AccAddress().String(),
Subdenom: "nusd",
}.String(), sender: adminSender, wantErr: ""},
}.String(), sender: adminSender, wantErr: "",
},

{name: "happy: token factory denom",
{
name: "happy: token factory denom",
newDenom: tftypes.TFDenom{
Creator: testutil.AccAddress().String(),
Subdenom: "nusd",
}.String(), sender: adminSender, wantErr: "",
},

{name: "happy: IBC denom",
{
name: "happy: IBC denom",
newDenom: "ibc/46B44899322F3CD854D2D46DEEF881958467CDD4B3B10086DA49296BBED94BED", // JUNO on Osmosis
sender: adminSender, wantErr: "",
},

{name: "sad: invalid denom",
{
name: "sad: invalid denom",
newDenom: "", sender: adminSender, wantErr: types.ErrInvalidCollateral.Error(),
},
{name: "sad: sender not in sudoers",
{
name: "sad: sender not in sudoers",
newDenom: "nusd", sender: nonAdminSender, wantErr: "insufficient permissions on smart contract",
},
} {
Expand Down
9 changes: 8 additions & 1 deletion x/perp/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (m msgServer) SettlePosition(ctx context.Context, msg *types.MsgSettlePosit
// These fields should have already been validated by MsgSettlePosition.ValidateBasic() prior to being sent to the msgServer.
traderAddr := sdk.MustAccAddressFromBech32(msg.Sender)
resp, err := m.k.SettlePosition(sdk.UnwrapSDKContext(ctx), msg.Pair, msg.Version, traderAddr)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,6 +158,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 +175,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
96 changes: 94 additions & 2 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 @@ -348,7 +374,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",
})
Expand Down Expand Up @@ -412,3 +441,66 @@ 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")

require.NoError(t,
app.BankKeeper.MintCoins(ctx,
inflationtypes.ModuleName,
sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100))),
),
)
require.NoError(t,
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 de0675a

Please sign in to comment.