PnL & Risk
These helpers are commonly used for risk checks, dashboards, and liquidation logic.
User health
const user = driftClient.getUser();
const health = user.getHealth();
console.log(health.toString());Method User.getHealthReference ↗| Name | Type | Default |
|---|---|---|
perpMarketIndex | number |
Collateral, margin requirement, leverage
const total = driftClient.getUser().getTotalCollateral();
console.log(total.toString());Method User.getTotalCollateralReference ↗| Name | Type | Default |
|---|---|---|
marginCategory | MarginCategory | |
strict | boolean | |
includeOpenOrders | boolean | |
liquidationBuffer | any | |
perpMarketIndex | number |
const req = driftClient.getUser().getMarginRequirement();
console.log(req.toString());Method User.getMarginRequirementReference ↗const free = driftClient.getUser().getFreeCollateral();
console.log(free.toString());Method User.getFreeCollateralReference ↗| Name | Type | Default |
|---|---|---|
marginCategory | MarginCategory | |
enterHighLeverageMode | boolean | |
perpMarketIndex | number |
const lev = driftClient.getUser().getLeverage();
console.log(lev.toString());Method User.getLeverageReference ↗| Name | Type | Default |
|---|---|---|
includeOpenOrders | boolean | |
perpMarketIndex | number |
Unrealized PnL
const pnl = driftClient.getUser().getUnrealizedPNL();
console.log(pnl.toString());Method User.getUnrealizedPNLReference ↗| Name | Type | Default |
|---|---|---|
withFunding | boolean | |
marketIndex | number | |
withWeightMarginCategory | MarginCategory | |
strict | boolean | |
liquidationBuffer | any |
const pnl = driftClient.getUser().getUnrealizedFundingPNL();
console.log(pnl.toString());Method User.getUnrealizedFundingPNLReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
Entry price helper
import { calculateEntryPrice } from "@drift-labs/sdk";
const position = driftClient.getUser().getPerpPosition(0);
if (position) {
const entryPrice = calculateEntryPrice(position);
console.log(entryPrice.toString());
}Function calculateEntryPriceReference ↗| Name | Type | Default |
|---|---|---|
userPosition | PerpPosition |
Settle perp PnL
const user = driftClient.getUser();
await driftClient.settlePNL(user.userAccountPublicKey, user.getUserAccount(), 0);Method DriftClient.settlePNLReference ↗| Name | Type | Default |
|---|---|---|
settleeUserAccountPublicKey | PublicKey | |
settleeUserAccount | UserAccount | |
marketIndex | number | |
txParams | TxParams | |
optionalIxs | TransactionInstruction[] | |
revenueShareEscrowMap | RevenueShareEscrowMap |
Last updated on