Skip to main content

PerpLib

PerpLib

Library for core Domination Finance perpetual exchange logic

Computes PnL, funding payments, withdraw delays, etc. Mostly used by DomFiPerp and OrderBookV2.

Addresses

ChainAddress

Functions

_canTakeProfit

Return whether a position is allowed to take profit: if either

  • the price has changed by more than minPriceChange
  • minProfitTime has elapsed
function _canTakeProfit(
bool isLong,
uint256 positionTimestamp,
FPUnsigned positionPrice,
FPUnsigned oraclePrice,
FPUnsigned minPriceChange,
uint256 minProfitTime
) internal view returns (
bool canTakeProfit
)

Parameters

NameTypeDescription
isLongboolWhether this is a long position
positionTimestampuint256Time position was last increased
positionPriceFPUnsignedComposite price when position was increase/updated
oraclePriceFPUnsignedCurrent oracle price
minPriceChangeFPUnsignedMinimum price change to take profit. This should be larger than the minimum oracle price change
minProfitTimeuint256Time to wait for minPriceChange before realizing profit anyway.

Return Values

NameTypeDescription
canTakeProfitboolIf a position is allowed to take profit.

_getPnl

Compute the PnL of a position, in collateral units, based on price action.

function _getPnl(
bool isLong,
FPUnsigned positionPrice,
FPUnsigned positionLeverage,
uint256 margin,
FPUnsigned price
) internal pure returns (
FPSigned pnl
)

Parameters

NameTypeDescription
isLongboolIf this is a long position (false=short)
positionPriceFPUnsignedComposite price when position was increase/updated
positionLeverageFPUnsignedLeverage of the position, used to compute size
marginuint256Margin of the position, used to compute size
priceFPUnsignedCurrent price of the position, including any modifications

Return Values

NameTypeDescription
pnlFPSignedProfit (positive) or loss (negative) attributable to position

_getFundingPayment

Compute the funding payment owed by a position

function _getFundingPayment(
bool isLong,
FPUnsigned positionLeverage,
uint256 margin,
FPSigned funding,
FPSigned cumulativeFunding,
FPUnsigned cumulativeInterest
) internal pure returns (
FPSigned fundingPayment
)

Parameters

NameTypeDescription
isLongboolWhether this position is long (false=short)
positionLeverageFPUnsignedLeverage of the position, used to compute size
marginuint256Margin of the position, used to compute size
fundingFPSignedComposite cumulative funding+interest when the position was created/updated
cumulativeFundingFPSignedCurrent cumulative funding for the position's product.
cumulativeInterestFPUnsignedCurrent cumulative base interest rate for the position's product

Return Values

NameTypeDescription
fundingPaymentFPSignedFunding payment owed by the position. Negative values increase the value of the position.

_getTradeFee

Compute the trade fee incurred by changing a position's size

function _getTradeFee(
uint256 margin,
FPUnsigned leverage,
FPUnsigned productFee,
address user,
address sender,
contract IFeeCalculator feeCalculator
) internal view returns (
uint256 tradeFee
)

Parameters

NameTypeDescription
marginuint256Margin of the increase/decrease
leverageFPUnsignedLeverage at which the increase/decrease was applied
productFeeFPUnsignedProduct's normal trade fee rate
useraddressOwner of the position
senderaddressSender of the transaction
feeCalculatorcontract IFeeCalculatorFeeCalculator instance to apply user/sender discount

Return Values

NameTypeDescription
tradeFeeuint256Collateral owed by user

_getUtilizationRatio

Compute utilization ratio of the vault, and whether instant redemptions should be allowed.

function _getUtilizationRatio(
FPUnsigned totalOpenInterest,
uint256 balance,
FPSigned totalPnl,
uint256 pendingWithdraw,
FPUnsigned healthyUtilizationRatio
) internal pure returns (
bool instantRedeemAvailable,
FPSigned utilizationRatio
)
Dev note

Utilization ratio: totalOpenInterestbalancependingWithdrawtotalPnl\frac{totalOpenInterest}{balance - pendingWithdraw - totalPnl}. If the denominator, effective balance, is zero, return utilizationRatio=0 and allow instant redemptions only if there is no open interest.

Parameters

NameTypeDescription
totalOpenInterestFPUnsignedSum of sizes (margin * leverage) of all open positions
balanceuint256Collateral balance of the vault (deposits - trader PnL)
totalPnlFPSignedOutstanding total PnL of all positions
pendingWithdrawuint256Estimated collateral value of all pending withdraw requests
healthyUtilizationRatioFPUnsignedThe maximum utilization ratio before instant withdrawals are forbidden

Return Values

NameTypeDescription
instantRedeemAvailableboolutilizationRatiohealthyUtilizationRatioutilizationRatio \leq healthyUtilizationRatio
utilizationRatioFPSignedtotalOpenInterestbalancependingWithdrawtotalPnl\frac{totalOpenInterest}{balance - pendingWithdraw - totalPnl}

_getWithdrawDuration

Compute the duration of a withdraw request

function _getWithdrawDuration(
FPSigned utilizationRatio,
FPUnsigned requestedShareFraction,
FPUnsigned healthyUtilizationRatio,
uint256 maxWithdrawDuration,
uint256 durationPerUtilizationRatio
) internal pure returns (
uint64 withdrawDuration
)

Parameters

NameTypeDescription
utilizationRatioFPSignedutilization ratio (given by _getUtilizationRatio)
requestedShareFractionFPUnsignedFraction of the vault shares requested. [0,1][0,1]
healthyUtilizationRatioFPUnsignedUtilization ratio below which withdraws should be immediate
maxWithdrawDurationuint256Maximum permitted withdraw request duration
durationPerUtilizationRatiouint256See DomFiVault.setTotalDurationPerUtilizationRatio

Return Values

NameTypeDescription
withdrawDurationuint64Length of a withdraw request