diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e2048d2..552639fc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,4 +44,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#686](https://github.com/NibiruChain/nibiru/pull/686) Add changelog enforcer to github actions. -- [#686](https://github.com/NibiruChain/nibiru/pull/686) Reorganize PerpKeeper methods \ No newline at end of file +- [#686](https://github.com/NibiruChain/nibiru/pull/686) Reorganize PerpKeeper methods + +### State Machine Breaking Change + +- [*687](https://github.com/NibiruChain/nibiru/pull/686) Emit `PositionChangedEvent` upon changing margin. diff --git a/proto/perp/v1/event.proto b/proto/perp/v1/event.proto index a4d2a03b5..84a5b2ca8 100644 --- a/proto/perp/v1/event.proto +++ b/proto/perp/v1/event.proto @@ -188,25 +188,4 @@ message PositionSettledEvent { (gogoproto.moretags) = "yaml:\"settled_coins\"", (gogoproto.nullable) = false ]; -} - -// Emitted when a position's margin is changed (AddMargin or RemoveMargin). -message MarginChangedEvent { - // Identifier for the virtual pool of the position. - string pair = 1; - - // Owner of the position. - string trader_address = 2; - - // Amount of margin exchanged. - string margin_amount = 3 [ - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", - (gogoproto.nullable) = false - ]; - - // Amount of funding payment applied. - string funding_payment = 4 [ - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", - (gogoproto.nullable) = false - ]; } \ No newline at end of file diff --git a/x/perp/keeper/calc.go b/x/perp/keeper/calc.go index 05dd3bc10..91aae566b 100644 --- a/x/perp/keeper/calc.go +++ b/x/perp/keeper/calc.go @@ -49,8 +49,8 @@ func (k Keeper) CalcRemainMarginWithFundingPayment( if currentPosition.Size_.IsZero() { remaining.FundingPayment = sdk.ZeroDec() } else { - remaining.FundingPayment = remaining.LatestCumulativePremiumFraction. - Sub(currentPosition.LastUpdateCumulativePremiumFraction). + remaining.FundingPayment = (remaining.LatestCumulativePremiumFraction. + Sub(currentPosition.LastUpdateCumulativePremiumFraction)). Mul(currentPosition.Size_) } @@ -88,7 +88,7 @@ func (k Keeper) calcFreeCollateral( return sdk.Dec{}, err } - if err := k.requireVpool(ctx, pos.Pair); err != nil { + if err = k.requireVpool(ctx, pos.Pair); err != nil { return sdk.Dec{}, err } diff --git a/x/perp/keeper/margin.go b/x/perp/keeper/margin.go index 4456cc6c2..3dc0c8721 100644 --- a/x/perp/keeper/margin.go +++ b/x/perp/keeper/margin.go @@ -21,7 +21,7 @@ func (k Keeper) AddMargin( ctx := sdk.UnwrapSDKContext(goCtx) // validate trader - msgSender, err := sdk.AccAddressFromBech32(msg.Sender) + traderAddr, err := sdk.AccAddressFromBech32(msg.Sender) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func (k Keeper) AddMargin( } // ------------- AddMargin ------------- - position, err := k.PositionsState(ctx).Get(pair, msgSender) + position, err := k.PositionsState(ctx).Get(pair, traderAddr) if err != nil { return nil, err } @@ -67,7 +67,7 @@ func (k Keeper) AddMargin( coinToSend := sdk.NewCoin(pair.GetQuoteTokenDenom(), msg.Margin.Amount) if err = k.BankKeeper.SendCoinsFromAccountToModule( - ctx, msgSender, types.VaultModuleAccount, sdk.NewCoins(coinToSend), + ctx, traderAddr, types.VaultModuleAccount, sdk.NewCoins(coinToSend), ); err != nil { return nil, err } @@ -75,14 +75,35 @@ func (k Keeper) AddMargin( position.Margin = remainingMargin.Margin position.LastUpdateCumulativePremiumFraction = remainingMargin.LatestCumulativePremiumFraction position.BlockNumber = ctx.BlockHeight() - k.PositionsState(ctx).Set(pair, msgSender, position) + k.PositionsState(ctx).Set(pair, traderAddr, position) + + positionNotional, unrealizedPnl, err := k.getPositionNotionalAndUnrealizedPnL(ctx, *position, types.PnLCalcOption_SPOT_PRICE) + if err != nil { + return nil, err + } + + spotPrice, err := k.VpoolKeeper.GetSpotPrice(ctx, pair) + if err != nil { + return nil, err + } err = ctx.EventManager().EmitTypedEvent( - &types.MarginChangedEvent{ - Pair: pair.String(), - TraderAddress: msgSender.String(), - MarginAmount: msg.Margin.Amount, - FundingPayment: remainingMargin.FundingPayment, + &types.PositionChangedEvent{ + Pair: pair.String(), + TraderAddress: traderAddr.String(), + Margin: sdk.NewCoin(pair.GetQuoteTokenDenom(), position.Margin.RoundInt()), + PositionNotional: positionNotional, + ExchangedPositionSize: sdk.ZeroDec(), // always zero when adding margin + TransactionFee: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin + PositionSize: position.Size_, + RealizedPnl: sdk.ZeroDec(), // always zero when adding margin + UnrealizedPnlAfter: unrealizedPnl, + BadDebt: remainingMargin.BadDebt, // always zero when adding margin + FundingPayment: remainingMargin.FundingPayment, + SpotPrice: spotPrice, + BlockHeight: ctx.BlockHeight(), + BlockTimeMs: ctx.BlockTime().UnixMilli(), + LiquidationPenalty: sdk.ZeroDec(), }, ) @@ -138,8 +159,7 @@ func (k Keeper) RemoveMargin( } marginDelta := msg.Margin.Amount.Neg() - remainingMargin, err := k.CalcRemainMarginWithFundingPayment( - ctx, *position, marginDelta.ToDec()) + remainingMargin, err := k.CalcRemainMarginWithFundingPayment(ctx, *position, marginDelta.ToDec()) if err != nil { return nil, err } @@ -151,28 +171,49 @@ func (k Keeper) RemoveMargin( position.LastUpdateCumulativePremiumFraction = remainingMargin.LatestCumulativePremiumFraction freeCollateral, err := k.calcFreeCollateral(ctx, *position) if err != nil { - return res, err + return nil, err } else if !freeCollateral.IsPositive() { - return res, fmt.Errorf("not enough free collateral") + return nil, fmt.Errorf("not enough free collateral") } k.PositionsState(ctx).Set(pair, traderAddr, position) - coinToSend := sdk.NewCoin(pair.GetQuoteTokenDenom(), msg.Margin.Amount) - err = k.Withdraw(ctx, pair.GetQuoteTokenDenom(), traderAddr, msg.Margin.Amount) + positionNotional, unrealizedPnl, err := k.getPositionNotionalAndUnrealizedPnL(ctx, *position, types.PnLCalcOption_SPOT_PRICE) if err != nil { return nil, err } - err = ctx.EventManager().EmitTypedEvent(&types.MarginChangedEvent{ - Pair: pair.String(), - TraderAddress: traderAddr.String(), - MarginAmount: msg.Margin.Amount, - FundingPayment: remainingMargin.FundingPayment, - }) + spotPrice, err := k.VpoolKeeper.GetSpotPrice(ctx, pair) + if err != nil { + return nil, err + } + + if err = k.Withdraw(ctx, pair.GetQuoteTokenDenom(), traderAddr, msg.Margin.Amount); err != nil { + return nil, err + } + + err = ctx.EventManager().EmitTypedEvent( + &types.PositionChangedEvent{ + Pair: pair.String(), + TraderAddress: traderAddr.String(), + Margin: sdk.NewCoin(pair.GetQuoteTokenDenom(), position.Margin.RoundInt()), + PositionNotional: positionNotional, + ExchangedPositionSize: sdk.ZeroDec(), // always zero when removing margin + TransactionFee: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when removing margin + PositionSize: position.Size_, + RealizedPnl: sdk.ZeroDec(), // always zero when removing margin + UnrealizedPnlAfter: unrealizedPnl, + BadDebt: remainingMargin.BadDebt, // always zero when removing margin + FundingPayment: remainingMargin.FundingPayment, + SpotPrice: spotPrice, + BlockHeight: ctx.BlockHeight(), + BlockTimeMs: ctx.BlockTime().UnixMilli(), + LiquidationPenalty: sdk.ZeroDec(), + }, + ) return &types.MsgRemoveMarginResponse{ - MarginOut: coinToSend, + MarginOut: sdk.NewCoin(pair.GetQuoteTokenDenom(), msg.Margin.Amount), FundingPayment: remainingMargin.FundingPayment, }, err } diff --git a/x/perp/keeper/margin_test.go b/x/perp/keeper/margin_test.go index c1f5fe19a..06812174f 100644 --- a/x/perp/keeper/margin_test.go +++ b/x/perp/keeper/margin_test.go @@ -464,10 +464,8 @@ func TestRemoveMargin(t *testing.T) { t.Log("Set vpool defined by pair on PerpKeeper") perpKeeper := &nibiruApp.PerpKeeper perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ - Pair: pair, - CumulativePremiumFractions: []sdk.Dec{ - sdk.ZeroDec(), - sdk.MustNewDecFromStr("0.1")}, + Pair: pair, + CumulativePremiumFractions: []sdk.Dec{sdk.ZeroDec()}, }) t.Log("increment block height and time for twap calculation") @@ -475,24 +473,19 @@ func TestRemoveMargin(t *testing.T) { WithBlockTime(time.Now().Add(time.Minute)) t.Log("Fund trader account with sufficient quote") - - err := simapp.FundAccount(nibiruApp.BankKeeper, ctx, traderAddr, - sdk.NewCoins( - sdk.NewInt64Coin("yyy", 66), - )) - require.NoError(t, err) + require.NoError(t, simapp.FundAccount(nibiruApp.BankKeeper, ctx, traderAddr, + sdk.NewCoins(sdk.NewInt64Coin("yyy", 60))), + ) t.Log("Open long position with 5x leverage") side := types.Side_BUY quote := sdk.NewInt(60) leverage := sdk.NewDec(5) - baseLimit := sdk.NewInt(10) - err = nibiruApp.PerpKeeper.OpenPosition( - ctx, pair, side, traderAddr, quote, leverage, baseLimit.ToDec()) - require.NoError(t, err) + baseLimit := sdk.ZeroDec() + require.NoError(t, perpKeeper.OpenPosition(ctx, pair, side, traderAddr, quote, leverage, baseLimit)) t.Log("Position should be accessible following 'OpenPosition'") - _, err = nibiruApp.PerpKeeper.PositionsState(ctx).Get(pair, traderAddr) + _, err := nibiruApp.PerpKeeper.PositionsState(ctx).Get(pair, traderAddr) require.NoError(t, err) t.Log("Verify correct events emitted for 'OpenPosition'") @@ -513,12 +506,25 @@ func TestRemoveMargin(t *testing.T) { assert.EqualValues(t, sdk.ZeroDec(), res.FundingPayment) t.Log("Verify correct events emitted for 'RemoveMargin'") - testutilevents.RequireHasTypedEvent(t, ctx, &types.MarginChangedEvent{ - Pair: pair.String(), - TraderAddress: traderAddr.String(), - MarginAmount: msg.Margin.Amount, - FundingPayment: res.FundingPayment, - }) + testutilevents.RequireContainsTypedEvent(t, ctx, + &types.PositionChangedEvent{ + Pair: msg.TokenPair, + TraderAddress: traderAddr.String(), + Margin: sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 54), + PositionNotional: sdk.NewDec(300), + ExchangedPositionSize: sdk.ZeroDec(), // always zero when removing margin + TransactionFee: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when removing margin + PositionSize: sdk.MustNewDecFromStr("299.910026991902429271"), + RealizedPnl: sdk.ZeroDec(), // always zero when removing margin + UnrealizedPnlAfter: sdk.ZeroDec(), + BadDebt: sdk.ZeroDec(), // always zero when removing margin + FundingPayment: sdk.ZeroDec(), + SpotPrice: sdk.MustNewDecFromStr("1.00060009"), + BlockHeight: ctx.BlockHeight(), + BlockTimeMs: ctx.BlockTime().UnixMilli(), + LiquidationPenalty: sdk.ZeroDec(), + }, + ) }, }, } diff --git a/x/perp/keeper/margin_unit_test.go b/x/perp/keeper/margin_unit_test.go index 931732074..652b9945d 100644 --- a/x/perp/keeper/margin_unit_test.go +++ b/x/perp/keeper/margin_unit_test.go @@ -19,7 +19,7 @@ import ( vpooltypes "github.com/NibiruChain/nibiru/x/vpool/types" ) -func Test_requireMoreMarginRatio(t *testing.T) { +func TestRequireMoreMarginRatio(t *testing.T) { type test struct { marginRatio, baseMarginRatio sdk.Dec largerThanEqualTo bool @@ -267,34 +267,15 @@ func TestRemoveMargin(t *testing.T) { pair := common.MustNewAssetPair(msg.TokenPair) - mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair). - AnyTimes().Return(true) - - t.Log("Set vpool defined by pair on PerpKeeper") - perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ - Pair: pair, - CumulativePremiumFractions: []sdk.Dec{ - sdk.ZeroDec(), - sdk.MustNewDecFromStr("0.1")}, - }) - - t.Log("Set position a healthy position that has 0 unrealized funding") - perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ - TraderAddress: traderAddr.String(), - Pair: pair, - Size_: sdk.NewDec(1_000), - OpenNotional: sdk.NewDec(1_000), - Margin: sdk.NewDec(500), - LastUpdateCumulativePremiumFraction: sdk.MustNewDecFromStr("0.1"), - BlockNumber: ctx.BlockHeight(), - }) - + t.Log("mock vpool keeper") + mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair).AnyTimes().Return(true) + mocks.mockVpoolKeeper.EXPECT().GetSpotPrice(ctx, pair).Return(sdk.OneDec(), nil) mocks.mockVpoolKeeper.EXPECT().GetBaseAssetPrice( ctx, pair, vpooltypes.Direction_ADD_TO_POOL, sdk.NewDec(1_000), - ).Return(sdk.NewDec(1000), nil) + ).Return(sdk.NewDec(1000), nil).Times(2) mocks.mockVpoolKeeper.EXPECT().GetBaseAssetTWAP( ctx, pair, @@ -303,18 +284,38 @@ func TestRemoveMargin(t *testing.T) { 15*time.Minute, ).Return(sdk.NewDec(1000), nil) - t.Log("Attempt to RemoveMargin when the vault lacks funds") - expectedError := fmt.Errorf("not enough funds in vault module account") + t.Log("mock account keeper") mocks.mockAccountKeeper. EXPECT().GetModuleAddress(types.VaultModuleAccount). Return(authtypes.NewModuleAddress(types.VaultModuleAccount)) + t.Log("mock bank keeper") + expectedError := fmt.Errorf("not enough funds in vault module account") mocks.mockBankKeeper.EXPECT().SendCoinsFromModuleToModule( ctx, types.PerpEFModuleAccount, types.VaultModuleAccount, sdk.NewCoins(msg.Margin), ).Return(expectedError) - mocks.mockBankKeeper.EXPECT().GetBalance(ctx, authtypes.NewModuleAddress(types.VaultModuleAccount), pair.GetQuoteTokenDenom()).Return(sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt())) + t.Log("set pair metadata") + perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ + Pair: pair, + CumulativePremiumFractions: []sdk.Dec{ + sdk.ZeroDec(), + }, + }) + + t.Log("Set position a healthy position that has 0 unrealized funding") + perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ + TraderAddress: traderAddr.String(), + Pair: pair, + Size_: sdk.NewDec(1_000), + OpenNotional: sdk.NewDec(1_000), + Margin: sdk.NewDec(500), + LastUpdateCumulativePremiumFraction: sdk.ZeroDec(), + BlockNumber: ctx.BlockHeight(), + }) + + t.Log("Attempt to RemoveMargin when the vault lacks funds") _, err := perpKeeper.RemoveMargin(goCtx, msg) require.Error(t, err) @@ -336,48 +337,49 @@ func TestRemoveMargin(t *testing.T) { pair := common.MustNewAssetPair(msg.TokenPair) - mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair). - AnyTimes().Return(true) - - t.Log("Set vpool defined by pair on PerpKeeper") - perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ - Pair: pair, - CumulativePremiumFractions: []sdk.Dec{ - sdk.ZeroDec(), - sdk.MustNewDecFromStr("0.1")}, - }) - - t.Log("Set position a healthy position that has 0 unrealized funding") - perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ - TraderAddress: traderAddr.String(), - Pair: pair, - Size_: sdk.NewDec(1_000), - OpenNotional: sdk.NewDec(1_000), - Margin: sdk.NewDec(500), - LastUpdateCumulativePremiumFraction: sdk.MustNewDecFromStr("0.1"), - BlockNumber: ctx.BlockHeight(), - }) - + t.Log("mock vpool keeper") + mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair).Return(true).Times(2) + mocks.mockVpoolKeeper.EXPECT().GetSpotPrice(ctx, pair).Return(sdk.OneDec(), nil) mocks.mockVpoolKeeper.EXPECT().GetBaseAssetPrice( ctx, pair, vpooltypes.Direction_ADD_TO_POOL, sdk.NewDec(1_000)). - Return(sdk.NewDec(1000), nil) + Return(sdk.NewDec(1000), nil).Times(2) mocks.mockVpoolKeeper.EXPECT().GetBaseAssetTWAP( ctx, pair, vpooltypes.Direction_ADD_TO_POOL, sdk.NewDec(1_000), 15*time.Minute, ).Return(sdk.NewDec(1000), nil) + t.Log("mock account keeper") mocks.mockAccountKeeper. EXPECT().GetModuleAddress(types.VaultModuleAccount). Return(authtypes.NewModuleAddress(types.VaultModuleAccount)) + t.Log("mock bank keeper") mocks.mockBankKeeper. EXPECT().GetBalance(ctx, authtypes.NewModuleAddress(types.VaultModuleAccount), pair.GetQuoteTokenDenom()). Return(sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.NewInt(math.MaxInt64))) - mocks.mockBankKeeper.EXPECT().SendCoinsFromModuleToAccount( ctx, types.VaultModuleAccount, traderAddr, sdk.NewCoins(msg.Margin), ).Return(nil) + t.Log("set pair metadata") + perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ + Pair: pair, + CumulativePremiumFractions: []sdk.Dec{ + sdk.ZeroDec(), + }, + }) + + t.Log("Set position a healthy position that has 0 unrealized funding") + perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ + TraderAddress: traderAddr.String(), + Pair: pair, + Size_: sdk.NewDec(1_000), + OpenNotional: sdk.NewDec(1_000), + Margin: sdk.NewDec(500), + LastUpdateCumulativePremiumFraction: sdk.ZeroDec(), + BlockNumber: ctx.BlockHeight(), + }) + t.Log("'RemoveMargin' from the position") res, err := perpKeeper.RemoveMargin(goCtx, msg) @@ -386,12 +388,25 @@ func TestRemoveMargin(t *testing.T) { assert.EqualValues(t, sdk.ZeroDec(), res.FundingPayment) t.Log("Verify correct events emitted for 'RemoveMargin'") - testutilevents.RequireHasTypedEvent(t, ctx, &types.MarginChangedEvent{ - Pair: msg.TokenPair, - TraderAddress: traderAddr.String(), - MarginAmount: msg.Margin.Amount, - FundingPayment: res.FundingPayment, - }) + testutilevents.RequireHasTypedEvent(t, ctx, + &types.PositionChangedEvent{ + Pair: msg.TokenPair, + TraderAddress: traderAddr.String(), + Margin: sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 400), + PositionNotional: sdk.NewDec(1000), + ExchangedPositionSize: sdk.ZeroDec(), // always zero when removing margin + TransactionFee: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when removing margin + PositionSize: sdk.NewDec(1000), + RealizedPnl: sdk.ZeroDec(), // always zero when removing margin + UnrealizedPnlAfter: sdk.ZeroDec(), + BadDebt: sdk.ZeroDec(), // always zero when removing margin + FundingPayment: sdk.ZeroDec(), + SpotPrice: sdk.OneDec(), + BlockHeight: ctx.BlockHeight(), + BlockTimeMs: ctx.BlockTime().UnixMilli(), + LiquidationPenalty: sdk.ZeroDec(), + }, + ) pos, err := perpKeeper.PositionsState(ctx).Get(pair, traderAddr) require.NoError(t, err) @@ -400,6 +415,56 @@ func TestRemoveMargin(t *testing.T) { assert.EqualValues(t, traderAddr.String(), pos.TraderAddress) }, }, + { + name: "happy path - massive funding payment", + test: func() { + perpKeeper, mocks, ctx := getKeeper(t) + goCtx := sdk.WrapSDKContext(ctx) + + traderAddr := sample.AccAddress() + msg := &types.MsgRemoveMargin{ + Sender: traderAddr.String(), + TokenPair: "osmo:nusd", + Margin: sdk.NewCoin("nusd", sdk.NewInt(100)), + } + + pair := common.MustNewAssetPair(msg.TokenPair) + + t.Log("mock vpool keeper") + mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair).Return(true) + + t.Log("set pair metadata") + perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ + Pair: pair, + CumulativePremiumFractions: []sdk.Dec{ + sdk.OneDec(), + }, + }) + + t.Log("Set position a healthy position that has 0 unrealized funding") + perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ + TraderAddress: traderAddr.String(), + Pair: pair, + Size_: sdk.NewDec(500), + OpenNotional: sdk.NewDec(500), + Margin: sdk.NewDec(500), + LastUpdateCumulativePremiumFraction: sdk.ZeroDec(), + BlockNumber: ctx.BlockHeight(), + }) + + t.Log("'RemoveMargin' from the position") + res, err := perpKeeper.RemoveMargin(goCtx, msg) + + require.ErrorIs(t, err, types.ErrFailedRemoveMarginCanCauseBadDebt) + require.Nil(t, res) + + pos, err := perpKeeper.PositionsState(ctx).Get(pair, traderAddr) + require.NoError(t, err) + assert.EqualValues(t, sdk.NewDec(500).String(), pos.Margin.String()) + assert.EqualValues(t, sdk.NewDec(500).String(), pos.Size_.String()) + assert.EqualValues(t, traderAddr.String(), pos.TraderAddress) + }, + }, } for _, testCase := range tests { @@ -500,31 +565,33 @@ func TestAddMargin(t *testing.T) { perpKeeper, mocks, ctx := getKeeper(t) goCtx := sdk.WrapSDKContext(ctx) - assetPair := common.MustNewAssetPair("uosmo:unusd") + pair := common.MustNewAssetPair("uosmo:unusd") traderAddr := sample.AccAddress() msg := &types.MsgAddMargin{ Sender: traderAddr.String(), - TokenPair: assetPair.String(), + TokenPair: pair.String(), Margin: sdk.NewInt64Coin("unusd", 100), } - mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, assetPair). + mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair). AnyTimes().Return(true) + mocks.mockVpoolKeeper.EXPECT().GetBaseAssetPrice(ctx, pair, vpooltypes.Direction_ADD_TO_POOL, sdk.NewDec(1000)).Return(sdk.NewDec(1000), nil) + mocks.mockVpoolKeeper.EXPECT().GetSpotPrice(ctx, pair).Return(sdk.OneDec(), nil) t.Log("set pair metadata") perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ - Pair: assetPair, + Pair: pair, CumulativePremiumFractions: []sdk.Dec{ sdk.ZeroDec(), }, }) t.Log("set position") - perpKeeper.PositionsState(ctx).Set(assetPair, traderAddr, &types.Position{ + perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ TraderAddress: traderAddr.String(), - Pair: assetPair, + Pair: pair, Size_: sdk.NewDec(1_000), OpenNotional: sdk.NewDec(1_000), Margin: sdk.NewDec(500), @@ -547,17 +614,28 @@ func TestAddMargin(t *testing.T) { assert.EqualValues(t, sdk.NewDec(1_000), resp.Position.OpenNotional) assert.EqualValues(t, sdk.NewDec(1_000), resp.Position.Size_) assert.EqualValues(t, traderAddr.String(), resp.Position.TraderAddress) - assert.EqualValues(t, assetPair, resp.Position.Pair) + assert.EqualValues(t, pair, resp.Position.Pair) assert.EqualValues(t, sdk.ZeroDec(), resp.Position.LastUpdateCumulativePremiumFraction) assert.EqualValues(t, ctx.BlockHeight(), resp.Position.BlockNumber) t.Log("Verify correct events emitted") testutilevents.RequireHasTypedEvent(t, ctx, - &types.MarginChangedEvent{ - Pair: msg.TokenPair, - TraderAddress: traderAddr.String(), - MarginAmount: msg.Margin.Amount, - FundingPayment: sdk.ZeroDec(), + &types.PositionChangedEvent{ + Pair: msg.TokenPair, + TraderAddress: traderAddr.String(), + Margin: sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 600), + PositionNotional: sdk.NewDec(1000), + ExchangedPositionSize: sdk.ZeroDec(), // always zero when adding margin + TransactionFee: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin + PositionSize: sdk.NewDec(1000), + RealizedPnl: sdk.ZeroDec(), // always zero when adding margin + UnrealizedPnlAfter: sdk.ZeroDec(), + BadDebt: sdk.ZeroDec(), // always zero when adding margin + FundingPayment: sdk.ZeroDec(), + SpotPrice: sdk.OneDec(), + BlockHeight: ctx.BlockHeight(), + BlockTimeMs: ctx.BlockTime().UnixMilli(), + LiquidationPenalty: sdk.ZeroDec(), }, ) }, @@ -567,31 +645,32 @@ func TestAddMargin(t *testing.T) { test: func() { perpKeeper, mocks, ctx := getKeeper(t) - assetPair := common.MustNewAssetPair("uosmo:unusd") + pair := common.MustNewAssetPair("uosmo:unusd") traderAddr := sample.AccAddress() msg := &types.MsgAddMargin{ Sender: traderAddr.String(), - TokenPair: assetPair.String(), + TokenPair: pair.String(), Margin: sdk.NewInt64Coin("unusd", 100), } - mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, assetPair). - AnyTimes().Return(true) + mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, pair).AnyTimes().Return(true) + mocks.mockVpoolKeeper.EXPECT().GetBaseAssetPrice(ctx, pair, vpooltypes.Direction_ADD_TO_POOL, sdk.NewDec(1000)).Return(sdk.NewDec(1000), nil) + mocks.mockVpoolKeeper.EXPECT().GetSpotPrice(ctx, pair).Return(sdk.OneDec(), nil) t.Log("set pair metadata") perpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{ - Pair: assetPair, + Pair: pair, CumulativePremiumFractions: []sdk.Dec{ sdk.MustNewDecFromStr("0.001"), }, }) t.Log("set position") - perpKeeper.PositionsState(ctx).Set(assetPair, traderAddr, &types.Position{ + perpKeeper.PositionsState(ctx).Set(pair, traderAddr, &types.Position{ TraderAddress: traderAddr.String(), - Pair: assetPair, + Pair: pair, Size_: sdk.NewDec(1_000), OpenNotional: sdk.NewDec(1_000), Margin: sdk.NewDec(500), @@ -613,24 +692,35 @@ func TestAddMargin(t *testing.T) { assert.EqualValues(t, sdk.NewDec(1_000), resp.Position.OpenNotional) assert.EqualValues(t, sdk.NewDec(1_000), resp.Position.Size_) assert.EqualValues(t, traderAddr.String(), resp.Position.TraderAddress) - assert.EqualValues(t, assetPair, resp.Position.Pair) + assert.EqualValues(t, pair, resp.Position.Pair) assert.EqualValues(t, sdk.MustNewDecFromStr("0.001"), resp.Position.LastUpdateCumulativePremiumFraction) assert.EqualValues(t, ctx.BlockHeight(), resp.Position.BlockNumber) t.Log("assert correct final position in state") - pos, err := perpKeeper.PositionsState(ctx).Get(assetPair, traderAddr) + pos, err := perpKeeper.PositionsState(ctx).Get(pair, traderAddr) require.NoError(t, err) assert.EqualValues(t, sdk.NewDec(599).String(), pos.Margin.String()) assert.EqualValues(t, sdk.NewDec(1000).String(), pos.Size_.String()) assert.EqualValues(t, traderAddr.String(), pos.TraderAddress) - t.Log("assert correct events emitted") + t.Log("Verify correct events emitted") testutilevents.RequireHasTypedEvent(t, ctx, - &types.MarginChangedEvent{ - Pair: msg.TokenPair, - TraderAddress: traderAddr.String(), - MarginAmount: msg.Margin.Amount, - FundingPayment: sdk.NewDec(1), + &types.PositionChangedEvent{ + Pair: msg.TokenPair, + TraderAddress: traderAddr.String(), + Margin: sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 599), + PositionNotional: sdk.NewDec(1000), + ExchangedPositionSize: sdk.ZeroDec(), // always zero when adding margin + TransactionFee: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin + PositionSize: sdk.NewDec(1000), + RealizedPnl: sdk.ZeroDec(), // always zero when adding margin + UnrealizedPnlAfter: sdk.ZeroDec(), + BadDebt: sdk.ZeroDec(), // always zero when adding margin + FundingPayment: sdk.OneDec(), + SpotPrice: sdk.OneDec(), + BlockHeight: ctx.BlockHeight(), + BlockTimeMs: ctx.BlockTime().UnixMilli(), + LiquidationPenalty: sdk.ZeroDec(), }, ) }, diff --git a/x/perp/types/event.pb.go b/x/perp/types/event.pb.go index f9c80841d..d0b4d521c 100644 --- a/x/perp/types/event.pb.go +++ b/x/perp/types/event.pb.go @@ -327,132 +327,70 @@ func (m *PositionSettledEvent) GetSettledCoins() github_com_cosmos_cosmos_sdk_ty return nil } -// Emitted when a position's margin is changed (AddMargin or RemoveMargin). -type MarginChangedEvent struct { - // Identifier for the virtual pool of the position. - Pair string `protobuf:"bytes,1,opt,name=pair,proto3" json:"pair,omitempty"` - // Owner of the position. - TraderAddress string `protobuf:"bytes,2,opt,name=trader_address,json=traderAddress,proto3" json:"trader_address,omitempty"` - // Amount of margin exchanged. - MarginAmount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,3,opt,name=margin_amount,json=marginAmount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"margin_amount"` - // Amount of funding payment applied. - FundingPayment github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=funding_payment,json=fundingPayment,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"funding_payment"` -} - -func (m *MarginChangedEvent) Reset() { *m = MarginChangedEvent{} } -func (m *MarginChangedEvent) String() string { return proto.CompactTextString(m) } -func (*MarginChangedEvent) ProtoMessage() {} -func (*MarginChangedEvent) Descriptor() ([]byte, []int) { - return fileDescriptor_19b7f9ebcf2fdb5b, []int{3} -} -func (m *MarginChangedEvent) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *MarginChangedEvent) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_MarginChangedEvent.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 *MarginChangedEvent) XXX_Merge(src proto.Message) { - xxx_messageInfo_MarginChangedEvent.Merge(m, src) -} -func (m *MarginChangedEvent) XXX_Size() int { - return m.Size() -} -func (m *MarginChangedEvent) XXX_DiscardUnknown() { - xxx_messageInfo_MarginChangedEvent.DiscardUnknown(m) -} - -var xxx_messageInfo_MarginChangedEvent proto.InternalMessageInfo - -func (m *MarginChangedEvent) GetPair() string { - if m != nil { - return m.Pair - } - return "" -} - -func (m *MarginChangedEvent) GetTraderAddress() string { - if m != nil { - return m.TraderAddress - } - return "" -} - func init() { proto.RegisterType((*PositionChangedEvent)(nil), "nibiru.perp.v1.PositionChangedEvent") proto.RegisterType((*PositionLiquidatedEvent)(nil), "nibiru.perp.v1.PositionLiquidatedEvent") proto.RegisterType((*PositionSettledEvent)(nil), "nibiru.perp.v1.PositionSettledEvent") - proto.RegisterType((*MarginChangedEvent)(nil), "nibiru.perp.v1.MarginChangedEvent") } func init() { proto.RegisterFile("perp/v1/event.proto", fileDescriptor_19b7f9ebcf2fdb5b) } var fileDescriptor_19b7f9ebcf2fdb5b = []byte{ - // 887 bytes of a gzipped FileDescriptorProto + // 854 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x96, 0x4d, 0x6f, 0xdc, 0x44, 0x18, 0xc7, 0x63, 0x12, 0xd2, 0xdd, 0xd9, 0x97, 0x90, 0xe9, 0xa6, 0x35, 0x55, 0xb5, 0x59, 0x2c, - 0x40, 0x11, 0x52, 0x6d, 0x05, 0x6e, 0xdc, 0xf2, 0xd2, 0x2a, 0x95, 0x9a, 0x6a, 0xeb, 0x44, 0x42, - 0x82, 0x83, 0x19, 0xdb, 0xcf, 0x7a, 0x47, 0xb1, 0x67, 0x5c, 0xcf, 0x6c, 0xd4, 0xcd, 0x27, 0xe0, - 0xc8, 0xe7, 0xe0, 0x93, 0xf4,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0x7c, 0xac, 0x03, 0x3e, 0x9e, 0x5a, 0x9f, 0x94, 0x2c, 0x5e, 0xba, 0xcb, + 0x8d, 0xff, 0x76, 0x97, 0xe7, 0x6f, 0x52, 0xf3, 0xff, 0x78, 0x93, 0xd0, 0x8a, 0xde, 0xa4, 0xb7, + 0x74, 0xbc, 0xb5, 0x02, 0x1d, 0x3f, 0x43, 0x9d, 0x25, 0xa1, 0xac, 0xa9, 0x34, 0xcb, 0x10, 0x25, + 0x5e, 0x19, 0x29, 0xce, 0x2b, 0xf1, 0xaa, 0x27, 0x34, 0x4d, 0x45, 0x30, 0xe2, 0xb5, 0x22, 0x8d, + 0xf9, 0xc5, 0x9a, 0xcf, 0x33, 0xa7, 0x20, 0x65, 0xba, 0x02, 0x81, 0xf9, 0xde, 0x42, 0x1d, 0x61, + 0x58, 0x81, 0x1a, 0x9d, 0x84, 0xbd, 0x3e, 0x58, 0xbf, 0xfd, 0x0c, 0x9d, 0x54, 0x67, 0xa8, 0x67, + 0xce, 0xd0, 0xd2, 0x6e, 0xe7, 0xc7, 0x5f, 0x77, 0xf7, 0xfe, 0x41, 0x83, 0x14, 0x48, 0xf8, 0xed, + 0x6a, 0xaf, 0xfe, 0x77, 0x78, 0xfc, 0xfa, 0xba, 0x6f, 0xbd, 0xb9, 0xee, 0x5b, 0xbf, 0x5d, 0xf7, + 0xad, 0x1f, 0x6e, 0xfa, 0x6b, 0x6f, 0x6e, 0xfa, 0x6b, 0x3f, 0xdd, 0xf4, 0xd7, 0xbe, 0xf9, 0x6c, + 0x81, 0xf8, 0x5c, 0x0f, 0x93, 0x47, 0x63, 0x42, 0x99, 0x67, 0x06, 0x4b, 0xef, 0x95, 0xa7, 0xa7, + 0x43, 0x4d, 0x0e, 0x37, 0xf5, 0xec, 0xf7, 0xc5, 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x13, 0x0d, + 0xec, 0x28, 0x8b, 0x0a, 0x00, 0x00, } func (m *PositionChangedEvent) Marshal() (dAtA []byte, err error) { @@ -817,63 +755,6 @@ func (m *PositionSettledEvent) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *MarginChangedEvent) 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 *MarginChangedEvent) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *MarginChangedEvent) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - { - size := m.FundingPayment.Size() - i -= size - if _, err := m.FundingPayment.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintEvent(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x22 - { - size := m.MarginAmount.Size() - i -= size - if _, err := m.MarginAmount.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintEvent(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a - if len(m.TraderAddress) > 0 { - i -= len(m.TraderAddress) - copy(dAtA[i:], m.TraderAddress) - i = encodeVarintEvent(dAtA, i, uint64(len(m.TraderAddress))) - i-- - dAtA[i] = 0x12 - } - if len(m.Pair) > 0 { - i -= len(m.Pair) - copy(dAtA[i:], m.Pair) - i = encodeVarintEvent(dAtA, i, uint64(len(m.Pair))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - func encodeVarintEvent(dAtA []byte, offset int, v uint64) int { offset -= sovEvent(v) base := offset @@ -1000,27 +881,6 @@ func (m *PositionSettledEvent) Size() (n int) { return n } -func (m *MarginChangedEvent) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Pair) - if l > 0 { - n += 1 + l + sovEvent(uint64(l)) - } - l = len(m.TraderAddress) - if l > 0 { - n += 1 + l + sovEvent(uint64(l)) - } - l = m.MarginAmount.Size() - n += 1 + l + sovEvent(uint64(l)) - l = m.FundingPayment.Size() - n += 1 + l + sovEvent(uint64(l)) - return n -} - func sovEvent(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -2220,188 +2080,6 @@ func (m *PositionSettledEvent) Unmarshal(dAtA []byte) error { } return nil } -func (m *MarginChangedEvent) 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 ErrIntOverflowEvent - } - 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: MarginChangedEvent: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: MarginChangedEvent: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - 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 ErrIntOverflowEvent - } - 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 ErrInvalidLengthEvent - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthEvent - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Pair = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field TraderAddress", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowEvent - } - 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 ErrInvalidLengthEvent - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthEvent - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.TraderAddress = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field MarginAmount", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowEvent - } - 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 ErrInvalidLengthEvent - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthEvent - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.MarginAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 4: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field FundingPayment", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowEvent - } - 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 ErrInvalidLengthEvent - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthEvent - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.FundingPayment.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipEvent(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthEvent - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipEvent(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0