Skip to content

Commit

Permalink
Merge branch 'master' into realu/perp-sg
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Nov 28, 2023
2 parents 1220834 + 2d59c9d commit 696d636
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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 All @@ -93,6 +94,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1606](https://github.com/NibiruChain/nibiru/pull/1606) - fix(perp): emit `MarketUpdatedEvent` in the absence of index price
* [#1649](https://github.com/NibiruChain/nibiru/pull/1649) - fix(ledger): fix ledger for newer macos versions
* [#1655](https://github.com/NibiruChain/nibiru/pull/1655) - fix(inflation): inflate NIBI correctly to strategic treasury account
* [#1677](https://github.com/NibiruChain/nibiru/pull/1677) - fix(perp): make Gen_market set initial perp versions

## [v0.21.10]

Expand Down
10 changes: 10 additions & 0 deletions x/perp/v2/client/cli/gen_market.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ func AddMarketGenesisCmd(defaultNodeHome string) *cobra.Command {
perpGenState.Markets = append(perpGenState.Markets, market)
perpGenState.Amms = append(perpGenState.Amms, amm)

var marketsLastVersion []types.GenesisMarketLastVersion
for _, market := range perpGenState.Markets {
marketsLastVersion = append(marketsLastVersion, types.GenesisMarketLastVersion{
Pair: market.Pair,
Version: market.Version,
})
}

perpGenState.MarketLastVersions = marketsLastVersion

perpGenStateBz, err := clientCtx.Codec.MarshalJSON(perpGenState)
if err != nil {
return fmt.Errorf("failed to marshal market genesis state: %w", err)
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()
}
1 change: 1 addition & 0 deletions x/perp/v2/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func DefaultGenesis() *GenesisState {
Amms: []AMM{},
Positions: []GenesisPosition{},
ReserveSnapshots: []ReserveSnapshot{},
CollateralDenom: TestingCollateralDenomNUSD,
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestDefaultGenesis(t *testing.T) {
require.Empty(t, genesis.Amms)
require.Empty(t, genesis.Positions)
require.Empty(t, genesis.ReserveSnapshots)
require.Empty(t, genesis.CollateralDenom)
require.NotNil(t, genesis.CollateralDenom)
}

func TestGenesisValidate(t *testing.T) {
Expand Down

0 comments on commit 696d636

Please sign in to comment.