Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/migrate-set-mark…
Browse files Browse the repository at this point in the history
…et-enabled-to-msdserver
  • Loading branch information
jgimeno committed Jan 2, 2024
2 parents db327d9 + c2a96d5 commit eb48c50
Show file tree
Hide file tree
Showing 51 changed files with 1,295 additions and 739 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1573](https://github.com/NibiruChain/nibiru/pull/1573) - feat(perp): Close markets and compute settlement price
* [#1705](https://github.com/NibiruChain/nibiru/pull/1705) - feat(perp): Add oracle pair to market object
* [#1718](https://github.com/NibiruChain/nibiru/pull/1718) - fix: fees does not require additional funds
* [#1734](https://github.com/NibiruChain/nibiru/pull/1734) - feat(perp): MsgDonateToPerpFund sudo call as part of #1642
* [#1755](https://github.com/NibiruChain/nibiru/pull/1755) - feat(oracle): Add more events on validator's performance
* [#1749](https://github.com/NibiruChain/nibiru/pull/1749) - feat(perp): move close market from Wasm Binding to MsgCloseMarket

Expand All @@ -83,11 +84,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1728](https://github.com/NibiruChain/nibiru/pull/1728) - test(devgas-cli): CLI tests for devgas txs
* [#1735](https://github.com/NibiruChain/nibiru/pull/1735) - test(sim): fix simulation tests
* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs
* [#1736](https://github.com/NibiruChain/nibiru/pull/1736) - test(sim): add sim genesis state for all cusom modules

### Dependencies
- Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))
- Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))
- Bump `github.com/cometbft/cometbft-db` from 0.9.0 to 0.9.1 ([#1760](https://github.com/NibiruChain/nibiru/pull/1760))

* Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))
* Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))
* Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720))
* Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724))
* Bump `github.com/holiman/uint256` from 1.2.3 to 1.2.4 ([#1730](https://github.com/NibiruChain/nibiru/pull/1730))
Expand Down
2 changes: 1 addition & 1 deletion app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ func ModuleAccPerms() map[string][]string {

perptypes.ModuleName: {},
perptypes.VaultModuleAccount: {},
perptypes.PerpEFModuleAccount: {},
perptypes.PerpFundModuleAccount: {},
perptypes.FeePoolModuleAccount: {},
perptypes.DNRAllocationModuleAccount: {},
perptypes.DNREscrowModuleAccount: {},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/NibiruChain/collections v0.4.0
github.com/armon/go-metrics v0.4.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.9.0
github.com/cometbft/cometbft-db v0.9.1
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/go-bip39 v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ github.com/coinbase/rosetta-sdk-go v0.7.9 h1:lqllBjMnazTjIqYrOGv8h8jxjg9+hJazIGZ
github.com/coinbase/rosetta-sdk-go v0.7.9/go.mod h1:0/knutI7XGVqXmmH4OQD8OckFrbQ8yMsUZTG7FXCR2M=
github.com/cometbft/cometbft v0.37.2 h1:XB0yyHGT0lwmJlFmM4+rsRnczPlHoAKFX6K8Zgc2/Jc=
github.com/cometbft/cometbft v0.37.2/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/cometbft/cometbft-db v0.9.0 h1:J+7haCZCddpM8DBESe6R7/ScnqigdQKph8V2yucApx8=
github.com/cometbft/cometbft-db v0.9.0/go.mod h1:iliyWaoV0mRwBJoizElCwwRA9Tf7jZJOURcRZF9m60U=
github.com/cometbft/cometbft-db v0.9.1 h1:MIhVX5ja5bXNHF8EYrThkG9F7r9kSfv8BX4LWaxWJ4M=
github.com/cometbft/cometbft-db v0.9.1/go.mod h1:iliyWaoV0mRwBJoizElCwwRA9Tf7jZJOURcRZF9m60U=
github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4=
github.com/confio/ics23/go v0.9.0/go.mod h1:4LPZ2NYqnYIVRklaozjNR1FScgDJ2s5Xrp+e/mYVRak=
github.com/consensys/bavard v0.1.8-0.20210406032232-f3452dc9b572/go.mod h1:Bpd0/3mZuaj6Sj+PqrmIquiOKy397AKGThQPaGzNXAQ=
Expand Down
38 changes: 30 additions & 8 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ service Msg {
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.
// is possible to make an sdk.Coin using it. [SUDO] Only callable by sudoers.
rpc ChangeCollateralDenom(MsgChangeCollateralDenom)
returns (MsgChangeCollateralDenomResponse) {}

Expand All @@ -41,15 +41,20 @@ service Msg {
returns (MsgWithdrawEpochRebatesResponse) {}

// ShiftPegMultiplier: gRPC tx msg for changing a market's peg multiplier.
// [Admin] Only callable by sudoers.
// [SUDO] 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.
// [SUDO] Only callable by sudoers.
rpc ShiftSwapInvariant(MsgShiftSwapInvariant)
returns (MsgShiftSwapInvariantResponse) {}

// WithdrawFromPerpFund: gRPC tx msg to withdraw from the perp fund module
// account. [SUDO] Only callable by sudoers.
rpc WithdrawFromPerpFund(MsgWithdrawFromPerpFund)
returns (MsgWithdrawFromPerpFundResponse) {}

// CloseMarket: gRPC tx msg for closing a market.
// [Admin] Only callable by sudoers.
rpc CloseMarket(MsgCloseMarket) returns (MsgCloseMarketResponse) {}
Expand Down Expand Up @@ -358,7 +363,7 @@ message MsgDonateToEcosystemFundResponse {}
// ----------------------- MsgChangeCollateralDenom -----------------------

// MsgChangeCollateralDenom: Changes the collateral denom for the module.
// [Admin] Only callable by sudoers.
// [SUDO] Only callable by sudoers.
message MsgChangeCollateralDenom {
string sender = 1;
string new_denom = 2;
Expand Down Expand Up @@ -399,8 +404,8 @@ message MsgWithdrawEpochRebatesResponse {

// -------------------------- ShiftPegMultiplier --------------------------

// ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier.
// Admin-only.
// MsgShiftPegMultiplier: gRPC tx msg for changing the peg multiplier.
// [SUDO] Only callable sudoers.
message MsgShiftPegMultiplier {
string sender = 1;
string pair = 2 [
Expand All @@ -418,8 +423,8 @@ message MsgShiftPegMultiplierResponse {}

// -------------------------- ShiftSwapInvariant --------------------------

// ShiftSwapInvariant: gRPC tx msg for changing the swap invariant.
// Admin-only.
// MsgShiftSwapInvariant: gRPC tx msg for changing the swap invariant.
// [SUDO] Only callable sudoers.
message MsgShiftSwapInvariant {
string sender = 1;
string pair = 2 [
Expand All @@ -435,6 +440,23 @@ message MsgShiftSwapInvariant {

message MsgShiftSwapInvariantResponse {}

// -------------------------- WithdrawFromPerpFund --------------------------

// MsgWithdrawFromPerpFund: gRPC tx msg for changing the swap invariant.
// [SUDO] Only callable sudoers.
message MsgWithdrawFromPerpFund {
string sender = 1;
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// Optional denom in case withdrawing assets aside from NUSD.
string denom = 3;
string to_addr = 4;
}

message MsgWithdrawFromPerpFundResponse {}

// -------------------------- CloseMarket --------------------------

// CloseMarket: gRPC tx msg for closing a market.
Expand Down
21 changes: 0 additions & 21 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@ import (
type NibiruMsg struct {
// bindings-perp ExecuteMsg enum types
// MultiLiquidate *MultiLiquidate `json:"multi_liquidate,omitempty"` // TODO
DonateToInsuranceFund *DonateToInsuranceFund `json:"donate_to_insurance_fund,omitempty"` // TODO
InsuranceFundWithdraw *InsuranceFundWithdraw `json:"insurance_fund_withdraw,omitempty"`
CreateMarket *CreateMarket `json:"create_market,omitempty"`

EditOracleParams *EditOracleParams `json:"edit_oracle_params,omitempty"`

// Short for "no operation". A wasm binding payload that does nothing.
NoOp *NoOp `json:"no_op,omitempty"`
}

type DonateToInsuranceFund struct {
Sender string `json:"sender"`
Donation sdk.Coin `json:"donation"`
}

type EditOracleParams struct {
VotePeriod *sdkmath.Int `json:"vote_period,omitempty"`
VoteThreshold *sdk.Dec `json:"vote_threshold,omitempty"`
Expand All @@ -42,18 +33,6 @@ type EditOracleParams struct {
ValidatorFeeRatio *sdk.Dec `json:"validator_fee_ratio,omitempty"`
}

type InsuranceFundWithdraw struct {
Amount sdkmath.Int `json:"amount"`
To string `json:"to"`
}

type CreateMarket struct {
Pair string `json:"pair"`
PegMult sdk.Dec `json:"peg_mult,omitempty"`
SqrtDepth sdk.Dec `json:"sqrt_depth,omitempty"`
MarketParams *MarketParams `json:"market_params,omitempty"`
}

type MarketParams struct {
Pair string
Enabled bool `json:"enabled,omitempty"`
Expand Down
69 changes: 0 additions & 69 deletions wasmbinding/exec_perp.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package wasmbinding

import (
"time"

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"
perpv2keeper "github.com/NibiruChain/nibiru/x/perp/v2/keeper"
perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types"
)
Expand All @@ -19,65 +12,3 @@ type ExecutorPerp struct {
func (exec *ExecutorPerp) MsgServer() perpv2types.MsgServer {
return perpv2keeper.NewMsgServerImpl(exec.PerpV2)
}

func (exec *ExecutorPerp) InsuranceFundWithdraw(
cwMsg *bindings.InsuranceFundWithdraw, ctx sdk.Context,
) (err error) {
if cwMsg == nil {
return wasmvmtypes.InvalidRequest{Err: "null msg"}
}

to, err := sdk.AccAddressFromBech32(cwMsg.To)
if err != nil {
return err
}

return exec.PerpV2.Admin.WithdrawFromInsuranceFund(
ctx,
cwMsg.Amount,
to,
)
}

func (exec *ExecutorPerp) CreateMarket(
cwMsg *bindings.CreateMarket, 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
}

var market perpv2types.Market
if cwMsg.MarketParams == nil {
market = perpv2types.DefaultMarket(pair)
} else {
mp := cwMsg.MarketParams
market = perpv2types.Market{
Pair: pair,
Enabled: true,
MaintenanceMarginRatio: mp.MaintenanceMarginRatio,
MaxLeverage: mp.MaxLeverage,
LatestCumulativePremiumFraction: mp.LatestCumulativePremiumFraction,
ExchangeFeeRatio: mp.ExchangeFeeRatio,
EcosystemFundFeeRatio: mp.EcosystemFundFeeRatio,
LiquidationFeeRatio: mp.LiquidationFeeRatio,
PartialLiquidationRatio: mp.PartialLiquidationRatio,
FundingRateEpochId: mp.FundingRateEpochId,
MaxFundingRate: mp.MaxFundingRate,
TwapLookbackWindow: time.Duration(mp.TwapLookbackWindow.Int64()),
PrepaidBadDebt: sdk.NewCoin(pair.QuoteDenom(), sdk.ZeroInt()),
OraclePair: asset.MustNewPair(mp.OraclePair),
}
}

return exec.PerpV2.Admin.CreateMarket(ctx, perpv2keeper.ArgsCreateMarket{
Pair: pair,
PriceMultiplier: cwMsg.PegMult,
SqrtDepth: cwMsg.SqrtDepth,
Market: &market,
})
}
91 changes: 0 additions & 91 deletions wasmbinding/exec_perp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"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"
Expand Down Expand Up @@ -130,93 +129,3 @@ func (s *TestSuitePerpExecutor) OnSetupEnd() {
s.contractPerp = ContractMap[wasmbin.WasmKeyPerpBinding]
s.ratesMap = SetExchangeRates(&s.Suite, s.nibiru, s.ctx)
}

// Happy path coverage
func (s *TestSuitePerpExecutor) TestPerpExecutorHappy() {
for _, err := range []error{
s.DoInsuranceFundWithdrawTest(sdk.NewInt(69), s.contractDeployer),
s.DoCreateMarketTest(asset.MustNewPair("ufoo:ubar")),
s.DoCreateMarketTestWithParams(asset.MustNewPair("ufoo2:ubar")),
} {
s.NoError(err)
}
}

func (s *TestSuitePerpExecutor) DoInsuranceFundWithdrawTest(
amt sdkmath.Int, to sdk.AccAddress,
) error {
cwMsg := &bindings.InsuranceFundWithdraw{
Amount: amt,
To: to.String(),
}

err := testapp.FundModuleAccount(
s.nibiru.BankKeeper,
s.ctx,
perpv2types.PerpEFModuleAccount,
sdk.NewCoins(sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(420))),
)
s.NoError(err)

return s.exec.InsuranceFundWithdraw(cwMsg, s.ctx)
}

func (s *TestSuitePerpExecutor) DoCreateMarketTest(pair asset.Pair) error {
cwMsg := &bindings.CreateMarket{
Pair: pair.String(),
PegMult: sdk.NewDec(2_500),
SqrtDepth: sdk.NewDec(1_000),
MarketParams: nil,
}

return s.exec.CreateMarket(cwMsg, s.ctx)
}

func (s *TestSuitePerpExecutor) DoCreateMarketTestWithParams(pair asset.Pair) error {
cwMsg := &bindings.CreateMarket{
Pair: pair.String(),
PegMult: sdk.NewDec(2_500),
SqrtDepth: sdk.NewDec(1_000),
MarketParams: &bindings.MarketParams{
Pair: pair.String(),
Enabled: true,
MaintenanceMarginRatio: sdk.OneDec(),
MaxLeverage: sdk.OneDec(),
LatestCumulativePremiumFraction: sdk.OneDec(),
ExchangeFeeRatio: sdk.OneDec(),
EcosystemFundFeeRatio: sdk.OneDec(),
LiquidationFeeRatio: sdk.OneDec(),
PartialLiquidationRatio: sdk.OneDec(),
FundingRateEpochId: "hi",
MaxFundingRate: sdk.OneDec(),
TwapLookbackWindow: sdk.OneInt(),
OraclePair: pair.String(),
},
}

return s.exec.CreateMarket(cwMsg, s.ctx)
}

func (s *TestSuitePerpExecutor) TestSadPaths_Nil() {
err := s.exec.InsuranceFundWithdraw(nil, s.ctx)
s.Error(err)
}

func (s *TestSuitePerpExecutor) TestSadPath_InsuranceFundWithdraw() {
fundsToWithdraw := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69_000))

err := s.DoInsuranceFundWithdrawTest(fundsToWithdraw.Amount, s.contractDeployer)
s.Error(err)
}

func (s *TestSuitePerpExecutor) TestSadPaths_InvalidPair() {
sadPair := asset.Pair("ftt:ust:doge")
pair := sadPair

for _, err := range []error{
s.DoCreateMarketTest(pair),
s.DoCreateMarketTestWithParams(pair),
} {
s.Error(err)
}
}
Loading

0 comments on commit eb48c50

Please sign in to comment.