Skip to content

Commit

Permalink
Merge branch 'master' into fix/fix-version-at-genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt authored Nov 27, 2023
2 parents 43f0335 + b102818 commit 864317b
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1639](https://github.com/NibiruChain/nibiru/pull/1639) - fix(perp): by default, disable new markets until they are toggled on.
* [#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

### 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
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,
}
}

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")
}

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()
}

0 comments on commit 864317b

Please sign in to comment.