From 596c66445baf6cead53c96ed45c00043787b9738 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 17 Nov 2023 18:57:51 +0000 Subject: [PATCH 01/21] chore(deps): Bump cosmossdk.io/math from 1.1.2 to 1.2.0 Bumps [cosmossdk.io/math](https://github.com/cosmos/cosmos-sdk) from 1.1.2 to 1.2.0. - [Release notes](https://github.com/cosmos/cosmos-sdk/releases) - [Changelog](https://github.com/cosmos/cosmos-sdk/blob/main/CHANGELOG.md) - [Commits](https://github.com/cosmos/cosmos-sdk/compare/math/v1.1.2...log/v1.2.0) --- updated-dependencies: - dependency-name: cosmossdk.io/math dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f5dd99804..c85396c06 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( cosmossdk.io/errors v1.0.0 - cosmossdk.io/math v1.1.2 + cosmossdk.io/math v1.2.0 github.com/CosmWasm/wasmd v0.44.0 github.com/CosmWasm/wasmvm v1.5.0 github.com/MakeNowJust/heredoc/v2 v2.0.1 diff --git a/go.sum b/go.sum index d589c820f..111a49b0b 100644 --- a/go.sum +++ b/go.sum @@ -201,8 +201,8 @@ cosmossdk.io/errors v1.0.0 h1:nxF07lmlBbB8NKQhtJ+sJm6ef5uV1XkvPXG2bUntb04= cosmossdk.io/errors v1.0.0/go.mod h1:+hJZLuhdDE0pYN8HkOrVNwrIOYvUGnn6+4fjnJs/oV0= cosmossdk.io/log v1.2.1 h1:Xc1GgTCicniwmMiKwDxUjO4eLhPxoVdI9vtMW8Ti/uk= cosmossdk.io/log v1.2.1/go.mod h1:GNSCc/6+DhFIj1aLn/j7Id7PaO8DzNylUZoOYBL9+I4= -cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM= -cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0= +cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig= +cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0= cosmossdk.io/tools/rosetta v0.2.1 h1:ddOMatOH+pbxWbrGJKRAawdBkPYLfKXutK9IETnjYxw= cosmossdk.io/tools/rosetta v0.2.1/go.mod h1:Pqdc1FdvkNV3LcNIkYWt2RQY6IP1ge6YWZk8MhhO9Hw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= From 8722fce73c76f83810943a21d043800577c0a1f3 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Mon, 27 Nov 2023 18:29:28 -0600 Subject: [PATCH 02/21] feat(perp): msg server methods for shift peg and shift swap invariant --- proto/nibiru/perp/v2/tx.proto | 56 +- wasmbinding/exec_perp.go | 4 +- x/perp/v2/integration/action/market.go | 4 +- x/perp/v2/keeper/admin.go | 30 + x/perp/v2/keeper/admin_test.go | 126 +++ x/perp/v2/keeper/amm.go | 24 +- x/perp/v2/keeper/amm_test.go | 18 +- x/perp/v2/keeper/msg_server.go | 26 + x/perp/v2/types/codec.go | 4 + x/perp/v2/types/codec_test.go | 2 + x/perp/v2/types/msgs.go | 78 +- x/perp/v2/types/tx.pb.go | 1146 +++++++++++++++++++++--- 12 files changed, 1340 insertions(+), 178 deletions(-) diff --git a/proto/nibiru/perp/v2/tx.proto b/proto/nibiru/perp/v2/tx.proto index 433b78eb5..f30737952 100644 --- a/proto/nibiru/perp/v2/tx.proto +++ b/proto/nibiru/perp/v2/tx.proto @@ -32,9 +32,21 @@ service Msg { rpc ChangeCollateralDenom(MsgChangeCollateralDenom) returns (MsgChangeCollateralDenomResponse) {} - rpc AllocateEpochRebates(MsgAllocateEpochRebates) returns (MsgAllocateEpochRebatesResponse) {} + rpc AllocateEpochRebates(MsgAllocateEpochRebates) + returns (MsgAllocateEpochRebatesResponse) {} - rpc WithdrawEpochRebates(MsgWithdrawEpochRebates) returns (MsgWithdrawEpochRebatesResponse) {} + rpc WithdrawEpochRebates(MsgWithdrawEpochRebates) + returns (MsgWithdrawEpochRebatesResponse) {} + + // ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. + // Admin-only. + rpc ShiftPegMultiplier(MsgShiftPegMultiplier) + returns (MsgShiftPegMultiplierResponse) {} + + // ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. + // Admin-only. + rpc ShiftSwapInvariant(MsgShiftSwapInvariant) + returns (MsgShiftSwapInvariantResponse) {} } // -------------------------- Settle Position -------------------------- @@ -376,4 +388,42 @@ message MsgWithdrawEpochRebatesResponse { (gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins" ]; -} \ No newline at end of file +} + +// -------------------------- ShiftPegMultiplier -------------------------- + +// ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. +// Admin-only. +message MsgShiftPegMultiplier { + string sender = 1; + string pair = 2 [ + (gogoproto.customtype) = + "github.com/NibiruChain/nibiru/x/common/asset.Pair", + (gogoproto.nullable) = false + ]; + string new_peg_mult = 3 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; +} + +message MsgShiftPegMultiplierResponse {} + +// -------------------------- ShiftSwapInvariant -------------------------- + +// ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. +// Admin-only. +message MsgShiftSwapInvariant { + string sender = 1; + string pair = 2 [ + (gogoproto.customtype) = + "github.com/NibiruChain/nibiru/x/common/asset.Pair", + (gogoproto.nullable) = false + ]; + string new_swap_invariant = 3 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false + ]; +} + +message MsgShiftSwapInvariantResponse {} diff --git a/wasmbinding/exec_perp.go b/wasmbinding/exec_perp.go index e3abaf869..f9f773317 100644 --- a/wasmbinding/exec_perp.go +++ b/wasmbinding/exec_perp.go @@ -149,7 +149,7 @@ func (exec *ExecutorPerp) PegShift( return err } - return exec.PerpV2.EditPriceMultiplier( + return exec.PerpV2.UnsafeShiftPegMultiplier( ctx, // contractAddr, pair, @@ -167,7 +167,7 @@ func (exec *ExecutorPerp) DepthShift(cwMsg *bindings.DepthShift, ctx sdk.Context return err } - return exec.PerpV2.EditSwapInvariant(ctx, pair, cwMsg.DepthMult) + return exec.PerpV2.UnsafeShiftSwapInvariant(ctx, pair, cwMsg.DepthMult) } func (exec *ExecutorPerp) InsuranceFundWithdraw( diff --git a/x/perp/v2/integration/action/market.go b/x/perp/v2/integration/action/market.go index 805252b07..5617181cd 100644 --- a/x/perp/v2/integration/action/market.go +++ b/x/perp/v2/integration/action/market.go @@ -140,7 +140,7 @@ type editPriceMultiplier struct { } func (e editPriceMultiplier) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - err := app.PerpKeeperV2.EditPriceMultiplier(ctx, e.pair, e.newValue) + err := app.PerpKeeperV2.UnsafeShiftPegMultiplier(ctx, e.pair, e.newValue) return ctx, err, true } @@ -157,7 +157,7 @@ type editSwapInvariant struct { } func (e editSwapInvariant) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - err := app.PerpKeeperV2.EditSwapInvariant(ctx, e.pair, e.newValue) + err := app.PerpKeeperV2.UnsafeShiftSwapInvariant(ctx, e.pair, e.newValue) return ctx, err, true } diff --git a/x/perp/v2/keeper/admin.go b/x/perp/v2/keeper/admin.go index 34ec601f3..b3a9f3205 100644 --- a/x/perp/v2/keeper/admin.go +++ b/x/perp/v2/keeper/admin.go @@ -174,3 +174,33 @@ func (k admin) UnsafeChangeCollateralDenom( k.Collateral.Set(ctx, denom) return nil } + +// ShiftPegMultiplier: Edit the peg multiplier of an amm pool after making +// sure there's enough money in the perp EF fund to pay for the repeg. These +// funds get send to the vault to pay for trader's new net margin. +func (k admin) ShiftPegMultiplier( + ctx sdk.Context, + pair asset.Pair, + newPriceMultiplier sdk.Dec, + sender sdk.AccAddress, +) error { + if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { + return err + } + return k.UnsafeShiftPegMultiplier(ctx, pair, newPriceMultiplier) +} + +// ShiftSwapInvariant: Edit the peg multiplier of an amm pool after making +// sure there's enough money in the perp EF fund to pay for the repeg. These +// funds get send to the vault to pay for trader's new net margin. +func (k admin) ShiftSwapInvariant( + ctx sdk.Context, + pair asset.Pair, + newSwapInvariant sdk.Dec, + sender sdk.AccAddress, +) error { + if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { + return err + } + return k.UnsafeShiftSwapInvariant(ctx, pair, newSwapInvariant) +} diff --git a/x/perp/v2/keeper/admin_test.go b/x/perp/v2/keeper/admin_test.go index 5e8d66e25..4dacd365a 100644 --- a/x/perp/v2/keeper/admin_test.go +++ b/x/perp/v2/keeper/admin_test.go @@ -5,13 +5,16 @@ import ( "time" sdkmath "cosmossdk.io/math" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" "github.com/NibiruChain/nibiru/app" "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" + "github.com/NibiruChain/nibiru/x/common/testutil/genesis" "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" @@ -339,3 +342,126 @@ func TestAdmin_ChangeCollateralDenom(t *testing.T) { }) } } + +type TestSuiteSmartContracts struct { + suite.Suite + + nibiru *app.NibiruApp + ctx sdk.Context + addrAdmin sdk.AccAddress + + ratesMap map[asset.Pair]sdk.Dec +} + +func (s *TestSuiteSmartContracts) SetupSuite() { + sender := testutil.AccAddress() + s.addrAdmin = sender + + genesisState := genesis.NewTestGenesisState(app.MakeEncodingConfig()) + genesisState = genesis.AddOracleGenesis(genesisState) + genesisState = genesis.AddPerpV2Genesis(genesisState) + nibiru := testapp.NewNibiruTestApp(genesisState) + ctx := nibiru.NewContext(false, tmproto.Header{ + Height: 1, + ChainID: "nibiru-wasmnet-1", + Time: time.Now().UTC(), + }) + coins := sdk.NewCoins( + sdk.NewCoin(denoms.NIBI, sdk.NewInt(1_000_000)), + sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(420_000*69)), + sdk.NewCoin(denoms.USDT, sdk.NewInt(420_000*69)), + ) + s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins)) + + s.nibiru = nibiru + s.ctx = ctx + s.nibiru.PerpKeeperV2.Collateral.Set(s.ctx, types.TestingCollateralDenomNUSD) +} + +func (s *TestSuiteSmartContracts) DoPegShiftTest(pair asset.Pair) error { + sender := s.addrAdmin + newPegMult := sdk.NewDec(420) + err := s.nibiru.AppKeepers.PerpKeeperV2.Admin.ShiftPegMultiplier( + s.ctx, pair, newPegMult, sender, + ) + return err +} + +func TestAdmin_PegShift(t *testing.T) { + pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) + amm := *mock.TestAMMDefault() + app, ctx := testapp.NewNibiruTestAppAndContext() + admin := app.PerpKeeperV2.Admin + + // Error because of invalid market + market := types.DefaultMarket(pair).WithMaintenanceMarginRatio(sdk.NewDec(2)) + err := admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ + Pair: pair, + PriceMultiplier: amm.PriceMultiplier, + SqrtDepth: amm.SqrtDepth, + Market: &market, // Invalid maintenance ratio + }) + require.ErrorContains(t, err, "maintenance margin ratio ratio must be 0 <= ratio <= 1") + + // Error because of invalid amm + err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ + Pair: pair, + PriceMultiplier: sdk.NewDec(-1), + SqrtDepth: amm.SqrtDepth, + }) + require.ErrorContains(t, err, "init price multiplier must be > 0") + + // Set it correctly + err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ + Pair: pair, + PriceMultiplier: amm.PriceMultiplier, + SqrtDepth: amm.SqrtDepth, + EnableMarket: true, + }) + require.NoError(t, err) + + lastVersion, err := app.PerpKeeperV2.MarketLastVersion.Get(ctx, pair) + require.NoError(t, err) + require.Equal(t, uint64(1), lastVersion.Version) + + // Check that amm and market have version 1 + amm, err = app.PerpKeeperV2.GetAMM(ctx, pair) + require.NoError(t, err) + require.Equal(t, uint64(1), amm.Version) + + market, err = app.PerpKeeperV2.GetMarket(ctx, pair) + require.NoError(t, err) + require.Equal(t, uint64(1), market.Version) + + // Fail since it already exists and it is not disabled + err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ + Pair: pair, + PriceMultiplier: amm.PriceMultiplier, + SqrtDepth: amm.SqrtDepth, + }) + require.ErrorContains(t, err, "already exists") + + // Close the market to test that we can create it again but with an increased version + err = admin.CloseMarket(ctx, pair) + require.NoError(t, err) + + err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ + Pair: pair, + PriceMultiplier: amm.PriceMultiplier, + SqrtDepth: amm.SqrtDepth, + }) + require.NoError(t, err) + + lastVersion, err = app.PerpKeeperV2.MarketLastVersion.Get(ctx, pair) + require.NoError(t, err) + require.Equal(t, uint64(2), lastVersion.Version) + + // Check that amm and market have version 2 + amm, err = app.PerpKeeperV2.GetAMM(ctx, pair) + require.NoError(t, err) + require.Equal(t, uint64(2), amm.Version) + + market, err = app.PerpKeeperV2.GetMarket(ctx, pair) + require.NoError(t, err) + require.Equal(t, uint64(2), market.Version) +} diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index 722e6d349..b197ec405 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -11,10 +11,11 @@ import ( types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) -// EditPriceMultiplier edits the peg multiplier of an amm pool after making -// sure there's enough money in the perp EF fund to pay for the repeg. These -// funds get send to the vault to pay for trader's new net margin. -func (k Keeper) EditPriceMultiplier( +// UnsafeShiftPegMultiplier: [Without checking x/sudo permissions] Edits the peg +// multiplier of an amm pool after making sure there's enough money in the perp +// EF fund to pay for the repeg. These funds get send to the vault to pay for +// trader's new net margin. +func (k Keeper) UnsafeShiftPegMultiplier( ctx sdk.Context, pair asset.Pair, newPriceMultiplier sdk.Dec, @@ -47,10 +48,13 @@ func (k Keeper) EditPriceMultiplier( return nil } -// EditSwapInvariant edits the swap invariant of an amm pool after making -// sure there's enough money in the perp EF fund to pay for the repeg. These -// funds get send to the vault to pay for trader's new net margin. -func (k Keeper) EditSwapInvariant(ctx sdk.Context, pair asset.Pair, newSwapInvariant sdk.Dec) (err error) { +// UnsafeShiftSwapInvariant: [Without checking x/sudo permissions] Edit the swap +// invariant of an amm pool after making sure there's enough money in the perp EF +// fund to pay for the repeg. These funds get send to the vault to pay for +// trader's new net margin. +func (k Keeper) UnsafeShiftSwapInvariant( + ctx sdk.Context, pair asset.Pair, newSwapInvariant sdk.Dec, +) (err error) { // Get the pool amm, err := k.GetAMM(ctx, pair) if err != nil { @@ -78,7 +82,9 @@ func (k Keeper) EditSwapInvariant(ctx sdk.Context, pair asset.Pair, newSwapInvar return nil } -func (k Keeper) handleMarketUpdateCost(ctx sdk.Context, pair asset.Pair, costAmt sdkmath.Int) (err error) { +func (k Keeper) handleMarketUpdateCost( + ctx sdk.Context, pair asset.Pair, costAmt sdkmath.Int, +) (err error) { collateral, err := k.Collateral.Get(ctx) if err != nil { return err diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index b2ff2513a..07f4058d7 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -185,11 +185,11 @@ func TestEditPriceMultiplerFail(t *testing.T) { require.NoError(t, err) // Error because of invalid pair - err = app.PerpKeeperV2.Admin.EditPriceMultiplier(ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1)) require.ErrorContains(t, err, "market luna:usdt not found") // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.EditPriceMultiplier(ctx, pair, sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(-1)) require.ErrorIs(t, err, types.ErrNonPositivePegMultiplier) // Add market activity @@ -211,11 +211,11 @@ func TestEditPriceMultiplerFail(t *testing.T) { require.NoError(t, err) // Error because no money in perp ef fund - err = app.PerpKeeperV2.Admin.EditPriceMultiplier(ctx, pair, sdk.NewDec(3)) + err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(3)) require.ErrorContains(t, err, "not enough fund in perp ef to pay for repeg") // Works because it goes in the other way - err = app.PerpKeeperV2.Admin.EditPriceMultiplier(ctx, pair, sdk.NewDec(1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(1)) require.NoError(t, err) } @@ -251,11 +251,11 @@ func TestEditSwapInvariantFail(t *testing.T) { require.NoError(t, err) // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.EditSwapInvariant(ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1)) require.ErrorContains(t, err, "market luna:usdt not found") // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.EditSwapInvariant(ctx, pair, sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(-1)) require.ErrorIs(t, err, types.ErrNegativeSwapInvariant) // Add market activity @@ -277,15 +277,15 @@ func TestEditSwapInvariantFail(t *testing.T) { require.NoError(t, err) // Error because no money in perp ef fund - err = app.PerpKeeperV2.Admin.EditSwapInvariant(ctx, pair, sdk.NewDec(2_000_000)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(2_000_000)) require.ErrorContains(t, err, "not enough fund in perp ef to pay for repeg") // Fail at validate - err = app.PerpKeeperV2.Admin.EditSwapInvariant(ctx, pair, sdk.NewDec(0)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(0)) require.ErrorContains(t, err, "swap multiplier must be > 0") // Works because it goes in the other way - err = app.PerpKeeperV2.Admin.EditSwapInvariant(ctx, pair, sdk.NewDec(500_000)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(500_000)) require.NoError(t, err) } diff --git a/x/perp/v2/keeper/msg_server.go b/x/perp/v2/keeper/msg_server.go index ae2e9d35c..c0ad132c3 100644 --- a/x/perp/v2/keeper/msg_server.go +++ b/x/perp/v2/keeper/msg_server.go @@ -189,3 +189,29 @@ func (m msgServer) WithdrawEpochRebates(ctx context.Context, msg *types.MsgWithd WithdrawnRebates: totalWithdrawn, }, nil } + +// TODO: test ShiftPegMultiplier +func (m msgServer) ShiftPegMultiplier( + goCtx context.Context, msg *types.MsgShiftPegMultiplier, +) (*types.MsgShiftPegMultiplierResponse, error) { + sender, err := sdk.AccAddressFromBech32(msg.Sender) + if err != nil { + return nil, err + } + ctx := sdk.UnwrapSDKContext(goCtx) + err = m.k.Admin.ShiftPegMultiplier(ctx, msg.Pair, msg.NewPegMult, sender) + return &types.MsgShiftPegMultiplierResponse{}, err +} + +// TODO: test ShiftSwapInvariant +func (m msgServer) ShiftSwapInvariant( + goCtx context.Context, msg *types.MsgShiftSwapInvariant, +) (*types.MsgShiftSwapInvariantResponse, error) { + sender, err := sdk.AccAddressFromBech32(msg.Sender) + if err != nil { + return nil, err + } + ctx := sdk.UnwrapSDKContext(goCtx) + err = m.k.Admin.ShiftSwapInvariant(ctx, msg.Pair, msg.NewSwapInvariant.ToLegacyDec(), sender) + return &types.MsgShiftSwapInvariantResponse{}, err +} diff --git a/x/perp/v2/types/codec.go b/x/perp/v2/types/codec.go index b35212a01..739ef814e 100644 --- a/x/perp/v2/types/codec.go +++ b/x/perp/v2/types/codec.go @@ -16,6 +16,8 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&MsgDonateToEcosystemFund{}, "perpv2/donate_to_ef", nil) cdc.RegisterConcrete(&MsgMultiLiquidate{}, "perpv2/multi_liquidate", nil) cdc.RegisterConcrete(&MsgChangeCollateralDenom{}, "perpv2/change_collateral_denom", nil) + cdc.RegisterConcrete(&MsgShiftPegMultiplier{}, "perpv2/shift_peg_multiplier", nil) + cdc.RegisterConcrete(&MsgShiftSwapInvariant{}, "perpv2/shift_swap_invariant", nil) } func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { @@ -29,6 +31,8 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { &MsgPartialClose{}, &MsgMultiLiquidate{}, &MsgChangeCollateralDenom{}, + &MsgShiftPegMultiplier{}, + &MsgShiftSwapInvariant{}, ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) diff --git a/x/perp/v2/types/codec_test.go b/x/perp/v2/types/codec_test.go index e17a2d56c..afae79b86 100644 --- a/x/perp/v2/types/codec_test.go +++ b/x/perp/v2/types/codec_test.go @@ -25,6 +25,8 @@ func TestCodec(t *testing.T) { &MsgPartialClose{}, &MsgDonateToEcosystemFund{}, &MsgMultiLiquidate{}, + &MsgShiftPegMultiplier{}, + &MsgShiftSwapInvariant{}, } for _, msg := range msgs { diff --git a/x/perp/v2/types/msgs.go b/x/perp/v2/types/msgs.go index d67370e3c..5d9ad325e 100644 --- a/x/perp/v2/types/msgs.go +++ b/x/perp/v2/types/msgs.go @@ -19,9 +19,11 @@ var ( _ sdk.Msg = &MsgPartialClose{} _ sdk.Msg = &MsgAllocateEpochRebates{} _ sdk.Msg = &MsgWithdrawEpochRebates{} + _ sdk.Msg = &MsgShiftPegMultiplier{} + _ sdk.Msg = &MsgShiftSwapInvariant{} ) -// MsgRemoveMargin +// ------------------------ MsgRemoveMargin ------------------------ func (m MsgRemoveMargin) Route() string { return "perp" } func (m MsgRemoveMargin) Type() string { return "remove_margin_msg" } @@ -55,7 +57,7 @@ func (m MsgRemoveMargin) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgAddMargin +// ------------------------ MsgAddMargin ------------------------ func (m MsgAddMargin) Route() string { return "perp" } func (m MsgAddMargin) Type() string { return "add_margin_msg" } @@ -89,7 +91,7 @@ func (m MsgAddMargin) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgMarketOrder +// ------------------------ MsgMarketOrder ------------------------ func (m MsgMarketOrder) Route() string { return "perp" } func (m MsgMarketOrder) Type() string { return "market_order_msg" } @@ -129,7 +131,7 @@ func (m *MsgMarketOrder) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgMultiLiquidate +// ------------------------ MsgMultiLiquidate ------------------------ func (m MsgMultiLiquidate) Route() string { return "perp" } func (m MsgMultiLiquidate) Type() string { return "multi_liquidate_msg" } @@ -165,7 +167,7 @@ func (m *MsgMultiLiquidate) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{addr} } -// MsgClosePosition +// ------------------------ MsgClosePosition ------------------------ func (m MsgClosePosition) Route() string { return "perp" } func (m MsgClosePosition) Type() string { return "close_position_msg" } @@ -192,7 +194,7 @@ func (m MsgClosePosition) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgSettlePosition +// ------------------------ MsgSettlePosition ------------------------ func (m MsgSettlePosition) Route() string { return "perp" } func (m MsgSettlePosition) Type() string { return "settle_position_msg" } @@ -220,7 +222,7 @@ func (m MsgSettlePosition) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgDonateToEcosystemFund +// ------------------------ MsgDonateToEcosystemFund ------------------------ func (m MsgDonateToEcosystemFund) Route() string { return "perp" } func (m MsgDonateToEcosystemFund) Type() string { return "donate_to_ef_msg" } @@ -247,7 +249,7 @@ func (m MsgDonateToEcosystemFund) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgPartialClose +// ------------------------ MsgPartialClose ------------------------ func (m MsgPartialClose) Route() string { return "perp" } func (m MsgPartialClose) Type() string { return "partial_close_msg" } @@ -277,7 +279,7 @@ func (m MsgPartialClose) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } -// MsgChangeCollateralDenom +// ------------------------ MsgChangeCollateralDenom ------------------------ func (m MsgChangeCollateralDenom) Route() string { return "perp" } func (m MsgChangeCollateralDenom) Type() string { return "partial_close_msg" } @@ -304,6 +306,8 @@ func (m MsgChangeCollateralDenom) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } +// ------------------------ MsgAllocateEpochRebates ------------------------ + func (m MsgAllocateEpochRebates) ValidateBasic() error { if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { return sdkerrors.Wrapf(errors.ErrInvalidAddress, "invalid sender address (%s)", err) @@ -326,6 +330,8 @@ func (m MsgAllocateEpochRebates) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// ------------------------ MsgWithdrawEpochRebates ------------------------ + func (m MsgWithdrawEpochRebates) ValidateBasic() error { if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { return sdkerrors.Wrapf(errors.ErrInvalidAddress, "invalid sender address (%s)", err) @@ -347,3 +353,57 @@ func (m MsgWithdrawEpochRebates) GetSigners() []sdk.AccAddress { func (m MsgWithdrawEpochRebates) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } + +// ------------------------ MsgShiftPegMultiplier ------------------------ + +func (m MsgShiftPegMultiplier) ValidateBasic() error { + if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { + return sdkerrors.Wrapf(errors.ErrInvalidAddress, "invalid sender address (%s)", err) + } + if err := m.Pair.Validate(); err != nil { + return err + } + if !m.NewPegMult.IsPositive() { + return fmt.Errorf("new peg multiplier must be positive: got value %s", m.NewPegMult) + } + return nil +} + +func (m MsgShiftPegMultiplier) GetSigners() []sdk.AccAddress { + signer, err := sdk.AccAddressFromBech32(m.Sender) + if err != nil { + panic(err) + } + return []sdk.AccAddress{signer} +} + +func (m MsgShiftPegMultiplier) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) +} + +// ------------------------ MsgShiftSwapInvariant ------------------------ + +func (m MsgShiftSwapInvariant) ValidateBasic() error { + if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { + return sdkerrors.Wrapf(errors.ErrInvalidAddress, "invalid sender address (%s)", err) + } + if err := m.Pair.Validate(); err != nil { + return err + } + if !m.NewSwapInvariant.IsPositive() { + return fmt.Errorf("new swap invariant must be positive: got value %s", m.NewSwapInvariant) + } + return nil +} + +func (m MsgShiftSwapInvariant) GetSigners() []sdk.AccAddress { + signer, err := sdk.AccAddressFromBech32(m.Sender) + if err != nil { + panic(err) + } + return []sdk.AccAddress{signer} +} + +func (m MsgShiftSwapInvariant) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) +} diff --git a/x/perp/v2/types/tx.pb.go b/x/perp/v2/types/tx.pb.go index 54ba4245b..916f3a080 100644 --- a/x/perp/v2/types/tx.pb.go +++ b/x/perp/v2/types/tx.pb.go @@ -1199,6 +1199,174 @@ func (m *MsgWithdrawEpochRebatesResponse) GetWithdrawnRebates() github_com_cosmo return nil } +// ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. +// Admin-only. +type MsgShiftPegMultiplier struct { + Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty"` + Pair github_com_NibiruChain_nibiru_x_common_asset.Pair `protobuf:"bytes,2,opt,name=pair,proto3,customtype=github.com/NibiruChain/nibiru/x/common/asset.Pair" json:"pair"` + NewPegMult github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=new_peg_mult,json=newPegMult,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"new_peg_mult"` +} + +func (m *MsgShiftPegMultiplier) Reset() { *m = MsgShiftPegMultiplier{} } +func (m *MsgShiftPegMultiplier) String() string { return proto.CompactTextString(m) } +func (*MsgShiftPegMultiplier) ProtoMessage() {} +func (*MsgShiftPegMultiplier) Descriptor() ([]byte, []int) { + return fileDescriptor_b95cda40bf0a0f91, []int{21} +} +func (m *MsgShiftPegMultiplier) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgShiftPegMultiplier) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgShiftPegMultiplier.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgShiftPegMultiplier) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgShiftPegMultiplier.Merge(m, src) +} +func (m *MsgShiftPegMultiplier) XXX_Size() int { + return m.Size() +} +func (m *MsgShiftPegMultiplier) XXX_DiscardUnknown() { + xxx_messageInfo_MsgShiftPegMultiplier.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgShiftPegMultiplier proto.InternalMessageInfo + +func (m *MsgShiftPegMultiplier) GetSender() string { + if m != nil { + return m.Sender + } + return "" +} + +type MsgShiftPegMultiplierResponse struct { +} + +func (m *MsgShiftPegMultiplierResponse) Reset() { *m = MsgShiftPegMultiplierResponse{} } +func (m *MsgShiftPegMultiplierResponse) String() string { return proto.CompactTextString(m) } +func (*MsgShiftPegMultiplierResponse) ProtoMessage() {} +func (*MsgShiftPegMultiplierResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_b95cda40bf0a0f91, []int{22} +} +func (m *MsgShiftPegMultiplierResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgShiftPegMultiplierResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgShiftPegMultiplierResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgShiftPegMultiplierResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgShiftPegMultiplierResponse.Merge(m, src) +} +func (m *MsgShiftPegMultiplierResponse) XXX_Size() int { + return m.Size() +} +func (m *MsgShiftPegMultiplierResponse) XXX_DiscardUnknown() { + xxx_messageInfo_MsgShiftPegMultiplierResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgShiftPegMultiplierResponse proto.InternalMessageInfo + +// ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. +// Admin-only. +type MsgShiftSwapInvariant struct { + Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty"` + Pair github_com_NibiruChain_nibiru_x_common_asset.Pair `protobuf:"bytes,2,opt,name=pair,proto3,customtype=github.com/NibiruChain/nibiru/x/common/asset.Pair" json:"pair"` + NewSwapInvariant github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,3,opt,name=new_swap_invariant,json=newSwapInvariant,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"new_swap_invariant"` +} + +func (m *MsgShiftSwapInvariant) Reset() { *m = MsgShiftSwapInvariant{} } +func (m *MsgShiftSwapInvariant) String() string { return proto.CompactTextString(m) } +func (*MsgShiftSwapInvariant) ProtoMessage() {} +func (*MsgShiftSwapInvariant) Descriptor() ([]byte, []int) { + return fileDescriptor_b95cda40bf0a0f91, []int{23} +} +func (m *MsgShiftSwapInvariant) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgShiftSwapInvariant) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgShiftSwapInvariant.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgShiftSwapInvariant) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgShiftSwapInvariant.Merge(m, src) +} +func (m *MsgShiftSwapInvariant) XXX_Size() int { + return m.Size() +} +func (m *MsgShiftSwapInvariant) XXX_DiscardUnknown() { + xxx_messageInfo_MsgShiftSwapInvariant.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgShiftSwapInvariant proto.InternalMessageInfo + +func (m *MsgShiftSwapInvariant) GetSender() string { + if m != nil { + return m.Sender + } + return "" +} + +type MsgShiftSwapInvariantResponse struct { +} + +func (m *MsgShiftSwapInvariantResponse) Reset() { *m = MsgShiftSwapInvariantResponse{} } +func (m *MsgShiftSwapInvariantResponse) String() string { return proto.CompactTextString(m) } +func (*MsgShiftSwapInvariantResponse) ProtoMessage() {} +func (*MsgShiftSwapInvariantResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_b95cda40bf0a0f91, []int{24} +} +func (m *MsgShiftSwapInvariantResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgShiftSwapInvariantResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgShiftSwapInvariantResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgShiftSwapInvariantResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgShiftSwapInvariantResponse.Merge(m, src) +} +func (m *MsgShiftSwapInvariantResponse) XXX_Size() int { + return m.Size() +} +func (m *MsgShiftSwapInvariantResponse) XXX_DiscardUnknown() { + xxx_messageInfo_MsgShiftSwapInvariantResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgShiftSwapInvariantResponse proto.InternalMessageInfo + func init() { proto.RegisterType((*MsgSettlePosition)(nil), "nibiru.perp.v2.MsgSettlePosition") proto.RegisterType((*MsgRemoveMargin)(nil), "nibiru.perp.v2.MsgRemoveMargin") @@ -1223,103 +1391,115 @@ func init() { proto.RegisterType((*MsgAllocateEpochRebatesResponse)(nil), "nibiru.perp.v2.MsgAllocateEpochRebatesResponse") proto.RegisterType((*MsgWithdrawEpochRebates)(nil), "nibiru.perp.v2.MsgWithdrawEpochRebates") proto.RegisterType((*MsgWithdrawEpochRebatesResponse)(nil), "nibiru.perp.v2.MsgWithdrawEpochRebatesResponse") + proto.RegisterType((*MsgShiftPegMultiplier)(nil), "nibiru.perp.v2.MsgShiftPegMultiplier") + proto.RegisterType((*MsgShiftPegMultiplierResponse)(nil), "nibiru.perp.v2.MsgShiftPegMultiplierResponse") + proto.RegisterType((*MsgShiftSwapInvariant)(nil), "nibiru.perp.v2.MsgShiftSwapInvariant") + proto.RegisterType((*MsgShiftSwapInvariantResponse)(nil), "nibiru.perp.v2.MsgShiftSwapInvariantResponse") } func init() { proto.RegisterFile("nibiru/perp/v2/tx.proto", fileDescriptor_b95cda40bf0a0f91) } var fileDescriptor_b95cda40bf0a0f91 = []byte{ - // 1447 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x59, 0xcf, 0x6f, 0x1b, 0xc5, - 0x17, 0xf7, 0xc6, 0x6e, 0xea, 0xbc, 0xa4, 0x4e, 0xb2, 0x4d, 0x13, 0xd7, 0xdf, 0xca, 0xc9, 0x77, - 0x85, 0x4a, 0x38, 0x64, 0xb7, 0x0d, 0x48, 0x08, 0x24, 0x40, 0xf9, 0xd1, 0xa2, 0xa2, 0xba, 0x4d, - 0xb7, 0x55, 0x8b, 0x0a, 0x68, 0x3b, 0xb1, 0x27, 0x9b, 0x51, 0xd7, 0x33, 0xee, 0xce, 0xac, 0x93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0x51, 0x6a, 0x56, - 0x2e, 0x0c, 0xaa, 0xd9, 0xed, 0x56, 0x3d, 0x9b, 0x95, 0xdc, 0x55, 0x9a, 0x4a, 0xb7, 0xfd, 0xc7, - 0x73, 0x4e, 0xf7, 0x61, 0x4a, 0x39, 0x9c, 0x55, 0x15, 0x51, 0x29, 0x56, 0xac, 0x01, 0x15, 0xbb, - 0x7d, 0x2a, 0xa7, 0x9a, 0xca, 0xa7, 0x4a, 0x51, 0xe9, 0xb3, 0xdf, 0x6c, 0x33, 0x72, 0xcb, 0xef, - 0x3e, 0x7a, 0x5a, 0xd5, 0x1e, 0x3f, 0xad, 0x6a, 0xbf, 0x3e, 0xad, 0x6a, 0x9f, 0xee, 0x56, 0x73, - 0x8f, 0x77, 0xab, 0xb9, 0x9f, 0x76, 0xab, 0xb9, 0xbb, 0x0b, 0xfb, 0x9d, 0x18, 0xd3, 0x7f, 0x3a, - 0xc8, 0x21, 0xb6, 0x3e, 0x1c, 0x7e, 0xf8, 0x7f, 0xf5, 0xaf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x70, - 0xcc, 0xc4, 0x19, 0x93, 0x18, 0x00, 0x00, + // 1570 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x59, 0xcd, 0x6f, 0x5b, 0xc5, + 0x16, 0xf7, 0x8d, 0xdd, 0x34, 0x39, 0x49, 0xf3, 0x71, 0x9b, 0x26, 0xae, 0x5f, 0x9f, 0x93, 0x77, + 0xf5, 0x5e, 0x5f, 0x58, 0xc4, 0x6e, 0x03, 0x12, 0x02, 0x09, 0x50, 0x3e, 0x5a, 0x54, 0x54, 0xb7, + 0xee, 0x4d, 0xd5, 0xa2, 0x52, 0x74, 0x3b, 0xb1, 0x27, 0x37, 0xa3, 0x5e, 0xcf, 0xb8, 0x77, 0xe6, + 0xda, 0x49, 0xd9, 0xf1, 0x17, 0xb0, 0x60, 0x81, 0x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0xe6, 0xf1, 0x5e, 0x31, 0xf3, + 0xc3, 0x5e, 0x31, 0x73, 0x67, 0xe9, 0xa0, 0x8e, 0x10, 0xff, 0x88, 0x26, 0xeb, 0xf7, 0xe6, 0xb0, + 0xff, 0x43, 0xd6, 0xcb, 0x7f, 0x04, 0x00, 0x00, 0xff, 0xff, 0x4e, 0xc7, 0xc2, 0xd7, 0x63, 0x1b, + 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -1345,6 +1525,12 @@ type MsgClient interface { ChangeCollateralDenom(ctx context.Context, in *MsgChangeCollateralDenom, opts ...grpc.CallOption) (*MsgChangeCollateralDenomResponse, error) AllocateEpochRebates(ctx context.Context, in *MsgAllocateEpochRebates, opts ...grpc.CallOption) (*MsgAllocateEpochRebatesResponse, error) WithdrawEpochRebates(ctx context.Context, in *MsgWithdrawEpochRebates, opts ...grpc.CallOption) (*MsgWithdrawEpochRebatesResponse, error) + // ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. + // Admin-only. + ShiftPegMultiplier(ctx context.Context, in *MsgShiftPegMultiplier, opts ...grpc.CallOption) (*MsgShiftPegMultiplierResponse, error) + // ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. + // Admin-only. + ShiftSwapInvariant(ctx context.Context, in *MsgShiftSwapInvariant, opts ...grpc.CallOption) (*MsgShiftSwapInvariantResponse, error) } type msgClient struct { @@ -1454,6 +1640,24 @@ func (c *msgClient) WithdrawEpochRebates(ctx context.Context, in *MsgWithdrawEpo return out, nil } +func (c *msgClient) ShiftPegMultiplier(ctx context.Context, in *MsgShiftPegMultiplier, opts ...grpc.CallOption) (*MsgShiftPegMultiplierResponse, error) { + out := new(MsgShiftPegMultiplierResponse) + err := c.cc.Invoke(ctx, "/nibiru.perp.v2.Msg/ShiftPegMultiplier", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *msgClient) ShiftSwapInvariant(ctx context.Context, in *MsgShiftSwapInvariant, opts ...grpc.CallOption) (*MsgShiftSwapInvariantResponse, error) { + out := new(MsgShiftSwapInvariantResponse) + err := c.cc.Invoke(ctx, "/nibiru.perp.v2.Msg/ShiftSwapInvariant", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // MsgServer is the server API for Msg service. type MsgServer interface { RemoveMargin(context.Context, *MsgRemoveMargin) (*MsgRemoveMarginResponse, error) @@ -1467,6 +1671,12 @@ type MsgServer interface { ChangeCollateralDenom(context.Context, *MsgChangeCollateralDenom) (*MsgChangeCollateralDenomResponse, error) AllocateEpochRebates(context.Context, *MsgAllocateEpochRebates) (*MsgAllocateEpochRebatesResponse, error) WithdrawEpochRebates(context.Context, *MsgWithdrawEpochRebates) (*MsgWithdrawEpochRebatesResponse, error) + // ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. + // Admin-only. + ShiftPegMultiplier(context.Context, *MsgShiftPegMultiplier) (*MsgShiftPegMultiplierResponse, error) + // ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. + // Admin-only. + ShiftSwapInvariant(context.Context, *MsgShiftSwapInvariant) (*MsgShiftSwapInvariantResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. @@ -1506,6 +1716,12 @@ func (*UnimplementedMsgServer) AllocateEpochRebates(ctx context.Context, req *Ms func (*UnimplementedMsgServer) WithdrawEpochRebates(ctx context.Context, req *MsgWithdrawEpochRebates) (*MsgWithdrawEpochRebatesResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method WithdrawEpochRebates not implemented") } +func (*UnimplementedMsgServer) ShiftPegMultiplier(ctx context.Context, req *MsgShiftPegMultiplier) (*MsgShiftPegMultiplierResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ShiftPegMultiplier not implemented") +} +func (*UnimplementedMsgServer) ShiftSwapInvariant(ctx context.Context, req *MsgShiftSwapInvariant) (*MsgShiftSwapInvariantResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ShiftSwapInvariant not implemented") +} func RegisterMsgServer(s grpc1.Server, srv MsgServer) { s.RegisterService(&_Msg_serviceDesc, srv) @@ -1709,6 +1925,42 @@ func _Msg_WithdrawEpochRebates_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } +func _Msg_ShiftPegMultiplier_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(MsgShiftPegMultiplier) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MsgServer).ShiftPegMultiplier(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/nibiru.perp.v2.Msg/ShiftPegMultiplier", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MsgServer).ShiftPegMultiplier(ctx, req.(*MsgShiftPegMultiplier)) + } + return interceptor(ctx, in, info, handler) +} + +func _Msg_ShiftSwapInvariant_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(MsgShiftSwapInvariant) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MsgServer).ShiftSwapInvariant(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/nibiru.perp.v2.Msg/ShiftSwapInvariant", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MsgServer).ShiftSwapInvariant(ctx, req.(*MsgShiftSwapInvariant)) + } + return interceptor(ctx, in, info, handler) +} + var _Msg_serviceDesc = grpc.ServiceDesc{ ServiceName: "nibiru.perp.v2.Msg", HandlerType: (*MsgServer)(nil), @@ -1757,6 +2009,14 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ MethodName: "WithdrawEpochRebates", Handler: _Msg_WithdrawEpochRebates_Handler, }, + { + MethodName: "ShiftPegMultiplier", + Handler: _Msg_ShiftPegMultiplier_Handler, + }, + { + MethodName: "ShiftSwapInvariant", + Handler: _Msg_ShiftSwapInvariant_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "nibiru/perp/v2/tx.proto", @@ -2914,76 +3174,222 @@ func (m *MsgWithdrawEpochRebatesResponse) MarshalToSizedBuffer(dAtA []byte) (int return len(dAtA) - i, nil } -func encodeVarintTx(dAtA []byte, offset int, v uint64) int { - offset -= sovTx(v) - base := offset - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ +func (m *MsgShiftPegMultiplier) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err } - dAtA[offset] = uint8(v) - return base + return dAtA[:n], nil } -func (m *MsgSettlePosition) Size() (n int) { - if m == nil { - return 0 - } + +func (m *MsgShiftPegMultiplier) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgShiftPegMultiplier) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i var l int _ = l - l = len(m.Sender) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) + { + size := m.NewPegMult.Size() + i -= size + if _, err := m.NewPegMult.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) } - l = m.Pair.Size() - n += 1 + l + sovTx(uint64(l)) - if m.Version != 0 { - n += 1 + sovTx(uint64(m.Version)) + i-- + dAtA[i] = 0x1a + { + size := m.Pair.Size() + i -= size + if _, err := m.Pair.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) } - return n + i-- + dAtA[i] = 0x12 + if len(m.Sender) > 0 { + i -= len(m.Sender) + copy(dAtA[i:], m.Sender) + i = encodeVarintTx(dAtA, i, uint64(len(m.Sender))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil } -func (m *MsgRemoveMargin) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Sender) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) +func (m *MsgShiftPegMultiplierResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err } - l = m.Pair.Size() - n += 1 + l + sovTx(uint64(l)) - l = m.Margin.Size() - n += 1 + l + sovTx(uint64(l)) - return n + return dAtA[:n], nil } -func (m *MsgRemoveMarginResponse) Size() (n int) { - if m == nil { - return 0 - } +func (m *MsgShiftPegMultiplierResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgShiftPegMultiplierResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i var l int _ = l - l = m.MarginOut.Size() - n += 1 + l + sovTx(uint64(l)) - l = m.FundingPayment.Size() - n += 1 + l + sovTx(uint64(l)) - if m.Position != nil { - l = m.Position.Size() - n += 1 + l + sovTx(uint64(l)) - } - return n + return len(dAtA) - i, nil } -func (m *MsgAddMargin) Size() (n int) { - if m == nil { - return 0 +func (m *MsgShiftSwapInvariant) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err } + return dAtA[:n], nil +} + +func (m *MsgShiftSwapInvariant) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgShiftSwapInvariant) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i var l int _ = l - l = len(m.Sender) + { + size := m.NewSwapInvariant.Size() + i -= size + if _, err := m.NewSwapInvariant.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + { + size := m.Pair.Size() + i -= size + if _, err := m.Pair.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Sender) > 0 { + i -= len(m.Sender) + copy(dAtA[i:], m.Sender) + i = encodeVarintTx(dAtA, i, uint64(len(m.Sender))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *MsgShiftSwapInvariantResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgShiftSwapInvariantResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgShiftSwapInvariantResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + +func encodeVarintTx(dAtA []byte, offset int, v uint64) int { + offset -= sovTx(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *MsgSettlePosition) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = m.Pair.Size() + n += 1 + l + sovTx(uint64(l)) + if m.Version != 0 { + n += 1 + sovTx(uint64(m.Version)) + } + return n +} + +func (m *MsgRemoveMargin) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = m.Pair.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.Margin.Size() + n += 1 + l + sovTx(uint64(l)) + return n +} + +func (m *MsgRemoveMarginResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = m.MarginOut.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.FundingPayment.Size() + n += 1 + l + sovTx(uint64(l)) + if m.Position != nil { + l = m.Position.Size() + n += 1 + l + sovTx(uint64(l)) + } + return n +} + +func (m *MsgAddMargin) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) if l > 0 { n += 1 + l + sovTx(uint64(l)) } @@ -3328,6 +3734,58 @@ func (m *MsgWithdrawEpochRebatesResponse) Size() (n int) { return n } +func (m *MsgShiftPegMultiplier) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = m.Pair.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.NewPegMult.Size() + n += 1 + l + sovTx(uint64(l)) + return n +} + +func (m *MsgShiftPegMultiplierResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + +func (m *MsgShiftSwapInvariant) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = m.Pair.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.NewSwapInvariant.Size() + n += 1 + l + sovTx(uint64(l)) + return n +} + +func (m *MsgShiftSwapInvariantResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + func sovTx(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -6634,6 +7092,406 @@ func (m *MsgWithdrawEpochRebatesResponse) Unmarshal(dAtA []byte) error { } return nil } +func (m *MsgShiftPegMultiplier) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgShiftPegMultiplier: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgShiftPegMultiplier: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Sender", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Sender = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pair", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Pair.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field NewPegMult", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.NewPegMult.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MsgShiftPegMultiplierResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgShiftPegMultiplierResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgShiftPegMultiplierResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MsgShiftSwapInvariant) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgShiftSwapInvariant: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgShiftSwapInvariant: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Sender", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Sender = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pair", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Pair.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field NewSwapInvariant", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.NewSwapInvariant.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MsgShiftSwapInvariantResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgShiftSwapInvariantResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgShiftSwapInvariantResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipTx(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 From e295eb34ad73031e947ae96a6085f7ba9bc764e1 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Mon, 27 Nov 2023 23:19:29 -0600 Subject: [PATCH 03/21] (wasmbinding): remove pegshitft and depth shift --- wasmbinding/bindings/msg.go | 12 ------- wasmbinding/exec_perp.go | 33 ----------------- wasmbinding/exec_perp_test.go | 31 ---------------- wasmbinding/exec_test.go | 68 ----------------------------------- wasmbinding/message_plugin.go | 16 --------- 5 files changed, 160 deletions(-) diff --git a/wasmbinding/bindings/msg.go b/wasmbinding/bindings/msg.go index 8085a6be0..bce9b362e 100644 --- a/wasmbinding/bindings/msg.go +++ b/wasmbinding/bindings/msg.go @@ -20,8 +20,6 @@ type NibiruMsg struct { RemoveMargin *RemoveMargin `json:"remove_margin,omitempty"` DonateToInsuranceFund *DonateToInsuranceFund `json:"donate_to_insurance_fund,omitempty"` // TODO InsuranceFundWithdraw *InsuranceFundWithdraw `json:"insurance_fund_withdraw,omitempty"` - PegShift *PegShift `json:"peg_shift,omitempty"` - DepthShift *DepthShift `json:"depth_shift,omitempty"` SetMarketEnabled *SetMarketEnabled `json:"set_market_enabled,omitempty"` CreateMarket *CreateMarket `json:"create_market,omitempty"` @@ -62,16 +60,6 @@ type RemoveMargin struct { Margin sdk.Coin `json:"margin"` } -type PegShift struct { - Pair string `json:"pair"` - PegMult sdk.Dec `json:"peg_mult"` -} - -type DepthShift struct { - Pair string `json:"pair"` - DepthMult sdk.Dec `json:"depth_mult"` -} - type DonateToInsuranceFund struct { Sender string `json:"sender"` Donation sdk.Coin `json:"donation"` diff --git a/wasmbinding/exec_perp.go b/wasmbinding/exec_perp.go index f9f773317..992718e9e 100644 --- a/wasmbinding/exec_perp.go +++ b/wasmbinding/exec_perp.go @@ -137,39 +137,6 @@ func (exec *ExecutorPerp) RemoveMargin( return exec.MsgServer().RemoveMargin(goCtx, sdkMsg) } -func (exec *ExecutorPerp) PegShift( - cwMsg *bindings.PegShift, contractAddr sdk.AccAddress, ctx sdk.Context, -) (err error) { - if cwMsg == nil { - return wasmvmtypes.InvalidRequest{Err: "null msg"} - } - - pair, err := asset.TryNewPair(cwMsg.Pair) - if err != nil { - return err - } - - return exec.PerpV2.UnsafeShiftPegMultiplier( - ctx, - // contractAddr, - pair, - cwMsg.PegMult, - ) -} - -func (exec *ExecutorPerp) DepthShift(cwMsg *bindings.DepthShift, ctx sdk.Context) (err error) { - if cwMsg == nil { - return wasmvmtypes.InvalidRequest{Err: "null msg"} - } - - pair, err := asset.TryNewPair(cwMsg.Pair) - if err != nil { - return err - } - - return exec.PerpV2.UnsafeShiftSwapInvariant(ctx, pair, cwMsg.DepthMult) -} - func (exec *ExecutorPerp) InsuranceFundWithdraw( cwMsg *bindings.InsuranceFundWithdraw, ctx sdk.Context, ) (err error) { diff --git a/wasmbinding/exec_perp_test.go b/wasmbinding/exec_perp_test.go index 20a2159ad..b5db53479 100644 --- a/wasmbinding/exec_perp_test.go +++ b/wasmbinding/exec_perp_test.go @@ -91,7 +91,6 @@ func (s *TestSuitePerpExecutor) TestOpenAddRemoveClose() { s.DoRemoveIncorrectMarginTest(pair, incorrectMargin), s.DoRemoveMarginTest(pair, margin), s.DoClosePositionTest(pair), - s.DoPegShiftTest(pair), s.DoInsuranceFundWithdrawTest(sdk.NewInt(69), s.contractDeployer), s.DoCreateMarketTest(asset.MustNewPair("ufoo:ubar")), s.DoCreateMarketTestWithParams(asset.MustNewPair("ufoo2:ubar")), @@ -212,27 +211,6 @@ func (s *TestSuitePerpExecutor) DoClosePositionTest(pair asset.Pair) error { return err } -func (s *TestSuitePerpExecutor) DoPegShiftTest(pair asset.Pair) error { - contractAddr := s.contractPerp - cwMsg := &bindings.PegShift{ - Pair: pair.String(), - PegMult: sdk.NewDec(420), - } - - err := s.exec.PegShift(cwMsg, contractAddr, s.ctx) - return err -} - -func (s *TestSuitePerpExecutor) DoDepthShiftTest(pair asset.Pair) error { - cwMsg := &bindings.DepthShift{ - Pair: pair.String(), - DepthMult: sdk.NewDec(420), - } - - err := s.exec.DepthShift(cwMsg, s.ctx) - return err -} - func (s *TestSuitePerpExecutor) DoInsuranceFundWithdrawTest( amt sdkmath.Int, to sdk.AccAddress, ) error { @@ -302,13 +280,6 @@ func (s *TestSuitePerpExecutor) TestSadPaths_Nil() { _, err = s.exec.ClosePosition(nil, nil, s.ctx) s.Error(err) - err = s.exec.PegShift( - nil, sdk.AccAddress([]byte("contract")), s.ctx) - s.Error(err) - - err = s.exec.DepthShift(nil, s.ctx) - s.Error(err) - err = s.exec.InsuranceFundWithdraw(nil, s.ctx) s.Error(err) } @@ -348,8 +319,6 @@ func (s *TestSuitePerpExecutor) TestSadPaths_InvalidPair() { s.DoAddMarginTest(pair, margin), s.DoRemoveMarginTest(pair, margin), s.DoClosePositionTest(pair), - s.DoPegShiftTest(pair), - s.DoDepthShiftTest(pair), s.DoSetMarketEnabledTest(pair, true), s.DoSetMarketEnabledTest(pair, false), s.DoCreateMarketTest(pair), diff --git a/wasmbinding/exec_test.go b/wasmbinding/exec_test.go index a02fd36a9..d5b5fd9a5 100644 --- a/wasmbinding/exec_test.go +++ b/wasmbinding/exec_test.go @@ -362,40 +362,6 @@ func (s *TestSuiteExecutor) TestOracleParams() { s.Require().Equal(theValidatorFeeRatio, params.ValidatorFeeRatio) } -func (s *TestSuiteExecutor) TestPegShift() { - pair := asset.MustNewPair(s.happyFields.Pair) - execMsg := bindings.NibiruMsg{ - PegShift: &bindings.PegShift{ - Pair: pair.String(), - PegMult: sdk.NewDec(420), - }, - } - - s.T().Log("Executing with permission should succeed") - contract := s.contractShifter - s.keeper.SetSudoContracts( - []string{contract.String()}, s.ctx, - ) - contractRespBz, err := s.ExecuteAgainstContract(contract, execMsg) - s.NoErrorf(err, "contractRespBz: %s", contractRespBz) - - s.T().Log("Executing without permission should fail") - s.keeper.SetSudoContracts( - []string{}, s.ctx, - ) - contractRespBz, err = s.ExecuteAgainstContract(contract, execMsg) - s.Errorf(err, "contractRespBz: %s", contractRespBz) - - s.T().Log("Executing the wrong contract should fail") - contract = s.contractPerp - s.keeper.SetSudoContracts( - []string{contract.String()}, s.ctx, - ) - contractRespBz, err = s.ExecuteAgainstContract(contract, execMsg) - s.Errorf(err, "contractRespBz: %s", contractRespBz) - s.Contains(err.Error(), "Error parsing into type") -} - func (s *TestSuiteExecutor) TestNoOp() { contract := s.contractShifter execMsg := bindings.NibiruMsg{ @@ -405,40 +371,6 @@ func (s *TestSuiteExecutor) TestNoOp() { s.NoErrorf(err, "contractRespBz: %s", contractRespBz) } -func (s *TestSuiteExecutor) TestDepthShift() { - pair := asset.MustNewPair(s.happyFields.Pair) - execMsg := bindings.NibiruMsg{ - DepthShift: &bindings.DepthShift{ - Pair: pair.String(), - DepthMult: sdk.NewDec(2), - }, - } - - s.T().Log("Executing with permission should succeed") - contract := s.contractShifter - s.keeper.SetSudoContracts( - []string{contract.String()}, s.ctx, - ) - contractRespBz, err := s.ExecuteAgainstContract(contract, execMsg) - s.NoErrorf(err, "contractRespBz: %s", contractRespBz) - - s.T().Log("Executing without permission should fail") - s.keeper.SetSudoContracts( - []string{}, s.ctx, - ) - contractRespBz, err = s.ExecuteAgainstContract(contract, execMsg) - s.Errorf(err, "contractRespBz: %s", contractRespBz) - - s.T().Log("Executing the wrong contract should fail") - contract = s.contractPerp - s.keeper.SetSudoContracts( - []string{contract.String()}, s.ctx, - ) - contractRespBz, err = s.ExecuteAgainstContract(contract, execMsg) - s.Errorf(err, "contractRespBz: %s", contractRespBz) - s.Contains(err.Error(), "Error parsing into type") -} - func (s *TestSuiteExecutor) TestInsuranceFundWithdraw() { admin := s.contractDeployer.String() amtToWithdraw := sdk.NewInt(69) diff --git a/wasmbinding/message_plugin.go b/wasmbinding/message_plugin.go index 2b2c726bf..a05b249c7 100644 --- a/wasmbinding/message_plugin.go +++ b/wasmbinding/message_plugin.go @@ -80,22 +80,6 @@ func (messenger *CustomMessenger) DispatchMsg( _, err = messenger.Perp.RemoveMargin(cwMsg, contractAddr, ctx) return events, data, err - // Perp module | shifter - case contractExecuteMsg.ExecuteMsg.PegShift != nil: - if err := messenger.Sudo.CheckPermissions(contractAddr, ctx); err != nil { - return events, data, err - } - cwMsg := contractExecuteMsg.ExecuteMsg.PegShift - err = messenger.Perp.PegShift(cwMsg, contractAddr, ctx) - return events, data, err - case contractExecuteMsg.ExecuteMsg.DepthShift != nil: - if err := messenger.Sudo.CheckPermissions(contractAddr, ctx); err != nil { - return events, data, err - } - cwMsg := contractExecuteMsg.ExecuteMsg.DepthShift - err = messenger.Perp.DepthShift(cwMsg, ctx) - return events, data, err - // Perp module | controller case contractExecuteMsg.ExecuteMsg.CreateMarket != nil: if err := messenger.Sudo.CheckPermissions(contractAddr, ctx); err != nil { From 02328528ccf8acee3d0629d55f1cb6ab083672eb Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 08:45:55 -0600 Subject: [PATCH 04/21] test(perp): msg tests --- x/perp/v2/integration/action/dnr.go | 3 +- x/perp/v2/keeper/admin_test.go | 259 +++++++++++++++------------- x/perp/v2/keeper/amm_test.go | 2 +- x/perp/v2/keeper/fixture_test.go | 1 + x/perp/v2/keeper/msg_server.go | 7 +- x/perp/v2/types/amm.go | 2 +- x/perp/v2/types/amm_test.go | 2 +- x/perp/v2/types/errors.go | 6 +- x/perp/v2/types/msgs.go | 4 +- x/perp/v2/types/msgs_test.go | 54 ++++++ x/sudo/keeper/keeper.go | 4 +- 11 files changed, 207 insertions(+), 137 deletions(-) create mode 100644 x/perp/v2/keeper/fixture_test.go diff --git a/x/perp/v2/integration/action/dnr.go b/x/perp/v2/integration/action/dnr.go index 110c4779e..9c5b69a54 100644 --- a/x/perp/v2/integration/action/dnr.go +++ b/x/perp/v2/integration/action/dnr.go @@ -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) diff --git a/x/perp/v2/keeper/admin_test.go b/x/perp/v2/keeper/admin_test.go index 4dacd365a..495e83d18 100644 --- a/x/perp/v2/keeper/admin_test.go +++ b/x/perp/v2/keeper/admin_test.go @@ -1,11 +1,11 @@ package keeper_test import ( + "fmt" "testing" "time" sdkmath "cosmossdk.io/math" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -18,7 +18,7 @@ import ( "github.com/NibiruChain/nibiru/x/common/testutil/mock" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" - "github.com/NibiruChain/nibiru/x/perp/v2/types" + perptypes "github.com/NibiruChain/nibiru/x/perp/v2/types" sudotypes "github.com/NibiruChain/nibiru/x/sudo/types" tftypes "github.com/NibiruChain/nibiru/x/tokenfactory/types" @@ -31,9 +31,9 @@ func TestAdmin_WithdrawFromInsuranceFund(t *testing.T) { expectBalance := func( want sdkmath.Int, t *testing.T, nibiru *app.NibiruApp, ctx sdk.Context, ) { - insuranceFund := nibiru.AccountKeeper.GetModuleAddress(types.PerpEFModuleAccount) + insuranceFund := nibiru.AccountKeeper.GetModuleAddress(perptypes.PerpEFModuleAccount) balances := nibiru.BankKeeper.GetAllBalances(ctx, insuranceFund) - got := balances.AmountOf(types.TestingCollateralDenomNUSD) + got := balances.AmountOf(perptypes.TestingCollateralDenomNUSD) require.EqualValues(t, want.String(), got.String()) } @@ -41,14 +41,14 @@ func TestAdmin_WithdrawFromInsuranceFund(t *testing.T) { testapp.EnsureNibiruPrefix() nibiru, ctx = testapp.NewNibiruTestAppAndContext() expectBalance(sdk.ZeroInt(), t, nibiru, ctx) - nibiru.PerpKeeperV2.Collateral.Set(ctx, types.TestingCollateralDenomNUSD) + nibiru.PerpKeeperV2.Collateral.Set(ctx, perptypes.TestingCollateralDenomNUSD) return nibiru, ctx } fundModule := func(t *testing.T, amount sdkmath.Int, ctx sdk.Context, nibiru *app.NibiruApp) { - coins := sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, amount)) + coins := sdk.NewCoins(sdk.NewCoin(perptypes.TestingCollateralDenomNUSD, amount)) err := testapp.FundModuleAccount( - nibiru.BankKeeper, ctx, types.PerpEFModuleAccount, + nibiru.BankKeeper, ctx, perptypes.PerpEFModuleAccount, coins, ) require.NoError(t, err) @@ -70,7 +70,7 @@ func TestAdmin_WithdrawFromInsuranceFund(t *testing.T) { require.EqualValues(t, amountToFund.String(), - nibiru.BankKeeper.GetBalance(ctx, admin, types.TestingCollateralDenomNUSD).Amount.String(), + nibiru.BankKeeper.GetBalance(ctx, admin, perptypes.TestingCollateralDenomNUSD).Amount.String(), ) expectBalance(sdk.ZeroInt(), t, nibiru, ctx) }, @@ -101,7 +101,7 @@ func TestCreateMarket(t *testing.T) { admin := app.PerpKeeperV2.Admin // Error because of invalid market - market := types.DefaultMarket(pair).WithMaintenanceMarginRatio(sdk.NewDec(2)) + market := perptypes.DefaultMarket(pair).WithMaintenanceMarginRatio(sdk.NewDec(2)) err := admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ Pair: pair, PriceMultiplier: amm.PriceMultiplier, @@ -208,7 +208,7 @@ func TestCloseMarket(t *testing.T) { SetBlockNumber(1), SetBlockTime(startTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(perptypes.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( CloseMarket(pairBtcUsdc), @@ -217,11 +217,11 @@ func TestCloseMarket(t *testing.T) { MarketOrderFails( alice, pairBtcUsdc, - types.Direction_LONG, + perptypes.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec(), - types.ErrMarketNotEnabled, + perptypes.ErrMarketNotEnabled, ), ), TC("cannot close position on disabled market").When( @@ -233,11 +233,11 @@ func TestCloseMarket(t *testing.T) { ), SetBlockNumber(1), SetBlockTime(startTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(10_200)))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(perptypes.TestingCollateralDenomNUSD, sdk.NewInt(10_200)))), MarketOrder( alice, pairBtcUsdc, - types.Direction_LONG, + perptypes.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec(), @@ -247,7 +247,7 @@ func TestCloseMarket(t *testing.T) { CloseMarketShouldFail(pairBtcUsdc), CloseMarketShouldFail("random:pair"), ).Then( - ClosePositionFails(alice, pairBtcUsdc, types.ErrMarketNotEnabled), + ClosePositionFails(alice, pairBtcUsdc, perptypes.ErrMarketNotEnabled), ), TC("cannot partial close position on disabled market").When( CreateCustomMarket( @@ -258,11 +258,11 @@ func TestCloseMarket(t *testing.T) { ), SetBlockNumber(1), SetBlockTime(startTime), - FundAccount(alice, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(10_200)))), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(perptypes.TestingCollateralDenomNUSD, sdk.NewInt(10_200)))), MarketOrder( alice, pairBtcUsdc, - types.Direction_LONG, + perptypes.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec(), @@ -271,7 +271,7 @@ func TestCloseMarket(t *testing.T) { CloseMarket(pairBtcUsdc), AMMShouldBeEqual(pairBtcUsdc, AMM_SettlementPriceShoulBeEqual(sdk.MustNewDecFromStr("1.1"))), ).Then( - PartialCloseFails(alice, pairBtcUsdc, sdk.NewDec(5_000), types.ErrMarketNotEnabled), + PartialCloseFails(alice, pairBtcUsdc, sdk.NewDec(5_000), perptypes.ErrMarketNotEnabled), ), } @@ -299,28 +299,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", - newDenom: "", sender: adminSender, wantErr: types.ErrInvalidCollateral.Error(), + { + name: "sad: invalid denom", + newDenom: "", sender: adminSender, wantErr: perptypes.ErrInvalidCollateral.Error(), }, - {name: "sad: sender not in sudoers", + { + name: "sad: sender not in sudoers", newDenom: "nusd", sender: nonAdminSender, wantErr: "insufficient permissions on smart contract", }, } { @@ -343,125 +349,138 @@ func TestAdmin_ChangeCollateralDenom(t *testing.T) { } } -type TestSuiteSmartContracts struct { +type TestSuiteAdmin struct { suite.Suite - nibiru *app.NibiruApp - ctx sdk.Context - addrAdmin sdk.AccAddress - - ratesMap map[asset.Pair]sdk.Dec + nibiru *app.NibiruApp + ctx sdk.Context + addrAdmin sdk.AccAddress + perpKeeper keeper.Keeper + perpMsgServer perptypes.MsgServer } -func (s *TestSuiteSmartContracts) SetupSuite() { - sender := testutil.AccAddress() - s.addrAdmin = sender +var _ suite.SetupTestSuite = (*TestSuiteAdmin)(nil) + +func TestSuite_Admin_RunAll(t *testing.T) { + suite.Run(t, new(TestSuiteAdmin)) +} +// SetupTest: Runs before every test in the suite. +func (s *TestSuiteAdmin) SetupTest() { + s.addrAdmin = testutil.AccAddress() genesisState := genesis.NewTestGenesisState(app.MakeEncodingConfig()) - genesisState = genesis.AddOracleGenesis(genesisState) genesisState = genesis.AddPerpV2Genesis(genesisState) nibiru := testapp.NewNibiruTestApp(genesisState) - ctx := nibiru.NewContext(false, tmproto.Header{ - Height: 1, - ChainID: "nibiru-wasmnet-1", - Time: time.Now().UTC(), + ctx := testapp.NewContext(nibiru) + nibiru.SudoKeeper.Sudoers.Set(ctx, sudotypes.Sudoers{ + Root: "mock-root", // unused + Contracts: []string{s.addrAdmin.String()}, }) + s.nibiru = nibiru + s.ctx = ctx + s.perpKeeper = s.nibiru.PerpKeeperV2 + s.perpMsgServer = keeper.NewMsgServerImpl(s.perpKeeper) + s.nibiru.PerpKeeperV2.Collateral.Set(s.ctx, perptypes.TestingCollateralDenomNUSD) + + sender := s.addrAdmin coins := sdk.NewCoins( sdk.NewCoin(denoms.NIBI, sdk.NewInt(1_000_000)), - sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(420_000*69)), + sdk.NewCoin(perptypes.TestingCollateralDenomNUSD, sdk.NewInt(420_000*69)), sdk.NewCoin(denoms.USDT, sdk.NewInt(420_000*69)), ) s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins)) - - s.nibiru = nibiru - s.ctx = ctx - s.nibiru.PerpKeeperV2.Collateral.Set(s.ctx, types.TestingCollateralDenomNUSD) } -func (s *TestSuiteSmartContracts) DoPegShiftTest(pair asset.Pair) error { - sender := s.addrAdmin - newPegMult := sdk.NewDec(420) - err := s.nibiru.AppKeepers.PerpKeeperV2.Admin.ShiftPegMultiplier( - s.ctx, pair, newPegMult, sender, - ) +// HandleMsg: Convenience Executes a tx msg from the MsgServer +func (s *TestSuiteAdmin) HandleMsg(txMsg sdk.Msg) (err error) { + ctx := s.ctx + switch msg := txMsg.(type) { + case *perptypes.MsgShiftPegMultiplier: + _, err = s.perpMsgServer.ShiftPegMultiplier(ctx, msg) + case *perptypes.MsgShiftSwapInvariant: + _, err = s.perpMsgServer.ShiftSwapInvariant(ctx, msg) + case *perptypes.MsgChangeCollateralDenom: + _, err = s.perpMsgServer.ChangeCollateralDenom(ctx, msg) + default: + return fmt.Errorf("unexpected message of type %T encountered", msg) + } return err } -func TestAdmin_PegShift(t *testing.T) { - pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) - amm := *mock.TestAMMDefault() - app, ctx := testapp.NewNibiruTestAppAndContext() - admin := app.PerpKeeperV2.Admin - - // Error because of invalid market - market := types.DefaultMarket(pair).WithMaintenanceMarginRatio(sdk.NewDec(2)) - err := admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ - Pair: pair, - PriceMultiplier: amm.PriceMultiplier, - SqrtDepth: amm.SqrtDepth, - Market: &market, // Invalid maintenance ratio - }) - require.ErrorContains(t, err, "maintenance margin ratio ratio must be 0 <= ratio <= 1") - - // Error because of invalid amm - err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ - Pair: pair, - PriceMultiplier: sdk.NewDec(-1), - SqrtDepth: amm.SqrtDepth, - }) - require.ErrorContains(t, err, "init price multiplier must be > 0") - - // Set it correctly - err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ - Pair: pair, - PriceMultiplier: amm.PriceMultiplier, - SqrtDepth: amm.SqrtDepth, - EnableMarket: true, - }) - require.NoError(t, err) - - lastVersion, err := app.PerpKeeperV2.MarketLastVersion.Get(ctx, pair) - require.NoError(t, err) - require.Equal(t, uint64(1), lastVersion.Version) - - // Check that amm and market have version 1 - amm, err = app.PerpKeeperV2.GetAMM(ctx, pair) - require.NoError(t, err) - require.Equal(t, uint64(1), amm.Version) +// TestCheckPermissions: Verify that all of the expected admin functions fail +// when called without sudo permissions. +func (s *TestSuiteAdmin) TestCheckPermissions() { + // Sender should not be equal to the admin. + senderAddr := testutil.AccAddress() + if senderAddr.Equals(s.addrAdmin) { + senderAddr = testutil.AccAddress() + } + sender := senderAddr.String() - market, err = app.PerpKeeperV2.GetMarket(ctx, pair) - require.NoError(t, err) - require.Equal(t, uint64(1), market.Version) + state, err := s.nibiru.AppKeepers.SudoKeeper.Sudoers.Get(s.ctx) + fmt.Printf("state: %v\n", state) + fmt.Printf("err: %v\n", err) - // Fail since it already exists and it is not disabled - err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ - Pair: pair, - PriceMultiplier: amm.PriceMultiplier, - SqrtDepth: amm.SqrtDepth, - }) - require.ErrorContains(t, err, "already exists") + for _, testCaseMsg := range []sdk.Msg{ + &perptypes.MsgShiftPegMultiplier{ + Sender: sender, Pair: asset.Pair("valid:pair"), NewPegMult: sdk.NewDec(420), + }, + &perptypes.MsgShiftSwapInvariant{ + Sender: sender, Pair: asset.Pair("valid:pair"), NewSwapInvariant: sdk.NewInt(420), + }, + &perptypes.MsgChangeCollateralDenom{ + Sender: sender, NewDenom: "newdenom", + }, + } { + s.Run(fmt.Sprintf("%T", testCaseMsg), func() { + err := s.HandleMsg(testCaseMsg) + s.ErrorContains(err, sudotypes.ErrUnauthorized.Error()) + }) + } +} - // Close the market to test that we can create it again but with an increased version - err = admin.CloseMarket(ctx, pair) - require.NoError(t, err) +func (s *TestSuiteAdmin) DoShiftPegTest(pair asset.Pair) error { + _, err := s.perpMsgServer.ShiftPegMultiplier( + sdk.WrapSDKContext(s.ctx), &perptypes.MsgShiftPegMultiplier{ + Sender: s.addrAdmin.String(), + Pair: pair, + NewPegMult: sdk.NewDec(420), + }, + ) + return err +} - err = admin.CreateMarket(ctx, keeper.ArgsCreateMarket{ - Pair: pair, - PriceMultiplier: amm.PriceMultiplier, - SqrtDepth: amm.SqrtDepth, - }) - require.NoError(t, err) +func (s *TestSuiteAdmin) DoShiftSwapInvariantTest(pair asset.Pair) error { + _, err := s.perpMsgServer.ShiftSwapInvariant( + sdk.WrapSDKContext(s.ctx), &perptypes.MsgShiftSwapInvariant{ + Sender: s.addrAdmin.String(), + Pair: pair, + NewSwapInvariant: sdk.NewInt(420), + }, + ) + return err +} - lastVersion, err = app.PerpKeeperV2.MarketLastVersion.Get(ctx, pair) - require.NoError(t, err) - require.Equal(t, uint64(2), lastVersion.Version) +// TestAdmin_DoHappy: Happy path test cases +func (s *TestSuiteAdmin) TestAdmin_DoHappy() { + pair := asset.Registry.Pair(denoms.ATOM, denoms.NUSD) - // Check that amm and market have version 2 - amm, err = app.PerpKeeperV2.GetAMM(ctx, pair) - require.NoError(t, err) - require.Equal(t, uint64(2), amm.Version) + for _, err := range []error{ + s.DoShiftPegTest(pair), + s.DoShiftSwapInvariantTest(pair), + } { + s.NoError(err) + } +} - market, err = app.PerpKeeperV2.GetMarket(ctx, pair) - require.NoError(t, err) - require.Equal(t, uint64(2), market.Version) +// TestAdmin_SadPathsInvalidPair: Test scenarios that fail due to the use of a +// market that doesn't exist. +func (s *TestSuiteAdmin) TestAdmin_SadPathsInvalidPair() { + pair := asset.Pair("ftt:ust:doge") + for _, err := range []error{ + s.DoShiftPegTest(pair), + s.DoShiftSwapInvariantTest(pair), + } { + s.Error(err) + } } diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 07f4058d7..813ef0482 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -256,7 +256,7 @@ func TestEditSwapInvariantFail(t *testing.T) { // Error because of invalid price multiplier err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(-1)) - require.ErrorIs(t, err, types.ErrNegativeSwapInvariant) + require.ErrorIs(t, err, types.ErrNonPositiveSwapInvariant) // Add market activity err = app.BankKeeper.MintCoins(ctx, "inflation", sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(102)))) diff --git a/x/perp/v2/keeper/fixture_test.go b/x/perp/v2/keeper/fixture_test.go new file mode 100644 index 000000000..942926490 --- /dev/null +++ b/x/perp/v2/keeper/fixture_test.go @@ -0,0 +1 @@ +package keeper_test diff --git a/x/perp/v2/keeper/msg_server.go b/x/perp/v2/keeper/msg_server.go index c0ad132c3..303d47471 100644 --- a/x/perp/v2/keeper/msg_server.go +++ b/x/perp/v2/keeper/msg_server.go @@ -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 } @@ -190,7 +189,8 @@ func (m msgServer) WithdrawEpochRebates(ctx context.Context, msg *types.MsgWithd }, nil } -// TODO: test ShiftPegMultiplier +// ShiftPegMultiplier: gRPC tx msg for changing a market's peg multiplier. +// [Admin] Only callable by sudoers. func (m msgServer) ShiftPegMultiplier( goCtx context.Context, msg *types.MsgShiftPegMultiplier, ) (*types.MsgShiftPegMultiplierResponse, error) { @@ -203,7 +203,8 @@ func (m msgServer) ShiftPegMultiplier( return &types.MsgShiftPegMultiplierResponse{}, err } -// TODO: test ShiftSwapInvariant +// ShiftSwapInvariant: gRPC tx msg for changing a market's swap invariant. +// [Admin] Only callable by sudoers. func (m msgServer) ShiftSwapInvariant( goCtx context.Context, msg *types.MsgShiftSwapInvariant, ) (*types.MsgShiftSwapInvariantResponse, error) { diff --git a/x/perp/v2/types/amm.go b/x/perp/v2/types/amm.go index fcb7feab0..57f93a410 100644 --- a/x/perp/v2/types/amm.go +++ b/x/perp/v2/types/amm.go @@ -387,7 +387,7 @@ func (amm AMM) CalcUpdateSwapInvariantCost(newSwapInvariant sdk.Dec) (sdkmath.In } if !newSwapInvariant.IsPositive() { - return sdkmath.Int{}, ErrNegativeSwapInvariant + return sdkmath.Int{}, ErrNonPositiveSwapInvariant } marketValueBefore, err := amm.GetMarketValue() diff --git a/x/perp/v2/types/amm_test.go b/x/perp/v2/types/amm_test.go index ab4e13a5f..8c40d643a 100644 --- a/x/perp/v2/types/amm_test.go +++ b/x/perp/v2/types/amm_test.go @@ -589,7 +589,7 @@ func TestCalcUpdateSwapInvariantCost(t *testing.T) { TotalShort: sdk.ZeroDec(), }, newSwapInvariant: sdk.NewDec(-1), - expectedErr: types.ErrNegativeSwapInvariant, + expectedErr: types.ErrNonPositiveSwapInvariant, }, } diff --git a/x/perp/v2/types/errors.go b/x/perp/v2/types/errors.go index 4c49842fc..a369f80d5 100644 --- a/x/perp/v2/types/errors.go +++ b/x/perp/v2/types/errors.go @@ -2,10 +2,6 @@ package types import sdkerrors "cosmossdk.io/errors" -// highestErrorCode = 33 -// NOTE: Please increment this when you add an error to make it easier for -// other developers to know which "code" value should be used next. - var ( ErrPairNotSupported = sdkerrors.Register(ModuleName, 1, "pair not supported") ErrQuoteReserveAtZero = sdkerrors.Register(ModuleName, 2, "quote reserve after at zero") @@ -42,7 +38,7 @@ var ( ErrLiquidityDepthOverflow = sdkerrors.Register(ModuleName, 24, "liquidty depth overflow") ErrMarketNotEnabled = sdkerrors.Register(ModuleName, 25, "market is not enabled, you can only fully close your position") ErrNonPositivePegMultiplier = sdkerrors.Register(ModuleName, 26, "peg multiplier must be > 0") - ErrNegativeSwapInvariant = sdkerrors.Register(ModuleName, 27, "swap multiplier must be > 0") + ErrNonPositiveSwapInvariant = sdkerrors.Register(ModuleName, 27, "swap multiplier must be > 0") ErrNilSwapInvariant = sdkerrors.Register(ModuleName, 28, "swap multiplier must be not nil") ErrNotEnoughFundToPayAction = sdkerrors.Register(ModuleName, 29, "not enough fund in perp EF to pay for action") diff --git a/x/perp/v2/types/msgs.go b/x/perp/v2/types/msgs.go index 5d9ad325e..9eed9cf3f 100644 --- a/x/perp/v2/types/msgs.go +++ b/x/perp/v2/types/msgs.go @@ -364,7 +364,7 @@ func (m MsgShiftPegMultiplier) ValidateBasic() error { return err } if !m.NewPegMult.IsPositive() { - return fmt.Errorf("new peg multiplier must be positive: got value %s", m.NewPegMult) + return fmt.Errorf("%w: got value %s", ErrNonPositivePegMultiplier, m.NewPegMult) } return nil } @@ -391,7 +391,7 @@ func (m MsgShiftSwapInvariant) ValidateBasic() error { return err } if !m.NewSwapInvariant.IsPositive() { - return fmt.Errorf("new swap invariant must be positive: got value %s", m.NewSwapInvariant) + return fmt.Errorf("%w: got value %s", ErrNonPositiveSwapInvariant, m.NewSwapInvariant) } return nil } diff --git a/x/perp/v2/types/msgs_test.go b/x/perp/v2/types/msgs_test.go index 083e5b137..ae92f4a67 100644 --- a/x/perp/v2/types/msgs_test.go +++ b/x/perp/v2/types/msgs_test.go @@ -371,6 +371,48 @@ func TestMsgValidateBasic(t *testing.T) { true, "invalid liquidation at index 0: invalid base asset", }, + // MsgShiftPegMultiplier + { + name: "MsgShiftPegMultiplier: Invalid pair", + msg: &MsgShiftPegMultiplier{ + Sender: validSender, + Pair: asset.Pair("not_a_pair"), + NewPegMult: sdk.NewDec(420), + }, + expectErr: true, + expectedError: asset.ErrInvalidTokenPair.Error(), + }, + { + name: "MsgShiftPegMultiplier: nonpositive peg multiplier", + msg: &MsgShiftPegMultiplier{ + Sender: validSender, + Pair: asset.Pair("valid:pair"), + NewPegMult: sdk.NewDec(-420), + }, + expectErr: true, + expectedError: ErrNonPositivePegMultiplier.Error(), + }, + // MsgDonateToEcosystemFund test cases + { + name: "MsgShiftSwapInvariant: Invalid pair", + msg: &MsgShiftSwapInvariant{ + Sender: validSender, + Pair: asset.Pair("not_a_pair"), + NewSwapInvariant: sdk.NewInt(420), + }, + expectErr: true, + expectedError: asset.ErrInvalidTokenPair.Error(), + }, + { + name: "MsgShiftSwapInvariant: nonpositive swap invariant", + msg: &MsgShiftSwapInvariant{ + Sender: validSender, + Pair: asset.Pair("valid:pair"), + NewSwapInvariant: sdk.NewInt(-420), + }, + expectErr: true, + expectedError: ErrNonPositiveSwapInvariant.Error(), + }, } for _, tc := range testCases { @@ -399,6 +441,8 @@ func TestMsg_GetSigners(t *testing.T) { &MsgPartialClose{Sender: validSender}, &MsgDonateToEcosystemFund{Sender: validSender}, &MsgMultiLiquidate{Sender: validSender}, + &MsgShiftPegMultiplier{Sender: validSender}, + &MsgShiftSwapInvariant{Sender: validSender}, } msgInvalidSenderList := []sdk.Msg{ &MsgAddMargin{Sender: invalidSender}, @@ -409,6 +453,8 @@ func TestMsg_GetSigners(t *testing.T) { &MsgPartialClose{Sender: invalidSender}, &MsgDonateToEcosystemFund{Sender: invalidSender}, &MsgMultiLiquidate{Sender: invalidSender}, + &MsgShiftPegMultiplier{Sender: invalidSender}, + &MsgShiftSwapInvariant{Sender: invalidSender}, } for _, msg := range msgValidSenderList { @@ -545,6 +591,14 @@ func TestMsg_GetSignBytes(t *testing.T) { name: "MsgMultiLiquidate", msg: &MsgMultiLiquidate{}, }, + { + name: "MsgShiftPegMultiplier", + msg: &MsgShiftPegMultiplier{}, + }, + { + name: "MsgShiftSwapInvariant", + msg: &MsgShiftSwapInvariant{}, + }, } for _, tc := range testCases { diff --git a/x/sudo/keeper/keeper.go b/x/sudo/keeper/keeper.go index f28a9169d..7dc623058 100644 --- a/x/sudo/keeper/keeper.go +++ b/x/sudo/keeper/keeper.go @@ -141,8 +141,8 @@ func (k Keeper) CheckPermissions( hasPermission := set.New(contracts...).Has(contract.String()) || contract.String() == state.Root if !hasPermission { return fmt.Errorf( - "insufficient permissions on smart contract: %s. The sudo contracts are: %s", - contract, contracts, + "%w: insufficient permissions on smart contract: %s. The sudo contracts are: %s", + sudotypes.ErrUnauthorized, contract, contracts, ) } return nil From 1220834caa441b57445cac528598d833f9f01bbf Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 09:38:13 -0600 Subject: [PATCH 05/21] change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9cb1daee..3c63bfdc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1617](https://github.com/NibiruChain/nibiru/pull/1617) - fix(app)!: non-nil snapshot manager is not guaranteed in testapp * [#1645](https://github.com/NibiruChain/nibiru/pull/1645) - fix(tokenfactory)!: token supply in bank keeper must be correct after MsgBurn. * [#1646](https://github.com/NibiruChain/nibiru/pull/1646) - feat(wasmbinding)!: whitelisted stargate queries for QueryRequest::Stargate: auth, bank, gov, tokenfactory, epochs, inflation, oracle, sudo, devgas +* [#1680](https://github.com/NibiruChain/nibiru/pull/1680) - feat(perp): MsgShiftPegMultiplier, MsgShiftSwapInvariant. ### Improvements From 63b72aa41d9cd2fbe58a41b9fa905df263a3e692 Mon Sep 17 00:00:00 2001 From: Unique Divine <51418232+Unique-Divine@users.noreply.github.com> Date: Tue, 28 Nov 2023 09:47:44 -0600 Subject: [PATCH 06/21] Update x/perp/v2/types/msgs.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- x/perp/v2/types/msgs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/perp/v2/types/msgs.go b/x/perp/v2/types/msgs.go index 9eed9cf3f..b3ccc223f 100644 --- a/x/perp/v2/types/msgs.go +++ b/x/perp/v2/types/msgs.go @@ -282,7 +282,7 @@ func (m MsgPartialClose) GetSigners() []sdk.AccAddress { // ------------------------ MsgChangeCollateralDenom ------------------------ func (m MsgChangeCollateralDenom) Route() string { return "perp" } -func (m MsgChangeCollateralDenom) Type() string { return "partial_close_msg" } +func (m MsgChangeCollateralDenom) Type() string { return "change_collateral_denom_msg" } func (m MsgChangeCollateralDenom) ValidateBasic() error { if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { From b081818de00b4ab67da4b7ad2e217c53acddd047 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 10:03:32 -0600 Subject: [PATCH 07/21] PR comments changes --- x/perp/v2/keeper/admin.go | 12 ++++++------ x/perp/v2/keeper/admin_test.go | 4 ---- x/perp/v2/keeper/amm.go | 4 ++-- x/perp/v2/keeper/amm_test.go | 12 ++++++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/x/perp/v2/keeper/admin.go b/x/perp/v2/keeper/admin.go index b3a9f3205..3783d0355 100644 --- a/x/perp/v2/keeper/admin.go +++ b/x/perp/v2/keeper/admin.go @@ -175,9 +175,9 @@ func (k admin) UnsafeChangeCollateralDenom( return nil } -// ShiftPegMultiplier: Edit the peg multiplier of an amm pool after making -// sure there's enough money in the perp EF fund to pay for the repeg. These -// funds get send to the vault to pay for trader's new net margin. +// ShiftPegMultiplier: Edit the peg multiplier of an amm pool after making sure +// there's enough money in the perp fund to pay for the repeg. These funds get +// send to the vault to pay for trader's new net margin. func (k admin) ShiftPegMultiplier( ctx sdk.Context, pair asset.Pair, @@ -190,9 +190,9 @@ func (k admin) ShiftPegMultiplier( return k.UnsafeShiftPegMultiplier(ctx, pair, newPriceMultiplier) } -// ShiftSwapInvariant: Edit the peg multiplier of an amm pool after making -// sure there's enough money in the perp EF fund to pay for the repeg. These -// funds get send to the vault to pay for trader's new net margin. +// ShiftSwapInvariant: Edit the swap invariant (liquidity depth) of an amm pool, +// ensuring that there's enough money in the perp fund to pay for the operation. +// These funds get send to the vault to pay for trader's new net margin. func (k admin) ShiftSwapInvariant( ctx sdk.Context, pair asset.Pair, diff --git a/x/perp/v2/keeper/admin_test.go b/x/perp/v2/keeper/admin_test.go index 495e83d18..6bcf3eaf8 100644 --- a/x/perp/v2/keeper/admin_test.go +++ b/x/perp/v2/keeper/admin_test.go @@ -417,10 +417,6 @@ func (s *TestSuiteAdmin) TestCheckPermissions() { } sender := senderAddr.String() - state, err := s.nibiru.AppKeepers.SudoKeeper.Sudoers.Get(s.ctx) - fmt.Printf("state: %v\n", state) - fmt.Printf("err: %v\n", err) - for _, testCaseMsg := range []sdk.Msg{ &perptypes.MsgShiftPegMultiplier{ Sender: sender, Pair: asset.Pair("valid:pair"), NewPegMult: sdk.NewDec(420), diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index b197ec405..c8da1be71 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -49,8 +49,8 @@ func (k Keeper) UnsafeShiftPegMultiplier( } // UnsafeShiftSwapInvariant: [Without checking x/sudo permissions] Edit the swap -// invariant of an amm pool after making sure there's enough money in the perp EF -// fund to pay for the repeg. These funds get send to the vault to pay for +// invariant of an amm pool after making sure there's enough money in the perp +// fund to pay for the operation. These funds get send to the vault to pay for // trader's new net margin. func (k Keeper) UnsafeShiftSwapInvariant( ctx sdk.Context, pair asset.Pair, newSwapInvariant sdk.Dec, diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 813ef0482..7dc0b967a 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -21,7 +21,7 @@ import ( types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) -func TestEditPriceMultipler(t *testing.T) { +func TestUnsafeShiftPegMultiplier(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) tests := TestCases{ @@ -152,7 +152,9 @@ func TestEditPriceMultipler(t *testing.T) { NewTestSuite(t).WithTestCases(tests...).Run() } -func TestEditPriceMultiplerFail(t *testing.T) { +// TestUnsafeShiftPegMultiplier_Fail: Test scenarios for the +// `UnsafeShiftPegMultiplier` function that should error +func TestUnsafeShiftPegMultiplier_Fail(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) app, ctx := testapp.NewNibiruTestAppAndContext() @@ -219,7 +221,9 @@ func TestEditPriceMultiplerFail(t *testing.T) { require.NoError(t, err) } -func TestEditSwapInvariantFail(t *testing.T) { +// TestUnsafeShiftSwapInvariant_Fail: Test scenarios for the +// `UnsafeShiftSwapInvariant` function that should error +func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) app, ctx := testapp.NewNibiruTestAppAndContext() account := testutil.AccAddress() @@ -289,7 +293,7 @@ func TestEditSwapInvariantFail(t *testing.T) { require.NoError(t, err) } -func TestEditSwapInvariant(t *testing.T) { +func TestUnsafeShiftSwapInvariant(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) tests := TestCases{ From 1b5c83654bfaeaa06b88b6971f8a7631c712248b Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 11:13:48 -0600 Subject: [PATCH 08/21] add events for logging purposes --- proto/nibiru/perp/v2/event.proto | 24 + x/perp/v2/integration/action/market.go | 4 +- x/perp/v2/keeper/admin.go | 2 +- x/perp/v2/keeper/amm.go | 49 +- x/perp/v2/keeper/amm_test.go | 22 +- x/perp/v2/keeper/clearing_house_test.go | 23 +- x/perp/v2/keeper/msg_server.go | 16 +- x/perp/v2/types/event.pb.go | 681 +++++++++++++++++++++--- 8 files changed, 700 insertions(+), 121 deletions(-) diff --git a/proto/nibiru/perp/v2/event.proto b/proto/nibiru/perp/v2/event.proto index f8a6b7a04..4cd8f6cd5 100644 --- a/proto/nibiru/perp/v2/event.proto +++ b/proto/nibiru/perp/v2/event.proto @@ -234,3 +234,27 @@ message MarketUpdatedEvent { // the final state of the market nibiru.perp.v2.Market final_market = 1 [ (gogoproto.nullable) = false ]; } + +message EventShiftPegMultiplier { + string old_peg_multiplier = 1 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; + string new_peg_multiplier = 2 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; + cosmos.base.v1beta1.Coin cost_paid = 3 [ (gogoproto.nullable) = false ]; +} + +message EventShiftSwapInvariant { + string old_swap_invariant = 1 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false + ]; + string new_swap_invariant = 2 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false + ]; + cosmos.base.v1beta1.Coin cost_paid = 3 [ (gogoproto.nullable) = false ]; +} diff --git a/x/perp/v2/integration/action/market.go b/x/perp/v2/integration/action/market.go index 5617181cd..57139755f 100644 --- a/x/perp/v2/integration/action/market.go +++ b/x/perp/v2/integration/action/market.go @@ -153,7 +153,7 @@ func EditPriceMultiplier(pair asset.Pair, newValue sdk.Dec) action.Action { type editSwapInvariant struct { pair asset.Pair - newValue sdk.Dec + newValue sdk.Int } func (e editSwapInvariant) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { @@ -161,7 +161,7 @@ func (e editSwapInvariant) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, return ctx, err, true } -func EditSwapInvariant(pair asset.Pair, newValue sdk.Dec) action.Action { +func EditSwapInvariant(pair asset.Pair, newValue sdk.Int) action.Action { return editSwapInvariant{ pair: pair, newValue: newValue, diff --git a/x/perp/v2/keeper/admin.go b/x/perp/v2/keeper/admin.go index 3783d0355..f3ee22077 100644 --- a/x/perp/v2/keeper/admin.go +++ b/x/perp/v2/keeper/admin.go @@ -196,7 +196,7 @@ func (k admin) ShiftPegMultiplier( func (k admin) ShiftSwapInvariant( ctx sdk.Context, pair asset.Pair, - newSwapInvariant sdk.Dec, + newSwapInvariant sdk.Int, sender sdk.AccAddress, ) error { if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index c8da1be71..7ca0322f7 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -24,8 +24,9 @@ func (k Keeper) UnsafeShiftPegMultiplier( if err != nil { return err } + oldPriceMult := amm.PriceMultiplier - if newPriceMultiplier.Equal(amm.PriceMultiplier) { + if newPriceMultiplier.Equal(oldPriceMult) { // same price multiplier, no-op return nil } @@ -36,7 +37,7 @@ func (k Keeper) UnsafeShiftPegMultiplier( return err } - err = k.handleMarketUpdateCost(ctx, pair, cost) + costPaid, err := k.handleMarketUpdateCost(ctx, pair, cost) if err != nil { return err } @@ -45,7 +46,11 @@ func (k Keeper) UnsafeShiftPegMultiplier( amm.PriceMultiplier = newPriceMultiplier k.SaveAMM(ctx, amm) - return nil + return ctx.EventManager().EmitTypedEvent(&types.EventShiftPegMultiplier{ + OldPegMultiplier: oldPriceMult, + NewPegMultiplier: newPriceMultiplier, + CostPaid: costPaid, + }) } // UnsafeShiftSwapInvariant: [Without checking x/sudo permissions] Edit the swap @@ -53,7 +58,7 @@ func (k Keeper) UnsafeShiftPegMultiplier( // fund to pay for the operation. These funds get send to the vault to pay for // trader's new net margin. func (k Keeper) UnsafeShiftSwapInvariant( - ctx sdk.Context, pair asset.Pair, newSwapInvariant sdk.Dec, + ctx sdk.Context, pair asset.Pair, newSwapInvariant sdk.Int, ) (err error) { // Get the pool amm, err := k.GetAMM(ctx, pair) @@ -61,33 +66,36 @@ func (k Keeper) UnsafeShiftSwapInvariant( return err } - // Compute cost of re-pegging the pool - cost, err := amm.CalcUpdateSwapInvariantCost(newSwapInvariant) + cost, err := amm.CalcUpdateSwapInvariantCost(newSwapInvariant.ToLegacyDec()) if err != nil { return err } - err = k.handleMarketUpdateCost(ctx, pair, cost) + costPaid, err := k.handleMarketUpdateCost(ctx, pair, cost) if err != nil { return err } - err = amm.UpdateSwapInvariant(newSwapInvariant) + err = amm.UpdateSwapInvariant(newSwapInvariant.ToLegacyDec()) if err != nil { return err } k.SaveAMM(ctx, amm) - return nil + return ctx.EventManager().EmitTypedEvent(&types.EventShiftSwapInvariant{ + OldSwapInvariant: amm.BaseReserve.Mul(amm.QuoteReserve).RoundInt(), + NewSwapInvariant: newSwapInvariant, + CostPaid: costPaid, + }) } func (k Keeper) handleMarketUpdateCost( ctx sdk.Context, pair asset.Pair, costAmt sdkmath.Int, -) (err error) { +) (costPaid sdk.Coin, err error) { collateral, err := k.Collateral.Get(ctx) if err != nil { - return err + return costPaid, err } if costAmt.IsPositive() { @@ -102,11 +110,13 @@ func (k Keeper) handleMarketUpdateCost( cost, ) if err != nil { - return types.ErrNotEnoughFundToPayAction.Wrapf( + return costPaid, types.ErrNotEnoughFundToPayAction.Wrapf( "not enough fund in perp ef to pay for repeg, need %s got %s", cost.String(), k.BankKeeper.GetBalance(ctx, k.AccountKeeper.GetModuleAddress(types.PerpEFModuleAccount), collateral).String(), ) + } else { + costPaid = cost[0] } } else if costAmt.IsNegative() { // Negative cost, send from margin vault to perp ef. @@ -119,13 +129,18 @@ func (k Keeper) handleMarketUpdateCost( ), ) if err != nil { // nolint:staticcheck - // if there's no money in margin to pay for the repeg, we still repeg. It's surprising if it's - // happening on mainnet, but it's not a problem. - // It means there's bad debt in the system, and it's preventing to pay for the repeg down. But the bad debt - // end up being paid up by the perp EF anyway. + costPaid = sdk.NewInt64Coin(collateral, 0) + // Explanation: If there's no money in the vault to pay for the + // operation, the execution should still be successful. It's + // surprising if it's happening on mainnet, but it's not a problem. + // It means there's bad debt in the system, and it's preventing to + // pay for the repeg down. But the bad debt end up being paid up by + // the perp EF anyway. + } else { + costPaid = sdk.NewCoin(collateral, costAmt.Abs()) } } - return nil + return costPaid, nil } // GetMarket returns the market that is enabled. It is the last version of the market. diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 7dc0b967a..06d0caee9 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -255,11 +255,11 @@ func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { require.NoError(t, err) // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, asset.MustNewPair("luna:usdt"), sdk.NewInt(-1)) require.ErrorContains(t, err, "market luna:usdt not found") // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(-1)) require.ErrorIs(t, err, types.ErrNonPositiveSwapInvariant) // Add market activity @@ -281,15 +281,15 @@ func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { require.NoError(t, err) // Error because no money in perp ef fund - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(2_000_000)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(2_000_000)) require.ErrorContains(t, err, "not enough fund in perp ef to pay for repeg") // Fail at validate - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(0)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(0)) require.ErrorContains(t, err, "swap multiplier must be > 0") // Works because it goes in the other way - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewDec(500_000)) + err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(500_000)) require.NoError(t, err) } @@ -308,7 +308,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e12)), + EditSwapInvariant(pair, sdk.NewInt(1e12)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)), @@ -332,7 +332,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e18)), + EditSwapInvariant(pair, sdk.NewInt(1e18)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)), @@ -352,7 +352,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e14)), + EditSwapInvariant(pair, sdk.NewInt(1e14)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1008101)), @@ -372,7 +372,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e6)), + EditSwapInvariant(pair, sdk.NewInt(1e6)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(999991)), @@ -392,7 +392,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e14)), + EditSwapInvariant(pair, sdk.NewInt(1e14)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1010102)), @@ -412,7 +412,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewDec(1e6)), + EditSwapInvariant(pair, sdk.NewInt(1e6)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(999989)), diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index 270b1dba2..f8d587864 100644 --- a/x/perp/v2/keeper/clearing_house_test.go +++ b/x/perp/v2/keeper/clearing_house_test.go @@ -48,7 +48,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), MarketOrder(bob, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, sdk.OneDec()), + EditSwapInvariant(pairBtcNusd, sdk.NewInt(1)), ). When( PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), types.ErrBaseReserveAtZero), @@ -264,7 +264,7 @@ func TestMarketOrder(t *testing.T) { FundAccount(alice, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(10_000)))), MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.NewDec(10), sdk.ZeroDec()), FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(100_000_000)))), - EditSwapInvariant(pairBtcNusd, sdk.OneDec()), + EditSwapInvariant(pairBtcNusd, sdk.NewInt(1)), ). When( ClosePosition(alice, pairBtcNusd), @@ -1956,7 +1956,7 @@ func TestUpdateSwapInvariant(t *testing.T) { pairBtcNusd := asset.Registry.Pair(denoms.BTC, denoms.NUSD) startBlockTime := time.Now() - startingSwapInvariant := sdk.NewDec(1_000_000_000_000).Mul(sdk.NewDec(1_000_000_000_000)) + startingSwapInvariant := sdk.NewInt(1e12).Mul(sdk.NewInt(1e12)) tc := TestCases{ TC("only long position - no change to swap invariant"). @@ -1999,7 +1999,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), AMMShouldBeEqual( pairBtcNusd, AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), @@ -2019,7 +2019,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), AMMShouldBeEqual( pairBtcNusd, AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), @@ -2040,7 +2040,9 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + EditSwapInvariant( + pairBtcNusd, + startingSwapInvariant.QuoRaw(10)), AMMShouldBeEqual( pairBtcNusd, AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987715651277660"))), @@ -2060,7 +2062,8 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + EditSwapInvariant(pairBtcNusd, + startingSwapInvariant.QuoRaw(10)), AMMShouldBeEqual( pairBtcNusd, AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999873578.871987801032774485"))), @@ -2083,7 +2086,8 @@ func TestUpdateSwapInvariant(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulInt64(100)), + EditSwapInvariant(pairBtcNusd, + startingSwapInvariant.MulRaw(100)), AMMShouldBeEqual( pairBtcNusd, AMM_BiasShouldBeEqual(sdk.ZeroDec()), @@ -2114,7 +2118,8 @@ func TestUpdateSwapInvariant(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.Mul(sdk.MustNewDecFromStr("0.1"))), + EditSwapInvariant(pairBtcNusd, + startingSwapInvariant.QuoRaw(10)), AMMShouldBeEqual( pairBtcNusd, AMM_BiasShouldBeEqual(sdk.ZeroDec()), diff --git a/x/perp/v2/keeper/msg_server.go b/x/perp/v2/keeper/msg_server.go index 303d47471..9add4d5ca 100644 --- a/x/perp/v2/keeper/msg_server.go +++ b/x/perp/v2/keeper/msg_server.go @@ -194,12 +194,10 @@ func (m msgServer) WithdrawEpochRebates(ctx context.Context, msg *types.MsgWithd func (m msgServer) ShiftPegMultiplier( goCtx context.Context, msg *types.MsgShiftPegMultiplier, ) (*types.MsgShiftPegMultiplierResponse, error) { - sender, err := sdk.AccAddressFromBech32(msg.Sender) - if err != nil { - return nil, err - } + // The `msg` here is checked with ValidateBasic before this fn is called. + sender, _ := sdk.AccAddressFromBech32(msg.Sender) ctx := sdk.UnwrapSDKContext(goCtx) - err = m.k.Admin.ShiftPegMultiplier(ctx, msg.Pair, msg.NewPegMult, sender) + err := m.k.Admin.ShiftPegMultiplier(ctx, msg.Pair, msg.NewPegMult, sender) return &types.MsgShiftPegMultiplierResponse{}, err } @@ -208,11 +206,9 @@ func (m msgServer) ShiftPegMultiplier( func (m msgServer) ShiftSwapInvariant( goCtx context.Context, msg *types.MsgShiftSwapInvariant, ) (*types.MsgShiftSwapInvariantResponse, error) { - sender, err := sdk.AccAddressFromBech32(msg.Sender) - if err != nil { - return nil, err - } + // The `msg` here is checked with ValidateBasic before this fn is called. + sender, _ := sdk.AccAddressFromBech32(msg.Sender) ctx := sdk.UnwrapSDKContext(goCtx) - err = m.k.Admin.ShiftSwapInvariant(ctx, msg.Pair, msg.NewSwapInvariant.ToLegacyDec(), sender) + err := m.k.Admin.ShiftSwapInvariant(ctx, msg.Pair, msg.NewSwapInvariant, sender) return &types.MsgShiftSwapInvariantResponse{}, err } diff --git a/x/perp/v2/types/event.pb.go b/x/perp/v2/types/event.pb.go index 598fae243..3a2e1756c 100644 --- a/x/perp/v2/types/event.pb.go +++ b/x/perp/v2/types/event.pb.go @@ -523,6 +523,98 @@ func (m *MarketUpdatedEvent) GetFinalMarket() Market { return Market{} } +type EventShiftPegMultiplier struct { + OldPegMultiplier github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=old_peg_multiplier,json=oldPegMultiplier,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"old_peg_multiplier"` + NewPegMultiplier github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=new_peg_multiplier,json=newPegMultiplier,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"new_peg_multiplier"` + CostPaid types.Coin `protobuf:"bytes,3,opt,name=cost_paid,json=costPaid,proto3" json:"cost_paid"` +} + +func (m *EventShiftPegMultiplier) Reset() { *m = EventShiftPegMultiplier{} } +func (m *EventShiftPegMultiplier) String() string { return proto.CompactTextString(m) } +func (*EventShiftPegMultiplier) ProtoMessage() {} +func (*EventShiftPegMultiplier) Descriptor() ([]byte, []int) { + return fileDescriptor_a5313bbc89fa31dd, []int{7} +} +func (m *EventShiftPegMultiplier) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *EventShiftPegMultiplier) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_EventShiftPegMultiplier.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *EventShiftPegMultiplier) XXX_Merge(src proto.Message) { + xxx_messageInfo_EventShiftPegMultiplier.Merge(m, src) +} +func (m *EventShiftPegMultiplier) XXX_Size() int { + return m.Size() +} +func (m *EventShiftPegMultiplier) XXX_DiscardUnknown() { + xxx_messageInfo_EventShiftPegMultiplier.DiscardUnknown(m) +} + +var xxx_messageInfo_EventShiftPegMultiplier proto.InternalMessageInfo + +func (m *EventShiftPegMultiplier) GetCostPaid() types.Coin { + if m != nil { + return m.CostPaid + } + return types.Coin{} +} + +type EventShiftSwapInvariant struct { + OldSwapInvariant github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,1,opt,name=old_swap_invariant,json=oldSwapInvariant,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"old_swap_invariant"` + NewSwapInvariant github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,2,opt,name=new_swap_invariant,json=newSwapInvariant,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"new_swap_invariant"` + CostPaid types.Coin `protobuf:"bytes,3,opt,name=cost_paid,json=costPaid,proto3" json:"cost_paid"` +} + +func (m *EventShiftSwapInvariant) Reset() { *m = EventShiftSwapInvariant{} } +func (m *EventShiftSwapInvariant) String() string { return proto.CompactTextString(m) } +func (*EventShiftSwapInvariant) ProtoMessage() {} +func (*EventShiftSwapInvariant) Descriptor() ([]byte, []int) { + return fileDescriptor_a5313bbc89fa31dd, []int{8} +} +func (m *EventShiftSwapInvariant) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *EventShiftSwapInvariant) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_EventShiftSwapInvariant.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *EventShiftSwapInvariant) XXX_Merge(src proto.Message) { + xxx_messageInfo_EventShiftSwapInvariant.Merge(m, src) +} +func (m *EventShiftSwapInvariant) XXX_Size() int { + return m.Size() +} +func (m *EventShiftSwapInvariant) XXX_DiscardUnknown() { + xxx_messageInfo_EventShiftSwapInvariant.DiscardUnknown(m) +} + +var xxx_messageInfo_EventShiftSwapInvariant proto.InternalMessageInfo + +func (m *EventShiftSwapInvariant) GetCostPaid() types.Coin { + if m != nil { + return m.CostPaid + } + return types.Coin{} +} + func init() { proto.RegisterEnum("nibiru.perp.v2.LiquidationFailedEvent_LiquidationFailedReason", LiquidationFailedEvent_LiquidationFailedReason_name, LiquidationFailedEvent_LiquidationFailedReason_value) proto.RegisterType((*PositionChangedEvent)(nil), "nibiru.perp.v2.PositionChangedEvent") @@ -532,82 +624,91 @@ func init() { proto.RegisterType((*LiquidationFailedEvent)(nil), "nibiru.perp.v2.LiquidationFailedEvent") proto.RegisterType((*AmmUpdatedEvent)(nil), "nibiru.perp.v2.AmmUpdatedEvent") proto.RegisterType((*MarketUpdatedEvent)(nil), "nibiru.perp.v2.MarketUpdatedEvent") + proto.RegisterType((*EventShiftPegMultiplier)(nil), "nibiru.perp.v2.EventShiftPegMultiplier") + proto.RegisterType((*EventShiftSwapInvariant)(nil), "nibiru.perp.v2.EventShiftSwapInvariant") } func init() { proto.RegisterFile("nibiru/perp/v2/event.proto", fileDescriptor_a5313bbc89fa31dd) } var fileDescriptor_a5313bbc89fa31dd = []byte{ - // 1109 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x96, 0x4f, 0x6f, 0x1b, 0x45, - 0x14, 0xc0, 0xe3, 0xb8, 0xa4, 0xcd, 0xf8, 0x6f, 0x06, 0x93, 0x6e, 0x43, 0xe5, 0x84, 0x55, 0x41, - 0xb9, 0x64, 0x57, 0x09, 0x12, 0x52, 0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xdb, 0xf5, 0xda, 0x4e, 0x7b, 0xfb, 0x69, 0x39, - 0x93, 0x48, 0x9b, 0x7b, 0xcd, 0xfa, 0x93, 0xc6, 0x7e, 0xbb, 0xde, 0x6c, 0xbb, 0xad, 0x5a, 0xc3, - 0x29, 0x4f, 0x43, 0x03, 0x54, 0x46, 0xa4, 0xda, 0xae, 0x9c, 0x35, 0xff, 0xca, 0x80, 0x52, 0x2d, - 0x0c, 0x0f, 0xa2, 0xa1, 0x7d, 0xff, 0x11, 0x98, 0x55, 0x5f, 0x59, 0x28, 0x0c, 0xf5, 0x8a, 0x7f, - 0x7b, 0x3c, 0xc1, 0xda, 0xee, 0xae, 0xde, 0xe8, 0x77, 0xe4, 0xd9, 0x5a, 0x18, 0xfe, 0xff, 0x86, - 0xc6, 0x3c, 0x00, 0x70, 0x17, 0xc5, 0x47, 0x58, 0x8c, 0xe4, 0xff, 0x09, 0xc8, 0xab, 0xfc, 0x43, - 0xa9, 0xd3, 0x25, 0x98, 0x1f, 0x2f, 0x81, 0xb2, 0xd4, 0x55, 0xc8, 0x49, 0x0b, 0x2d, 0xfa, 0xf4, - 0xf9, 0x45, 0x35, 0xf3, 0xe2, 0xa2, 0x9a, 0xf9, 0xf5, 0xa2, 0x9a, 0xf9, 0xfe, 0xb2, 0x3a, 0xf5, - 0xe2, 0xb2, 0x3a, 0xf5, 0xd3, 0x65, 0x75, 0xea, 0x8b, 0x95, 0x9b, 0x3a, 0x35, 0xfd, 0x5e, 0x96, - 0x31, 0x7b, 0x33, 0xf2, 0x7b, 0xf8, 0xc3, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xc9, 0xe7, 0x3a, - 0x1d, 0xce, 0x0b, 0x00, 0x00, + // 1230 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x97, 0xcf, 0x6f, 0x1b, 0x45, + 0x14, 0xc7, 0xe3, 0xb8, 0xa4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, 0xf4, 0xf7, 0xd1, 0xbc, 0xc7, 0x85, 0x74, 0x23, 0x42, + 0xfd, 0xb3, 0xef, 0x5b, 0x5d, 0x1e, 0xc9, 0x8e, 0x26, 0xa1, 0xfe, 0x84, 0x2a, 0xad, 0x01, 0x89, + 0xea, 0xec, 0x88, 0xc4, 0x94, 0x30, 0x99, 0xa9, 0x22, 0x06, 0x24, 0x72, 0x69, 0x66, 0x9d, 0x42, + 0x95, 0xe4, 0x4b, 0x2e, 0x51, 0xe5, 0x14, 0x3d, 0x51, 0x65, 0x82, 0x3e, 0x3b, 0x1d, 0x9d, 0xc1, + 0x60, 0x9c, 0xfe, 0x8f, 0x54, 0xd9, 0xfa, 0xe8, 0xf1, 0xb3, 0x6a, 0xee, 0xc9, 0xb3, 0x6a, 0xee, + 0x97, 0x67, 0xd5, 0xdc, 0xb7, 0xcf, 0xab, 0x33, 0x4f, 0x9e, 0x57, 0x67, 0x7e, 0x7c, 0x5e, 0x9d, + 0xf9, 0xf4, 0xfa, 0x59, 0x53, 0x2d, 0xfb, 0x6d, 0xa5, 0x92, 0xeb, 0xcc, 0xa9, 0xdf, 0x4e, 0xef, + 0xfe, 0x15, 0x00, 0x00, 0xff, 0xff, 0x50, 0x37, 0x90, 0x96, 0xfa, 0x0d, 0x00, 0x00, } func (m *PositionChangedEvent) Marshal() (dAtA []byte, err error) { @@ -1063,6 +1164,112 @@ func (m *MarketUpdatedEvent) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *EventShiftPegMultiplier) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *EventShiftPegMultiplier) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *EventShiftPegMultiplier) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.CostPaid.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintEvent(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + { + size := m.NewPegMultiplier.Size() + i -= size + if _, err := m.NewPegMultiplier.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintEvent(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + { + size := m.OldPegMultiplier.Size() + i -= size + if _, err := m.OldPegMultiplier.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintEvent(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + return len(dAtA) - i, nil +} + +func (m *EventShiftSwapInvariant) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *EventShiftSwapInvariant) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *EventShiftSwapInvariant) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.CostPaid.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintEvent(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + { + size := m.NewSwapInvariant.Size() + i -= size + if _, err := m.NewSwapInvariant.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintEvent(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + { + size := m.OldSwapInvariant.Size() + i -= size + if _, err := m.OldSwapInvariant.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintEvent(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + return len(dAtA) - i, nil +} + func encodeVarintEvent(dAtA []byte, offset int, v uint64) int { offset -= sovEvent(v) base := offset @@ -1213,6 +1420,36 @@ func (m *MarketUpdatedEvent) Size() (n int) { return n } +func (m *EventShiftPegMultiplier) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = m.OldPegMultiplier.Size() + n += 1 + l + sovEvent(uint64(l)) + l = m.NewPegMultiplier.Size() + n += 1 + l + sovEvent(uint64(l)) + l = m.CostPaid.Size() + n += 1 + l + sovEvent(uint64(l)) + return n +} + +func (m *EventShiftSwapInvariant) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = m.OldSwapInvariant.Size() + n += 1 + l + sovEvent(uint64(l)) + l = m.NewSwapInvariant.Size() + n += 1 + l + sovEvent(uint64(l)) + l = m.CostPaid.Size() + n += 1 + l + sovEvent(uint64(l)) + return n +} + func sovEvent(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -2577,6 +2814,308 @@ func (m *MarketUpdatedEvent) Unmarshal(dAtA []byte) error { } return nil } +func (m *EventShiftPegMultiplier) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: EventShiftPegMultiplier: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: EventShiftPegMultiplier: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field OldPegMultiplier", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.OldPegMultiplier.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field NewPegMultiplier", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.NewPegMultiplier.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CostPaid", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.CostPaid.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipEvent(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthEvent + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *EventShiftSwapInvariant) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: EventShiftSwapInvariant: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: EventShiftSwapInvariant: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field OldSwapInvariant", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.OldSwapInvariant.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field NewSwapInvariant", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.NewSwapInvariant.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CostPaid", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowEvent + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthEvent + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthEvent + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.CostPaid.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipEvent(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthEvent + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipEvent(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 From 2f0a1256cc3f37fa79d3bf382f5cbb1848eb6d0b Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 11:18:31 -0600 Subject: [PATCH 09/21] refactor: consistent error name usage PR comment --- x/perp/v2/keeper/amm.go | 2 +- x/perp/v2/keeper/amm_test.go | 6 +++--- x/perp/v2/types/errors.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index 7ca0322f7..0450e4c3a 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -111,7 +111,7 @@ func (k Keeper) handleMarketUpdateCost( ) if err != nil { return costPaid, types.ErrNotEnoughFundToPayAction.Wrapf( - "not enough fund in perp ef to pay for repeg, need %s got %s", + "need %s, got %s", cost.String(), k.BankKeeper.GetBalance(ctx, k.AccountKeeper.GetModuleAddress(types.PerpEFModuleAccount), collateral).String(), ) diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 06d0caee9..7b5e8f76d 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -214,7 +214,7 @@ func TestUnsafeShiftPegMultiplier_Fail(t *testing.T) { // Error because no money in perp ef fund err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(3)) - require.ErrorContains(t, err, "not enough fund in perp ef to pay for repeg") + require.ErrorContains(t, err, types.ErrNotEnoughFundToPayAction.Error()) // Works because it goes in the other way err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(1)) @@ -282,11 +282,11 @@ func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { // Error because no money in perp ef fund err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(2_000_000)) - require.ErrorContains(t, err, "not enough fund in perp ef to pay for repeg") + require.ErrorContains(t, err, types.ErrNotEnoughFundToPayAction.Error()) // Fail at validate err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(0)) - require.ErrorContains(t, err, "swap multiplier must be > 0") + require.ErrorContains(t, err, types.ErrNonPositivePegMultiplier.Error()) // Works because it goes in the other way err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(500_000)) diff --git a/x/perp/v2/types/errors.go b/x/perp/v2/types/errors.go index a369f80d5..a38500e85 100644 --- a/x/perp/v2/types/errors.go +++ b/x/perp/v2/types/errors.go @@ -38,8 +38,8 @@ var ( ErrLiquidityDepthOverflow = sdkerrors.Register(ModuleName, 24, "liquidty depth overflow") ErrMarketNotEnabled = sdkerrors.Register(ModuleName, 25, "market is not enabled, you can only fully close your position") ErrNonPositivePegMultiplier = sdkerrors.Register(ModuleName, 26, "peg multiplier must be > 0") - ErrNonPositiveSwapInvariant = sdkerrors.Register(ModuleName, 27, "swap multiplier must be > 0") - ErrNilSwapInvariant = sdkerrors.Register(ModuleName, 28, "swap multiplier must be not nil") + ErrNonPositiveSwapInvariant = sdkerrors.Register(ModuleName, 27, "swap invariant must be > 0") + ErrNilSwapInvariant = sdkerrors.Register(ModuleName, 28, "swap invariant must not be nil") ErrNotEnoughFundToPayAction = sdkerrors.Register(ModuleName, 29, "not enough fund in perp EF to pay for action") ErrSettlementPositionMarketEnabled = sdkerrors.Register(ModuleName, 32, "market is enabled, you can only settle position on disabled market") From 552a2d5da3923c7eead2a0d8b656c22cd97bf253 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 11:54:03 -0600 Subject: [PATCH 10/21] refactor: more PR comments --- x/perp/v2/integration/action/market.go | 18 ++++++++--------- x/perp/v2/keeper/admin.go | 2 +- x/perp/v2/keeper/amm.go | 2 +- x/perp/v2/keeper/amm_test.go | 26 ++++++++++++------------- x/perp/v2/keeper/clearing_house_test.go | 16 +++++++-------- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/x/perp/v2/integration/action/market.go b/x/perp/v2/integration/action/market.go index 57139755f..06e338260 100644 --- a/x/perp/v2/integration/action/market.go +++ b/x/perp/v2/integration/action/market.go @@ -134,35 +134,35 @@ func WithEnabled(enabled bool) MarketModifier { } } -type editPriceMultiplier struct { +type shiftPegMultiplier struct { pair asset.Pair newValue sdk.Dec } -func (e editPriceMultiplier) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { +func (e shiftPegMultiplier) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { err := app.PerpKeeperV2.UnsafeShiftPegMultiplier(ctx, e.pair, e.newValue) return ctx, err, true } -func EditPriceMultiplier(pair asset.Pair, newValue sdk.Dec) action.Action { - return editPriceMultiplier{ +func ShiftPegMultiplier(pair asset.Pair, newValue sdk.Dec) action.Action { + return shiftPegMultiplier{ pair: pair, newValue: newValue, } } -type editSwapInvariant struct { +type shiftSwapInvariant struct { pair asset.Pair - newValue sdk.Int + newValue sdkmath.Int } -func (e editSwapInvariant) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { +func (e shiftSwapInvariant) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { err := app.PerpKeeperV2.UnsafeShiftSwapInvariant(ctx, e.pair, e.newValue) return ctx, err, true } -func EditSwapInvariant(pair asset.Pair, newValue sdk.Int) action.Action { - return editSwapInvariant{ +func ShiftSwapInvariant(pair asset.Pair, newValue sdkmath.Int) action.Action { + return shiftSwapInvariant{ pair: pair, newValue: newValue, } diff --git a/x/perp/v2/keeper/admin.go b/x/perp/v2/keeper/admin.go index f3ee22077..af6ed5b03 100644 --- a/x/perp/v2/keeper/admin.go +++ b/x/perp/v2/keeper/admin.go @@ -196,7 +196,7 @@ func (k admin) ShiftPegMultiplier( func (k admin) ShiftSwapInvariant( ctx sdk.Context, pair asset.Pair, - newSwapInvariant sdk.Int, + newSwapInvariant sdkmath.Int, sender sdk.AccAddress, ) error { if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index 0450e4c3a..68ba85d18 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -58,7 +58,7 @@ func (k Keeper) UnsafeShiftPegMultiplier( // fund to pay for the operation. These funds get send to the vault to pay for // trader's new net margin. func (k Keeper) UnsafeShiftSwapInvariant( - ctx sdk.Context, pair asset.Pair, newSwapInvariant sdk.Int, + ctx sdk.Context, pair asset.Pair, newSwapInvariant sdkmath.Int, ) (err error) { // Get the pool amm, err := k.GetAMM(ctx, pair) diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 7b5e8f76d..072aa0d21 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -32,7 +32,7 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.OneDec()), + ShiftPegMultiplier(pair, sdk.OneDec()), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)), @@ -55,7 +55,7 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.NewDec(10)), + ShiftPegMultiplier(pair, sdk.NewDec(10)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)), @@ -75,7 +75,7 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.NewDec(10)), + ShiftPegMultiplier(pair, sdk.NewDec(10)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1004500)), @@ -95,7 +95,7 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), + ShiftPegMultiplier(pair, sdk.MustNewDecFromStr("0.25")), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(999626)), @@ -115,7 +115,7 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.NewDec(10)), + ShiftPegMultiplier(pair, sdk.NewDec(10)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(995500)), @@ -135,7 +135,7 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditPriceMultiplier(pair, sdk.MustNewDecFromStr("0.25")), + ShiftPegMultiplier(pair, sdk.MustNewDecFromStr("0.25")), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1000376)), @@ -286,7 +286,7 @@ func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { // Fail at validate err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(0)) - require.ErrorContains(t, err, types.ErrNonPositivePegMultiplier.Error()) + require.ErrorContains(t, err, types.ErrNonPositiveSwapInvariant.Error()) // Works because it goes in the other way err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(500_000)) @@ -308,7 +308,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewInt(1e12)), + ShiftSwapInvariant(pair, sdk.NewInt(1e12)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)), @@ -332,7 +332,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewInt(1e18)), + ShiftSwapInvariant(pair, sdk.NewInt(1e18)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)), @@ -352,7 +352,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewInt(1e14)), + ShiftSwapInvariant(pair, sdk.NewInt(1e14)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1008101)), @@ -372,7 +372,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewInt(1e6)), + ShiftSwapInvariant(pair, sdk.NewInt(1e6)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(999991)), @@ -392,7 +392,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewInt(1e14)), + ShiftSwapInvariant(pair, sdk.NewInt(1e14)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(1010102)), @@ -412,7 +412,7 @@ func TestUnsafeShiftSwapInvariant(t *testing.T) { FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(1e6)))), ). When( - EditSwapInvariant(pair, sdk.NewInt(1e6)), + ShiftSwapInvariant(pair, sdk.NewInt(1e6)), ). Then( ModuleBalanceEqual(types.VaultModuleAccount, types.TestingCollateralDenomNUSD, sdk.NewInt(999989)), diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index f8d587864..b9cee6504 100644 --- a/x/perp/v2/keeper/clearing_house_test.go +++ b/x/perp/v2/keeper/clearing_house_test.go @@ -48,7 +48,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), MarketOrder(bob, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, sdk.NewInt(1)), + ShiftSwapInvariant(pairBtcNusd, sdk.NewInt(1)), ). When( PartialCloseFails(alice, pairBtcNusd, sdk.NewDec(5_000), types.ErrBaseReserveAtZero), @@ -264,7 +264,7 @@ func TestMarketOrder(t *testing.T) { FundAccount(alice, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(10_000)))), MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(9_000), sdk.NewDec(10), sdk.ZeroDec()), FundModule(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(types.TestingCollateralDenomNUSD, sdk.NewInt(100_000_000)))), - EditSwapInvariant(pairBtcNusd, sdk.NewInt(1)), + ShiftSwapInvariant(pairBtcNusd, sdk.NewInt(1)), ). When( ClosePosition(alice, pairBtcNusd), @@ -1999,7 +1999,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), + ShiftSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), AMMShouldBeEqual( pairBtcNusd, AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), @@ -2019,7 +2019,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), + ShiftSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), AMMShouldBeEqual( pairBtcNusd, AMM_SwapInvariantShouldBeEqual(sdk.MustNewDecFromStr("99999999999999999999999999.999999000000000000"))), @@ -2040,7 +2040,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant( + ShiftSwapInvariant( pairBtcNusd, startingSwapInvariant.QuoRaw(10)), AMMShouldBeEqual( @@ -2062,7 +2062,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ). When( MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), - EditSwapInvariant(pairBtcNusd, + ShiftSwapInvariant(pairBtcNusd, startingSwapInvariant.QuoRaw(10)), AMMShouldBeEqual( pairBtcNusd, @@ -2086,7 +2086,7 @@ func TestUpdateSwapInvariant(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - EditSwapInvariant(pairBtcNusd, + ShiftSwapInvariant(pairBtcNusd, startingSwapInvariant.MulRaw(100)), AMMShouldBeEqual( pairBtcNusd, @@ -2118,7 +2118,7 @@ func TestUpdateSwapInvariant(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), MarketOrder(bob, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.NewDec(10_000_000_000_000)), - EditSwapInvariant(pairBtcNusd, + ShiftSwapInvariant(pairBtcNusd, startingSwapInvariant.QuoRaw(10)), AMMShouldBeEqual( pairBtcNusd, From fe1fe03cb9cf99369486d7455c06a3d43c5eef25 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 19:05:55 -0600 Subject: [PATCH 11/21] Address Gimeno PR comments + more tests --- proto/nibiru/perp/v2/event.proto | 2 + proto/nibiru/perp/v2/tx.proto | 10 +-- x/common/testutil/testapp/testapp.go | 17 +++++ x/perp/v2/integration/action/dnr.go | 29 +++++++-- x/perp/v2/integration/action/market.go | 9 ++- x/perp/v2/integration/action/position.go | 20 +++++- x/perp/v2/keeper/admin.go | 61 +++++++++++++++++- x/perp/v2/keeper/amm.go | 79 ------------------------ x/perp/v2/keeper/amm_test.go | 37 ++++++----- x/perp/v2/keeper/msg_server.go | 20 +++--- x/perp/v2/keeper/msg_server_test.go | 6 +- x/perp/v2/types/event.pb.go | 2 + x/perp/v2/types/tx.pb.go | 20 +++--- 13 files changed, 178 insertions(+), 134 deletions(-) diff --git a/proto/nibiru/perp/v2/event.proto b/proto/nibiru/perp/v2/event.proto index 4cd8f6cd5..c3854dd85 100644 --- a/proto/nibiru/perp/v2/event.proto +++ b/proto/nibiru/perp/v2/event.proto @@ -235,6 +235,7 @@ message MarketUpdatedEvent { nibiru.perp.v2.Market final_market = 1 [ (gogoproto.nullable) = false ]; } +// EventShiftPegMultiplier: ABCI event emitted from MsgShiftPegMultiplier message EventShiftPegMultiplier { string old_peg_multiplier = 1 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", @@ -247,6 +248,7 @@ message EventShiftPegMultiplier { cosmos.base.v1beta1.Coin cost_paid = 3 [ (gogoproto.nullable) = false ]; } +// EventShiftSwapInvariant: ABCI event emitted from MsgShiftSwapInvariant message EventShiftSwapInvariant { string old_swap_invariant = 1 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", diff --git a/proto/nibiru/perp/v2/tx.proto b/proto/nibiru/perp/v2/tx.proto index f30737952..7dba213c7 100644 --- a/proto/nibiru/perp/v2/tx.proto +++ b/proto/nibiru/perp/v2/tx.proto @@ -29,6 +29,8 @@ service Msg { rpc DonateToEcosystemFund(MsgDonateToEcosystemFund) returns (MsgDonateToEcosystemFundResponse) {} + // ChangeCollateralDenom: Updates the collateral denom. A denom is valid if it + // is possible to make an sdk.Coin using it. [Admin] Only callable by sudoers. rpc ChangeCollateralDenom(MsgChangeCollateralDenom) returns (MsgChangeCollateralDenomResponse) {} @@ -38,13 +40,13 @@ service Msg { rpc WithdrawEpochRebates(MsgWithdrawEpochRebates) returns (MsgWithdrawEpochRebatesResponse) {} - // ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. - // Admin-only. + // ShiftPegMultiplier: gRPC tx msg for changing a market's peg multiplier. + // [Admin] Only callable by sudoers. rpc ShiftPegMultiplier(MsgShiftPegMultiplier) returns (MsgShiftPegMultiplierResponse) {} - // ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. - // Admin-only. + // ShiftSwapInvariant: gRPC tx msg for changing a market's swap invariant. + // [Admin] Only callable by sudoers. rpc ShiftSwapInvariant(MsgShiftSwapInvariant) returns (MsgShiftSwapInvariantResponse) {} } diff --git a/x/common/testutil/testapp/testapp.go b/x/common/testutil/testapp/testapp.go index 4682a29a6..96f7fd4d8 100644 --- a/x/common/testutil/testapp/testapp.go +++ b/x/common/testutil/testapp/testapp.go @@ -18,6 +18,7 @@ import ( epochstypes "github.com/NibiruChain/nibiru/x/epochs/types" inflationtypes "github.com/NibiruChain/nibiru/x/inflation/types" "github.com/NibiruChain/nibiru/x/perp/v2/types" + sudotypes "github.com/NibiruChain/nibiru/x/sudo/types" ) // NewNibiruTestAppAndContext creates an 'app.NibiruApp' instance with an @@ -36,10 +37,12 @@ func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) { app.OracleKeeper.SetPrice(ctx, "xxx:yyy", sdk.NewDec(20000)) app.PerpKeeperV2.Collateral.Set(ctx, types.TestingCollateralDenomNUSD) + app.SudoKeeper.Sudoers.Set(ctx, DefaultSudoers()) return app, ctx } +// NewContext: Returns a fresh sdk.Context corresponding to the given NibiruApp. func NewContext(nibiru *app.NibiruApp) sdk.Context { return nibiru.NewContext(false, tmproto.Header{ Height: 1, @@ -47,6 +50,20 @@ func NewContext(nibiru *app.NibiruApp) sdk.Context { }) } +// DefaultSudoers: State for the x/sudo module for the default test app. +func DefaultSudoers() sudotypes.Sudoers { + EnsureNibiruPrefix() + addr := DefaultSudoRoot().String() + return sudotypes.Sudoers{ + Root: addr, + Contracts: []string{addr}, + } +} + +func DefaultSudoRoot() sdk.AccAddress { + return sdk.MustAccAddressFromBech32("nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl") +} + // NewNibiruTestAppAndZeroTimeCtx: Runs NewNibiruTestAppAndZeroTimeCtx with the // block time set to time zero. func NewNibiruTestAppAndContextAtTime(startTime time.Time) (*app.NibiruApp, sdk.Context) { diff --git a/x/perp/v2/integration/action/dnr.go b/x/perp/v2/integration/action/dnr.go index 9c5b69a54..96f17b929 100644 --- a/x/perp/v2/integration/action/dnr.go +++ b/x/perp/v2/integration/action/dnr.go @@ -12,6 +12,7 @@ import ( "github.com/NibiruChain/nibiru/app" "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/testutil/action" + perpkeeper "github.com/NibiruChain/nibiru/x/perp/v2/keeper" "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -216,16 +217,20 @@ func (s *setCustomDiscountAction) Do(app *app.NibiruApp, ctx sdk.Context) (outCt } type fundDnREpoch struct { - amt sdk.Coins + funds sdk.Coins } func (f fundDnREpoch) Do(app *app.NibiruApp, ctx sdk.Context) (outCtx sdk.Context, err error, isMandatory bool) { tmpAcc := testutil.AccAddress() - ctx, err, _ = action.FundAccount(tmpAcc, f.amt).Do(app, ctx) + ctx, err, _ = action.FundAccount(tmpAcc, f.funds).Do(app, ctx) if err != nil { return ctx, err, true } - _, err = app.PerpKeeperV2.AllocateEpochRebates(ctx, tmpAcc, f.amt) + _, err = perpkeeper.NewMsgServerImpl(app.PerpKeeperV2).AllocateEpochRebates( + ctx, &types.MsgAllocateEpochRebates{ + Sender: tmpAcc.String(), + Rebates: f.funds, + }) if err != nil { return ctx, err, true } @@ -261,10 +266,15 @@ type dnrRebateIsAction struct { } func (d dnrRebateIsAction) Do(app *app.NibiruApp, ctx sdk.Context) (outCtx sdk.Context, err error, isMandatory bool) { - withdrawn, err := app.PerpKeeperV2.WithdrawEpochRebates(ctx, d.epoch, d.user) + resp, err := perpkeeper.NewMsgServerImpl(app.PerpKeeperV2).WithdrawEpochRebates( + ctx, &types.MsgWithdrawEpochRebates{ + Sender: d.user.String(), + Epochs: []uint64{d.epoch}, + }) if err != nil { return ctx, err, true } + withdrawn := resp.WithdrawnRebates if !withdrawn.IsEqual(d.expectedRewards) { return ctx, fmt.Errorf("expected %s, got %s", d.expectedRewards, withdrawn), true } @@ -291,9 +301,16 @@ type dnrRebateFailsAction struct { epoch uint64 } -func (d dnrRebateFailsAction) Do(app *app.NibiruApp, ctx sdk.Context) (outCtx sdk.Context, err error, isMandatory bool) { - withdrawn, err := app.PerpKeeperV2.WithdrawEpochRebates(ctx, d.epoch, d.user) +func (d dnrRebateFailsAction) Do( + app *app.NibiruApp, ctx sdk.Context, +) (outCtx sdk.Context, err error, isMandatory bool) { + resp, err := perpkeeper.NewMsgServerImpl(app.PerpKeeperV2).WithdrawEpochRebates( + ctx, &types.MsgWithdrawEpochRebates{ + Sender: d.user.String(), + Epochs: []uint64{d.epoch}, + }) if err == nil { + withdrawn := resp.WithdrawnRebates return ctx, fmt.Errorf("expected withdrawal error but got instead: %s rewards", withdrawn.String()), true } return ctx, nil, true diff --git a/x/perp/v2/integration/action/market.go b/x/perp/v2/integration/action/market.go index 06e338260..020bbfd6a 100644 --- a/x/perp/v2/integration/action/market.go +++ b/x/perp/v2/integration/action/market.go @@ -9,6 +9,7 @@ import ( "github.com/NibiruChain/nibiru/x/common" "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/testutil/action" + "github.com/NibiruChain/nibiru/x/common/testutil/testapp" "github.com/NibiruChain/nibiru/app" "github.com/NibiruChain/nibiru/x/perp/v2/keeper" @@ -140,7 +141,9 @@ type shiftPegMultiplier struct { } func (e shiftPegMultiplier) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - err := app.PerpKeeperV2.UnsafeShiftPegMultiplier(ctx, e.pair, e.newValue) + err := app.PerpKeeperV2.Admin.ShiftPegMultiplier( + ctx, e.pair, e.newValue, testapp.DefaultSudoRoot(), + ) return ctx, err, true } @@ -157,7 +160,9 @@ type shiftSwapInvariant struct { } func (e shiftSwapInvariant) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - err := app.PerpKeeperV2.UnsafeShiftSwapInvariant(ctx, e.pair, e.newValue) + err := app.PerpKeeperV2.Admin.ShiftSwapInvariant( + ctx, e.pair, e.newValue, testapp.DefaultSudoRoot(), + ) return ctx, err, true } diff --git a/x/perp/v2/integration/action/position.go b/x/perp/v2/integration/action/position.go index a50405404..88c4b515a 100644 --- a/x/perp/v2/integration/action/position.go +++ b/x/perp/v2/integration/action/position.go @@ -13,6 +13,7 @@ import ( "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" "github.com/NibiruChain/nibiru/x/common/testutil/action" + perpkeeper "github.com/NibiruChain/nibiru/x/perp/v2/keeper" "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -343,7 +344,14 @@ type partialClose struct { } func (p partialClose) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - _, err := app.PerpKeeperV2.PartialClose(ctx, p.pair, p.trader, p.amount) + txMsg := &types.MsgPartialClose{ + Sender: p.trader.String(), + Pair: p.pair, + Size_: p.amount, + } + goCtx := sdk.WrapSDKContext(ctx) + _, err := perpkeeper.NewMsgServerImpl(app.PerpKeeperV2).PartialClose( + goCtx, txMsg) if err != nil { return ctx, err, true } @@ -368,7 +376,15 @@ type partialCloseFails struct { } func (p partialCloseFails) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - _, err := app.PerpKeeperV2.PartialClose(ctx, p.pair, p.trader, p.amount) + txMsg := &types.MsgPartialClose{ + Sender: p.trader.String(), + Pair: p.pair, + Size_: p.amount, + } + goCtx := sdk.WrapSDKContext(ctx) + _, err := perpkeeper.NewMsgServerImpl(app.PerpKeeperV2).PartialClose( + goCtx, txMsg, + ) if !errors.Is(err, p.expectedErr) { return ctx, fmt.Errorf("expected error %s, got %s", p.expectedErr, err), false diff --git a/x/perp/v2/keeper/admin.go b/x/perp/v2/keeper/admin.go index af6ed5b03..78270101a 100644 --- a/x/perp/v2/keeper/admin.go +++ b/x/perp/v2/keeper/admin.go @@ -187,7 +187,38 @@ func (k admin) ShiftPegMultiplier( if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { return err } - return k.UnsafeShiftPegMultiplier(ctx, pair, newPriceMultiplier) + + amm, err := k.GetAMM(ctx, pair) + if err != nil { + return err + } + oldPriceMult := amm.PriceMultiplier + + if newPriceMultiplier.Equal(oldPriceMult) { + // same price multiplier, no-op + return nil + } + + // Compute cost of re-pegging the pool + cost, err := amm.CalcRepegCost(newPriceMultiplier) + if err != nil { + return err + } + + costPaid, err := k.handleMarketUpdateCost(ctx, pair, cost) + if err != nil { + return err + } + + // Do the re-peg + amm.PriceMultiplier = newPriceMultiplier + k.SaveAMM(ctx, amm) + + return ctx.EventManager().EmitTypedEvent(&types.EventShiftPegMultiplier{ + OldPegMultiplier: oldPriceMult, + NewPegMultiplier: newPriceMultiplier, + CostPaid: costPaid, + }) } // ShiftSwapInvariant: Edit the swap invariant (liquidity depth) of an amm pool, @@ -202,5 +233,31 @@ func (k admin) ShiftSwapInvariant( if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { return err } - return k.UnsafeShiftSwapInvariant(ctx, pair, newSwapInvariant) + amm, err := k.GetAMM(ctx, pair) + if err != nil { + return err + } + + cost, err := amm.CalcUpdateSwapInvariantCost(newSwapInvariant.ToLegacyDec()) + if err != nil { + return err + } + + costPaid, err := k.handleMarketUpdateCost(ctx, pair, cost) + if err != nil { + return err + } + + err = amm.UpdateSwapInvariant(newSwapInvariant.ToLegacyDec()) + if err != nil { + return err + } + + k.SaveAMM(ctx, amm) + + return ctx.EventManager().EmitTypedEvent(&types.EventShiftSwapInvariant{ + OldSwapInvariant: amm.BaseReserve.Mul(amm.QuoteReserve).RoundInt(), + NewSwapInvariant: newSwapInvariant, + CostPaid: costPaid, + }) } diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index 68ba85d18..7fc741f6b 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -11,85 +11,6 @@ import ( types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) -// UnsafeShiftPegMultiplier: [Without checking x/sudo permissions] Edits the peg -// multiplier of an amm pool after making sure there's enough money in the perp -// EF fund to pay for the repeg. These funds get send to the vault to pay for -// trader's new net margin. -func (k Keeper) UnsafeShiftPegMultiplier( - ctx sdk.Context, - pair asset.Pair, - newPriceMultiplier sdk.Dec, -) (err error) { - amm, err := k.GetAMM(ctx, pair) - if err != nil { - return err - } - oldPriceMult := amm.PriceMultiplier - - if newPriceMultiplier.Equal(oldPriceMult) { - // same price multiplier, no-op - return nil - } - - // Compute cost of re-pegging the pool - cost, err := amm.CalcRepegCost(newPriceMultiplier) - if err != nil { - return err - } - - costPaid, err := k.handleMarketUpdateCost(ctx, pair, cost) - if err != nil { - return err - } - - // Do the re-peg - amm.PriceMultiplier = newPriceMultiplier - k.SaveAMM(ctx, amm) - - return ctx.EventManager().EmitTypedEvent(&types.EventShiftPegMultiplier{ - OldPegMultiplier: oldPriceMult, - NewPegMultiplier: newPriceMultiplier, - CostPaid: costPaid, - }) -} - -// UnsafeShiftSwapInvariant: [Without checking x/sudo permissions] Edit the swap -// invariant of an amm pool after making sure there's enough money in the perp -// fund to pay for the operation. These funds get send to the vault to pay for -// trader's new net margin. -func (k Keeper) UnsafeShiftSwapInvariant( - ctx sdk.Context, pair asset.Pair, newSwapInvariant sdkmath.Int, -) (err error) { - // Get the pool - amm, err := k.GetAMM(ctx, pair) - if err != nil { - return err - } - - cost, err := amm.CalcUpdateSwapInvariantCost(newSwapInvariant.ToLegacyDec()) - if err != nil { - return err - } - - costPaid, err := k.handleMarketUpdateCost(ctx, pair, cost) - if err != nil { - return err - } - - err = amm.UpdateSwapInvariant(newSwapInvariant.ToLegacyDec()) - if err != nil { - return err - } - - k.SaveAMM(ctx, amm) - - return ctx.EventManager().EmitTypedEvent(&types.EventShiftSwapInvariant{ - OldSwapInvariant: amm.BaseReserve.Mul(amm.QuoteReserve).RoundInt(), - NewSwapInvariant: newSwapInvariant, - CostPaid: costPaid, - }) -} - func (k Keeper) handleMarketUpdateCost( ctx sdk.Context, pair asset.Pair, costAmt sdkmath.Int, ) (costPaid sdk.Coin, err error) { diff --git a/x/perp/v2/keeper/amm_test.go b/x/perp/v2/keeper/amm_test.go index 072aa0d21..e593a5a86 100644 --- a/x/perp/v2/keeper/amm_test.go +++ b/x/perp/v2/keeper/amm_test.go @@ -21,7 +21,7 @@ import ( types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) -func TestUnsafeShiftPegMultiplier(t *testing.T) { +func TestShiftPegMultiplier(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) tests := TestCases{ @@ -152,9 +152,9 @@ func TestUnsafeShiftPegMultiplier(t *testing.T) { NewTestSuite(t).WithTestCases(tests...).Run() } -// TestUnsafeShiftPegMultiplier_Fail: Test scenarios for the -// `UnsafeShiftPegMultiplier` function that should error -func TestUnsafeShiftPegMultiplier_Fail(t *testing.T) { +// TestShiftPegMultiplier_Fail: Test scenarios for the `ShiftPegMultiplier` +// function that should error. +func TestShiftPegMultiplier_Fail(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) app, ctx := testapp.NewNibiruTestAppAndContext() @@ -186,12 +186,14 @@ func TestUnsafeShiftPegMultiplier_Fail(t *testing.T) { }) require.NoError(t, err) + adminAddr := testapp.DefaultSudoRoot() // Error because of invalid pair - err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.ShiftPegMultiplier( + ctx, asset.MustNewPair("luna:usdt"), sdk.NewDec(-1), adminAddr) require.ErrorContains(t, err, "market luna:usdt not found") // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(-1)) + err = app.PerpKeeperV2.Admin.ShiftPegMultiplier(ctx, pair, sdk.NewDec(-1), adminAddr) require.ErrorIs(t, err, types.ErrNonPositivePegMultiplier) // Add market activity @@ -213,17 +215,17 @@ func TestUnsafeShiftPegMultiplier_Fail(t *testing.T) { require.NoError(t, err) // Error because no money in perp ef fund - err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(3)) + err = app.PerpKeeperV2.Admin.ShiftPegMultiplier(ctx, pair, sdk.NewDec(3), adminAddr) require.ErrorContains(t, err, types.ErrNotEnoughFundToPayAction.Error()) // Works because it goes in the other way - err = app.PerpKeeperV2.Admin.UnsafeShiftPegMultiplier(ctx, pair, sdk.NewDec(1)) + err = app.PerpKeeperV2.Admin.ShiftPegMultiplier(ctx, pair, sdk.NewDec(1), adminAddr) require.NoError(t, err) } -// TestUnsafeShiftSwapInvariant_Fail: Test scenarios for the -// `UnsafeShiftSwapInvariant` function that should error -func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { +// TestShiftSwapInvariant_Fail: Test scenarios for the `ShiftSwapInvariant` +// function that should error +func TestShiftSwapInvariant_Fail(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) app, ctx := testapp.NewNibiruTestAppAndContext() account := testutil.AccAddress() @@ -254,12 +256,13 @@ func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { }) require.NoError(t, err) + adminAddr := testapp.DefaultSudoRoot() // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, asset.MustNewPair("luna:usdt"), sdk.NewInt(-1)) + err = app.PerpKeeperV2.Admin.ShiftSwapInvariant(ctx, asset.MustNewPair("luna:usdt"), sdk.NewInt(-1), adminAddr) require.ErrorContains(t, err, "market luna:usdt not found") // Error because of invalid price multiplier - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(-1)) + err = app.PerpKeeperV2.Admin.ShiftSwapInvariant(ctx, pair, sdk.NewInt(-1), adminAddr) require.ErrorIs(t, err, types.ErrNonPositiveSwapInvariant) // Add market activity @@ -281,19 +284,19 @@ func TestUnsafeShiftSwapInvariant_Fail(t *testing.T) { require.NoError(t, err) // Error because no money in perp ef fund - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(2_000_000)) + err = app.PerpKeeperV2.Admin.ShiftSwapInvariant(ctx, pair, sdk.NewInt(2_000_000), adminAddr) require.ErrorContains(t, err, types.ErrNotEnoughFundToPayAction.Error()) // Fail at validate - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(0)) + err = app.PerpKeeperV2.Admin.ShiftSwapInvariant(ctx, pair, sdk.NewInt(0), adminAddr) require.ErrorContains(t, err, types.ErrNonPositiveSwapInvariant.Error()) // Works because it goes in the other way - err = app.PerpKeeperV2.Admin.UnsafeShiftSwapInvariant(ctx, pair, sdk.NewInt(500_000)) + err = app.PerpKeeperV2.Admin.ShiftSwapInvariant(ctx, pair, sdk.NewInt(500_000), adminAddr) require.NoError(t, err) } -func TestUnsafeShiftSwapInvariant(t *testing.T) { +func TestShiftSwapInvariant(t *testing.T) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) tests := TestCases{ diff --git a/x/perp/v2/keeper/msg_server.go b/x/perp/v2/keeper/msg_server.go index 9add4d5ca..b950333d6 100644 --- a/x/perp/v2/keeper/msg_server.go +++ b/x/perp/v2/keeper/msg_server.go @@ -157,11 +157,11 @@ func (m msgServer) ChangeCollateralDenom( return &types.MsgChangeCollateralDenomResponse{}, err } -func (m msgServer) AllocateEpochRebates(ctx context.Context, msg *types.MsgAllocateEpochRebates) (*types.MsgAllocateEpochRebatesResponse, error) { - sender, err := sdk.AccAddressFromBech32(msg.Sender) - if err != nil { - return nil, err - } +func (m msgServer) AllocateEpochRebates( + ctx context.Context, msg *types.MsgAllocateEpochRebates, +) (*types.MsgAllocateEpochRebatesResponse, error) { + // Sender is checked in `msg.ValidateBasic` before reaching this fn call. + sender, _ := sdk.AccAddressFromBech32(msg.Sender) total, err := m.k.AllocateEpochRebates(sdk.UnwrapSDKContext(ctx), sender, msg.Rebates) if err != nil { return nil, err @@ -171,10 +171,8 @@ func (m msgServer) AllocateEpochRebates(ctx context.Context, msg *types.MsgAlloc } func (m msgServer) WithdrawEpochRebates(ctx context.Context, msg *types.MsgWithdrawEpochRebates) (*types.MsgWithdrawEpochRebatesResponse, error) { - sender, err := sdk.AccAddressFromBech32(msg.Sender) - if err != nil { - return nil, err - } + // Sender is checked in `msg.ValidateBasic` before reaching this fn call. + sender, _ := sdk.AccAddressFromBech32(msg.Sender) sdkCtx := sdk.UnwrapSDKContext(ctx) totalWithdrawn := sdk.NewCoins() for _, epoch := range msg.Epochs { @@ -194,7 +192,7 @@ func (m msgServer) WithdrawEpochRebates(ctx context.Context, msg *types.MsgWithd func (m msgServer) ShiftPegMultiplier( goCtx context.Context, msg *types.MsgShiftPegMultiplier, ) (*types.MsgShiftPegMultiplierResponse, error) { - // The `msg` here is checked with ValidateBasic before this fn is called. + // Sender is checked in `msg.ValidateBasic` before reaching this fn call. sender, _ := sdk.AccAddressFromBech32(msg.Sender) ctx := sdk.UnwrapSDKContext(goCtx) err := m.k.Admin.ShiftPegMultiplier(ctx, msg.Pair, msg.NewPegMult, sender) @@ -206,7 +204,7 @@ func (m msgServer) ShiftPegMultiplier( func (m msgServer) ShiftSwapInvariant( goCtx context.Context, msg *types.MsgShiftSwapInvariant, ) (*types.MsgShiftSwapInvariantResponse, error) { - // The `msg` here is checked with ValidateBasic before this fn is called. + // Sender is checked in `msg.ValidateBasic` before reaching this fn call. sender, _ := sdk.AccAddressFromBech32(msg.Sender) ctx := sdk.UnwrapSDKContext(goCtx) err := m.k.Admin.ShiftSwapInvariant(ctx, msg.Pair, msg.NewSwapInvariant, sender) diff --git a/x/perp/v2/keeper/msg_server_test.go b/x/perp/v2/keeper/msg_server_test.go index 96813846b..69d886d9f 100644 --- a/x/perp/v2/keeper/msg_server_test.go +++ b/x/perp/v2/keeper/msg_server_test.go @@ -15,7 +15,7 @@ import ( "github.com/NibiruChain/nibiru/x/common/testutil/testapp" . "github.com/NibiruChain/nibiru/x/perp/v2/integration/action" . "github.com/NibiruChain/nibiru/x/perp/v2/integration/assertion" - "github.com/NibiruChain/nibiru/x/perp/v2/keeper" + perpkeeper "github.com/NibiruChain/nibiru/x/perp/v2/keeper" "github.com/NibiruChain/nibiru/x/perp/v2/types" sudoerTypes "github.com/NibiruChain/nibiru/x/sudo/types" ) @@ -298,7 +298,7 @@ func TestFailMsgServer(t *testing.T) { sender := testutil.AccAddress().String() - msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2) + msgServer := perpkeeper.NewMsgServerImpl(app.PerpKeeperV2) _, err := msgServer.MarketOrder(ctx, &types.MsgMarketOrder{ Sender: sender, @@ -346,7 +346,7 @@ func TestMsgChangeCollateralDenom(t *testing.T) { sender := testutil.AccAddress().String() - msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2) + msgServer := perpkeeper.NewMsgServerImpl(app.PerpKeeperV2) _, err := msgServer.ChangeCollateralDenom(ctx, &types.MsgChangeCollateralDenom{ Sender: sender, diff --git a/x/perp/v2/types/event.pb.go b/x/perp/v2/types/event.pb.go index 3a2e1756c..4db1fe795 100644 --- a/x/perp/v2/types/event.pb.go +++ b/x/perp/v2/types/event.pb.go @@ -523,6 +523,7 @@ func (m *MarketUpdatedEvent) GetFinalMarket() Market { return Market{} } +// EventShiftPegMultiplier: ABCI event emitted from MsgShiftPegMultiplier type EventShiftPegMultiplier struct { OldPegMultiplier github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=old_peg_multiplier,json=oldPegMultiplier,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"old_peg_multiplier"` NewPegMultiplier github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=new_peg_multiplier,json=newPegMultiplier,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"new_peg_multiplier"` @@ -569,6 +570,7 @@ func (m *EventShiftPegMultiplier) GetCostPaid() types.Coin { return types.Coin{} } +// EventShiftSwapInvariant: ABCI event emitted from MsgShiftSwapInvariant type EventShiftSwapInvariant struct { OldSwapInvariant github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,1,opt,name=old_swap_invariant,json=oldSwapInvariant,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"old_swap_invariant"` NewSwapInvariant github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,2,opt,name=new_swap_invariant,json=newSwapInvariant,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"new_swap_invariant"` diff --git a/x/perp/v2/types/tx.pb.go b/x/perp/v2/types/tx.pb.go index 916f3a080..a20200646 100644 --- a/x/perp/v2/types/tx.pb.go +++ b/x/perp/v2/types/tx.pb.go @@ -1522,14 +1522,16 @@ type MsgClient interface { PartialClose(ctx context.Context, in *MsgPartialClose, opts ...grpc.CallOption) (*MsgPartialCloseResponse, error) SettlePosition(ctx context.Context, in *MsgSettlePosition, opts ...grpc.CallOption) (*MsgClosePositionResponse, error) DonateToEcosystemFund(ctx context.Context, in *MsgDonateToEcosystemFund, opts ...grpc.CallOption) (*MsgDonateToEcosystemFundResponse, error) + // ChangeCollateralDenom: Updates the collateral denom. A denom is valid if it + // is possible to make an sdk.Coin using it. [Admin] Only callable by sudoers. ChangeCollateralDenom(ctx context.Context, in *MsgChangeCollateralDenom, opts ...grpc.CallOption) (*MsgChangeCollateralDenomResponse, error) AllocateEpochRebates(ctx context.Context, in *MsgAllocateEpochRebates, opts ...grpc.CallOption) (*MsgAllocateEpochRebatesResponse, error) WithdrawEpochRebates(ctx context.Context, in *MsgWithdrawEpochRebates, opts ...grpc.CallOption) (*MsgWithdrawEpochRebatesResponse, error) - // ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. - // Admin-only. + // ShiftPegMultiplier: gRPC tx msg for changing a market's peg multiplier. + // [Admin] Only callable by sudoers. ShiftPegMultiplier(ctx context.Context, in *MsgShiftPegMultiplier, opts ...grpc.CallOption) (*MsgShiftPegMultiplierResponse, error) - // ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. - // Admin-only. + // ShiftSwapInvariant: gRPC tx msg for changing a market's swap invariant. + // [Admin] Only callable by sudoers. ShiftSwapInvariant(ctx context.Context, in *MsgShiftSwapInvariant, opts ...grpc.CallOption) (*MsgShiftSwapInvariantResponse, error) } @@ -1668,14 +1670,16 @@ type MsgServer interface { PartialClose(context.Context, *MsgPartialClose) (*MsgPartialCloseResponse, error) SettlePosition(context.Context, *MsgSettlePosition) (*MsgClosePositionResponse, error) DonateToEcosystemFund(context.Context, *MsgDonateToEcosystemFund) (*MsgDonateToEcosystemFundResponse, error) + // ChangeCollateralDenom: Updates the collateral denom. A denom is valid if it + // is possible to make an sdk.Coin using it. [Admin] Only callable by sudoers. ChangeCollateralDenom(context.Context, *MsgChangeCollateralDenom) (*MsgChangeCollateralDenomResponse, error) AllocateEpochRebates(context.Context, *MsgAllocateEpochRebates) (*MsgAllocateEpochRebatesResponse, error) WithdrawEpochRebates(context.Context, *MsgWithdrawEpochRebates) (*MsgWithdrawEpochRebatesResponse, error) - // ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier. - // Admin-only. + // ShiftPegMultiplier: gRPC tx msg for changing a market's peg multiplier. + // [Admin] Only callable by sudoers. ShiftPegMultiplier(context.Context, *MsgShiftPegMultiplier) (*MsgShiftPegMultiplierResponse, error) - // ShiftSwapInvariant: gRPC tx msg for changing the swap invariant. - // Admin-only. + // ShiftSwapInvariant: gRPC tx msg for changing a market's swap invariant. + // [Admin] Only callable by sudoers. ShiftSwapInvariant(context.Context, *MsgShiftSwapInvariant) (*MsgShiftSwapInvariantResponse, error) } From c3ef701d3334455866a5567284c63c6917755131 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 19:24:03 -0600 Subject: [PATCH 12/21] fix strange address name conflicts --- x/common/constants.go | 4 ++-- x/common/testutil/const.go | 6 ++++++ x/common/testutil/testapp/testapp.go | 4 ++-- x/perp/v2/integration/action/market.go | 4 ++-- x/perp/v2/module/genesis_test.go | 2 +- x/spot/genesis_test.go | 1 + x/spot/keeper/balances_test.go | 1 + 7 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 x/common/testutil/const.go diff --git a/x/common/constants.go b/x/common/constants.go index ae5fe5522..031803825 100644 --- a/x/common/constants.go +++ b/x/common/constants.go @@ -7,9 +7,9 @@ const ( // TO_MICRO: multiplier for converting between units and micro-units. TO_MICRO = int64(1_000_000) - NibiruTeam = "nibi1l8dxzwz9d4peazcqjclnkj2mhvtj7mpnkqx85mg0ndrlhwrnh7gskkzg0v" + NIBIRU_TEAM = "nibi1l8dxzwz9d4peazcqjclnkj2mhvtj7mpnkqx85mg0ndrlhwrnh7gskkzg0v" ) func NibiruTeamAddr() sdk.AccAddress { - return sdk.MustAccAddressFromBech32(NibiruTeam) + return sdk.MustAccAddressFromBech32(NIBIRU_TEAM) } diff --git a/x/common/testutil/const.go b/x/common/testutil/const.go new file mode 100644 index 000000000..7366624ce --- /dev/null +++ b/x/common/testutil/const.go @@ -0,0 +1,6 @@ +package testutil + +const ( + ADDR_GUARD_CREAM = "nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl" + ADDR_SUDO_ROOT = "nibi1qqx5reauy4glpskmppy88pz25qp2py5yxvpxdt" +) diff --git a/x/common/testutil/testapp/testapp.go b/x/common/testutil/testapp/testapp.go index 96f7fd4d8..2db6fd867 100644 --- a/x/common/testutil/testapp/testapp.go +++ b/x/common/testutil/testapp/testapp.go @@ -15,6 +15,7 @@ import ( "github.com/NibiruChain/nibiru/app" "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" + "github.com/NibiruChain/nibiru/x/common/testutil" epochstypes "github.com/NibiruChain/nibiru/x/epochs/types" inflationtypes "github.com/NibiruChain/nibiru/x/inflation/types" "github.com/NibiruChain/nibiru/x/perp/v2/types" @@ -52,7 +53,6 @@ func NewContext(nibiru *app.NibiruApp) sdk.Context { // DefaultSudoers: State for the x/sudo module for the default test app. func DefaultSudoers() sudotypes.Sudoers { - EnsureNibiruPrefix() addr := DefaultSudoRoot().String() return sudotypes.Sudoers{ Root: addr, @@ -61,7 +61,7 @@ func DefaultSudoers() sudotypes.Sudoers { } func DefaultSudoRoot() sdk.AccAddress { - return sdk.MustAccAddressFromBech32("nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl") + return sdk.MustAccAddressFromBech32(testutil.ADDR_SUDO_ROOT) } // NewNibiruTestAppAndZeroTimeCtx: Runs NewNibiruTestAppAndZeroTimeCtx with the diff --git a/x/perp/v2/integration/action/market.go b/x/perp/v2/integration/action/market.go index 020bbfd6a..150286f74 100644 --- a/x/perp/v2/integration/action/market.go +++ b/x/perp/v2/integration/action/market.go @@ -207,7 +207,7 @@ func (c setCollateral) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, err if err != nil { return ctx, err, true } - sudoers.Root = common.NibiruTeam + sudoers.Root = common.NIBIRU_TEAM app.SudoKeeper.Sudoers.Set(ctx, sudoers) senderAddr, err := sdk.AccAddressFromBech32(c.Sender) @@ -221,6 +221,6 @@ func (c setCollateral) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, err func SetCollateral(denom string) action.Action { return setCollateral{ Denom: denom, - Sender: common.NibiruTeam, + Sender: common.NIBIRU_TEAM, } } diff --git a/x/perp/v2/module/genesis_test.go b/x/perp/v2/module/genesis_test.go index 48097d770..eadbdf381 100644 --- a/x/perp/v2/module/genesis_test.go +++ b/x/perp/v2/module/genesis_test.go @@ -84,7 +84,7 @@ func RunTestGenesis(t *testing.T, tc TestCase) { pair := asset.Registry.Pair(denoms.BTC, denoms.NUSD) // Initialize sudo state - nibiruTeam := common.NibiruTeam + nibiruTeam := common.NIBIRU_TEAM sudoers := PerpSudoers() sudoersRoot := sdk.MustAccAddressFromBech32(nibiruTeam) sudoers.Root = sudoersRoot.String() diff --git a/x/spot/genesis_test.go b/x/spot/genesis_test.go index 1cd5207e8..837d4de0a 100644 --- a/x/spot/genesis_test.go +++ b/x/spot/genesis_test.go @@ -13,6 +13,7 @@ import ( ) func TestGenesis(t *testing.T) { + testapp.EnsureNibiruPrefix() genesisState := types.GenesisState{ Params: types.DefaultParams(), Pools: []types.Pool{ diff --git a/x/spot/keeper/balances_test.go b/x/spot/keeper/balances_test.go index c7df38110..053da1e86 100644 --- a/x/spot/keeper/balances_test.go +++ b/x/spot/keeper/balances_test.go @@ -52,6 +52,7 @@ func TestCheckBalances(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { + testapp.EnsureNibiruPrefix() app, ctx := testapp.NewNibiruTestAppAndContext() // fund user account From a2ee8aa5e7fa5e33a0efa6570c733f11a23709e8 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 01:36:21 -0600 Subject: [PATCH 13/21] test: fix all tests --- app/keepers.go | 22 +++++++++++----------- x/common/testutil/testapp/testapp.go | 6 +++++- x/devgas/v1/ante/ante_test.go | 1 + x/inflation/keeper/inflation_test.go | 12 +++++------- x/oracle/keeper/hooks_test.go | 4 ++++ 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/keepers.go b/app/keepers.go index 84619298c..a203ca48e 100644 --- a/app/keepers.go +++ b/app/keepers.go @@ -238,13 +238,14 @@ func (app *NibiruApp) InitKeepers( tkeys := app.tkeys memKeys := app.memKeys + govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String() app.paramsKeeper = initParamsKeeper( appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey], ) // set the BaseApp's parameter store - app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String()) + app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], govModuleAddr) bApp.SetParamStore(&app.ConsensusParamsKeeper) /* Add capabilityKeeper and ScopeToModule for the ibc module @@ -274,21 +275,21 @@ func (app *NibiruApp) InitKeepers( authtypes.ProtoBaseAccount, maccPerms, sdk.GetConfig().GetBech32AccountAddrPrefix(), - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) app.BankKeeper = bankkeeper.NewBaseKeeper( appCodec, keys[banktypes.StoreKey], app.AccountKeeper, BlockedAddresses(), - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) app.stakingKeeper = stakingkeeper.NewKeeper( appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) app.DistrKeeper = distrkeeper.NewKeeper( appCodec, @@ -297,7 +298,7 @@ func (app *NibiruApp) InitKeepers( app.BankKeeper, app.stakingKeeper, authtypes.FeeCollectorName, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) invCheckPeriod := cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)) @@ -307,7 +308,7 @@ func (app *NibiruApp) InitKeepers( invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, keys[feegrant.StoreKey], app.AccountKeeper) @@ -326,7 +327,7 @@ func (app *NibiruApp) InitKeepers( appCodec, homePath, app.BaseApp, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) // register the staking hooks @@ -336,7 +337,7 @@ func (app *NibiruApp) InitKeepers( legacyAmino, keys[slashingtypes.StoreKey], app.stakingKeeper, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) app.stakingKeeper.SetHooks( @@ -442,12 +443,11 @@ func (app *NibiruApp) InitKeepers( wasmDir, wasmConfig, supportedFeatures, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, GetWasmOpts(*app, appOpts)..., ) // DevGas uses WasmKeeper - govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String() app.DevGasKeeper = devgaskeeper.NewKeeper( keys[devgastypes.StoreKey], appCodec, @@ -553,7 +553,7 @@ func (app *NibiruApp) InitKeepers( app.stakingKeeper, app.MsgServiceRouter(), govConfig, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), + govModuleAddr, ) govKeeper.SetLegacyRouter(govRouter) diff --git a/x/common/testutil/testapp/testapp.go b/x/common/testutil/testapp/testapp.go index 2db6fd867..6826da359 100644 --- a/x/common/testutil/testapp/testapp.go +++ b/x/common/testutil/testapp/testapp.go @@ -25,6 +25,10 @@ import ( // NewNibiruTestAppAndContext creates an 'app.NibiruApp' instance with an // in-memory 'tmdb.MemDB' and fresh 'sdk.Context'. func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) { + // Prevent "invalid Bech32 prefix; expected nibi, got ...." error + EnsureNibiruPrefix() + + // Set up base app encoding := app.MakeEncodingConfig() var appGenesis app.GenesisState = app.NewDefaultGenesisState(encoding.Marshaler) genModEpochs := epochstypes.DefaultGenesisFromTime(time.Now().UTC()) @@ -34,9 +38,9 @@ func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) { app := NewNibiruTestApp(appGenesis) ctx := NewContext(app) + // Set defaults for certain modules. app.OracleKeeper.SetPrice(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), sdk.NewDec(20000)) app.OracleKeeper.SetPrice(ctx, "xxx:yyy", sdk.NewDec(20000)) - app.PerpKeeperV2.Collateral.Set(ctx, types.TestingCollateralDenomNUSD) app.SudoKeeper.Sudoers.Set(ctx, DefaultSudoers()) diff --git a/x/devgas/v1/ante/ante_test.go b/x/devgas/v1/ante/ante_test.go index 9b9b00d43..6e4a0d332 100644 --- a/x/devgas/v1/ante/ante_test.go +++ b/x/devgas/v1/ante/ante_test.go @@ -25,6 +25,7 @@ type AnteTestSuite struct { } func TestAnteSuite(t *testing.T) { + testapp.EnsureNibiruPrefix() suite.Run(t, new(AnteTestSuite)) } diff --git a/x/inflation/keeper/inflation_test.go b/x/inflation/keeper/inflation_test.go index 0394dfc5e..4012fa74d 100644 --- a/x/inflation/keeper/inflation_test.go +++ b/x/inflation/keeper/inflation_test.go @@ -72,13 +72,11 @@ func TestMintAndAllocateInflation(t *testing.T) { t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) { nibiruApp, ctx := testapp.NewNibiruTestAppAndContext() - if tc.rootAccount != "" { - t.Logf("setting root account to %s", tc.rootAccount) - nibiruApp.SudoKeeper.Sudoers.Set(ctx, sudotypes.Sudoers{ - Root: sdk.MustAccAddressFromBech32(tc.rootAccount).String(), - Contracts: []string{}, - }) - } + t.Logf("setting root account to %s", tc.rootAccount) + nibiruApp.SudoKeeper.Sudoers.Set(ctx, sudotypes.Sudoers{ + Root: tc.rootAccount, + Contracts: []string{}, + }) staking, strategic, community, err := nibiruApp.InflationKeeper.MintAndAllocateInflation(ctx, tc.coinsToMint, types.DefaultParams()) require.NoError(t, err) diff --git a/x/oracle/keeper/hooks_test.go b/x/oracle/keeper/hooks_test.go index 61cc84fd4..516c0dc38 100644 --- a/x/oracle/keeper/hooks_test.go +++ b/x/oracle/keeper/hooks_test.go @@ -14,6 +14,10 @@ import ( perptypes "github.com/NibiruChain/nibiru/x/perp/v2/types" ) +func init() { + testapp.EnsureNibiruPrefix() +} + func TestHooks_AfterEpochEnd(t *testing.T) { tests := []struct { name string From ad8c56bbee745ec7b77be2eeeadd97b63db16134 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 01:43:53 -0600 Subject: [PATCH 14/21] test: more calls of EnsureNibiruPrefix --- app/genesis.go | 1 + app/ibc_test.go | 1 + wasmbinding/wasmbin_test.go | 4 ++++ x/perp/v2/integration/action/position.go | 4 ++-- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/genesis.go b/app/genesis.go index ead2fc081..0710a5b9a 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -78,6 +78,7 @@ func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState { gen := ModuleBasics.DefaultGenesis(cdc) authGenesis := new(authtypes.GenesisState) + authtypes.DefaultGenesisState() cdc.MustUnmarshalJSON(gen[authtypes.ModuleName], authGenesis) return gen diff --git a/app/ibc_test.go b/app/ibc_test.go index e5b8147d8..9e7d9aee2 100644 --- a/app/ibc_test.go +++ b/app/ibc_test.go @@ -16,6 +16,7 @@ import ( // init changes the value of 'DefaultTestingAppInit' to use custom initialization. func init() { ibctesting.DefaultTestingAppInit = SetupNibiruTestingApp + testapp.EnsureNibiruPrefix() } /* diff --git a/wasmbinding/wasmbin_test.go b/wasmbinding/wasmbin_test.go index 76729ede7..3b4c50532 100644 --- a/wasmbinding/wasmbin_test.go +++ b/wasmbinding/wasmbin_test.go @@ -18,6 +18,10 @@ import ( "github.com/NibiruChain/nibiru/x/common/testutil/testapp" ) +func init() { + testapp.EnsureNibiruPrefix() +} + // TestSetupContracts acts as an integration test by storing and instantiating // each production smart contract is expected to interact with x/wasm/binding. func TestSetupContracts(t *testing.T) { diff --git a/x/perp/v2/integration/action/position.go b/x/perp/v2/integration/action/position.go index 88c4b515a..f73c6ad60 100644 --- a/x/perp/v2/integration/action/position.go +++ b/x/perp/v2/integration/action/position.go @@ -393,11 +393,11 @@ func (p partialCloseFails) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, return ctx, nil, false } -func PartialCloseFails(trader sdk.AccAddress, pair asset.Pair, amount sdk.Dec, expecedErr error) action.Action { +func PartialCloseFails(trader sdk.AccAddress, pair asset.Pair, amount sdk.Dec, expectedErr error) action.Action { return partialCloseFails{ trader: trader, pair: pair, amount: amount, - expectedErr: expecedErr, + expectedErr: expectedErr, } } From bd5599437a933442e2b6cffc5dba0b9f1a89d979 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 08:13:26 +0000 Subject: [PATCH 15/21] Updated changelog - dependabot --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ff77aac3..84371830e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1682](https://github.com/NibiruChain/nibiru/pull/1682) - feat: add upgrade handler for v1.1.0 +### Dependencies +- Bump `cosmossdk.io/math` from 1.1.2 to 1.2.0 ([#1676](https://github.com/NibiruChain/nibiru/pull/1676)) + ## [v1.0.0](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.0) ### Features From 02600a4f53cfd0b9aca5a2deac6987747d940818 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 02:29:29 -0600 Subject: [PATCH 16/21] chore(wasmbinding): delete CustomQuerier since we have QueryRequest::Stargate now --- wasmbinding/bindings/marshalling_test.go | 58 ---- wasmbinding/bindings/msg.go | 35 --- wasmbinding/bindings/query.go | 230 --------------- wasmbinding/exec_perp.go | 117 -------- wasmbinding/exec_perp_test.go | 204 ++++--------- wasmbinding/exec_test.go | 58 ---- wasmbinding/message_plugin.go | 18 -- wasmbinding/querier.go | 331 --------------------- wasmbinding/querier_perp_test.go | 319 -------------------- wasmbinding/querier_test.go | 353 ----------------------- wasmbinding/wasm.go | 2 - 11 files changed, 58 insertions(+), 1667 deletions(-) delete mode 100644 wasmbinding/bindings/query.go delete mode 100644 wasmbinding/querier.go delete mode 100644 wasmbinding/querier_perp_test.go delete mode 100644 wasmbinding/querier_test.go diff --git a/wasmbinding/bindings/marshalling_test.go b/wasmbinding/bindings/marshalling_test.go index 8d6957da2..931ecdd49 100644 --- a/wasmbinding/bindings/marshalling_test.go +++ b/wasmbinding/bindings/marshalling_test.go @@ -12,7 +12,6 @@ import ( "github.com/NibiruChain/nibiru/app" "github.com/NibiruChain/nibiru/wasmbinding/bindings" - "github.com/NibiruChain/nibiru/x/common/testutil/genesis" ) type TestSuiteBindingJsonTypes struct { @@ -36,57 +35,6 @@ func (s *TestSuiteBindingJsonTypes) SetupSuite() { s.fileJson = fileJson } -func (s *TestSuiteBindingJsonTypes) TestQueries() { - testCaseMap := map[string]any{ - "all_markets": new(bindings.AllMarketsResponse), - "reserves": new(bindings.ReservesResponse), - "base_price": new(bindings.BasePriceResponse), - "position": new(bindings.PositionResponse), - "positions": new(bindings.PositionsResponse), - "module_params": new(bindings.PerpParamsResponse), - "premium_fraction": new(bindings.PremiumFractionResponse), - "metrics": new(bindings.MetricsResponse), - "module_accounts": new(bindings.ModuleAccountsResponse), - "oracle_prices": new(bindings.OraclePricesResponse), - } - - for name, cwRespPtr := range testCaseMap { - s.T().Run(name, func(t *testing.T) { - err := json.Unmarshal(s.fileJson[name], cwRespPtr) - s.Assert().NoErrorf(err, "name: %v", name) - jsonBz, err := json.Marshal(cwRespPtr) - s.NoErrorf(err, "jsonBz: %s", jsonBz) - }) - } -} - -func (s *TestSuiteBindingJsonTypes) TestToAppMarket() { - var lastCwMarket bindings.Market - for _, ammMarket := range genesis.START_MARKETS { - dummyBlockHeight := int64(1) - cwMarket := bindings.NewMarket( - ammMarket.Market, - ammMarket.Amm, - "index price", - ammMarket.Amm.InstMarkPrice().String(), - dummyBlockHeight, - ) - - // Test the ToAppMarket fn - gotAppMarket, err := cwMarket.ToAppMarket() - s.Assert().NoError(err) - s.Assert().EqualValues(ammMarket.Market, gotAppMarket) - - lastCwMarket = cwMarket - } - - // Test failure case - sadCwMarket := lastCwMarket - sadCwMarket.Pair = "ftt:ust:xxx-yyy!!!" - _, err := sadCwMarket.ToAppMarket() - s.Error(err) -} - func getFileJson(t *testing.T) (fileJson map[string]json.RawMessage) { file, err := os.Open("execute_msg.json") require.NoError(t, err) @@ -102,13 +50,7 @@ func (s *TestSuiteBindingJsonTypes) TestExecuteMsgs() { fileJson := getFileJson(t) testCaseMap := []string{ - "market_order", - "close_position", - "add_margin", - "remove_margin", "donate_to_insurance_fund", - "peg_shift", - "depth_shift", "edit_oracle_params", "set_market_enabled", "insurance_fund_withdraw", diff --git a/wasmbinding/bindings/msg.go b/wasmbinding/bindings/msg.go index bce9b362e..8e360a09d 100644 --- a/wasmbinding/bindings/msg.go +++ b/wasmbinding/bindings/msg.go @@ -13,11 +13,7 @@ import ( // - https://github.com/NibiruChain/cw-nibiru/blob/90df123f8d32d47b5b280ec6ae7dde0f9dbf2787/contracts/bindings-perp/src/msg.rs type NibiruMsg struct { // bindings-perp ExecuteMsg enum types - MarketOrder *MarketOrder `json:"market_order,omitempty"` - ClosePosition *ClosePosition `json:"close_position,omitempty"` // MultiLiquidate *MultiLiquidate `json:"multi_liquidate,omitempty"` // TODO - AddMargin *AddMargin `json:"add_margin,omitempty"` - RemoveMargin *RemoveMargin `json:"remove_margin,omitempty"` DonateToInsuranceFund *DonateToInsuranceFund `json:"donate_to_insurance_fund,omitempty"` // TODO InsuranceFundWithdraw *InsuranceFundWithdraw `json:"insurance_fund_withdraw,omitempty"` SetMarketEnabled *SetMarketEnabled `json:"set_market_enabled,omitempty"` @@ -29,37 +25,6 @@ type NibiruMsg struct { NoOp *NoOp `json:"no_op,omitempty"` } -type MarketOrder struct { - Pair string `json:"pair"` - IsLong bool `json:"is_long"` - QuoteAmount sdkmath.Int `json:"quote_amount"` - Leverage sdk.Dec `json:"leverage"` - BaseAmountLimit sdkmath.Int `json:"base_amount_limit"` -} - -type ClosePosition struct { - Pair string `json:"pair"` -} - -type MultiLiquidate struct { - Liquidations []LiquidationArgs `json:"liquidations"` -} - -type LiquidationArgs struct { - Pair string `json:"pair"` - Trader string `json:"trader"` -} - -type AddMargin struct { - Pair string `json:"pair"` - Margin sdk.Coin `json:"margin"` -} - -type RemoveMargin struct { - Pair string `json:"pair"` - Margin sdk.Coin `json:"margin"` -} - type DonateToInsuranceFund struct { Sender string `json:"sender"` Donation sdk.Coin `json:"donation"` diff --git a/wasmbinding/bindings/query.go b/wasmbinding/bindings/query.go deleted file mode 100644 index fe54aaca3..000000000 --- a/wasmbinding/bindings/query.go +++ /dev/null @@ -1,230 +0,0 @@ -package bindings - -import ( - "time" - - sdkmath "cosmossdk.io/math" - - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/NibiruChain/nibiru/x/common/asset" - epochstypes "github.com/NibiruChain/nibiru/x/epochs/types" - perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types" -) - -// BindingQuery corresponds to the NibiruQuery enum in CosmWasm binding -// contracts (Rust). It specifies which queries can be called with -// Nibiru bindings and specifies the JSON schema that connects app ⇔ Wasm. -// -// ### Note -// 1. The JSON field names must match the ones on the smart contract -// 2. You use a pointer so that each field can be nil, which will be missing in -// the input or output json. What's actually sent from the contract will be an -// instance of the parent type, but the message body will be on one of these -// nullable fields. -// This is part of the reason we need the "omitempty" struct tags -// -// See: -// - https://github.com/NibiruChain/cw-nibiru/blob/90df123f8d32d47b5b280ec6ae7dde0f9dbf2787/contracts/bindings-perp/src/query.rs -type BindingQuery struct { - // bindings-perp NibiruQuery enum types - Reserves *ReservesRequest `json:"reserves,omitempty"` - AllMarkets *AllMarketsRequest `json:"all_markets,omitempty"` - BasePrice *BasePriceRequest `json:"base_price,omitempty"` - Positions *PositionsRequest `json:"positions,omitempty"` - Position *PositionRequest `json:"position,omitempty"` - PremiumFraction *PremiumFractionRequest `json:"premium_fraction,omitempty"` - Metrics *MetricsRequest `json:"metrics,omitempty"` - ModuleAccounts *ModuleAccountsRequest `json:"module_accounts,omitempty"` - PerpParams *PerpParamsRequest `json:"module_params,omitempty"` - OraclePrices *OraclePrices `json:"oracle_prices,omitempty"` -} - -type ReservesRequest struct { - Pair string `json:"pair"` -} - -type ReservesResponse struct { - Pair string `json:"pair"` - BaseReserve sdk.Dec `json:"base_reserve"` - QuoteReserve sdk.Dec `json:"quote_reserve"` -} - -type AllMarketsRequest struct{} - -type AllMarketsResponse struct { - MarketMap map[string]Market `json:"market_map"` -} - -type Market struct { - Pair string `json:"pair"` - Version sdkmath.Int `json:"version"` - BaseReserve sdk.Dec `json:"base_reserve"` - QuoteReserve sdk.Dec `json:"quote_reserve"` - SqrtDepth sdk.Dec `json:"sqrt_depth"` - Depth sdkmath.Int `json:"depth"` - TotalLong sdk.Dec `json:"total_long"` - TotalShort sdk.Dec `json:"total_short"` - PegMult sdk.Dec `json:"peg_mult"` - Config *MarketConfig `json:"config,omitempty"` - MarkPrice sdk.Dec `json:"mark_price"` - IndexPrice string `json:"index_price"` - TwapMark string `json:"twap_mark"` - BlockNumber sdkmath.Int `json:"block_number"` -} - -// ToAppMarket Converts the JSON market, which comes in from Rust, to its corresponding -// protobuf (Golang) type in the app: perpv2types.Market. -func (m Market) ToAppMarket() (appMarket perpv2types.Market, err error) { - config := m.Config - pair, err := asset.TryNewPair(m.Pair) - if err != nil { - return appMarket, err - } - return perpv2types.Market{ - Pair: pair, - Enabled: true, - Version: m.Version.Uint64(), - MaintenanceMarginRatio: config.MaintenanceMarginRatio, - MaxLeverage: config.MaxLeverage, - LatestCumulativePremiumFraction: sdk.ZeroDec(), - ExchangeFeeRatio: sdk.MustNewDecFromStr("0.0010"), - EcosystemFundFeeRatio: sdk.MustNewDecFromStr("0.0010"), - LiquidationFeeRatio: sdk.MustNewDecFromStr("0.0500"), - PartialLiquidationRatio: sdk.MustNewDecFromStr("0.5"), - FundingRateEpochId: epochstypes.ThirtyMinuteEpochID, - MaxFundingRate: sdk.NewDec(1), - TwapLookbackWindow: 30 * time.Minute, - PrepaidBadDebt: sdk.NewCoin(pair.QuoteDenom(), sdk.ZeroInt()), - }, nil -} - -func NewMarket(appMarket perpv2types.Market, appAmm perpv2types.AMM, indexPrice, twapMark string, blockNumber int64) Market { - return Market{ - Pair: appMarket.Pair.String(), - Version: sdk.NewIntFromUint64(appMarket.Version), - BaseReserve: appAmm.BaseReserve, - QuoteReserve: appAmm.QuoteReserve, - SqrtDepth: appAmm.SqrtDepth, - // Depth: base.Mul(quote).RoundInt(), - TotalLong: appAmm.TotalLong, - TotalShort: appAmm.TotalShort, - PegMult: appAmm.PriceMultiplier, - Config: &MarketConfig{ - MaintenanceMarginRatio: appMarket.MaintenanceMarginRatio, - MaxLeverage: appMarket.MaxLeverage, - }, - MarkPrice: appAmm.InstMarkPrice(), - IndexPrice: indexPrice, - TwapMark: twapMark, - BlockNumber: sdk.NewInt(blockNumber), - } -} - -type MarketConfig struct { - MaintenanceMarginRatio sdk.Dec `json:"maintenance_margin_ratio"` - MaxLeverage sdk.Dec `json:"max_leverage"` -} - -type BasePriceRequest struct { - Pair string `json:"pair"` - IsLong bool `json:"is_long"` - BaseAmount sdkmath.Int `json:"base_amount"` -} - -type BasePriceResponse struct { - Pair string `json:"pair"` - BaseAmount sdk.Dec `json:"base_amount"` - IsLong bool `json:"is_long"` -} - -type PositionsRequest struct { - Trader string `json:"trader"` -} - -type Position struct { - TraderAddr string `json:"trader_addr"` - Pair string `json:"pair"` - Size sdk.Dec `json:"size"` - Margin sdk.Dec `json:"margin"` - OpenNotional sdk.Dec `json:"open_notional"` - LatestCPF sdk.Dec `json:"latest_cpf"` - BlockNumber sdkmath.Int `json:"block_number"` -} - -type PositionsResponse struct { - Positions map[string]Position `json:"positions"` -} - -type PositionRequest struct { - Trader string `json:"trader"` - Pair string `json:"pair"` -} - -type PositionResponse struct { - Position Position `json:"position"` - Notional sdk.Dec `json:"notional"` - Upnl sdk.Dec `json:"upnl"` - Margin_ratio_mark sdk.Dec `json:"margin_ratio_mark"` - Margin_ratio_index sdk.Dec `json:"margin_ratio_index"` - Block_number sdkmath.Int `json:"block_number"` -} - -type PremiumFractionRequest struct { - Pair string `json:"pair"` -} - -type PremiumFractionResponse struct { - Pair string `json:"pair"` - CPF sdk.Dec `json:"cpf"` - EstimatedNextCPF sdk.Dec `json:"estimated_next_cpf"` -} - -type MetricsRequest struct { - Pair string `json:"pair"` -} - -type MetricsResponse struct { - Metrics Metrics `json:"metrics"` -} - -type Metrics struct { - Pair string `json:"pair"` - NetSize sdkmath.LegacyDec `json:"net_size"` - VolumeQuote sdkmath.LegacyDec `json:"volume_quote"` - VolumeBase sdkmath.LegacyDec `json:"volume_base"` - BlockNumber sdkmath.Int `json:"block_number"` -} - -type ModuleAccountsRequest struct{} - -type ModuleAccountWithBalance struct { - Name string `json:"name"` - Addr sdk.AccAddress `json:"addr"` - Balance []sdk.Coin `json:"balance"` -} - -type ModuleAccountsResponse struct { - ModuleAccounts map[string]ModuleAccountWithBalance `json:"module_accounts"` -} - -type PerpParamsRequest struct{} - -type PerpParamsResponse struct { - ModuleParams PerpParams `json:"module_params"` -} - -type PerpParams struct { - Stopped bool `json:"stopped"` - FeePoolFeeRatio sdkmath.LegacyDec `json:"fee_pool_fee_ratio"` - EcosystemFundFeeRatio sdkmath.LegacyDec `json:"ecosystem_fund_fee_ratio"` - LiquidationFeeRatio sdkmath.LegacyDec `json:"liquidation_fee_ratio"` - PartialLiquidationRatio sdkmath.LegacyDec `json:"partial_liquidation_ratio"` - FundingRateInterval string `json:"funding_rate_interval"` - TwapLookbackWindow sdkmath.Int `json:"twap_lookback_window"` - WhitelistedLiquidators []string `json:"whitelisted_liquidators"` -} - -type OraclePrices struct{} - -type OraclePricesResponse = map[string]sdk.Dec diff --git a/wasmbinding/exec_perp.go b/wasmbinding/exec_perp.go index 992718e9e..9ef201feb 100644 --- a/wasmbinding/exec_perp.go +++ b/wasmbinding/exec_perp.go @@ -20,123 +20,6 @@ func (exec *ExecutorPerp) MsgServer() perpv2types.MsgServer { return perpv2keeper.NewMsgServerImpl(exec.PerpV2) } -func (exec *ExecutorPerp) MarketOrder( - cwMsg *bindings.MarketOrder, sender sdk.AccAddress, ctx sdk.Context, -) ( - sdkResp *perpv2types.MsgMarketOrderResponse, err error, -) { - if cwMsg == nil { - return sdkResp, wasmvmtypes.InvalidRequest{Err: "null open position msg"} - } - - pair, err := asset.TryNewPair(cwMsg.Pair) - if err != nil { - return sdkResp, err - } - - var side perpv2types.Direction - if cwMsg.IsLong { - side = perpv2types.Direction_LONG - } else { - side = perpv2types.Direction_SHORT - } - - sdkMsg := &perpv2types.MsgMarketOrder{ - Sender: sender.String(), - Pair: pair, - Side: side, - QuoteAssetAmount: cwMsg.QuoteAmount, - Leverage: cwMsg.Leverage, - BaseAssetAmountLimit: cwMsg.BaseAmountLimit, - } - if err := sdkMsg.ValidateBasic(); err != nil { - return sdkResp, err - } - - goCtx := sdk.WrapSDKContext(ctx) - return exec.MsgServer().MarketOrder(goCtx, sdkMsg) -} - -func (exec *ExecutorPerp) ClosePosition( - cwMsg *bindings.ClosePosition, sender sdk.AccAddress, ctx sdk.Context, -) ( - sdkResp *perpv2types.MsgClosePositionResponse, err error, -) { - if cwMsg == nil { - return sdkResp, wasmvmtypes.InvalidRequest{Err: "null close position msg"} - } - - pair, err := asset.TryNewPair(cwMsg.Pair) - if err != nil { - return sdkResp, err - } - - sdkMsg := &perpv2types.MsgClosePosition{ - Sender: sender.String(), - Pair: pair, - } - if err := sdkMsg.ValidateBasic(); err != nil { - return sdkResp, err - } - - goCtx := sdk.WrapSDKContext(ctx) - return exec.MsgServer().ClosePosition(goCtx, sdkMsg) -} - -func (exec *ExecutorPerp) AddMargin( - cwMsg *bindings.AddMargin, sender sdk.AccAddress, ctx sdk.Context, -) ( - sdkResp *perpv2types.MsgAddMarginResponse, err error, -) { - if cwMsg == nil { - return sdkResp, wasmvmtypes.InvalidRequest{Err: "null add margin msg"} - } - - pair, err := asset.TryNewPair(cwMsg.Pair) - if err != nil { - return sdkResp, err - } - - sdkMsg := &perpv2types.MsgAddMargin{ - Sender: sender.String(), - Pair: pair, - Margin: cwMsg.Margin, - } - if err := sdkMsg.ValidateBasic(); err != nil { - return sdkResp, err - } - - goCtx := sdk.WrapSDKContext(ctx) - return exec.MsgServer().AddMargin(goCtx, sdkMsg) -} - -func (exec *ExecutorPerp) RemoveMargin( - cwMsg *bindings.RemoveMargin, sender sdk.AccAddress, ctx sdk.Context, -) ( - sdkResp *perpv2types.MsgRemoveMarginResponse, err error, -) { - if cwMsg == nil { - return sdkResp, wasmvmtypes.InvalidRequest{Err: "null remove margin msg"} - } - - pair, err := asset.TryNewPair(cwMsg.Pair) - if err != nil { - return sdkResp, err - } - - sdkMsg := &perpv2types.MsgRemoveMargin{ - Sender: sender.String(), - Pair: pair, - Margin: cwMsg.Margin, - } - if err := sdkMsg.ValidateBasic(); err != nil { - return sdkResp, err - } - - goCtx := sdk.WrapSDKContext(ctx) - return exec.MsgServer().RemoveMargin(goCtx, sdkMsg) -} - func (exec *ExecutorPerp) InsuranceFundWithdraw( cwMsg *bindings.InsuranceFundWithdraw, ctx sdk.Context, ) (err error) { diff --git a/wasmbinding/exec_perp_test.go b/wasmbinding/exec_perp_test.go index b5db53479..bfac0d502 100644 --- a/wasmbinding/exec_perp_test.go +++ b/wasmbinding/exec_perp_test.go @@ -1,7 +1,6 @@ package wasmbinding_test import ( - "errors" "testing" "time" @@ -17,7 +16,9 @@ import ( "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" + "github.com/NibiruChain/nibiru/x/common/testutil/genesis" "github.com/NibiruChain/nibiru/x/common/testutil/testapp" + oracletypes "github.com/NibiruChain/nibiru/x/oracle/types" perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types" ) @@ -38,6 +39,59 @@ type TestSuitePerpExecutor struct { happyFields ExampleFields } +func SetExchangeRates( + testSuite *suite.Suite, + nibiru *app.NibiruApp, + ctx sdk.Context, +) (exchangeRateMap map[asset.Pair]sdk.Dec) { + s := testSuite + exchangeRateTuples := []oracletypes.ExchangeRateTuple{ + { + Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD), + ExchangeRate: sdk.NewDec(1_000), + }, + { + Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), + ExchangeRate: sdk.NewDec(10), + }, + } + + for _, exchangeRateTuple := range exchangeRateTuples { + pair := exchangeRateTuple.Pair + exchangeRate := exchangeRateTuple.ExchangeRate + nibiru.OracleKeeper.SetPrice(ctx, pair, exchangeRate) + + rate, err := nibiru.OracleKeeper.ExchangeRates.Get(ctx, pair) + s.Assert().NoError(err) + s.Assert().EqualValues(exchangeRate, rate.ExchangeRate) + } + + return oracletypes.ExchangeRateTuples(exchangeRateTuples).ToMap() +} + +type ExampleFields struct { + Pair string + Trader sdk.AccAddress + Dec sdk.Dec + Int sdkmath.Int +} + +func GetHappyFields() ExampleFields { + return ExampleFields{ + Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD).String(), + Trader: sdk.AccAddress([]byte("trader")), + Dec: sdk.NewDec(50), + Int: sdk.NewInt(420), + } +} + +func SetupPerpGenesis() app.GenesisState { + genesisState := genesis.NewTestGenesisState(app.MakeEncodingConfig()) + genesisState = genesis.AddOracleGenesis(genesisState) + genesisState = genesis.AddPerpV2Genesis(genesisState) + return genesisState +} + func (s *TestSuitePerpExecutor) SetupSuite() { s.happyFields = GetHappyFields() sender := testutil.AccAddress() @@ -77,20 +131,9 @@ func (s *TestSuitePerpExecutor) OnSetupEnd() { s.ratesMap = SetExchangeRates(&s.Suite, s.nibiru, s.ctx) } -// Happy path coverage of MarketOrder, AddMargin, RemoveMargin, and ClosePosition -func (s *TestSuitePerpExecutor) TestOpenAddRemoveClose() { - pair := asset.MustNewPair(s.happyFields.Pair) - - margin := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69)) - incorrectMargin := sdk.NewCoin(denoms.USDT, sdk.NewInt(69)) - +// Happy path coverage +func (s *TestSuitePerpExecutor) TestPerpExecutorHappy() { for _, err := range []error{ - s.DoMarketOrderTest(pair), - s.DoAddMarginTest(pair, margin), - s.DoAddIncorrectMarginTest(pair, incorrectMargin), - s.DoRemoveIncorrectMarginTest(pair, incorrectMargin), - s.DoRemoveMarginTest(pair, margin), - s.DoClosePositionTest(pair), s.DoInsuranceFundWithdrawTest(sdk.NewInt(69), s.contractDeployer), s.DoCreateMarketTest(asset.MustNewPair("ufoo:ubar")), s.DoCreateMarketTestWithParams(asset.MustNewPair("ufoo2:ubar")), @@ -99,118 +142,6 @@ func (s *TestSuitePerpExecutor) TestOpenAddRemoveClose() { } } -func (s *TestSuitePerpExecutor) DoMarketOrderTest(pair asset.Pair) error { - cwMsg := &bindings.MarketOrder{ - Pair: pair.String(), - IsLong: false, - QuoteAmount: sdk.NewInt(4_200_000), - Leverage: sdk.NewDec(5), - BaseAmountLimit: sdk.ZeroInt(), - } - - _, err := s.exec.MarketOrder(cwMsg, s.contractPerp, s.ctx) - if err != nil { - return err - } - - // Verify position exists with PerpKeeper - _, err = s.exec.PerpV2.GetPosition(s.ctx, pair, 1, s.contractPerp) - if err != nil { - return err - } - - // Verify position exists with CustomQuerier - multi-position - bindingQuery := bindings.BindingQuery{ - Positions: &bindings.PositionsRequest{ - Trader: s.contractPerp.String(), - }, - } - bindingRespMulti := new(bindings.PositionsRequest) - _, err = DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingRespMulti, - ) - if err != nil { - return err - } - - // Verify position exists with CustomQuerier - single position - bindingQuery = bindings.BindingQuery{ - Position: &bindings.PositionRequest{ - Trader: s.contractPerp.String(), - Pair: pair.String(), - }, - } - bindingResp := new(bindings.PositionRequest) - _, err = DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - - return err -} - -func (s *TestSuitePerpExecutor) DoAddMarginTest( - pair asset.Pair, margin sdk.Coin, -) error { - cwMsg := &bindings.AddMargin{ - Pair: pair.String(), - Margin: margin, - } - - _, err := s.exec.AddMargin(cwMsg, s.contractPerp, s.ctx) - return err -} - -func (s *TestSuitePerpExecutor) DoAddIncorrectMarginTest( - pair asset.Pair, margin sdk.Coin, -) error { - cwMsg := &bindings.AddMargin{ - Pair: pair.String(), - Margin: margin, - } - - _, err := s.exec.AddMargin(cwMsg, s.contractPerp, s.ctx) - if err == nil { - return errors.New("incorrect margin type should have failed") - } - return nil -} - -func (s *TestSuitePerpExecutor) DoRemoveIncorrectMarginTest( - pair asset.Pair, margin sdk.Coin, -) error { - cwMsg := &bindings.RemoveMargin{ - Pair: pair.String(), - Margin: margin, - } - - _, err := s.exec.RemoveMargin(cwMsg, s.contractPerp, s.ctx) - if err == nil { - return errors.New("incorrect margin type should have failed") - } - return nil -} - -func (s *TestSuitePerpExecutor) DoRemoveMarginTest( - pair asset.Pair, margin sdk.Coin, -) error { - cwMsg := &bindings.RemoveMargin{ - Pair: pair.String(), - Margin: margin, - } - - _, err := s.exec.RemoveMargin(cwMsg, s.contractPerp, s.ctx) - return err -} - -func (s *TestSuitePerpExecutor) DoClosePositionTest(pair asset.Pair) error { - cwMsg := &bindings.ClosePosition{ - Pair: pair.String(), - } - - _, err := s.exec.ClosePosition(cwMsg, s.contractPerp, s.ctx) - return err -} - func (s *TestSuitePerpExecutor) DoInsuranceFundWithdrawTest( amt sdkmath.Int, to sdk.AccAddress, ) error { @@ -266,21 +197,7 @@ func (s *TestSuitePerpExecutor) DoCreateMarketTestWithParams(pair asset.Pair) er } func (s *TestSuitePerpExecutor) TestSadPaths_Nil() { - var err error - - _, err = s.exec.MarketOrder(nil, nil, s.ctx) - s.Error(err) - - _, err = s.exec.AddMargin(nil, nil, s.ctx) - s.Error(err) - - _, err = s.exec.RemoveMargin(nil, nil, s.ctx) - s.Error(err) - - _, err = s.exec.ClosePosition(nil, nil, s.ctx) - s.Error(err) - - err = s.exec.InsuranceFundWithdraw(nil, s.ctx) + err := s.exec.InsuranceFundWithdraw(nil, s.ctx) s.Error(err) } @@ -312,13 +229,8 @@ func (s *TestSuitePerpExecutor) TestSadPath_InsuranceFundWithdraw() { func (s *TestSuitePerpExecutor) TestSadPaths_InvalidPair() { sadPair := asset.Pair("ftt:ust:doge") pair := sadPair - margin := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69)) for _, err := range []error{ - s.DoMarketOrderTest(pair), - s.DoAddMarginTest(pair, margin), - s.DoRemoveMarginTest(pair, margin), - s.DoClosePositionTest(pair), s.DoSetMarketEnabledTest(pair, true), s.DoSetMarketEnabledTest(pair, false), s.DoCreateMarketTest(pair), diff --git a/wasmbinding/exec_test.go b/wasmbinding/exec_test.go index d5b5fd9a5..0ad880db0 100644 --- a/wasmbinding/exec_test.go +++ b/wasmbinding/exec_test.go @@ -127,64 +127,6 @@ func (s *TestSuiteExecutor) OnSetupEnd() { SetExchangeRates(&s.Suite, s.nibiru, s.ctx) } -func (s *TestSuiteExecutor) TestOpenAddRemoveClose() { - pair := asset.MustNewPair(s.happyFields.Pair) - margin := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69)) - - coins := sdk.NewCoins( - margin.Add(sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(1_000))), - ) - s.NoError(testapp.FundAccount(s.nibiru.BankKeeper, s.ctx, s.contractPerp, coins)) - - // TestMarketOrder (integration - real contract, real app) - execMsg := bindings.NibiruMsg{ - MarketOrder: &bindings.MarketOrder{ - Pair: s.happyFields.Pair, - IsLong: true, - QuoteAmount: sdk.NewInt(42), - Leverage: sdk.NewDec(5), - BaseAmountLimit: sdk.ZeroInt(), - }, - } - - s.T().Log("Executing with permission should succeed") - s.keeper.SetSudoContracts( - []string{s.contractPerp.String()}, s.ctx, - ) - - contractRespBz, err := s.ExecuteAgainstContract(s.contractPerp, execMsg) - s.NoErrorf(err, "contractRespBz: %s", contractRespBz) - - // TestAddMargin (integration - real contract, real app) - execMsg = bindings.NibiruMsg{ - AddMargin: &bindings.AddMargin{ - Pair: pair.String(), - Margin: margin, - }, - } - contractRespBz, err = s.ExecuteAgainstContract(s.contractPerp, execMsg) - s.NoErrorf(err, "contractRespBz: %s", contractRespBz) - - // TestRemoveMargin (integration - real contract, real app) - execMsg = bindings.NibiruMsg{ - RemoveMargin: &bindings.RemoveMargin{ - Pair: pair.String(), - Margin: margin, - }, - } - contractRespBz, err = s.ExecuteAgainstContract(s.contractPerp, execMsg) - s.NoErrorf(err, "contractRespBz: %s", contractRespBz) - - // TestClosePosition (integration - real contract, real app) - execMsg = bindings.NibiruMsg{ - ClosePosition: &bindings.ClosePosition{ - Pair: pair.String(), - }, - } - contractRespBz, err = s.ExecuteAgainstContract(s.contractPerp, execMsg) - s.NoErrorf(err, "contractRespBz: %s", contractRespBz) -} - func (s *TestSuiteExecutor) TestOracleParams() { defaultParams := types.DefaultParams() defaultParams.VotePeriod = 1_000 diff --git a/wasmbinding/message_plugin.go b/wasmbinding/message_plugin.go index a05b249c7..51ef552ba 100644 --- a/wasmbinding/message_plugin.go +++ b/wasmbinding/message_plugin.go @@ -62,24 +62,6 @@ func (messenger *CustomMessenger) DispatchMsg( } switch { - // Perp module | bindings-perp: for trading with smart contracts - case contractExecuteMsg.ExecuteMsg.MarketOrder != nil: - cwMsg := contractExecuteMsg.ExecuteMsg.MarketOrder - _, err = messenger.Perp.MarketOrder(cwMsg, contractAddr, ctx) - return events, data, err - case contractExecuteMsg.ExecuteMsg.ClosePosition != nil: - cwMsg := contractExecuteMsg.ExecuteMsg.ClosePosition - _, err = messenger.Perp.ClosePosition(cwMsg, contractAddr, ctx) - return events, data, err - case contractExecuteMsg.ExecuteMsg.AddMargin != nil: - cwMsg := contractExecuteMsg.ExecuteMsg.AddMargin - _, err = messenger.Perp.AddMargin(cwMsg, contractAddr, ctx) - return events, data, err - case contractExecuteMsg.ExecuteMsg.RemoveMargin != nil: - cwMsg := contractExecuteMsg.ExecuteMsg.RemoveMargin - _, err = messenger.Perp.RemoveMargin(cwMsg, contractAddr, ctx) - return events, data, err - // Perp module | controller case contractExecuteMsg.ExecuteMsg.CreateMarket != nil: if err := messenger.Sudo.CheckPermissions(contractAddr, ctx); err != nil { diff --git a/wasmbinding/querier.go b/wasmbinding/querier.go deleted file mode 100644 index 961df23c2..000000000 --- a/wasmbinding/querier.go +++ /dev/null @@ -1,331 +0,0 @@ -package wasmbinding - -import ( - "encoding/json" - "errors" - "fmt" - - sdkerrors "cosmossdk.io/errors" - wasmvmtypes "github.com/CosmWasm/wasmvm/types" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/NibiruChain/nibiru/wasmbinding/bindings" - "github.com/NibiruChain/nibiru/x/common/asset" - oraclekeeper "github.com/NibiruChain/nibiru/x/oracle/keeper" - oracletypes "github.com/NibiruChain/nibiru/x/oracle/types" - perpv2keeper "github.com/NibiruChain/nibiru/x/perp/v2/keeper" - perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types" -) - -type QueryPlugin struct { - Perp *PerpQuerier - Oracle *OracleQuerier -} - -// NewQueryPlugin returns a pointer to a new QueryPlugin -func NewQueryPlugin(perp perpv2keeper.Keeper, oracle oraclekeeper.Keeper) QueryPlugin { - return QueryPlugin{ - Perp: &PerpQuerier{ - perp: perpv2keeper.NewQuerier(perp), - }, - Oracle: &OracleQuerier{ - oracle: oraclekeeper.NewQuerier(oracle), - }, - } -} - -func (qp *QueryPlugin) ToBinary( - cwResp any, err error, cwReq any, -) ([]byte, error) { - if err != nil { - return nil, sdkerrors.Wrapf(err, - "failed to query: perp all markets: request: %v", - cwReq) - } - bz, err := json.Marshal(cwResp) - if err != nil { - return nil, sdkerrors.Wrapf(err, "failed to JSON marshal response: %v", cwResp) - } - return bz, nil -} - -// CustomQuerier returns a function that is an implementation of the custom -// querier mechanism for specific messages -func CustomQuerier(qp QueryPlugin) func(ctx sdk.Context, request json.RawMessage) ([]byte, error) { - return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) { - var wasmContractQuery bindings.BindingQuery - if err := json.Unmarshal(request, &wasmContractQuery); err != nil { - return nil, sdkerrors.Wrapf(err, "failed to JSON unmarshal nibiru query: %v", err) - } - - switch { - case wasmContractQuery.AllMarkets != nil: - cwReq := wasmContractQuery.AllMarkets - cwResp, err := qp.Perp.AllMarkets(ctx) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.Reserves != nil: - cwReq := wasmContractQuery.Reserves - cwResp, err := qp.Perp.Reserves(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.BasePrice != nil: - cwReq := wasmContractQuery.BasePrice - cwResp, err := qp.Perp.BasePrice(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.Positions != nil: - cwReq := wasmContractQuery.Positions - cwResp, err := qp.Perp.Positions(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.Position != nil: - cwReq := wasmContractQuery.Position - cwResp, err := qp.Perp.Position(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.PremiumFraction != nil: - cwReq := wasmContractQuery.PremiumFraction - cwResp, err := qp.Perp.PremiumFraction(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.Metrics != nil: - cwReq := wasmContractQuery.Metrics - cwResp, err := qp.Perp.Metrics(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.ModuleAccounts != nil: - cwReq := wasmContractQuery.ModuleAccounts - cwResp, err := qp.Perp.ModuleAccounts(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.PerpParams != nil: - cwReq := wasmContractQuery.PerpParams - cwResp, err := qp.Perp.ModuleParams(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - case wasmContractQuery.OraclePrices != nil: - cwReq := wasmContractQuery.OraclePrices - cwResp, err := qp.Oracle.ExchangeRates(ctx, cwReq) - return qp.ToBinary(cwResp, err, cwReq) - - default: - return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown nibiru query variant"} - } - } -} - -// ---------------------------------------------------------------------- -// PerpQuerier -// ---------------------------------------------------------------------- - -type PerpQuerier struct { - perp perpv2types.QueryServer -} - -func (perpExt *PerpQuerier) Reserves( - ctx sdk.Context, cwReq *bindings.ReservesRequest, -) (*bindings.ReservesResponse, error) { - pair := asset.Pair(cwReq.Pair) - sdkReq := &perpv2types.QueryMarketsRequest{} - goCtx := sdk.WrapSDKContext(ctx) - sdkResp, err := perpExt.perp.QueryMarkets(goCtx, sdkReq) - if err != nil { - return nil, err - } - - for _, market := range sdkResp.AmmMarkets { - if market.Amm.Pair.Equal(pair) { - return &bindings.ReservesResponse{ - Pair: pair.String(), - BaseReserve: market.Amm.BaseReserve, - QuoteReserve: market.Amm.QuoteReserve, - }, err - } - } - - return nil, fmt.Errorf("market not found for pair %s", pair) -} - -func (perpExt *PerpQuerier) AllMarkets( - ctx sdk.Context, -) (*bindings.AllMarketsResponse, error) { - sdkReq := &perpv2types.QueryMarketsRequest{} - goCtx := sdk.WrapSDKContext(ctx) - sdkResp, err := perpExt.perp.QueryMarkets(goCtx, sdkReq) - if err != nil { - return nil, err - } - - marketMap := make(map[string]bindings.Market) - for _, pbMarket := range sdkResp.AmmMarkets { - key := pbMarket.Amm.Pair.String() - marketMap[key] = bindings.Market{ - Pair: key, - Version: sdk.NewIntFromUint64(pbMarket.Market.Version), - BaseReserve: pbMarket.Amm.BaseReserve, - QuoteReserve: pbMarket.Amm.QuoteReserve, - SqrtDepth: pbMarket.Amm.SqrtDepth, - TotalLong: pbMarket.Amm.TotalLong, - TotalShort: pbMarket.Amm.TotalShort, - PegMult: pbMarket.Amm.PriceMultiplier, - Config: &bindings.MarketConfig{ - MaintenanceMarginRatio: pbMarket.Market.MaintenanceMarginRatio, - MaxLeverage: pbMarket.Market.MaxLeverage, - }, - MarkPrice: pbMarket.Amm.InstMarkPrice(), - BlockNumber: sdk.NewInt(ctx.BlockHeight()), - } - } - - return &bindings.AllMarketsResponse{ - MarketMap: marketMap, - }, err -} - -func (perpExt *PerpQuerier) BasePrice( - ctx sdk.Context, cwReq *bindings.BasePriceRequest, -) (*bindings.BasePriceResponse, error) { - return nil, fmt.Errorf("not implemented") -} - -func (perpExt *PerpQuerier) PremiumFraction( - ctx sdk.Context, cwReq *bindings.PremiumFractionRequest, -) (*bindings.PremiumFractionResponse, error) { - return nil, fmt.Errorf("not implemented") -} - -func (perpExt *PerpQuerier) Metrics( - ctx sdk.Context, cwReq *bindings.MetricsRequest, -) (*bindings.MetricsResponse, error) { - return nil, fmt.Errorf("not implemented") -} - -func (perpExt *PerpQuerier) ModuleAccounts( - ctx sdk.Context, cwReq *bindings.ModuleAccountsRequest, -) (*bindings.ModuleAccountsResponse, error) { - if cwReq == nil { - return nil, errors.New("nil request") - } - - sdkReq := &perpv2types.QueryModuleAccountsRequest{} - goCtx := sdk.WrapSDKContext(ctx) - sdkResp, err := perpExt.perp.ModuleAccounts(goCtx, sdkReq) - if err != nil { - return nil, err - } - - moduleAccounts := make(map[string]bindings.ModuleAccountWithBalance) - for _, acc := range sdkResp.Accounts { - addr, err := sdk.AccAddressFromBech32(acc.Address) - if err != nil { - return nil, err - } - moduleAccounts[acc.Name] = bindings.ModuleAccountWithBalance{ - Name: acc.Name, - Addr: addr, - Balance: acc.Balance, - } - } - - return &bindings.ModuleAccountsResponse{ - ModuleAccounts: moduleAccounts, - }, err -} - -func (perpExt *PerpQuerier) ModuleParams( - ctx sdk.Context, cwReq *bindings.PerpParamsRequest, -) (*bindings.PerpParamsResponse, error) { - return nil, fmt.Errorf("not implemented") -} - -func (perpExt *PerpQuerier) Position( - ctx sdk.Context, cwReq *bindings.PositionRequest, -) (*bindings.PositionResponse, error) { - pair, err := asset.TryNewPair(cwReq.Pair) - if err != nil { - return nil, err - } - sdkReq := &perpv2types.QueryPositionRequest{ - Pair: pair, - Trader: cwReq.Trader, - } - goCtx := sdk.WrapSDKContext(ctx) - sdkResp, err := perpExt.perp.QueryPosition(goCtx, sdkReq) - if err != nil { - return nil, err - } - return &bindings.PositionResponse{ - Position: bindings.Position{ - TraderAddr: sdkResp.Position.TraderAddress, - Pair: sdkResp.Position.Pair.String(), - Size: sdkResp.Position.Size_, - Margin: sdkResp.Position.Margin, - OpenNotional: sdkResp.Position.OpenNotional, - LatestCPF: sdkResp.Position.LatestCumulativePremiumFraction, - BlockNumber: sdk.NewInt(sdkResp.Position.LastUpdatedBlockNumber), - }, - Notional: sdkResp.PositionNotional, - Upnl: sdkResp.UnrealizedPnl, - Margin_ratio_mark: sdkResp.MarginRatio, - // Margin_ratio_index: sdkResp.MarginRatioIndex, - Block_number: sdk.NewInt(sdkResp.Position.LastUpdatedBlockNumber), - }, err -} - -func (perpExt *PerpQuerier) Positions( - ctx sdk.Context, cwReq *bindings.PositionsRequest, -) (*bindings.PositionsResponse, error) { - sdkReq := &perpv2types.QueryPositionsRequest{ - Trader: cwReq.Trader, - } - goCtx := sdk.WrapSDKContext(ctx) - sdkResp, err := perpExt.perp.QueryPositions(goCtx, sdkReq) - if err != nil { - return nil, err - } - - positionMap := make(map[string]bindings.Position) - for _, posResp := range sdkResp.Positions { - pair := posResp.Position.Pair.String() - pos := posResp.Position - positionMap[pair] = bindings.Position{ - TraderAddr: pos.TraderAddress, - Pair: pair, - Size: pos.Size_, - Margin: pos.Margin, - OpenNotional: pos.OpenNotional, - LatestCPF: pos.LatestCumulativePremiumFraction, - BlockNumber: sdk.NewInt(pos.LastUpdatedBlockNumber), - } - } - - return &bindings.PositionsResponse{ - Positions: positionMap, - }, err -} - -// ---------------------------------------------------------------------- -// OracleQuerier -// ---------------------------------------------------------------------- - -type OracleQuerier struct { - oracle oracletypes.QueryServer -} - -func (oracleExt *OracleQuerier) ExchangeRates( - ctx sdk.Context, cwReq *bindings.OraclePrices, -) (*bindings.OraclePricesResponse, error) { - queryExchangeRatesRequest := oracletypes.QueryExchangeRatesRequest{} - queryExchangeRates, err := oracleExt.oracle.ExchangeRates(ctx, &queryExchangeRatesRequest) - - // Transform Tuple to Map - exchangeRates := make(map[string]sdk.Dec) - for _, exchangeRate := range queryExchangeRates.ExchangeRates { - exchangeRates[exchangeRate.Pair.String()] = exchangeRate.ExchangeRate - } - - cwResp := new(bindings.OraclePricesResponse) - *cwResp = exchangeRates - return cwResp, err -} diff --git a/wasmbinding/querier_perp_test.go b/wasmbinding/querier_perp_test.go deleted file mode 100644 index efd8d644a..000000000 --- a/wasmbinding/querier_perp_test.go +++ /dev/null @@ -1,319 +0,0 @@ -package wasmbinding_test - -import ( - "encoding/json" - "testing" - "time" - - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/suite" - - "github.com/NibiruChain/nibiru/app" - "github.com/NibiruChain/nibiru/wasmbinding" - "github.com/NibiruChain/nibiru/wasmbinding/bindings" - "github.com/NibiruChain/nibiru/wasmbinding/wasmbin" - "github.com/NibiruChain/nibiru/x/common/asset" - "github.com/NibiruChain/nibiru/x/common/denoms" - "github.com/NibiruChain/nibiru/x/common/testutil" - "github.com/NibiruChain/nibiru/x/common/testutil/genesis" - "github.com/NibiruChain/nibiru/x/common/testutil/testapp" - oracletypes "github.com/NibiruChain/nibiru/x/oracle/types" - perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types" -) - -func TestSuitePerpQuerier_RunAll(t *testing.T) { - suite.Run(t, new(TestSuitePerpQuerier)) -} - -func SetExchangeRates( - testSuite *suite.Suite, - nibiru *app.NibiruApp, - ctx sdk.Context, -) (exchangeRateMap map[asset.Pair]sdk.Dec) { - s := testSuite - exchangeRateTuples := []oracletypes.ExchangeRateTuple{ - { - Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD), - ExchangeRate: sdk.NewDec(1_000), - }, - { - Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), - ExchangeRate: sdk.NewDec(10), - }, - } - - for _, exchangeRateTuple := range exchangeRateTuples { - pair := exchangeRateTuple.Pair - exchangeRate := exchangeRateTuple.ExchangeRate - nibiru.OracleKeeper.SetPrice(ctx, pair, exchangeRate) - - rate, err := nibiru.OracleKeeper.ExchangeRates.Get(ctx, pair) - s.Assert().NoError(err) - s.Assert().EqualValues(exchangeRate, rate.ExchangeRate) - } - - return oracletypes.ExchangeRateTuples(exchangeRateTuples).ToMap() -} - -// ———————————————————————————————————————————————————————————————————————————— -// # Test Setup -// ———————————————————————————————————————————————————————————————————————————— - -type TestSuitePerpQuerier struct { - suite.Suite - - nibiru *app.NibiruApp - ctx sdk.Context - contractDeployer sdk.AccAddress - queryPlugin wasmbinding.QueryPlugin - - contractPerp sdk.AccAddress - fields ExampleFields - ratesMap map[asset.Pair]sdk.Dec -} - -func SetupPerpGenesis() app.GenesisState { - genesisState := genesis.NewTestGenesisState(app.MakeEncodingConfig()) - genesisState = genesis.AddOracleGenesis(genesisState) - genesisState = genesis.AddPerpV2Genesis(genesisState) - return genesisState -} - -func (s *TestSuitePerpQuerier) SetupSuite() { - s.fields = GetHappyFields() - sender := testutil.AccAddress() - s.contractDeployer = sender - - genesisState := SetupPerpGenesis() - nibiru := testapp.NewNibiruTestApp(genesisState) - ctx := nibiru.NewContext(false, tmproto.Header{ - Height: 1, - ChainID: "nibiru-wasmnet-1", - Time: time.Now().UTC(), - }) - coins := sdk.NewCoins( - sdk.NewCoin(denoms.NIBI, sdk.NewInt(10_000_000)), - sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(1_420_000)), - ) - s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins)) - - nibiru, ctx = SetupAllContracts(s.T(), sender, nibiru, ctx) - s.nibiru = nibiru - s.ctx = ctx - - s.contractPerp = ContractMap[wasmbin.WasmKeyPerpBinding] - s.queryPlugin = wasmbinding.NewQueryPlugin( - nibiru.PerpKeeperV2, - nibiru.OracleKeeper, - ) - s.OnSetupEnd() -} - -func (s *TestSuitePerpQuerier) OnSetupEnd() { - s.ratesMap = SetExchangeRates(&s.Suite, s.nibiru, s.ctx) -} - -// ———————————————————————————————————————————————————————————————————————————— -// # Tests -// -// - TestPremiumFraction -// - TestAllMarkets -// - TestMetrics -// - TestModuleAccounts -// - TestModuleParams -// - TestPosition -// ———————————————————————————————————————————————————————————————————————————— - -func (s *TestSuitePerpQuerier) TestPremiumFraction() { - testCases := map[string]struct { - cwReq *bindings.PremiumFractionRequest - cwResp *bindings.PremiumFractionResponse - expectErr bool - }{ - "invalid pair": { - cwReq: &bindings.PremiumFractionRequest{Pair: "nonsense"}, - expectErr: true, - }, - "happy": { - cwReq: &bindings.PremiumFractionRequest{Pair: s.fields.Pair}, - cwResp: &bindings.PremiumFractionResponse{ - Pair: s.fields.Pair, - CPF: sdk.MustNewDecFromStr("0.5"), - EstimatedNextCPF: sdk.MustNewDecFromStr("0.5"), - }, - expectErr: false, - }, - } - - for name, testCase := range testCases { - s.T().Run(name, func(t *testing.T) { - cwResp, err := s.queryPlugin.Perp.PremiumFraction( - s.ctx, testCase.cwReq, - ) - - if testCase.expectErr { - s.Error(err) - return - } - - s.Errorf(err, "cwResp: %s", cwResp) - s.Nil(cwResp) - // s.Assert().EqualValues(cwResp.Pair, cwResp.Pair) - // s.Assert().EqualValues(cwResp.CPF.String(), cwResp.CPF.String()) - // s.Assert().EqualValues(cwResp.EstimatedNextCPF.String(), cwResp.EstimatedNextCPF.String()) - }) - } -} - -func (s *TestSuitePerpQuerier) TestAllMarkets() { - type CwMarketMap map[asset.Pair]bindings.Market - - marketMap := make(CwMarketMap) - for pair, ammMarket := range genesis.START_MARKETS { - cwMarket := bindings.NewMarket( - ammMarket.Market, - ammMarket.Amm, - "", - "", - s.ctx.BlockHeight(), - ) - marketMap[pair] = cwMarket - - // Test the ToAppMarket fn - gotAppMarket, err := cwMarket.ToAppMarket() - s.Assert().NoError(err) - s.Assert().EqualValues(ammMarket.Market, gotAppMarket) - } - - testCases := map[string]struct { - marketMap CwMarketMap - expectErr bool - }{ - "happy": { - marketMap: marketMap, - expectErr: false, - }, - } - - for name, testCase := range testCases { - s.T().Run(name, func(t *testing.T) { - cwResp, err := s.queryPlugin.Perp.AllMarkets(s.ctx) - - if testCase.expectErr { - s.Error(err) - return - } - - s.NoErrorf(err, "cwResp: %s", cwResp) - for pair, cwMarketWant := range testCase.marketMap { - cwMarketOut := cwResp.MarketMap[pair.String()] - - jsonWant, err := json.Marshal(cwMarketWant) - s.Assert().NoError(err) - jsonGot, err := json.Marshal(cwMarketOut) - s.Assert().NoError(err) - - s.Assert().EqualValuesf( - cwMarketWant, cwMarketOut, - "\nwant: %s\ngot: %s", jsonWant, jsonGot, - ) - } - }) - } -} - -func (s *TestSuitePerpQuerier) TestMetrics() { - // happy case - for pair := range genesis.START_MARKETS { - cwReq := &bindings.MetricsRequest{Pair: pair.String()} - cwResp, err := s.queryPlugin.Perp.Metrics(s.ctx, cwReq) - s.Error(err, "cwResp: %s", cwResp) - s.Nil(cwResp) - } - - // sad case - cwReq := &bindings.MetricsRequest{Pair: "ftt:ust"} - cwResp, err := s.queryPlugin.Perp.Metrics(s.ctx, cwReq) - s.Errorf(err, "cwResp: %s", cwResp) -} - -func (s *TestSuitePerpQuerier) TestModuleAccounts() { - cwReq := &bindings.ModuleAccountsRequest{} - cwResp, err := s.queryPlugin.Perp.ModuleAccounts(s.ctx, cwReq) - s.NoErrorf(err, "\ncwResp: %s", cwResp) -} - -func (s *TestSuitePerpQuerier) TestModuleParams() { - cwReq := &bindings.PerpParamsRequest{} - cwResp, err := s.queryPlugin.Perp.ModuleParams(s.ctx, cwReq) - s.Errorf(err, "\ncwResp: %s", cwResp) - s.Nil(cwResp) -} - -func (s *TestSuitePerpQuerier) TestPosition() { - trader := s.contractDeployer - pair := genesis.PerpV2Genesis().Markets[0].Pair - margin := sdk.NewInt(1_000_000) - leverage := sdk.NewDec(5) - baseAmtLimit := sdk.ZeroDec() - - s.T().Log("Request should error since the trader hasn't yet opened a position") - cwReq := &bindings.PositionRequest{ - Trader: trader.String(), - Pair: pair.String(), - } - cwResp, err := s.queryPlugin.Perp.Position(s.ctx, cwReq) - s.Errorf(err, "\ncwResp: %s", cwResp) - - s.T().Log("Open a position") - resp, err := s.nibiru.PerpKeeperV2.MarketOrder( - s.ctx, pair, perpv2types.Direction_LONG, - trader, margin, leverage, baseAmtLimit, - ) - s.NoError(err) - - s.T().Log("Successfully query position") - cwResp, err = s.queryPlugin.Perp.Position(s.ctx, cwReq) - s.NoErrorf(err, "\ncwResp: %s", cwResp) - - // Verify that the response marshals to JSON - jsonBz, err := json.Marshal(cwResp) - s.NoErrorf(err, "jsonBz: %s", jsonBz) - // and unmarshals from JSON - freshCwResp := new(bindings.PositionResponse) - err = json.Unmarshal(jsonBz, freshCwResp) - s.NoErrorf(err, "freshCwResp: %s", freshCwResp) - s.Assert().EqualValues(resp.ExchangedNotionalValue, leverage.MulInt(margin)) - s.Assert().EqualValues(cwResp.Position.OpenNotional, leverage.MulInt(margin)) - s.Assert().EqualValues(cwResp.Position.Margin, sdk.NewDecFromInt(margin)) - - s.T().Log("fail due to invalid pair") - cwReq = &bindings.PositionRequest{ - Trader: trader.String(), - Pair: "ftt:ust:xyz", - } - cwResp, err = s.queryPlugin.Perp.Position(s.ctx, cwReq) - s.Errorf(err, "\ncwResp: %s", cwResp) - - s.T().Log("test multiple positions query") - positionResponses := []bindings.PositionResponse{*freshCwResp} - s.DoPositionsTest(trader, positionResponses) -} - -func (s *TestSuitePerpQuerier) DoPositionsTest( - trader sdk.AccAddress, responses []bindings.PositionResponse, -) { - s.T().Log("test multiple positions query") - cwReq := &bindings.PositionsRequest{ - Trader: trader.String(), - } - cwResp, err := s.queryPlugin.Perp.Positions(s.ctx, cwReq) - s.NoErrorf(err, "\ncwResp: %s", cwResp) - - for _, resp := range responses { - pair := resp.Position.Pair - pos := cwResp.Positions[pair] - s.Assert().EqualValues(resp.Position, pos) - } -} diff --git a/wasmbinding/querier_test.go b/wasmbinding/querier_test.go deleted file mode 100644 index a5763c9b8..000000000 --- a/wasmbinding/querier_test.go +++ /dev/null @@ -1,353 +0,0 @@ -package wasmbinding_test - -import ( - "encoding/json" - "testing" - "time" - - sdkerrors "cosmossdk.io/errors" - sdkmath "cosmossdk.io/math" - - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/suite" - - "github.com/NibiruChain/nibiru/app" - "github.com/NibiruChain/nibiru/wasmbinding/bindings" - "github.com/NibiruChain/nibiru/wasmbinding/wasmbin" - "github.com/NibiruChain/nibiru/x/common" - "github.com/NibiruChain/nibiru/x/common/asset" - "github.com/NibiruChain/nibiru/x/common/denoms" - "github.com/NibiruChain/nibiru/x/common/testutil" - "github.com/NibiruChain/nibiru/x/common/testutil/genesis" - "github.com/NibiruChain/nibiru/x/common/testutil/testapp" -) - -func TestSuiteQuerier_RunAll(t *testing.T) { - suite.Run(t, new(TestSuiteQuerier)) -} - -func DoCustomBindingQuery( - ctx sdk.Context, - nibiru *app.NibiruApp, - contract sdk.AccAddress, - bindingRequest bindings.BindingQuery, - responsePointer interface{}, -) (contractRespBz []byte, err error) { - // Parse query type compatible with wasm vm - reqJsonBz, err := json.Marshal(bindingRequest) - if err != nil { - return contractRespBz, err - } - - // Query the smart contract - var originalError error - if err := common.TryCatch(func() { - // The WasmVM tends to panic pretty easily with "Wasmer runtimer error". - // TryCatch here makes it more safe and easy to debug. - bz, err := nibiru.WasmKeeper.QuerySmart( - ctx, contract, reqJsonBz, - ) - if err != nil { - originalError = err - } else { - contractRespBz = bz - } - })(); err != nil { - return contractRespBz, sdkerrors.Wrapf( - err, "contractRespBz: %s", contractRespBz) - } - - // originalError: the error raised if the WasmVM doesn't panic - if originalError != nil { - return contractRespBz, originalError - } - - // Parse the response data into the response pointer - err = json.Unmarshal(contractRespBz, responsePointer) - if err != nil { - return contractRespBz, sdkerrors.Wrapf( - err, "responsePointer: %s", responsePointer) - } - - return contractRespBz, nil -} - -type TestSuiteQuerier struct { - suite.Suite - - nibiru *app.NibiruApp - ctx sdk.Context - contractDeployer sdk.AccAddress - - contractPerp sdk.AccAddress - fields ExampleFields -} - -type ExampleFields struct { - Pair string - Trader sdk.AccAddress - Dec sdk.Dec - Int sdkmath.Int - Market bindings.Market -} - -func GetHappyFields() ExampleFields { - fields := ExampleFields{ - Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD).String(), - Trader: sdk.AccAddress([]byte("trader")), - Dec: sdk.NewDec(50), - Int: sdk.NewInt(420), - } - - fields.Market = bindings.Market{ - Pair: fields.Pair, - BaseReserve: fields.Dec, - QuoteReserve: fields.Dec, - SqrtDepth: fields.Dec, - Depth: fields.Int, - TotalLong: fields.Dec, - TotalShort: fields.Dec, - PegMult: fields.Dec, - Config: &bindings.MarketConfig{ - MaintenanceMarginRatio: fields.Dec, - MaxLeverage: fields.Dec, - }, - MarkPrice: fields.Dec, - IndexPrice: fields.Dec.String(), - TwapMark: fields.Dec.String(), - BlockNumber: sdk.NewInt(100), - } - return fields -} - -func (s *TestSuiteQuerier) SetupSuite() { - s.fields = GetHappyFields() - sender := testutil.AccAddress() - s.contractDeployer = sender - - genesisState := SetupPerpGenesis() - nibiru := testapp.NewNibiruTestApp(genesisState) - ctx := nibiru.NewContext(false, tmproto.Header{ - Height: 1, - ChainID: "nibiru-wasmnet-1", - Time: time.Now().UTC(), - }) - coins := sdk.NewCoins( - sdk.NewCoin(denoms.NIBI, sdk.NewInt(1_000)), - sdk.NewCoin(denoms.NUSD, sdk.NewInt(420)), - ) - s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins)) - - nibiru, ctx = SetupAllContracts(s.T(), sender, nibiru, ctx) - s.nibiru = nibiru - s.ctx = ctx - - s.contractPerp = ContractMap[wasmbin.WasmKeyPerpBinding] - s.OnSetupEnd() -} - -func (s *TestSuiteQuerier) OnSetupEnd() { - SetExchangeRates(&s.Suite, s.nibiru, s.ctx) -} - -func (s *TestSuiteQuerier) TestQueryReserves() { - testCases := map[string]struct { - pairStr string - wasmError bool - }{ - "happy": {pairStr: s.fields.Pair, wasmError: false}, - "sad - non existent pair": {pairStr: "ftt:ust", wasmError: true}, - } - - for name, testCase := range testCases { - s.T().Run(name, func(t *testing.T) { - pairStr := testCase.pairStr - bindingQuery := bindings.BindingQuery{ - Reserves: &bindings.ReservesRequest{Pair: pairStr}, - } - bindingResp := new(bindings.ReservesResponse) - - if testCase.wasmError { - _, err := DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, - bindingQuery, bindingResp, - ) - s.Assert().Contains(err.Error(), "query wasm contract failed") - return - } - - _, err := DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - s.Require().NoError(err) - - wantPair := asset.MustNewPair(pairStr) - s.Assert().EqualValues(bindingResp.Pair, wantPair) - s.Assert().EqualValues( - bindingResp.BaseReserve.String(), - genesis.START_MARKETS[wantPair].Amm.BaseReserve.String()) - s.Assert().EqualValues( - bindingResp.QuoteReserve.String(), - genesis.START_MARKETS[wantPair].Amm.QuoteReserve.String()) - }) - } -} - -// Integration test for BindingQuery::AllMarkets against real contract -func (s *TestSuiteQuerier) TestQueryAllMarkets() { - bindingQuery := bindings.BindingQuery{ - AllMarkets: &bindings.AllMarketsRequest{}, - } - bindingResp := new(bindings.AllMarketsResponse) - - respBz, err := DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - s.Require().NoErrorf(err, "resp bytes: %s", respBz) - - for pair, marketAmm := range genesis.START_MARKETS { - cwMarket := bindingResp.MarketMap[pair.String()] - s.Assert().EqualValues(marketAmm.Amm.BaseReserve, cwMarket.BaseReserve) - s.Assert().EqualValues(marketAmm.Amm.QuoteReserve, cwMarket.QuoteReserve) - s.Assert().EqualValues(marketAmm.Amm.SqrtDepth, cwMarket.SqrtDepth) - s.Assert().EqualValues(marketAmm.Amm.TotalLong, cwMarket.TotalLong) - s.Assert().EqualValues(marketAmm.Amm.TotalShort, cwMarket.TotalShort) - s.Assert().EqualValues(marketAmm.Amm.PriceMultiplier.String(), cwMarket.PegMult.String()) - s.Assert().EqualValues(marketAmm.Amm.InstMarkPrice().String(), cwMarket.MarkPrice.String()) - s.Assert().EqualValues(s.ctx.BlockHeight(), cwMarket.BlockNumber.Int64()) - } -} - -// Integration test for BindingQuery::AllMarkets against real contract -func (s *TestSuiteQuerier) TestQueryExchangeRate() { - bindingQuery := bindings.BindingQuery{ - OraclePrices: &bindings.OraclePrices{}, - } - bindingResp := new(bindings.OraclePricesResponse) - respBz, err := DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - priceMap := *bindingResp - s.Require().NoErrorf(err, "resp bytes: %s", respBz) - s.Assert().EqualValues(sdk.NewDec(1000).String(), priceMap["ueth:unusd"].String()) -} - -func (s *TestSuiteQuerier) TestQueryBasePrice() { - cwReq := &bindings.BasePriceRequest{ - Pair: s.fields.Pair, - IsLong: true, - BaseAmount: sdk.NewInt(69_420), - } - bindingQuery := bindings.BindingQuery{ - BasePrice: cwReq, - } - bindingResp := new(bindings.BasePriceResponse) - - var respBz []byte - var err error - err = common.TryCatch(func() { - respBz, err = DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - s.Require().Errorf(err, "expect error since query is not implemented: resp bytes: %s", respBz) - s.Require().Contains(err.Error(), "Wasmer runtime error") - })() - - // s.Require().NoErrorf(err, "resp bytes: %s", respBz) - // s.Assert().EqualValues(cwReq.Pair, bindingResp.Pair) - // s.Assert().EqualValues(cwReq.IsLong, bindingResp.IsLong) - // s.Assert().EqualValues(cwReq.BaseAmount.String(), bindingResp.BaseAmount.String()) - // s.Assert().True(bindingResp.QuoteAmount.GT(sdk.ZeroDec())) - // - // cwReqBz, err := json.Marshal(cwReq) - // s.T().Logf("cwReq: %s", cwReqBz) - // s.NoError(err) - // - // cwRespBz, err := json.Marshal(bindingResp) - // s.T().Logf("cwResp: %s", cwRespBz) - // s.NoError(err) -} - -func (s *TestSuiteQuerier) TestQueryPremiumFraction() { - cwReq := &bindings.PremiumFractionRequest{ - Pair: s.fields.Pair, - } - - bindingQuery := bindings.BindingQuery{ - PremiumFraction: cwReq, - } - bindingResp := new(bindings.PremiumFractionResponse) - - var respBz []byte - var err error - err = common.TryCatch(func() { - respBz, err = DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - s.Require().Errorf(err, "expect error since query is not implemented: resp bytes: %s", respBz) - s.Require().Contains(err.Error(), "Querier contract error") - })() - - // respBz, err := DoCustomBindingQuery( - // s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - // ) - // s.Require().NoErrorf(err, "resp bytes: %s", respBz) - - // s.Assert().EqualValues(cwReq.Pair, bindingResp.Pair) - // s.Assert().Truef( - // - // !bindingResp.CPF.IsNegative(), - // "cpf: %s", - // bindingResp.CPF) - // - // s.Assert().Truef( - // - // !bindingResp.EstimatedNextCPF.IsNegative(), - // "estimated_next_cpf: %s", - // bindingResp.EstimatedNextCPF) -} - -// func (s *TestSuiteQuerier) TestQueryMetrics() { -// cwReq := &bindings.MetricsRequest{ -// Pair: s.fields.Pair, -// } - -// bindingQuery := bindings.BindingQuery{ -// Metrics: cwReq, -// } -// bindingResp := new(bindings.MetricsResponse) - -// respBz, err := DoCustomBindingQuery( -// s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, -// ) -// s.Require().NoErrorf(err, "resp bytes: %s", respBz) -// } - -// func (s *TestSuiteQuerier) TestQueryPerpParams() { -// cwReq := &bindings.PerpParamsRequest{} - -// bindingQuery := bindings.BindingQuery{ -// PerpParams: cwReq, -// } -// bindingResp := new(bindings.PerpParamsResponse) - -// respBz, err := DoCustomBindingQuery( -// s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, -// ) -// s.Require().NoErrorf(err, "resp bytes: %s", respBz) -// } - -func (s *TestSuiteQuerier) TestQueryPerpModuleAccounts() { - cwReq := &bindings.ModuleAccountsRequest{} - - bindingQuery := bindings.BindingQuery{ - ModuleAccounts: cwReq, - } - bindingResp := new(bindings.ModuleAccountsResponse) - - respBz, err := DoCustomBindingQuery( - s.ctx, s.nibiru, s.contractPerp, bindingQuery, bindingResp, - ) - s.Require().NoErrorf(err, "resp bytes: %s", respBz) -} diff --git a/wasmbinding/wasm.go b/wasmbinding/wasm.go index baee43c27..972e5067f 100644 --- a/wasmbinding/wasm.go +++ b/wasmbinding/wasm.go @@ -20,9 +20,7 @@ func NibiruWasmOptions( sudoKeeper keeper.Keeper, oracleKeeper oraclekeeper.Keeper, ) []wasmkeeper.Option { - wasmQueryPlugin := NewQueryPlugin(perpv2, oracleKeeper) wasmQueryOption := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{ - Custom: CustomQuerier(wasmQueryPlugin), Stargate: wasmkeeper.AcceptListStargateQuerier( WasmAcceptedStargateQueries(), grpcQueryRouter, From e75f1ece38ce19cc4d94c6c22a5b918c0d9444d6 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 02:33:11 -0600 Subject: [PATCH 17/21] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19ca82776..01ca7a521 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### State Machine Breaking * [#1682](https://github.com/NibiruChain/nibiru/pull/1682) - feat: add upgrade handler for v1.1.0 +* [#xxx](https://github.com/NibiruChain/nibiru/pull/xxx) - chore(wasmbinding): delete CustomQuerier since we have QueryRequest::Stargate now ### Dependencies - Bump `cosmossdk.io/math` from 1.1.2 to 1.2.0 ([#1676](https://github.com/NibiruChain/nibiru/pull/1676)) From b52ff665c8fd3eca43e5b01eefd6088ae226bc70 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 02:37:09 -0600 Subject: [PATCH 18/21] change log --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01ca7a521..c7799e154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### State Machine Breaking * [#1682](https://github.com/NibiruChain/nibiru/pull/1682) - feat: add upgrade handler for v1.1.0 -* [#xxx](https://github.com/NibiruChain/nibiru/pull/xxx) - chore(wasmbinding): delete CustomQuerier since we have QueryRequest::Stargate now +* [#1687](https://github.com/NibiruChain/nibiru/pull/1687) - chore(wasmbinding): delete CustomQuerier since we have QueryRequest::Stargate now ### Dependencies - Bump `cosmossdk.io/math` from 1.1.2 to 1.2.0 ([#1676](https://github.com/NibiruChain/nibiru/pull/1676)) From 7de93412f9e899f791db33a40050faadbc6baa90 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Fri, 1 Dec 2023 02:42:18 -0600 Subject: [PATCH 19/21] test: fix no error cases in tests --- x/perp/v2/keeper/msg_server_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/perp/v2/keeper/msg_server_test.go b/x/perp/v2/keeper/msg_server_test.go index 5c0f50bcd..0d357178a 100644 --- a/x/perp/v2/keeper/msg_server_test.go +++ b/x/perp/v2/keeper/msg_server_test.go @@ -453,7 +453,7 @@ func TestAllocateEpochRebates(t *testing.T) { require.ErrorContains(t, err, "nil msg") _, err = msgServer.AllocateEpochRebates(ctx, &types.MsgAllocateEpochRebates{}) - require.ErrorContains(t, err, "empty address string is not allowed") + require.NoError(t, err) _, err = msgServer.AllocateEpochRebates(ctx, &types.MsgAllocateEpochRebates{ Sender: sender, @@ -484,7 +484,7 @@ func TestAllocateEpochRebates(t *testing.T) { require.ErrorContains(t, err, "nil msg") _, err = msgServer.WithdrawEpochRebates(ctx, &types.MsgWithdrawEpochRebates{}) - require.ErrorContains(t, err, "empty address string is not allowed") + require.NoError(t, err) _, err = msgServer.WithdrawEpochRebates(ctx, &types.MsgWithdrawEpochRebates{ Sender: sender, From 5ad427dc82ab2161e68dab0ac76f1876e94bf807 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Sun, 3 Dec 2023 16:47:42 -0600 Subject: [PATCH 20/21] changelog + address PR comments --- CHANGELOG.md | 1 + x/perp/v2/keeper/amm.go | 4 +++- x/perp/v2/types/msgs_test.go | 18 ++++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbad0064b..28f679000 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Dapp modules: perp, spot, etc. +* [#1687](https://github.com/NibiruChain/nibiru/pull/1687) - chore(wasmbinding): delete CustomQuerier since we have QueryRequest::Stargate now * [#1686](https://github.com/NibiruChain/nibiru/pull/1686) - test(perp): add more tests for perp module msg server for DnR * [#1683](https://github.com/NibiruChain/nibiru/pull/1683) - feat(perp): Add `StartDnREpoch` to `AfterEpochEnd` hook * [#1680](https://github.com/NibiruChain/nibiru/pull/1680) - feat(perp): MsgShiftPegMultiplier, MsgShiftSwapInvariant. diff --git a/x/perp/v2/keeper/amm.go b/x/perp/v2/keeper/amm.go index 7fc741f6b..ae317d7f0 100644 --- a/x/perp/v2/keeper/amm.go +++ b/x/perp/v2/keeper/amm.go @@ -19,7 +19,9 @@ func (k Keeper) handleMarketUpdateCost( return costPaid, err } - if costAmt.IsPositive() { + if costAmt.IsZero() { + costPaid = sdk.NewCoin(collateral, costAmt) + } else if costAmt.IsPositive() { // Positive cost, send from perp EF to vault cost := sdk.NewCoins( sdk.NewCoin(collateral, costAmt), diff --git a/x/perp/v2/types/msgs_test.go b/x/perp/v2/types/msgs_test.go index ae92f4a67..e57c074b3 100644 --- a/x/perp/v2/types/msgs_test.go +++ b/x/perp/v2/types/msgs_test.go @@ -377,7 +377,7 @@ func TestMsgValidateBasic(t *testing.T) { msg: &MsgShiftPegMultiplier{ Sender: validSender, Pair: asset.Pair("not_a_pair"), - NewPegMult: sdk.NewDec(420), + NewPegMult: sdk.NewDec(420), // valid positive value, unused }, expectErr: true, expectedError: asset.ErrInvalidTokenPair.Error(), @@ -387,7 +387,7 @@ func TestMsgValidateBasic(t *testing.T) { msg: &MsgShiftPegMultiplier{ Sender: validSender, Pair: asset.Pair("valid:pair"), - NewPegMult: sdk.NewDec(-420), + NewPegMult: sdk.NewDec(-420), // invalid nonpositive }, expectErr: true, expectedError: ErrNonPositivePegMultiplier.Error(), @@ -398,7 +398,7 @@ func TestMsgValidateBasic(t *testing.T) { msg: &MsgShiftSwapInvariant{ Sender: validSender, Pair: asset.Pair("not_a_pair"), - NewSwapInvariant: sdk.NewInt(420), + NewSwapInvariant: sdk.NewInt(420), // valid positive }, expectErr: true, expectedError: asset.ErrInvalidTokenPair.Error(), @@ -408,7 +408,17 @@ func TestMsgValidateBasic(t *testing.T) { msg: &MsgShiftSwapInvariant{ Sender: validSender, Pair: asset.Pair("valid:pair"), - NewSwapInvariant: sdk.NewInt(-420), + NewSwapInvariant: sdk.NewInt(-420), // invalid nonpositive + }, + expectErr: true, + expectedError: ErrNonPositiveSwapInvariant.Error(), + }, + { + name: "MsgShiftSwapInvariant: nonpositive swap invariant", + msg: &MsgShiftSwapInvariant{ + Sender: validSender, + Pair: asset.Pair("valid:pair"), + NewSwapInvariant: sdk.ZeroInt(), // invalid nonpositive }, expectErr: true, expectedError: ErrNonPositiveSwapInvariant.Error(), From 84b183171ebf4e0dfb9bd7416fc021873c4227bd Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Sun, 3 Dec 2023 17:18:01 -0600 Subject: [PATCH 21/21] ci(codecov.yml): handle removed code coverage relative to bse --- codecov.yml => .github/codecov.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) rename codecov.yml => .github/codecov.yml (69%) diff --git a/codecov.yml b/.github/codecov.yml similarity index 69% rename from codecov.yml rename to .github/codecov.yml index 45b3a6429..73f60319f 100644 --- a/codecov.yml +++ b/.github/codecov.yml @@ -14,14 +14,19 @@ parsers: method: no conditional: yes coverage: - range: 40..100 + range: 40..92 round: down precision: 2 + # status: See https://docs.codecov.com/docs/commit-status status: project: default: if_not_found: success if_ci_failed: error + target: auto + threshold: 1% # Allow coverage to drop by X%, posting a success status. + # removed_code_behavior: Takes values [off, removals_only, adjust_base] + removed_code_behavior: adjust_base comment: # this is a top-level key layout: " diff, flags, files"