From 2714bb7ebe10f387f0c87560da93f9c1da0ed928 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 10 Oct 2023 14:26:45 +0200 Subject: [PATCH 1/6] feat: create settle position function --- x/perp/v2/client/cli/gen_market.go | 2 - x/perp/v2/integration/action/market.go | 5 +- x/perp/v2/integration/action/settlement.go | 38 +++++ x/perp/v2/integration/assertion/position.go | 29 +++- x/perp/v2/keeper/clearing_house_test.go | 56 ++++---- x/perp/v2/keeper/liquidate_test.go | 14 +- x/perp/v2/keeper/msg_server_test.go | 6 +- x/perp/v2/keeper/settlement.go | 115 ++++++++++++++++ x/perp/v2/keeper/settlement_test.go | 145 ++++++++++++++++++++ 9 files changed, 364 insertions(+), 46 deletions(-) diff --git a/x/perp/v2/client/cli/gen_market.go b/x/perp/v2/client/cli/gen_market.go index f73d8c42b..ca22206fa 100644 --- a/x/perp/v2/client/cli/gen_market.go +++ b/x/perp/v2/client/cli/gen_market.go @@ -161,8 +161,6 @@ func newMarketFromFlags(flagSet *flag.FlagSet, return types.Market{}, types.AMM{}, err } - fmt.Println(maxFundingRate) - priceMultiplier, err := sdk.NewDecFromStr(priceMultiplierStr) if err != nil { return types.Market{}, types.AMM{}, err diff --git a/x/perp/v2/integration/action/market.go b/x/perp/v2/integration/action/market.go index d8ac00ab2..00408b7f9 100644 --- a/x/perp/v2/integration/action/market.go +++ b/x/perp/v2/integration/action/market.go @@ -1,8 +1,6 @@ package action import ( - "fmt" - sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,7 +19,6 @@ type logger struct { } func (e logger) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - fmt.Println(e.log) return ctx, nil, true } @@ -38,7 +35,7 @@ type createMarketAction struct { } func (c createMarketAction) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - app.PerpKeeperV2.MarketLastVersion.Insert(ctx, c.Market.Pair, types.MarketLastVersion{Version: 1}) + app.PerpKeeperV2.MarketLastVersion.Insert(ctx, c.Market.Pair, types.MarketLastVersion{Version: c.Market.Version}) app.PerpKeeperV2.SaveMarket(ctx, c.Market) app.PerpKeeperV2.SaveAMM(ctx, c.AMM) diff --git a/x/perp/v2/integration/action/settlement.go b/x/perp/v2/integration/action/settlement.go index a7b4abadc..b55b94c50 100644 --- a/x/perp/v2/integration/action/settlement.go +++ b/x/perp/v2/integration/action/settlement.go @@ -24,3 +24,41 @@ func (c closeMarket) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error func CloseMarket(pair asset.Pair) action.Action { return closeMarket{pair: pair} } + +type settlePosition struct { + pair asset.Pair + version uint64 + trader sdk.AccAddress +} + +func (c settlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { + _, err := app.PerpKeeperV2.SettlePosition(ctx, c.pair, c.version, c.trader) + if err != nil { + return ctx, err, false + } + + return ctx, nil, true +} + +func SettlePosition(pair asset.Pair, version uint64, trader sdk.AccAddress) action.Action { + return settlePosition{pair: pair, version: version, trader: trader} +} + +type settlePositionShouldFail struct { + pair asset.Pair + version uint64 + trader sdk.AccAddress +} + +func (c settlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { + _, err := app.PerpKeeperV2.SettlePosition(ctx, c.pair, c.version, c.trader) + if err == nil { + return ctx, err, false + } + + return ctx, nil, true +} + +func SettlePositionShouldFail(pair asset.Pair, version uint64, trader sdk.AccAddress) action.Action { + return settlePositionShouldFail{pair: pair, version: version, trader: trader} +} diff --git a/x/perp/v2/integration/assertion/position.go b/x/perp/v2/integration/assertion/position.go index 8f9184ad7..1b601d969 100644 --- a/x/perp/v2/integration/assertion/position.go +++ b/x/perp/v2/integration/assertion/position.go @@ -61,10 +61,11 @@ func Position_PositionShouldBeEqualTo(expectedPosition types.Position) PositionC type positionShouldNotExist struct { Account sdk.AccAddress Pair asset.Pair + Version uint64 } func (p positionShouldNotExist) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - _, err := app.PerpKeeperV2.GetPosition(ctx, p.Pair, 1, p.Account) + _, err := app.PerpKeeperV2.GetPosition(ctx, p.Pair, p.Version, p.Account) if err == nil { return ctx, fmt.Errorf("position should not exist, but it does with pair %s", p.Pair), false } @@ -72,9 +73,33 @@ func (p positionShouldNotExist) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Con return ctx, nil, false } -func PositionShouldNotExist(account sdk.AccAddress, pair asset.Pair) action.Action { +func PositionShouldNotExist(account sdk.AccAddress, pair asset.Pair, version uint64) action.Action { return positionShouldNotExist{ Account: account, Pair: pair, + Version: version, + } +} + +type positionShouldExist struct { + Account sdk.AccAddress + Pair asset.Pair + Version uint64 +} + +func (p positionShouldExist) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { + _, err := app.PerpKeeperV2.GetPosition(ctx, p.Pair, p.Version, p.Account) + if err != nil { + return ctx, fmt.Errorf("position should exist, but it does not with pair %s", p.Pair), false + } + + return ctx, nil, false +} + +func PositionShouldExist(account sdk.AccAddress, pair asset.Pair, version uint64) action.Action { + return positionShouldExist{ + Account: account, + Pair: pair, + Version: version, } } diff --git a/x/perp/v2/keeper/clearing_house_test.go b/x/perp/v2/keeper/clearing_house_test.go index 93b034732..b2cc2e30a 100644 --- a/x/perp/v2/keeper/clearing_house_test.go +++ b/x/perp/v2/keeper/clearing_house_test.go @@ -266,7 +266,7 @@ func TestMarketOrder(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("existing long position, decrease a bit"). @@ -870,7 +870,7 @@ func TestMarketOrder(t *testing.T) { sdkerrors.ErrInsufficientFunds), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("new long position, can not open new position after market is not enabled"). @@ -886,7 +886,7 @@ func TestMarketOrder(t *testing.T) { types.ErrMarketNotEnabled), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("market doesn't exist"). @@ -900,7 +900,7 @@ func TestMarketOrder(t *testing.T) { types.ErrPairNotFound), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("zero quote asset amount"). @@ -915,7 +915,7 @@ func TestMarketOrder(t *testing.T) { types.ErrInputQuoteAmtNegative), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("zero leverage"). @@ -930,7 +930,7 @@ func TestMarketOrder(t *testing.T) { types.ErrUserLeverageNegative), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("user leverage greater than market max leverage"). @@ -945,7 +945,7 @@ func TestMarketOrder(t *testing.T) { types.ErrLeverageIsTooHigh), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("position should not exist after opening a closing manually"). @@ -960,7 +960,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("position should not exist after opening a closing manually - reverse with leverage"). @@ -975,7 +975,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("position should not exist after opening a closing manually - open with leverage"). Given( @@ -989,7 +989,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("position should not exist after opening a closing manually - reverse with leverage"). @@ -1004,7 +1004,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("position should not exist after opening a closing manually - reverse with leverage - more steps"). @@ -1020,7 +1020,7 @@ func TestMarketOrder(t *testing.T) { MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), } @@ -1649,7 +1649,7 @@ func TestClosePosition(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, @@ -1695,7 +1695,7 @@ func TestClosePosition(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, @@ -1743,7 +1743,7 @@ func TestClosePosition(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ TraderAddress: alice.String(), @@ -1791,7 +1791,7 @@ func TestClosePosition(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, @@ -1837,7 +1837,7 @@ func TestClosePosition(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ Pair: pairBtcNusd, @@ -1885,7 +1885,7 @@ func TestClosePosition(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{ FinalPosition: types.Position{ TraderAddress: alice.String(), @@ -1937,7 +1937,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("only short position - no change to swap invariant"). Given( @@ -1952,7 +1952,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ), TC("only long position - increasing k"). Given( @@ -1971,7 +1971,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), TC("only short position - increasing k"). @@ -1991,7 +1991,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.OneInt()))), ), @@ -2012,7 +2012,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), TC("only short position - decreasing k"). @@ -2032,7 +2032,7 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(alice, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ), @@ -2061,8 +2061,8 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(bob, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionShouldNotExist(bob, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), + PositionShouldNotExist(bob, pairBtcNusd, 1), ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_782_394)))), @@ -2092,8 +2092,8 @@ func TestUpdateSwapInvariant(t *testing.T) { ClosePosition(bob, pairBtcNusd), ). Then( - PositionShouldNotExist(alice, pairBtcNusd), - PositionShouldNotExist(bob, pairBtcNusd), + PositionShouldNotExist(alice, pairBtcNusd, 1), + PositionShouldNotExist(bob, pairBtcNusd, 1), ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()), ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_200_810)))), diff --git a/x/perp/v2/keeper/liquidate_test.go b/x/perp/v2/keeper/liquidate_test.go index d0a408d9f..0415e0ed8 100644 --- a/x/perp/v2/keeper/liquidate_test.go +++ b/x/perp/v2/keeper/liquidate_test.go @@ -83,7 +83,7 @@ func TestMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), ), TC("full liquidation"). @@ -104,7 +104,7 @@ func TestMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), ), TC("one fail liquidation - one correct"). @@ -126,7 +126,7 @@ func TestMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), ), TC("one fail liquidation because market does not exists- one correct"). @@ -148,7 +148,7 @@ func TestMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), ), TC("realizes bad debt"). @@ -170,7 +170,7 @@ func TestMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(800)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), ), TC("uses prepaid bad debt"). @@ -191,7 +191,7 @@ func TestMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(750)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.ZeroInt()), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), MarketShouldBeEqual(pairBtcUsdc, Market_PrepaidBadDebtShouldBeEqualTo(sdk.ZeroInt())), ), @@ -273,7 +273,7 @@ func TestMultiLiquidate(t *testing.T) { }, ), ), - PositionShouldNotExist(alice, pairEthUsdc), + PositionShouldNotExist(alice, pairEthUsdc, 1), PositionShouldBeEqual(alice, pairAtomUsdc, Position_PositionShouldBeEqualTo( types.Position{ diff --git a/x/perp/v2/keeper/msg_server_test.go b/x/perp/v2/keeper/msg_server_test.go index 2f04d55fd..057bbf13c 100644 --- a/x/perp/v2/keeper/msg_server_test.go +++ b/x/perp/v2/keeper/msg_server_test.go @@ -90,7 +90,7 @@ func TestMsgServerClosePosition(t *testing.T) { MsgServerClosePosition(alice, pair), ). Then( - PositionShouldNotExist(alice, pair), + PositionShouldNotExist(alice, pair, 1), BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), ), @@ -105,7 +105,7 @@ func TestMsgServerClosePosition(t *testing.T) { MsgServerClosePosition(alice, pair), ). Then( - PositionShouldNotExist(alice, pair), + PositionShouldNotExist(alice, pair, 1), BalanceEqual(alice, denoms.NUSD, sdk.NewInt(100)), ), } @@ -283,7 +283,7 @@ func TestMsgServerMultiLiquidate(t *testing.T) { ModuleBalanceEqual(types.VaultModuleAccount, denoms.USDC, sdk.NewInt(600)), ModuleBalanceEqual(types.PerpEFModuleAccount, denoms.USDC, sdk.NewInt(150)), BalanceEqual(liquidator, denoms.USDC, sdk.NewInt(250)), - PositionShouldNotExist(alice, pairBtcUsdc), + PositionShouldNotExist(alice, pairBtcUsdc, 1), ), } diff --git a/x/perp/v2/keeper/settlement.go b/x/perp/v2/keeper/settlement.go index cc60c73e0..0e632d167 100644 --- a/x/perp/v2/keeper/settlement.go +++ b/x/perp/v2/keeper/settlement.go @@ -4,6 +4,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/NibiruChain/nibiru/x/common/asset" + "github.com/NibiruChain/nibiru/x/perp/v2/types" ) // CloseMarket closes the market. From now on, no new position can be opened on this market or closed. @@ -13,6 +14,10 @@ func (k Keeper) CloseMarket(ctx sdk.Context, pair asset.Pair) (err error) { if err != nil { return err } + if !market.Enabled { + return types.ErrMarketNotEnabled + } + amm, err := k.GetAMM(ctx, pair) if err != nil { return err @@ -31,3 +36,113 @@ func (k Keeper) CloseMarket(ctx sdk.Context, pair asset.Pair) (err error) { return nil } + +// SettlePosition settles a position and transfer the margin and funding payments to the trader. +func (k Keeper) SettlePosition(ctx sdk.Context, pair asset.Pair, version uint64, traderAddr sdk.AccAddress) (resp *types.PositionResp, err error) { + market, err := k.GetMarketByPairAndVersion(ctx, pair, version) + if err != nil { + return + } + if market.Enabled { + return nil, types.ErrSettlementPositionMarketEnabled + } + + amm, err := k.GetAMMByPairAndVersion(ctx, pair, version) + if err != nil { + return + } + + position, err := k.GetPosition(ctx, pair, version, traderAddr) + if err != nil { + return + } + + updatedAMM, positionResp, err := k.settlePosition(ctx, market, amm, position) + if err != nil { + return + } + + if positionResp.BadDebt.IsPositive() { + if err = k.realizeBadDebt( + ctx, + market, + positionResp.BadDebt.RoundInt(), + ); err != nil { + return nil, err + } + } + + if err = k.afterPositionUpdate( + ctx, + market, + *updatedAMM, + traderAddr, + *positionResp, + types.ChangeReason_Settlement, + position, + ); err != nil { + return nil, err + } + + return positionResp, nil +} + +// Settles a position and realizes PnL and funding payments. +// Returns the updated AMM and the realized PnL and funding payments. +func (k Keeper) settlePosition(ctx sdk.Context, market types.Market, amm types.AMM, position types.Position) (updatedAMM *types.AMM, resp *types.PositionResp, err error) { + positionNotional := position.Size_.Mul(amm.SettlementPrice) + + resp = &types.PositionResp{ + ExchangedPositionSize: position.Size_.Neg(), + PositionNotional: sdk.ZeroDec(), + FundingPayment: FundingPayment(position, market.LatestCumulativePremiumFraction), + RealizedPnl: UnrealizedPnl(position, positionNotional), + UnrealizedPnlAfter: sdk.ZeroDec(), + } + + remainingMargin := position.Margin.Add(resp.RealizedPnl).Sub(resp.FundingPayment) + + if remainingMargin.IsPositive() { + resp.BadDebt = sdk.ZeroDec() + resp.MarginToVault = remainingMargin.Neg() + } else { + resp.BadDebt = remainingMargin.Abs() + resp.MarginToVault = sdk.ZeroDec() + } + + var dir types.Direction + // flipped since we are going against the current position + if position.Size_.IsPositive() { + dir = types.Direction_SHORT + } else { + dir = types.Direction_LONG + } + updatedAMM, exchangedNotionalValue, err := k.SwapBaseAsset( + ctx, + amm, + dir, + position.Size_.Abs(), + sdk.ZeroDec(), + ) + if err != nil { + return nil, nil, err + } + + resp.ExchangedNotionalValue = exchangedNotionalValue + resp.Position = types.Position{ + TraderAddress: position.TraderAddress, + Pair: position.Pair, + Size_: sdk.ZeroDec(), + Margin: sdk.ZeroDec(), + OpenNotional: sdk.ZeroDec(), + LatestCumulativePremiumFraction: market.LatestCumulativePremiumFraction, + LastUpdatedBlockNumber: ctx.BlockHeight(), + } + + err = k.DeletePosition(ctx, position.Pair, market.Version, sdk.MustAccAddressFromBech32(position.TraderAddress)) + if err != nil { + return nil, nil, err + } + + return updatedAMM, resp, nil +} diff --git a/x/perp/v2/keeper/settlement_test.go b/x/perp/v2/keeper/settlement_test.go index b819b1866..8ad89bb73 100644 --- a/x/perp/v2/keeper/settlement_test.go +++ b/x/perp/v2/keeper/settlement_test.go @@ -114,3 +114,148 @@ func TestDisableMarket(t *testing.T) { NewTestSuite(t).WithTestCases(tc...).Run() } + +func TestSettlePosition(t *testing.T) { + pairBtcUsdc := asset.Registry.Pair(denoms.BTC, denoms.NUSD) + startTime := time.Now() + + alice := testutil.AccAddress() + + tc := TestCases{ + TC("Happy path").When( + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + ), + SetBlockNumber(1), + SetBlockTime(startTime), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(10_000), + sdk.OneDec(), + sdk.ZeroDec(), + ), + ).When( + CloseMarket(pairBtcUsdc), + SettlePosition(pairBtcUsdc, 1, alice), + ).Then( + PositionShouldNotExist(alice, pairBtcUsdc, 1), + ), + + TC("Error: can't settle on enabled market").When( + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + ), + SetBlockNumber(1), + SetBlockTime(startTime), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(10_200)))), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(10_000), + sdk.OneDec(), + sdk.ZeroDec(), + ), + ).When( + SettlePositionShouldFail(pairBtcUsdc, 1, alice), + ), + + TC("Error: can't settle on enabled market (with a live market in another version)").When( + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + WithVersion(1), + ), + SetBlockNumber(1), + SetBlockTime(startTime), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_400)))), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(10_000), + sdk.OneDec(), + sdk.ZeroDec(), + ), + ).When( + CloseMarket(pairBtcUsdc), + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + WithVersion(2), + ), + SetBlockNumber(2), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(10_000), + sdk.OneDec(), + sdk.ZeroDec(), + ), + ).Then( + + SettlePositionShouldFail(pairBtcUsdc, 3, alice), // can't settle on non existing market + SettlePositionShouldFail(pairBtcUsdc, 2, alice), // can't settle on live market + SettlePosition(pairBtcUsdc, 1, alice), + + PositionShouldNotExist(alice, pairBtcUsdc, 1), + PositionShouldExist(alice, pairBtcUsdc, 2), + ), + + TC("Error: can't settle on non existing market").When( + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + WithVersion(1), + ), + SetBlockNumber(1), + SetBlockTime(startTime), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(20_400)))), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(10_000), + sdk.OneDec(), + sdk.ZeroDec(), + ), + ).When( + CloseMarket(pairBtcUsdc), + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + WithVersion(2), + ), + SetBlockNumber(2), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(10_000), + sdk.OneDec(), + sdk.ZeroDec(), + ), + ).Then( + + SettlePositionShouldFail(pairBtcUsdc, 2, alice), + SettlePosition(pairBtcUsdc, 1, alice), + + PositionShouldNotExist(alice, pairBtcUsdc, 1), + PositionShouldExist(alice, pairBtcUsdc, 2), + ), + } + + NewTestSuite(t).WithTestCases(tc...).Run() +} From dd2167632910ec308145994b98bb46cdc3528fa7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 10 Oct 2023 15:28:12 +0200 Subject: [PATCH 2/6] feat: wire settlement cmd --- proto/nibiru/perp/v2/tx.proto | 17 ++ x/perp/v2/client/cli/tx.go | 46 ++++ x/perp/v2/keeper/msg_server.go | 18 ++ x/perp/v2/types/msgs.go | 28 ++ x/perp/v2/types/msgs_test.go | 42 +++ x/perp/v2/types/tx.pb.go | 476 ++++++++++++++++++++++++++------- 6 files changed, 535 insertions(+), 92 deletions(-) diff --git a/proto/nibiru/perp/v2/tx.proto b/proto/nibiru/perp/v2/tx.proto index 29bf3a6a2..261c4feef 100644 --- a/proto/nibiru/perp/v2/tx.proto +++ b/proto/nibiru/perp/v2/tx.proto @@ -24,10 +24,27 @@ service Msg { rpc PartialClose(MsgPartialClose) returns (MsgPartialCloseResponse) {} + rpc SettlePosition(MsgSettlePosition) returns (MsgClosePositionResponse) {} + rpc DonateToEcosystemFund(MsgDonateToEcosystemFund) returns (MsgDonateToEcosystemFundResponse) {} } +// -------------------------- Settle Position -------------------------- + +/* MsgSettlePosition: Msg to remove margin. */ +message MsgSettlePosition { + string sender = 1; + + string pair = 2 [ + (gogoproto.customtype) = + "github.com/NibiruChain/nibiru/x/common/asset.Pair", + (gogoproto.nullable) = false + ]; + + uint64 version = 3; +} + // -------------------------- RemoveMargin -------------------------- /* MsgRemoveMargin: Msg to remove margin. */ diff --git a/x/perp/v2/client/cli/tx.go b/x/perp/v2/client/cli/tx.go index bebd31c58..d45b81696 100644 --- a/x/perp/v2/client/cli/tx.go +++ b/x/perp/v2/client/cli/tx.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "strconv" "strings" "github.com/cosmos/cosmos-sdk/client" @@ -29,6 +30,7 @@ func GetTxCmd() *cobra.Command { AddMarginCmd(), MarketOrderCmd(), ClosePositionCmd(), + SettlePositionCmd(), PartialCloseCmd(), MultiLiquidateCmd(), DonateToEcosystemFundCmd(), @@ -315,6 +317,50 @@ func AddMarginCmd() *cobra.Command { return cmd } +func SettlePositionCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "settle [market] [version]", + Short: "Settle position in a specific market version when the market is disabled", + Long: strings.TrimSpace( + fmt.Sprintf(` + $ %s tx perp settle osmo:nusd 1 + `, version.AppName), + ), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + version, err := strconv.ParseUint(args[1], 10, 64) + if err != nil { + return err + } + + pair, err := asset.TryNewPair(args[0]) + if err != nil { + return err + } + + msg := &types.MsgSettlePosition{ + Sender: clientCtx.GetFromAddress().String(), + Pair: pair, + Version: version, + } + if err = msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} + func DonateToEcosystemFundCmd() *cobra.Command { cmd := &cobra.Command{ Use: "donate-ef [amount]", diff --git a/x/perp/v2/keeper/msg_server.go b/x/perp/v2/keeper/msg_server.go index 9c0bfb61e..efa5a38f3 100644 --- a/x/perp/v2/keeper/msg_server.go +++ b/x/perp/v2/keeper/msg_server.go @@ -108,6 +108,24 @@ func (m msgServer) MultiLiquidate(goCtx context.Context, req *types.MsgMultiLiqu return &types.MsgMultiLiquidateResponse{Liquidations: resp}, nil } +func (m msgServer) SettlePosition(ctx context.Context, msg *types.MsgSettlePosition) (*types.MsgClosePositionResponse, error) { + // These fields should have already been validated by MsgSettlePosition.ValidateBasic() prior to being sent to the msgServer. + traderAddr := sdk.MustAccAddressFromBech32(msg.Sender) + resp, err := m.k.SettlePosition(sdk.UnwrapSDKContext(ctx), msg.Pair, msg.Version, traderAddr) + + if err != nil { + return nil, err + } + + return &types.MsgClosePositionResponse{ + ExchangedNotionalValue: resp.ExchangedNotionalValue, + ExchangedPositionSize: resp.ExchangedPositionSize, + FundingPayment: resp.FundingPayment, + RealizedPnl: resp.RealizedPnl, + MarginToTrader: resp.MarginToVault.Neg(), + }, nil +} + func (m msgServer) DonateToEcosystemFund(ctx context.Context, msg *types.MsgDonateToEcosystemFund) (*types.MsgDonateToEcosystemFundResponse, error) { if err := m.k.BankKeeper.SendCoinsFromAccountToModule( sdk.UnwrapSDKContext(ctx), diff --git a/x/perp/v2/types/msgs.go b/x/perp/v2/types/msgs.go index 8eda3d24c..3378b0bc4 100644 --- a/x/perp/v2/types/msgs.go +++ b/x/perp/v2/types/msgs.go @@ -198,6 +198,34 @@ func (m MsgClosePosition) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } +// MsgSettlePosition + +func (m MsgSettlePosition) Route() string { return "perp" } +func (m MsgSettlePosition) Type() string { return "settle_position_msg" } + +func (m MsgSettlePosition) ValidateBasic() error { + if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { + return sdkerrors.Wrapf(errors.ErrInvalidAddress, "invalid sender address (%s)", err) + } + if err := m.Pair.Validate(); err != nil { + return err + } + + return nil +} + +func (m MsgSettlePosition) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) +} + +func (m MsgSettlePosition) GetSigners() []sdk.AccAddress { + signer, err := sdk.AccAddressFromBech32(m.Sender) + if err != nil { + panic(err) + } + return []sdk.AccAddress{signer} +} + // MsgDonateToEcosystemFund func (m MsgDonateToEcosystemFund) Route() string { return "perp" } diff --git a/x/perp/v2/types/msgs_test.go b/x/perp/v2/types/msgs_test.go index d06c612cf..8a3a146fe 100644 --- a/x/perp/v2/types/msgs_test.go +++ b/x/perp/v2/types/msgs_test.go @@ -243,6 +243,36 @@ func TestMsgValidateBasic(t *testing.T) { true, "decoding bech32 failed", }, + // MsgSettlePosition test cases + { + "Test MsgSettlePosition: Valid input", + &MsgSettlePosition{ + Sender: validSender, + Pair: validPair, + Version: 1, + }, + false, + "", + }, + { + "Test MsgSettlePosition: Invalid pair", + &MsgSettlePosition{ + Sender: validSender, + Pair: invalidPair, + Version: 1, + }, + true, + "invalid base asset", + }, + { + "Test MsgSettlePosition: Invalid sender", + &MsgSettlePosition{ + Sender: "invalid", + Pair: validPair, + }, + true, + "decoding bech32 failed", + }, // MsgPartialClose test cases { "Test MsgPartialClose: Valid input", @@ -385,6 +415,7 @@ func TestMsg_GetSigners(t *testing.T) { &MsgRemoveMargin{Sender: validSender}, &MsgMarketOrder{Sender: validSender}, &MsgClosePosition{Sender: validSender}, + &MsgSettlePosition{Sender: validSender}, &MsgPartialClose{Sender: validSender}, &MsgDonateToEcosystemFund{Sender: validSender}, &MsgMultiLiquidate{Sender: validSender}, @@ -394,6 +425,7 @@ func TestMsg_GetSigners(t *testing.T) { &MsgRemoveMargin{Sender: invalidSender}, &MsgMarketOrder{Sender: invalidSender}, &MsgClosePosition{Sender: invalidSender}, + &MsgSettlePosition{Sender: invalidSender}, &MsgPartialClose{Sender: invalidSender}, &MsgDonateToEcosystemFund{Sender: invalidSender}, &MsgMultiLiquidate{Sender: invalidSender}, @@ -457,6 +489,12 @@ func TestMsg_RouteAndType(t *testing.T) { expectedRoute: "perp", expectedType: "close_position_msg", }, + { + name: "MsgSettlePosition", + msg: &MsgSettlePosition{}, + expectedRoute: "perp", + expectedType: "settle_position_msg", + }, { name: "MsgPartialClose", msg: &MsgPartialClose{}, @@ -511,6 +549,10 @@ func TestMsg_GetSignBytes(t *testing.T) { name: "MsgClosePosition", msg: &MsgClosePosition{}, }, + { + name: "MsgSettlePosition", + msg: &MsgSettlePosition{}, + }, { name: "MsgPartialClose", msg: &MsgPartialClose{}, diff --git a/x/perp/v2/types/tx.pb.go b/x/perp/v2/types/tx.pb.go index a6993682b..77abdd52c 100644 --- a/x/perp/v2/types/tx.pb.go +++ b/x/perp/v2/types/tx.pb.go @@ -32,6 +32,60 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +// MsgSettlePosition: Msg to remove margin. +type MsgSettlePosition struct { + Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty"` + Pair github_com_NibiruChain_nibiru_x_common_asset.Pair `protobuf:"bytes,2,opt,name=pair,proto3,customtype=github.com/NibiruChain/nibiru/x/common/asset.Pair" json:"pair"` + Version uint64 `protobuf:"varint,3,opt,name=version,proto3" json:"version,omitempty"` +} + +func (m *MsgSettlePosition) Reset() { *m = MsgSettlePosition{} } +func (m *MsgSettlePosition) String() string { return proto.CompactTextString(m) } +func (*MsgSettlePosition) ProtoMessage() {} +func (*MsgSettlePosition) Descriptor() ([]byte, []int) { + return fileDescriptor_b95cda40bf0a0f91, []int{0} +} +func (m *MsgSettlePosition) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgSettlePosition) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgSettlePosition.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgSettlePosition) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgSettlePosition.Merge(m, src) +} +func (m *MsgSettlePosition) XXX_Size() int { + return m.Size() +} +func (m *MsgSettlePosition) XXX_DiscardUnknown() { + xxx_messageInfo_MsgSettlePosition.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgSettlePosition proto.InternalMessageInfo + +func (m *MsgSettlePosition) GetSender() string { + if m != nil { + return m.Sender + } + return "" +} + +func (m *MsgSettlePosition) GetVersion() uint64 { + if m != nil { + return m.Version + } + return 0 +} + // MsgRemoveMargin: Msg to remove margin. type MsgRemoveMargin struct { Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty"` @@ -43,7 +97,7 @@ func (m *MsgRemoveMargin) Reset() { *m = MsgRemoveMargin{} } func (m *MsgRemoveMargin) String() string { return proto.CompactTextString(m) } func (*MsgRemoveMargin) ProtoMessage() {} func (*MsgRemoveMargin) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{0} + return fileDescriptor_b95cda40bf0a0f91, []int{1} } func (m *MsgRemoveMargin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -99,7 +153,7 @@ func (m *MsgRemoveMarginResponse) Reset() { *m = MsgRemoveMarginResponse func (m *MsgRemoveMarginResponse) String() string { return proto.CompactTextString(m) } func (*MsgRemoveMarginResponse) ProtoMessage() {} func (*MsgRemoveMarginResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{1} + return fileDescriptor_b95cda40bf0a0f91, []int{2} } func (m *MsgRemoveMarginResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -153,7 +207,7 @@ func (m *MsgAddMargin) Reset() { *m = MsgAddMargin{} } func (m *MsgAddMargin) String() string { return proto.CompactTextString(m) } func (*MsgAddMargin) ProtoMessage() {} func (*MsgAddMargin) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{2} + return fileDescriptor_b95cda40bf0a0f91, []int{3} } func (m *MsgAddMargin) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -205,7 +259,7 @@ func (m *MsgAddMarginResponse) Reset() { *m = MsgAddMarginResponse{} } func (m *MsgAddMarginResponse) String() string { return proto.CompactTextString(m) } func (*MsgAddMarginResponse) ProtoMessage() {} func (*MsgAddMarginResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{3} + return fileDescriptor_b95cda40bf0a0f91, []int{4} } func (m *MsgAddMarginResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -250,7 +304,7 @@ func (m *MsgMultiLiquidate) Reset() { *m = MsgMultiLiquidate{} } func (m *MsgMultiLiquidate) String() string { return proto.CompactTextString(m) } func (*MsgMultiLiquidate) ProtoMessage() {} func (*MsgMultiLiquidate) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{4} + return fileDescriptor_b95cda40bf0a0f91, []int{5} } func (m *MsgMultiLiquidate) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -302,7 +356,7 @@ func (m *MsgMultiLiquidate_Liquidation) Reset() { *m = MsgMultiLiquidate func (m *MsgMultiLiquidate_Liquidation) String() string { return proto.CompactTextString(m) } func (*MsgMultiLiquidate_Liquidation) ProtoMessage() {} func (*MsgMultiLiquidate_Liquidation) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{4, 0} + return fileDescriptor_b95cda40bf0a0f91, []int{5, 0} } func (m *MsgMultiLiquidate_Liquidation) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -346,7 +400,7 @@ func (m *MsgMultiLiquidateResponse) Reset() { *m = MsgMultiLiquidateResp func (m *MsgMultiLiquidateResponse) String() string { return proto.CompactTextString(m) } func (*MsgMultiLiquidateResponse) ProtoMessage() {} func (*MsgMultiLiquidateResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{5} + return fileDescriptor_b95cda40bf0a0f91, []int{6} } func (m *MsgMultiLiquidateResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -399,7 +453,7 @@ func (m *MsgMultiLiquidateResponse_LiquidationResponse) String() string { } func (*MsgMultiLiquidateResponse_LiquidationResponse) ProtoMessage() {} func (*MsgMultiLiquidateResponse_LiquidationResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{5, 0} + return fileDescriptor_b95cda40bf0a0f91, []int{6, 0} } func (m *MsgMultiLiquidateResponse_LiquidationResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -476,7 +530,7 @@ func (m *MsgMarketOrder) Reset() { *m = MsgMarketOrder{} } func (m *MsgMarketOrder) String() string { return proto.CompactTextString(m) } func (*MsgMarketOrder) ProtoMessage() {} func (*MsgMarketOrder) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{6} + return fileDescriptor_b95cda40bf0a0f91, []int{7} } func (m *MsgMarketOrder) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -546,7 +600,7 @@ func (m *MsgMarketOrderResponse) Reset() { *m = MsgMarketOrderResponse{} func (m *MsgMarketOrderResponse) String() string { return proto.CompactTextString(m) } func (*MsgMarketOrderResponse) ProtoMessage() {} func (*MsgMarketOrderResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{7} + return fileDescriptor_b95cda40bf0a0f91, []int{8} } func (m *MsgMarketOrderResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -591,7 +645,7 @@ func (m *MsgClosePosition) Reset() { *m = MsgClosePosition{} } func (m *MsgClosePosition) String() string { return proto.CompactTextString(m) } func (*MsgClosePosition) ProtoMessage() {} func (*MsgClosePosition) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{8} + return fileDescriptor_b95cda40bf0a0f91, []int{9} } func (m *MsgClosePosition) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -647,7 +701,7 @@ func (m *MsgClosePositionResponse) Reset() { *m = MsgClosePositionRespon func (m *MsgClosePositionResponse) String() string { return proto.CompactTextString(m) } func (*MsgClosePositionResponse) ProtoMessage() {} func (*MsgClosePositionResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{9} + return fileDescriptor_b95cda40bf0a0f91, []int{10} } func (m *MsgClosePositionResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -686,7 +740,7 @@ func (m *MsgPartialClose) Reset() { *m = MsgPartialClose{} } func (m *MsgPartialClose) String() string { return proto.CompactTextString(m) } func (*MsgPartialClose) ProtoMessage() {} func (*MsgPartialClose) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{10} + return fileDescriptor_b95cda40bf0a0f91, []int{11} } func (m *MsgPartialClose) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -742,7 +796,7 @@ func (m *MsgPartialCloseResponse) Reset() { *m = MsgPartialCloseResponse func (m *MsgPartialCloseResponse) String() string { return proto.CompactTextString(m) } func (*MsgPartialCloseResponse) ProtoMessage() {} func (*MsgPartialCloseResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{11} + return fileDescriptor_b95cda40bf0a0f91, []int{12} } func (m *MsgPartialCloseResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -781,7 +835,7 @@ func (m *MsgDonateToEcosystemFund) Reset() { *m = MsgDonateToEcosystemFu func (m *MsgDonateToEcosystemFund) String() string { return proto.CompactTextString(m) } func (*MsgDonateToEcosystemFund) ProtoMessage() {} func (*MsgDonateToEcosystemFund) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{12} + return fileDescriptor_b95cda40bf0a0f91, []int{13} } func (m *MsgDonateToEcosystemFund) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -831,7 +885,7 @@ func (m *MsgDonateToEcosystemFundResponse) Reset() { *m = MsgDonateToEco func (m *MsgDonateToEcosystemFundResponse) String() string { return proto.CompactTextString(m) } func (*MsgDonateToEcosystemFundResponse) ProtoMessage() {} func (*MsgDonateToEcosystemFundResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_b95cda40bf0a0f91, []int{13} + return fileDescriptor_b95cda40bf0a0f91, []int{14} } func (m *MsgDonateToEcosystemFundResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -861,6 +915,7 @@ func (m *MsgDonateToEcosystemFundResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgDonateToEcosystemFundResponse proto.InternalMessageInfo func init() { + proto.RegisterType((*MsgSettlePosition)(nil), "nibiru.perp.v2.MsgSettlePosition") proto.RegisterType((*MsgRemoveMargin)(nil), "nibiru.perp.v2.MsgRemoveMargin") proto.RegisterType((*MsgRemoveMarginResponse)(nil), "nibiru.perp.v2.MsgRemoveMarginResponse") proto.RegisterType((*MsgAddMargin)(nil), "nibiru.perp.v2.MsgAddMargin") @@ -882,82 +937,85 @@ func init() { func init() { proto.RegisterFile("nibiru/perp/v2/tx.proto", fileDescriptor_b95cda40bf0a0f91) } var fileDescriptor_b95cda40bf0a0f91 = []byte{ - // 1200 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x58, 0xcf, 0x6f, 0x1b, 0xc5, - 0x17, 0xf7, 0xda, 0x4e, 0x9a, 0x3c, 0xa7, 0x4e, 0x3a, 0xdf, 0x34, 0x71, 0xad, 0xca, 0xc9, 0x77, - 0x85, 0x4a, 0x38, 0x64, 0x97, 0x1a, 0x24, 0x04, 0x12, 0xa0, 0xa4, 0x3f, 0x10, 0xa8, 0x6e, 0xdd, - 0xa5, 0x6a, 0x51, 0x01, 0x6d, 0x27, 0xf6, 0x78, 0x33, 0xea, 0x7a, 0xc6, 0xdd, 0x99, 0xb5, 0x9a, - 0x1c, 0xf9, 0x0b, 0xf8, 0x17, 0x38, 0x70, 0x45, 0xe2, 0x00, 0x5c, 0xf8, 0x03, 0x7a, 0xec, 0x11, - 0x71, 0x88, 0x50, 0x72, 0xe1, 0x4a, 0xc5, 0x1f, 0x80, 0x66, 0x7f, 0x79, 0xed, 0x6e, 0x12, 0xc7, - 0xa4, 0x91, 0x40, 0x9c, 0xec, 0xdd, 0xf7, 0xde, 0xe7, 0xfd, 0xf8, 0xbc, 0x79, 0x33, 0xb3, 0xb0, - 0xcc, 0xe8, 0x16, 0xf5, 0x7c, 0xb3, 0x47, 0xbc, 0x9e, 0xd9, 0xaf, 0x9b, 0xf2, 0xa9, 0xd1, 0xf3, - 0xb8, 0xe4, 0xa8, 0x1c, 0x0a, 0x0c, 0x25, 0x30, 0xfa, 0xf5, 0xea, 0x65, 0x87, 0x73, 0xc7, 0x25, - 0x26, 0xee, 0x51, 0x13, 0x33, 0xc6, 0x25, 0x96, 0x94, 0x33, 0x11, 0x6a, 0x57, 0x6b, 0x2d, 0x2e, - 0xba, 0x5c, 0x98, 0x5b, 0x58, 0x10, 0xb3, 0x7f, 0x75, 0x8b, 0x48, 0x7c, 0xd5, 0x6c, 0x71, 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e, 0x80, 0xd7, 0xb2, 0x6c, - 0x07, 0xf2, 0xea, 0x95, 0xa3, 0xe5, 0x29, 0xe0, 0x2f, 0xe1, 0xfc, 0xf0, 0xd6, 0xb9, 0x9a, 0x61, - 0x3a, 0xa4, 0x51, 0x5d, 0x3b, 0x4e, 0x23, 0x05, 0xff, 0x10, 0xe6, 0x86, 0x06, 0x65, 0x16, 0x91, - 0x69, 0x85, 0x4c, 0x22, 0xb3, 0x66, 0x95, 0x9e, 0x43, 0x3e, 0x5c, 0xcc, 0xee, 0xe8, 0xac, 0x00, - 0x33, 0x35, 0xab, 0x6f, 0x8e, 0xab, 0x39, 0x70, 0xbb, 0xf9, 0xd1, 0xb3, 0xfd, 0x9a, 0xf6, 0x7c, - 0xbf, 0xa6, 0xfd, 0xb6, 0x5f, 0xd3, 0xbe, 0x3e, 0xa8, 0xe5, 0x9e, 0x1f, 0xd4, 0x72, 0xbf, 0x1c, - 0xd4, 0x72, 0x0f, 0xd7, 0x8f, 0x9b, 0xeb, 0xc9, 0x37, 0x38, 0xb5, 0x52, 0xb7, 0xa6, 0x83, 0xef, - 0x60, 0x6f, 0xfd, 0x15, 0x00, 0x00, 0xff, 0xff, 0x1f, 0xd0, 0xb3, 0xf4, 0xa2, 0x13, 0x00, 0x00, + // 1234 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x58, 0x4d, 0x6f, 0x1b, 0xc5, + 0x1b, 0xf7, 0xda, 0xae, 0x9b, 0x3c, 0x4e, 0x9d, 0x74, 0xfe, 0x69, 0xe2, 0x5a, 0x95, 0x93, 0xff, + 0x0a, 0x95, 0x70, 0xc8, 0x2e, 0x35, 0x48, 0x08, 0x24, 0x40, 0x49, 0x5f, 0x10, 0xa8, 0x6e, 0xdd, + 0x6d, 0xd5, 0xa2, 0x02, 0xda, 0x4e, 0xec, 0xf1, 0x66, 0xd4, 0xf5, 0x8c, 0xbb, 0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xc8, 0xbc, 0x92, 0xb9, 0xdc, 0x55, 0x00, 0xc3, 0x48, 0x9f, 0x48, 0x47, 0x3a, 0x49, 0x68, + 0xe9, 0x48, 0x12, 0xaa, 0xff, 0x54, 0x80, 0x8a, 0x8a, 0x3b, 0xf6, 0xee, 0x13, 0x79, 0xdd, 0x53, + 0x1a, 0x8e, 0x69, 0x14, 0x6c, 0x42, 0x51, 0xd0, 0x4e, 0x18, 0xdf, 0x4a, 0xe3, 0xec, 0x78, 0x31, + 0x5c, 0xa2, 0x1e, 0x69, 0x07, 0xa9, 0x0c, 0xd8, 0xd0, 0x97, 0x80, 0x1e, 0xf8, 0x5c, 0x12, 0x3b, + 0x00, 0xb2, 0x71, 0x8f, 0xfb, 0x4c, 0x06, 0x71, 0x9d, 0xae, 0xd5, 0x3f, 0x65, 0xd2, 0x5a, 0x0a, + 0x90, 0xb6, 0x14, 0xd0, 0x56, 0x80, 0x83, 0x3e, 0x83, 0x39, 0x97, 0x0c, 0x88, 0x87, 0x1d, 0x12, + 0xc6, 0x7b, 0xea, 0xf1, 0x91, 0xc8, 0x23, 0x02, 0xab, 0x2a, 0xbf, 0x23, 0x86, 0xda, 0x2e, 0xed, + 0x51, 0x19, 0x25, 0x6d, 0x5a, 0x73, 0x97, 0x15, 0x5c, 0xca, 0xda, 0xab, 0x0a, 0x4b, 0xdf, 0x3f, + 0x01, 0x2b, 0xa3, 0x99, 0x4b, 0x8a, 0x3e, 0x3d, 0xba, 0xb4, 0x49, 0x47, 0x17, 0xda, 0x85, 0x2a, + 0x79, 0xd8, 0xde, 0xc5, 0xcc, 0x21, 0x1d, 0x9b, 0x71, 0xf5, 0x0d, 0xbb, 0xf6, 0x00, 0xbb, 0x3e, + 0x99, 0x71, 0xaf, 0x5a, 0x49, 0xf0, 0xae, 0x45, 0x70, 0xb7, 0x15, 0x1a, 0xea, 0xc2, 0xea, 0x50, + 0x53, 0xac, 0xdf, 0x16, 0xf4, 0x51, 0x58, 0x0d, 0xd3, 0x2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 0x51, 0x96, 0xa9, 0x8c, 0xf7, 0xe1, 0x4c, 0x76, 0xcb, 0x64, + 0x81, 0x64, 0x72, 0xd6, 0xde, 0x9e, 0x94, 0x73, 0xa8, 0x76, 0xfb, 0x93, 0x27, 0xcf, 0xeb, 0xda, + 0xd3, 0xe7, 0x75, 0xed, 0xb7, 0xe7, 0x75, 0xed, 0xeb, 0xfd, 0x7a, 0xee, 0xe9, 0x7e, 0x3d, 0xf7, + 0xcb, 0x7e, 0x3d, 0x77, 0x77, 0xf3, 0xb0, 0x8d, 0x23, 0x79, 0x7b, 0x54, 0xa3, 0x60, 0xa7, 0x14, + 0xbc, 0xff, 0xbd, 0xf3, 0x57, 0x00, 0x00, 0x00, 0xff, 0xff, 0x63, 0xdb, 0xdc, 0xe2, 0x9a, 0x14, + 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -978,6 +1036,7 @@ type MsgClient interface { MarketOrder(ctx context.Context, in *MsgMarketOrder, opts ...grpc.CallOption) (*MsgMarketOrderResponse, error) ClosePosition(ctx context.Context, in *MsgClosePosition, opts ...grpc.CallOption) (*MsgClosePositionResponse, error) 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) } @@ -1043,6 +1102,15 @@ func (c *msgClient) PartialClose(ctx context.Context, in *MsgPartialClose, opts return out, nil } +func (c *msgClient) SettlePosition(ctx context.Context, in *MsgSettlePosition, opts ...grpc.CallOption) (*MsgClosePositionResponse, error) { + out := new(MsgClosePositionResponse) + err := c.cc.Invoke(ctx, "/nibiru.perp.v2.Msg/SettlePosition", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *msgClient) DonateToEcosystemFund(ctx context.Context, in *MsgDonateToEcosystemFund, opts ...grpc.CallOption) (*MsgDonateToEcosystemFundResponse, error) { out := new(MsgDonateToEcosystemFundResponse) err := c.cc.Invoke(ctx, "/nibiru.perp.v2.Msg/DonateToEcosystemFund", in, out, opts...) @@ -1060,6 +1128,7 @@ type MsgServer interface { MarketOrder(context.Context, *MsgMarketOrder) (*MsgMarketOrderResponse, error) ClosePosition(context.Context, *MsgClosePosition) (*MsgClosePositionResponse, error) PartialClose(context.Context, *MsgPartialClose) (*MsgPartialCloseResponse, error) + SettlePosition(context.Context, *MsgSettlePosition) (*MsgClosePositionResponse, error) DonateToEcosystemFund(context.Context, *MsgDonateToEcosystemFund) (*MsgDonateToEcosystemFundResponse, error) } @@ -1085,6 +1154,9 @@ func (*UnimplementedMsgServer) ClosePosition(ctx context.Context, req *MsgCloseP func (*UnimplementedMsgServer) PartialClose(ctx context.Context, req *MsgPartialClose) (*MsgPartialCloseResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method PartialClose not implemented") } +func (*UnimplementedMsgServer) SettlePosition(ctx context.Context, req *MsgSettlePosition) (*MsgClosePositionResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method SettlePosition not implemented") +} func (*UnimplementedMsgServer) DonateToEcosystemFund(ctx context.Context, req *MsgDonateToEcosystemFund) (*MsgDonateToEcosystemFundResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method DonateToEcosystemFund not implemented") } @@ -1201,6 +1273,24 @@ func _Msg_PartialClose_Handler(srv interface{}, ctx context.Context, dec func(in return interceptor(ctx, in, info, handler) } +func _Msg_SettlePosition_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(MsgSettlePosition) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MsgServer).SettlePosition(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/nibiru.perp.v2.Msg/SettlePosition", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MsgServer).SettlePosition(ctx, req.(*MsgSettlePosition)) + } + return interceptor(ctx, in, info, handler) +} + func _Msg_DonateToEcosystemFund_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(MsgDonateToEcosystemFund) if err := dec(in); err != nil { @@ -1247,6 +1337,10 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ MethodName: "PartialClose", Handler: _Msg_PartialClose_Handler, }, + { + MethodName: "SettlePosition", + Handler: _Msg_SettlePosition_Handler, + }, { MethodName: "DonateToEcosystemFund", Handler: _Msg_DonateToEcosystemFund_Handler, @@ -1256,6 +1350,51 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ Metadata: "nibiru/perp/v2/tx.proto", } +func (m *MsgSettlePosition) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgSettlePosition) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgSettlePosition) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Version != 0 { + i = encodeVarintTx(dAtA, i, uint64(m.Version)) + i-- + dAtA[i] = 0x18 + } + { + size := m.Pair.Size() + i -= size + if _, err := m.Pair.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Sender) > 0 { + i -= len(m.Sender) + copy(dAtA[i:], m.Sender) + i = encodeVarintTx(dAtA, i, uint64(len(m.Sender))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func (m *MsgRemoveMargin) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -2148,6 +2287,24 @@ func encodeVarintTx(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } +func (m *MsgSettlePosition) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = m.Pair.Size() + n += 1 + l + sovTx(uint64(l)) + if m.Version != 0 { + n += 1 + sovTx(uint64(m.Version)) + } + return n +} + func (m *MsgRemoveMargin) Size() (n int) { if m == nil { return 0 @@ -2444,6 +2601,141 @@ func sovTx(x uint64) (n int) { func sozTx(x uint64) (n int) { return sovTx(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } +func (m *MsgSettlePosition) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgSettlePosition: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgSettlePosition: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Sender", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Sender = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pair", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Pair.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Version", wireType) + } + m.Version = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Version |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *MsgRemoveMargin) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 From 8c378d4b9d0892c3571482abb088d8fbcddf6d0f Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 10 Oct 2023 15:28:52 +0200 Subject: [PATCH 3/6] nit: changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e358bcec..69029d7f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1620](https://github.com/NibiruChain/nibiru/pull/1620) - Token factory transaction messages for Mint and Burn * [#1573](https://github.com/NibiruChain/nibiru/pull/1573) - feat(perp): Close markets and compute settlement price +* [#1632](https://github.com/NibiruChain/nibiru/pull/1632) - feat(perp): Add settle position transaction ### State Machine Breaking From 4ed63f1b8aa0a7998b4e867633123632d258bbf3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 10 Oct 2023 16:04:25 +0200 Subject: [PATCH 4/6] fix: fix command count --- x/perp/v2/module/genesis_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/perp/v2/module/genesis_test.go b/x/perp/v2/module/genesis_test.go index fd049b096..e8cfca265 100644 --- a/x/perp/v2/module/genesis_test.go +++ b/x/perp/v2/module/genesis_test.go @@ -155,7 +155,7 @@ func TestNewAppModuleBasic(t *testing.T) { appModule.EndBlock(ctx, abci.RequestEndBlock{}) cmds := appModule.GetTxCmd() - require.Len(t, cmds.Commands(), 7) + require.Len(t, cmds.Commands(), 8) cmds = appModule.GetQueryCmd() require.Len(t, cmds.Commands(), 4) From 6ee1d1f6e7f6aa172c72ca190ba363948b21d1bb Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 11 Oct 2023 20:25:16 +0200 Subject: [PATCH 5/6] fix: fix settlement pnl calculation --- x/perp/v2/integration/action/settlement.go | 53 ++++++++++++++-- x/perp/v2/keeper/settlement.go | 2 +- x/perp/v2/keeper/settlement_test.go | 70 ++++++++++++++++++++++ 3 files changed, 118 insertions(+), 7 deletions(-) diff --git a/x/perp/v2/integration/action/settlement.go b/x/perp/v2/integration/action/settlement.go index b55b94c50..742e9c618 100644 --- a/x/perp/v2/integration/action/settlement.go +++ b/x/perp/v2/integration/action/settlement.go @@ -1,13 +1,17 @@ package action import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/NibiruChain/nibiru/app" "github.com/NibiruChain/nibiru/x/common/asset" "github.com/NibiruChain/nibiru/x/common/testutil/action" + "github.com/NibiruChain/nibiru/x/perp/v2/types" ) +// closeMarket type closeMarket struct { pair asset.Pair } @@ -25,25 +29,62 @@ func CloseMarket(pair asset.Pair) action.Action { return closeMarket{pair: pair} } +// settlePosition type settlePosition struct { - pair asset.Pair - version uint64 - trader sdk.AccAddress + pair asset.Pair + version uint64 + trader sdk.AccAddress + responseCheckers []SettlePositionChecker } func (c settlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { - _, err := app.PerpKeeperV2.SettlePosition(ctx, c.pair, c.version, c.trader) + resp, err := app.PerpKeeperV2.SettlePosition(ctx, c.pair, c.version, c.trader) if err != nil { return ctx, err, false } + for _, checker := range c.responseCheckers { + if err := checker(*resp); err != nil { + return ctx, err, false + } + } + return ctx, nil, true } -func SettlePosition(pair asset.Pair, version uint64, trader sdk.AccAddress) action.Action { - return settlePosition{pair: pair, version: version, trader: trader} +func SettlePosition(pair asset.Pair, version uint64, trader sdk.AccAddress, responseCheckers ...SettlePositionChecker) action.Action { + return settlePosition{pair: pair, version: version, trader: trader, responseCheckers: responseCheckers} +} + +type SettlePositionChecker func(resp types.PositionResp) error + +func SettlePositionChecker_PositionEquals(expected types.Position) SettlePositionChecker { + return func(resp types.PositionResp) error { + return types.PositionsAreEqual(&expected, &resp.Position) + } +} + +func SettlePositionChecker_MarginToVault(expectedMarginToVault sdk.Dec) SettlePositionChecker { + return func(resp types.PositionResp) error { + if expectedMarginToVault.Equal(resp.MarginToVault) { + return nil + } else { + return fmt.Errorf("expected margin to vault %s, got %s", expectedMarginToVault, resp.MarginToVault) + } + } +} + +func SettlePositionChecker_BadDebt(expectedBadDebt sdk.Dec) SettlePositionChecker { + return func(resp types.PositionResp) error { + if expectedBadDebt.Equal(resp.BadDebt) { + return nil + } else { + return fmt.Errorf("expected bad debt %s, got %s", expectedBadDebt, resp.BadDebt) + } + } } +// settlePositionShouldFail type settlePositionShouldFail struct { pair asset.Pair version uint64 diff --git a/x/perp/v2/keeper/settlement.go b/x/perp/v2/keeper/settlement.go index 0e632d167..62df34791 100644 --- a/x/perp/v2/keeper/settlement.go +++ b/x/perp/v2/keeper/settlement.go @@ -90,7 +90,7 @@ func (k Keeper) SettlePosition(ctx sdk.Context, pair asset.Pair, version uint64, // Settles a position and realizes PnL and funding payments. // Returns the updated AMM and the realized PnL and funding payments. func (k Keeper) settlePosition(ctx sdk.Context, market types.Market, amm types.AMM, position types.Position) (updatedAMM *types.AMM, resp *types.PositionResp, err error) { - positionNotional := position.Size_.Mul(amm.SettlementPrice) + positionNotional := position.Size_.Abs().Mul(amm.SettlementPrice) resp = &types.PositionResp{ ExchangedPositionSize: position.Size_.Neg(), diff --git a/x/perp/v2/keeper/settlement_test.go b/x/perp/v2/keeper/settlement_test.go index 8ad89bb73..c2e9cb15b 100644 --- a/x/perp/v2/keeper/settlement_test.go +++ b/x/perp/v2/keeper/settlement_test.go @@ -10,6 +10,7 @@ import ( "github.com/NibiruChain/nibiru/x/common/denoms" "github.com/NibiruChain/nibiru/x/common/testutil" . "github.com/NibiruChain/nibiru/x/common/testutil/action" + . "github.com/NibiruChain/nibiru/x/common/testutil/assertion" . "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/types" @@ -120,6 +121,7 @@ func TestSettlePosition(t *testing.T) { startTime := time.Now() alice := testutil.AccAddress() + bob := testutil.AccAddress() tc := TestCases{ TC("Happy path").When( @@ -146,6 +148,74 @@ func TestSettlePosition(t *testing.T) { PositionShouldNotExist(alice, pairBtcUsdc, 1), ), + TC("Happy path, but with bad debt").When( + CreateCustomMarket( + pairBtcUsdc, + WithPricePeg(sdk.OneDec()), + WithSqrtDepth(sdk.NewDec(100_000)), + ), + SetBlockNumber(1), + SetBlockTime(startTime), + FundAccount(alice, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(104)))), // need 4 because we need to pay for the close position fee + FundAccount(bob, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1_020)))), + MarketOrder( + alice, + pairBtcUsdc, + types.Direction_SHORT, + sdk.NewInt(100), + sdk.NewDec(10), + sdk.ZeroDec(), + ), + MarketOrder( + bob, + pairBtcUsdc, + types.Direction_LONG, + sdk.NewInt(1_000), + sdk.NewDec(10), + sdk.ZeroDec(), + ), + QueryPosition(pairBtcUsdc, alice, QueryPosition_MarginRatioEquals(sdk.MustNewDecFromStr("-0.093502230451982156"))), + ).When( + // Alice opened a short position (leverage x10) while bob a bigger long position + // Price jumped by 10%, with a settlement price of 1.09 + // That creates a bad debt for alice + // Her Realized Pnl is -101.01010101 and her margin is 100, so -1.01010101 is bad debt + // Bob's Realized Pnl is 1010, so he has 1010 more than his margin + + CloseMarket(pairBtcUsdc), + SettlePosition( + pairBtcUsdc, + 1, + alice, + SettlePositionChecker_PositionEquals( + types.Position{ + TraderAddress: alice.String(), + Pair: "ubtc:unusd", + Size_: sdk.MustNewDecFromStr("0"), + Margin: sdk.MustNewDecFromStr("0"), + OpenNotional: sdk.MustNewDecFromStr("0"), + LatestCumulativePremiumFraction: sdk.MustNewDecFromStr("0"), + LastUpdatedBlockNumber: 1, + }, + ), + SettlePositionChecker_MarginToVault(sdk.ZeroDec()), + SettlePositionChecker_BadDebt(sdk.MustNewDecFromStr("1.010101010101010101")), + ), + SettlePosition( + pairBtcUsdc, + 1, + bob, + SettlePositionChecker_MarginToVault(sdk.MustNewDecFromStr("-1101.010101010101010100")), + SettlePositionChecker_BadDebt(sdk.ZeroDec()), + ), + ).Then( + PositionShouldNotExist(alice, pairBtcUsdc, 1), + PositionShouldNotExist(bob, pairBtcUsdc, 1), + SetBlockNumber(2), + BalanceEqual(alice, "unusd", sdk.NewInt(0)), + BalanceEqual(bob, "unusd", sdk.NewInt(1101-20)), + ), + TC("Error: can't settle on enabled market").When( CreateCustomMarket( pairBtcUsdc, From 5120410c08b0a9b993cb89ea5b91239f63fcde22 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 11 Oct 2023 20:31:05 +0200 Subject: [PATCH 6/6] fix: more tests --- x/perp/v2/integration/action/settlement.go | 18 ++++++++++++++++++ x/perp/v2/keeper/settlement_test.go | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/x/perp/v2/integration/action/settlement.go b/x/perp/v2/integration/action/settlement.go index 742e9c618..8a28697e3 100644 --- a/x/perp/v2/integration/action/settlement.go +++ b/x/perp/v2/integration/action/settlement.go @@ -29,6 +29,24 @@ func CloseMarket(pair asset.Pair) action.Action { return closeMarket{pair: pair} } +// closeMarketShouldFail +type closeMarketShouldFail struct { + pair asset.Pair +} + +func (c closeMarketShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) { + err := app.PerpKeeperV2.CloseMarket(ctx, c.pair) + if err == nil { + return ctx, err, false + } + + return ctx, nil, true +} + +func CloseMarketShouldFail(pair asset.Pair) action.Action { + return closeMarketShouldFail{pair: pair} +} + // settlePosition type settlePosition struct { pair asset.Pair diff --git a/x/perp/v2/keeper/settlement_test.go b/x/perp/v2/keeper/settlement_test.go index c2e9cb15b..23b501ef1 100644 --- a/x/perp/v2/keeper/settlement_test.go +++ b/x/perp/v2/keeper/settlement_test.go @@ -85,6 +85,8 @@ func TestDisableMarket(t *testing.T) { ), ).When( CloseMarket(pairBtcUsdc), + CloseMarketShouldFail(pairBtcUsdc), + CloseMarketShouldFail("random:pair"), ).Then( ClosePositionFails(alice, pairBtcUsdc, types.ErrMarketNotEnabled), ), @@ -273,7 +275,6 @@ func TestSettlePosition(t *testing.T) { sdk.ZeroDec(), ), ).Then( - SettlePositionShouldFail(pairBtcUsdc, 3, alice), // can't settle on non existing market SettlePositionShouldFail(pairBtcUsdc, 2, alice), // can't settle on live market SettlePosition(pairBtcUsdc, 1, alice),