Skip to content

Commit

Permalink
refactor: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jul 5, 2023
1 parent f8d3fa0 commit 8a24c81
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions x/perp/v2/keeper/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ func (k Keeper) afterPositionUpdate(
if err != nil {
return err
}
k.Positions.Insert(ctx, collections.Join(market.Pair, traderAddr), positionResp.Position)
}

// transfer trader <=> vault
Expand All @@ -566,17 +567,13 @@ func (k Keeper) afterPositionUpdate(
}
}

transferredFee, err := k.transferFee(ctx, market.Pair, traderAddr, positionResp.ExchangedNotionalValue,
fees, err := k.transferFee(ctx, market.Pair, traderAddr, positionResp.ExchangedNotionalValue,
market.ExchangeFeeRatio, market.EcosystemFundFeeRatio,
)
if err != nil {
return err
}

if !positionResp.Position.Size_.IsZero() {
k.Positions.Insert(ctx, collections.Join(market.Pair, traderAddr), positionResp.Position)
}

// calculate positionNotional (it's different depends on long or short side)
// long: unrealizedPnl = positionNotional - openNotional => positionNotional = openNotional + unrealizedPnl
// short: unrealizedPnl = openNotional - positionNotional => positionNotional = openNotional - unrealizedPnl
Expand All @@ -591,12 +588,12 @@ func (k Keeper) afterPositionUpdate(
&types.PositionChangedEvent{
FinalPosition: positionResp.Position,
PositionNotional: positionNotional,
TransactionFee: sdk.NewCoin(market.Pair.QuoteDenom(), transferredFee),
TransactionFee: sdk.NewCoin(market.Pair.QuoteDenom(), fees),
RealizedPnl: positionResp.RealizedPnl,
BadDebt: sdk.NewCoin(market.Pair.QuoteDenom(), positionResp.BadDebt.RoundInt()),
FundingPayment: positionResp.FundingPayment,
BlockHeight: ctx.BlockHeight(),
MarginToUser: marginToVault.Neg().Sub(transferredFee),
MarginToUser: marginToVault.Neg().Sub(fees),
ChangeReason: changeType,
},
)
Expand Down

0 comments on commit 8a24c81

Please sign in to comment.