Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions sdk/src/clearingHouseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
UserPosition,
UserPositionsAccount,
} from './types';
import { calculateEntryPrice } from './math/position';
import {
calculateEntryPrice,
calculatePositionPNLWithExitPrice,
} from './math/position';
import {
MARK_PRICE_PRECISION,
AMM_TO_QUOTE_PRECISION_RATIO,
Expand Down Expand Up @@ -254,15 +257,26 @@ export class ClearingHouseUser {
* calculates unrealized position price pnl
* @returns : Precision QUOTE_PRECISION
*/
public getUnrealizedPNL(withFunding?: boolean, marketIndex?: BN): BN {
public getUnrealizedPNL(
withFunding?: boolean,
marketIndex?: BN,
withoutSlippage?: boolean
): BN {
return this.getUserPositionsAccount()
.positions.filter((pos) =>
marketIndex ? pos.marketIndex === marketIndex : true
)
.reduce((pnl, marketPosition) => {
const market = this.clearingHouse.getMarket(marketPosition.marketIndex);
return pnl.add(
calculatePositionPNL(market, marketPosition, withFunding)
withoutSlippage
? calculatePositionPNLWithExitPrice(
market,
marketPosition,
calculateMarkPrice(market),
withFunding
)
: calculatePositionPNL(market, marketPosition, withFunding)
);
}, ZERO);
}
Expand All @@ -286,10 +300,11 @@ export class ClearingHouseUser {
* calculates TotalCollateral: collateral + unrealized pnl
* @returns : Precision QUOTE_PRECISION
*/
public getTotalCollateral(): BN {
public getTotalCollateral(withoutSlippage = false): BN {
return (
this.getUserAccount().collateral.add(this.getUnrealizedPNL(true)) ??
new BN(0)
this.getUserAccount().collateral.add(
this.getUnrealizedPNL(true, null, withoutSlippage)
) ?? new BN(0)
);
}

Expand Down
43 changes: 43 additions & 0 deletions sdk/src/math/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,49 @@ export function calculatePositionPNL(
return pnl;
}

/**
* calculatePositionPNLWithoutSlippage
* = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
* @param market
* @param marketPosition
* @param withFunding (adds unrealized funding payment pnl to result)
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
*/
export function calculatePositionPNLWithExitPrice(
market: Market,
marketPosition: UserPosition,
exitPrice: BN,
withFunding = false
): BN {
if (marketPosition.baseAssetAmount.eq(ZERO)) {
return ZERO;
}

const baseAssetValue = marketPosition.baseAssetAmount
.abs()
.mul(exitPrice)
.div(MARK_PRICE_PRECISION)
.div(AMM_TO_QUOTE_PRECISION_RATIO);

let pnl;
if (marketPosition.baseAssetAmount.gt(ZERO)) {
pnl = baseAssetValue.sub(marketPosition.quoteAssetAmount);
} else {
pnl = marketPosition.quoteAssetAmount.sub(baseAssetValue);
}

if (withFunding) {
const fundingRatePnL = calculatePositionFundingPNL(
market,
marketPosition
).div(PRICE_TO_QUOTE_PRECISION);

pnl = pnl.add(fundingRatePnL);
}

return pnl;
}

/**
*
* @param market
Expand Down