Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(perp): tests for denoms #1679

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions x/perp/v2/integration/action/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package action

import (
"fmt"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -184,6 +186,71 @@ func MsgServerRemoveMargin(
}
}

type msgServerSettlePosition struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}

func (m msgServerSettlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)

// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})

return ctx, err, true
}

func MsgServerSettlePosition(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePosition{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
}

type msgServerSettlePositionShouldFail struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}

func (m msgServerSettlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)

// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})
if err == nil {
return ctx, fmt.Errorf("should fail but no error returned"), true
}

return ctx, nil, true
}

func MsgServerSettlePositionShouldFail(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePositionShouldFail{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
Comment on lines +189 to +251
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new structs msgServerSettlePosition and msgServerSettlePositionShouldFail have been added, and they implement the action.Action interface. The Do methods for both structs are correctly implemented, and the error handling in msgServerSettlePositionShouldFail is appropriate for a test that expects failure. However, the field Version in both structs should follow Go naming conventions and start with a lowercase letter unless it is exported.

- Version       uint64
+ version       uint64

Commitable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
type msgServerSettlePosition struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}
func (m msgServerSettlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})
return ctx, err, true
}
func MsgServerSettlePosition(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePosition{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
}
type msgServerSettlePositionShouldFail struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}
func (m msgServerSettlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})
if err == nil {
return ctx, fmt.Errorf("should fail but no error returned"), true
}
return ctx, nil, true
}
func MsgServerSettlePositionShouldFail(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePositionShouldFail{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
type msgServerSettlePosition struct {
pair asset.Pair
traderAddress sdk.AccAddress
version uint64
}
func (m msgServerSettlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.version,
})
return ctx, err, true
}
func MsgServerSettlePosition(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePosition{
pair: pair,
traderAddress: traderAddress,
version: version,
}
}
type msgServerSettlePositionShouldFail struct {
pair asset.Pair
traderAddress sdk.AccAddress
version uint64
}
func (m msgServerSettlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.version,
})
if err == nil {
return ctx, fmt.Errorf("should fail but no error returned"), true
}
return ctx, nil, true
}
func MsgServerSettlePositionShouldFail(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePositionShouldFail{
pair: pair,
traderAddress: traderAddress,
version: version,
}

}

type msgServerDonateToPerpEf struct {
sender sdk.AccAddress
amount sdkmath.Int
Expand Down
73 changes: 73 additions & 0 deletions x/perp/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
. "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion"
"github.com/NibiruChain/nibiru/x/perp/v2/keeper"
"github.com/NibiruChain/nibiru/x/perp/v2/types"
sudoerTypes "github.com/NibiruChain/nibiru/x/sudo/types"
)

func TestMsgServerMarketOrder(t *testing.T) {
Expand Down Expand Up @@ -339,3 +340,75 @@ func TestFailMsgServer(t *testing.T) {
})
require.ErrorContains(t, err, "spendable balance is smaller than 1luna")
Comment on lines 340 to 341
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message in the test case for DonateToEcosystemFund seems to be missing the actual balance in the error string. It would be more informative if the error message included the available balance.

}

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

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

msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)

_, err := msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{
Sender: sender,
NewDenom: "luna",
})
require.ErrorContains(t, err, "insufficient permissions on smart contract")

app.SudoKeeper.Sudoers.Set(ctx, sudoerTypes.Sudoers{Contracts: []string{sender}})
_, err = msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{
Sender: sender,
NewDenom: "luna",
})
require.NoError(t, err)

app.SudoKeeper.Sudoers.Set(ctx, sudoerTypes.Sudoers{Contracts: []string{sender}})
_, err = msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{
Sender: sender,
NewDenom: "",
})
require.ErrorContains(t, err, "invalid denom")

app.SudoKeeper.Sudoers.Set(ctx, sudoerTypes.Sudoers{Contracts: []string{sender}})
_, err = msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{
NewDenom: "luna",
})
require.ErrorContains(t, err, "invalid sender address")
}

func TestMsgServerSettlePosition(t *testing.T) {
pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD)
alice := testutil.AccAddress()

tests := TestCases{
TC("Settleposition").
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(),
CloseMarket(pair),
).
When(
MsgServerSettlePosition(alice, pair, 1),
).
Then(
PositionShouldNotExist(alice, pair, 1),
BalanceEqual(alice, types.TestingCollateralDenomNUSD, sdk.NewInt(100)),
),
TC("SettlepositionOpenedMarket").
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(
MsgServerSettlePositionShouldFail(alice, pair, 1),
).
Then(
PositionShouldExist(alice, pair, 1),
),
}

NewTestSuite(t).WithTestCases(tests...).Run()
}
Loading