Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(perp): MsgShiftPegMultiplier, MsgShiftSwapInvariant #1680

Merged
merged 21 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
596c664
chore(deps): Bump cosmossdk.io/math from 1.1.2 to 1.2.0
dependabot[bot] Nov 17, 2023
8722fce
feat(perp): msg server methods for shift peg and shift swap invariant
Unique-Divine Nov 28, 2023
e295eb3
(wasmbinding): remove pegshitft and depth shift
Unique-Divine Nov 28, 2023
0232852
test(perp): msg tests
Unique-Divine Nov 28, 2023
1220834
change log
Unique-Divine Nov 28, 2023
696d636
Merge branch 'master' into realu/perp-sg
Unique-Divine Nov 28, 2023
63b72aa
Update x/perp/v2/types/msgs.go
Unique-Divine Nov 28, 2023
b081818
PR comments changes
Unique-Divine Nov 28, 2023
1b5c836
add events for logging purposes
Unique-Divine Nov 28, 2023
2f0a125
refactor: consistent error name usage PR comment
Unique-Divine Nov 28, 2023
552a2d5
refactor: more PR comments
Unique-Divine Nov 28, 2023
fe1fe03
Address Gimeno PR comments + more tests
Unique-Divine Nov 29, 2023
c3ef701
fix strange address name conflicts
Unique-Divine Nov 29, 2023
a2ee8aa
test: fix all tests
Unique-Divine Dec 1, 2023
ad8c56b
test: more calls of EnsureNibiruPrefix
Unique-Divine Dec 1, 2023
3ff816f
Merge branch 'master' into realu/perp-sg
Unique-Divine Dec 1, 2023
f5415fd
Merge branch 'master' into dependabot/go_modules/cosmossdk.io/math-1.2.0
Unique-Divine Dec 1, 2023
bd55994
Updated changelog - dependabot
Unique-Divine Dec 1, 2023
46cd514
Merge branch 'master' into dependabot/go_modules/cosmossdk.io/math-1.2.0
Unique-Divine Dec 1, 2023
05810f3
Merge branch 'dependabot/go_modules/cosmossdk.io/math-1.2.0' into rea…
Unique-Divine Dec 1, 2023
7de9341
test: fix no error cases in tests
Unique-Divine Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 53 additions & 3 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------
Expand Down Expand Up @@ -376,4 +388,42 @@ message MsgWithdrawEpochRebatesResponse {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
}

// -------------------------- 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 {}
12 changes: 0 additions & 12 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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"`
Expand Down
33 changes: 0 additions & 33 deletions wasmbinding/exec_perp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.EditPriceMultiplier(
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.EditSwapInvariant(ctx, pair, cwMsg.DepthMult)
}

func (exec *ExecutorPerp) InsuranceFundWithdraw(
cwMsg *bindings.InsuranceFundWithdraw, ctx sdk.Context,
) (err error) {
Expand Down
31 changes: 0 additions & 31 deletions wasmbinding/exec_perp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down
68 changes: 0 additions & 68 deletions wasmbinding/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
16 changes: 0 additions & 16 deletions wasmbinding/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
3 changes: 1 addition & 2 deletions x/perp/v2/integration/action/dnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/perp/v2/integration/action/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -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
}

Expand Down
30 changes: 30 additions & 0 deletions x/perp/v2/keeper/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
}
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved

// 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,
newSwapInvariant sdk.Dec,
sender sdk.AccAddress,
) error {
if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil {
return err
}
return k.UnsafeShiftSwapInvariant(ctx, pair, newSwapInvariant)
}
Loading
Loading