Skip to content

Commit

Permalink
feat(perp): MsgShiftPegMultiplier, MsgShiftSwapInvariant (#1680)
Browse files Browse the repository at this point in the history
* 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](cosmos/cosmos-sdk@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] <[email protected]>

* feat(perp): msg server methods for shift peg and shift swap invariant

* (wasmbinding): remove pegshitft and depth shift

* test(perp): msg tests

* change log

* Update x/perp/v2/types/msgs.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* PR comments changes

* add events for logging purposes

* refactor: consistent error name usage PR comment

* refactor: more PR comments

* Address Gimeno PR comments + more tests

* fix strange address name conflicts

* test: fix all tests

* test: more calls of EnsureNibiruPrefix

* Updated changelog - dependabot

* test: fix no error cases in tests

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Unique-Divine <[email protected]>
  • Loading branch information
4 people authored Dec 3, 2023
1 parent 33b27f5 commit 674cace
Show file tree
Hide file tree
Showing 42 changed files with 2,303 additions and 591 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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
1 change: 1 addition & 0 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// init changes the value of 'DefaultTestingAppInit' to use custom initialization.
func init() {
ibctesting.DefaultTestingAppInit = SetupNibiruTestingApp
testapp.EnsureNibiruPrefix()
}

/*
Expand Down
22 changes: 11 additions & 11 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -326,7 +327,7 @@ func (app *NibiruApp) InitKeepers(
appCodec,
homePath,
app.BaseApp,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
govModuleAddr,
)

// register the staking hooks
Expand All @@ -336,7 +337,7 @@ func (app *NibiruApp) InitKeepers(
legacyAmino,
keys[slashingtypes.StoreKey],
app.stakingKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
govModuleAddr,
)

app.stakingKeeper.SetHooks(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -553,7 +553,7 @@ func (app *NibiruApp) InitKeepers(
app.stakingKeeper,
app.MsgServiceRouter(),
govConfig,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
govModuleAddr,
)
govKeeper.SetLegacyRouter(govRouter)

Expand Down
26 changes: 26 additions & 0 deletions proto/nibiru/perp/v2/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,29 @@ message MarketUpdatedEvent {
// the final state of the market
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",
(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 ];
}

// EventShiftSwapInvariant: ABCI event emitted from MsgShiftSwapInvariant
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 ];
}
58 changes: 55 additions & 3 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ 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) {}

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 a market's peg multiplier.
// [Admin] Only callable by sudoers.
rpc ShiftPegMultiplier(MsgShiftPegMultiplier)
returns (MsgShiftPegMultiplierResponse) {}

// ShiftSwapInvariant: gRPC tx msg for changing a market's swap invariant.
// [Admin] Only callable by sudoers.
rpc ShiftSwapInvariant(MsgShiftSwapInvariant)
returns (MsgShiftSwapInvariantResponse) {}
}

// -------------------------- Settle Position --------------------------
Expand Down Expand Up @@ -376,4 +390,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
Loading

0 comments on commit 674cace

Please sign in to comment.