From 8a24c81cf1483b97f0bfe29e3cc632696b620258 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Wed, 5 Jul 2023 14:23:32 -0400 Subject: [PATCH] refactor: clean code --- x/perp/v2/keeper/clearing_house.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/x/perp/v2/keeper/clearing_house.go b/x/perp/v2/keeper/clearing_house.go index 6929e4ae9..7a53f5c6e 100644 --- a/x/perp/v2/keeper/clearing_house.go +++ b/x/perp/v2/keeper/clearing_house.go @@ -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 @@ -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 @@ -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, }, )