Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit e43672a
Author: Unique-Divine <[email protected]>
Date:   Wed Dec 27 23:27:28 2023 -0600

    test assertiong

commit 0090d2f
Author: Unique-Divine <[email protected]>
Date:   Wed Dec 27 23:17:30 2023 -0600

    linter

commit 5121341
Merge: 596a840 68bddeb
Author: Unique-Divine <[email protected]>
Date:   Wed Dec 27 23:12:10 2023 -0600

    Merge branch 'main' into realu/sg-perp

commit 596a840
Author: Unique-Divine <[email protected]>
Date:   Wed Dec 27 23:10:15 2023 -0600

    changelog

commit 240dffe
Author: Unique-Divine <[email protected]>
Date:   Wed Dec 27 23:07:46 2023 -0600

    feat(perp): MsgDonateToPerpFund
  • Loading branch information
Unique-Divine committed Dec 28, 2023
1 parent 68bddeb commit 42e4214
Show file tree
Hide file tree
Showing 32 changed files with 873 additions and 365 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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

### Non-breaking/Compatible Improvements

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
21 changes: 21 additions & 0 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ service Msg {
// [Admin] Only callable by sudoers.
rpc ShiftSwapInvariant(MsgShiftSwapInvariant)
returns (MsgShiftSwapInvariantResponse) {}

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

// -------------------------- Settle Position --------------------------
Expand Down Expand Up @@ -429,3 +434,19 @@ message MsgShiftSwapInvariant {
}

message MsgShiftSwapInvariantResponse {}

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

// WithdrawFromPerpFund: gRPC tx msg for changing the swap invariant.
// Admin-only.
message MsgWithdrawFromPerpFund {
string sender = 1;
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string denom = 3;
string to_addr = 4;
}

message MsgWithdrawFromPerpFundResponse {}
16 changes: 2 additions & 14 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,15 @@ 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"`
SetMarketEnabled *SetMarketEnabled `json:"set_market_enabled,omitempty"`
CreateMarket *CreateMarket `json:"create_market,omitempty"`
SetMarketEnabled *SetMarketEnabled `json:"set_market_enabled,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 @@ -43,11 +36,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 SetMarketEnabled struct {
Pair string `json:"pair"`
Enabled bool `json:"enabled"`
Expand Down
19 changes: 0 additions & 19 deletions wasmbinding/exec_perp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@ 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,
)
}

// TODO: rename to CloseMarket
func (exec *ExecutorPerp) SetMarketEnabled(
cwMsg *bindings.SetMarketEnabled, ctx sdk.Context,
Expand Down
32 changes: 0 additions & 32 deletions wasmbinding/exec_perp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,13 @@ func (s *TestSuitePerpExecutor) OnSetupEnd() {
// 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(),
Expand Down Expand Up @@ -197,11 +177,6 @@ func (s *TestSuitePerpExecutor) DoCreateMarketTestWithParams(pair asset.Pair) er
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) DoSetMarketEnabledTest(
pair asset.Pair, enabled bool,
) error {
Expand All @@ -220,13 +195,6 @@ func (s *TestSuitePerpExecutor) DoSetMarketEnabledTest(
return 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
Expand Down
49 changes: 0 additions & 49 deletions wasmbinding/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,55 +313,6 @@ func (s *TestSuiteExecutor) TestNoOp() {
s.NoErrorf(err, "contractRespBz: %s", contractRespBz)
}

func (s *TestSuiteExecutor) TestInsuranceFundWithdraw() {
admin := s.contractDeployer.String()
amtToWithdraw := sdk.NewInt(69)
execMsg := bindings.NibiruMsg{
InsuranceFundWithdraw: &bindings.InsuranceFundWithdraw{
Amount: amtToWithdraw,
To: admin,
},
}

s.T().Log("Executing should fail since the IF doesn't have funds")
contract := s.contractController
s.keeper.SetSudoContracts(
[]string{contract.String()}, s.ctx,
)
contractRespBz, err := s.ExecuteAgainstContract(contract, execMsg)
s.Errorf(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 should work when the IF has funds")
err = testapp.FundModuleAccount(
s.nibiru.BankKeeper,
s.ctx,
perpv2types.PerpEFModuleAccount,
sdk.NewCoins(sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(420))),
)
s.NoError(err)
s.keeper.SetSudoContracts(
[]string{contract.String()}, s.ctx,
)
contractRespBz, err = s.ExecuteAgainstContract(contract, execMsg)
s.NoErrorf(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) TestSetMarketEnabled() {
// admin := s.contractDeployer.String()
perpv2Genesis := genesis.PerpV2Genesis()
Expand Down
7 changes: 0 additions & 7 deletions wasmbinding/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ func (messenger *CustomMessenger) DispatchMsg(
err = messenger.Perp.CreateMarket(cwMsg, ctx)
return events, data, err

case contractExecuteMsg.ExecuteMsg.InsuranceFundWithdraw != nil:
if err := messenger.Sudo.CheckPermissions(contractAddr, ctx); err != nil {
return events, data, err
}
cwMsg := contractExecuteMsg.ExecuteMsg.InsuranceFundWithdraw
err = messenger.Perp.InsuranceFundWithdraw(cwMsg, ctx)
return events, data, err
case contractExecuteMsg.ExecuteMsg.SetMarketEnabled != nil:
if err := messenger.Sudo.CheckPermissions(contractAddr, ctx); err != nil {
return events, data, err
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ uint64)
}

if !totalRemainder.IsZero() {
err = h.bankKeeper.SendCoinsFromModuleToModule(ctx, perptypes.FeePoolModuleAccount, perptypes.PerpEFModuleAccount, totalRemainder)
err = h.bankKeeper.SendCoinsFromModuleToModule(ctx, perptypes.FeePoolModuleAccount, perptypes.PerpFundModuleAccount, totalRemainder)
if err != nil {
h.k.Logger(ctx).Error("Failed to send coins to perp ef module", "err", err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestHooks_AfterEpochEnd(t *testing.T) {
balances := app.BankKeeper.GetAllBalances(ctx, account.GetAddress())
assert.Equal(t, tt.expectedOracleBalances, balances)

account = app.AccountKeeper.GetModuleAccount(ctx, perptypes.PerpEFModuleAccount)
account = app.AccountKeeper.GetModuleAccount(ctx, perptypes.PerpFundModuleAccount)
balances = app.BankKeeper.GetAllBalances(ctx, account.GetAddress())
assert.Equal(t, tt.expectedEFBalances, balances)
})
Expand Down
5 changes: 4 additions & 1 deletion x/perp/v2/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,10 @@ func (s *IntegrationTestSuite) TestDonateToEcosystemFund() {
resp,
),
)
s.Require().EqualValues(sdk.NewInt64Coin(types.TestingCollateralDenomNUSD, 100), *resp)
s.Require().EqualValues(
sdk.NewInt64Coin(types.TestingCollateralDenomNUSD, 100).String(),
resp.String(),
)
}

func (s *IntegrationTestSuite) TestQueryModuleAccount() {
Expand Down
40 changes: 24 additions & 16 deletions x/perp/v2/keeper/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,42 @@ import (
// function is being used when it's called from the PerpKeeper.Admin struct.
type admin struct{ *Keeper }

/*
WithdrawFromInsuranceFund sends funds from the Insurance Fund to the given "to"
address.
Args:
- ctx: Blockchain context holding the current state
- amount: Amount of micro-NUSD to withdraw.
- to: Recipient address
*/
func (k admin) WithdrawFromInsuranceFund(
ctx sdk.Context, amount sdkmath.Int, to sdk.AccAddress,
// WithdrawFromPerpFund sends funds from the Perp Fund to the "to" address.
//
// Args:
// - ctx: Blockchain context holding the current state
// - amount: Amount of micro-NUSD to withdraw.
// - sender: Admin address registered in x/sudo
// - to: Recipient address
func (k admin) WithdrawFromPerpFund(
ctx sdk.Context, amount sdkmath.Int, sender, to sdk.AccAddress, denom string,
) (err error) {
collateral, err := k.Collateral.Get(ctx)
if err != nil {
if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil {
return err
}

coinToSend := sdk.NewCoin(collateral, amount)
var collateralDenom string
if denom == "" {
denomFromState, err := k.Collateral.Get(ctx)
if err != nil {
return err
}
collateralDenom = denomFromState
} else {
collateralDenom = denom
}

coinToSend := sdk.NewCoin(collateralDenom, amount)
if err = k.BankKeeper.SendCoinsFromModuleToAccount(
ctx,
/* from */ types.PerpEFModuleAccount,
/* from */ types.PerpFundModuleAccount,
/* to */ to,
/* amount */ sdk.NewCoins(coinToSend),
); err != nil {
return err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
"withdraw_from_if",
"withdraw_from_perp_fund",
sdk.NewAttribute("to", to.String()),
sdk.NewAttribute("funds", coinToSend.String()),
))
Expand Down
Loading

0 comments on commit 42e4214

Please sign in to comment.