From fe1fe03cb9cf99369486d7455c06a3d43c5eef25 Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Tue, 28 Nov 2023 19:05:55 -0600 Subject: [PATCH] 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) }