Skip to content

Commit

Permalink
feat: add differential fields to PositionChangedEvent: size and open …
Browse files Browse the repository at this point in the history
…notional
  • Loading branch information
matthiasmatt committed Jul 31, 2023
1 parent 89b005e commit b8e33f1
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 248 deletions.
18 changes: 18 additions & 0 deletions proto/nibiru/perp/v2/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ message PositionChangedEvent {
// - CHANGE_REASON_CLOSE_POSITION: An existing position was closed.
string change_reason = 9
[ (gogoproto.customtype) = "ChangeReason", (gogoproto.nullable) = false ];

// exchanged_size represent the change in size for an existing position
// after the change. A positive value indicates that the position size
// increased, while a negative value indicates that the position size
// decreased.
string exchanged_size = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// exchanged_notional represent the change in notional for an existing
// position after the change. A positive value indicates that the position
// notional increased, while a negative value indicates that the position
// notional decreased.
string exchanged_notional = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// Emitted when a position is liquidated. Wraps a PositionChanged event since a
Expand Down
25 changes: 15 additions & 10 deletions x/perp/v2/keeper/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (k Keeper) MarketOrder(
}

if err = k.afterPositionUpdate(
ctx, market, *updatedAMM, traderAddr, *positionResp, types.ChangeReason_MarketOrder,
ctx, market, *updatedAMM, traderAddr, *positionResp, types.ChangeReason_MarketOrder, position,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -537,6 +537,7 @@ func (k Keeper) afterPositionUpdate(
traderAddr sdk.AccAddress,
positionResp types.PositionResp,
changeType types.ChangeReason,
existingPosition types.Position,
) (err error) {
// transfer trader <=> vault
marginToVault := positionResp.MarginToVault.RoundInt()
Expand Down Expand Up @@ -576,15 +577,17 @@ func (k Keeper) afterPositionUpdate(

_ = ctx.EventManager().EmitTypedEvents(
&types.PositionChangedEvent{
FinalPosition: positionResp.Position,
PositionNotional: positionNotional,
TransactionFee: sdk.NewCoin(market.Pair.QuoteDenom(), transferredFee),
RealizedPnl: positionResp.RealizedPnl,
BadDebt: sdk.NewCoin(market.Pair.QuoteDenom(), positionResp.BadDebt.RoundInt()),
FundingPayment: positionResp.FundingPayment,
BlockHeight: ctx.BlockHeight(),
MarginToUser: marginToVault.Neg().Sub(transferredFee),
ChangeReason: changeType,
FinalPosition: positionResp.Position,
PositionNotional: positionNotional,
TransactionFee: sdk.NewCoin(market.Pair.QuoteDenom(), transferredFee),
RealizedPnl: positionResp.RealizedPnl,
BadDebt: sdk.NewCoin(market.Pair.QuoteDenom(), positionResp.BadDebt.RoundInt()),
FundingPayment: positionResp.FundingPayment,
BlockHeight: ctx.BlockHeight(),
MarginToUser: marginToVault.Neg().Sub(transferredFee),
ChangeReason: changeType,
ExchangedSize: positionResp.Position.Size_.Sub(existingPosition.Size_),
ExchangedNotional: positionResp.PositionNotional.Sub(existingPosition.OpenNotional),
},
)

Expand Down Expand Up @@ -726,6 +729,7 @@ func (k Keeper) ClosePosition(ctx sdk.Context, pair asset.Pair, traderAddr sdk.A
traderAddr,
*positionResp,
types.ChangeReason_ClosePosition,
position,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -887,6 +891,7 @@ func (k Keeper) PartialClose(
traderAddr,
*positionResp,
types.ChangeReason_PartialClose,
position,
)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit b8e33f1

Please sign in to comment.