> ## Documentation Index
> Fetch the complete documentation index at: https://docs.perpetradex.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Formula Reference

> Every formula used across Perpetra, in one place

This page compiles every formula referenced elsewhere in the docs, sourced directly from the deployed contracts. All prices and USD amounts are 18-decimal fixed point unless noted. All `Bps` values are out of `10,000` (e.g. `50 bps = 0.5%`).

## Pre-trade checks (RiskEngine)

**Leverage bounds:**

```
leverage = sizeUsd / collateralAmount
minLeverage <= leverage <= maxLeverage
```

Implemented without division, as: `sizeUsd >= collateralAmount * minLeverage` and `collateralAmount * maxLeverage >= sizeUsd`.

**Initial margin:**

```
initialMarginRequired = sizeUsd * initialMarginBps / 10_000
collateralAmount >= initialMarginRequired
```

This is checked independently of the leverage bounds above, both must pass.

**Open interest cap** (checked per side, long and short tracked separately):

```
newOI = currentOI + sizeUsd (or sizeDelta, on an increase)
newOI <= maxLongOpenInterest / maxShortOpenInterest
```

## PnL, margin ratio, and liquidation (RiskEngine)

**Unrealized PnL:**

```
currentValue = sizeUsd * markPrice / entryPrice

Long:  pnl = currentValue - sizeUsd
Short: pnl = sizeUsd - currentValue
```

**Effective collateral:**

```
effectiveCollateral = max(0, collateralAmount + unrealizedPnl)
```

**Margin ratio:**

```
marginRatioBps = effectiveCollateral * 10_000 / sizeUsd
```

**Maintenance margin:**

```
maintenanceMargin = sizeUsd * maintenanceMarginBps / 10_000
```

**Liquidation trigger:**

```
isLiquidatable = effectiveCollateral < maintenanceMargin
```

**Liquidation price (isolated):**

```
Long:  liqPrice = entryPrice * (sizeUsd - collateralAmount + maintenanceMargin) / sizeUsd
Short: liqPrice = entryPrice * (sizeUsd + collateralAmount - maintenanceMargin) / sizeUsd
```

See [Positions & Margin](/positions-and-margin) and [PnL & Liquidation](/pnl-and-liquidation) for the walkthroughs behind these.

## Cross-margin liquidation (RiskEngine)

**Account-level check**, across every open cross position for one collateral token:

```
accountEquity = max(0, vaultBalance + sum(unrealizedPnl across all cross positions))
totalMaintenanceRequired = sum(sizeUsd_i * maintenanceMarginBps_i / 10_000)

isLiquidatable = accountEquity < totalMaintenanceRequired
```

The caller-supplied position set must exactly match the trader's actual open cross-position count for that token, or the check reverts, this prevents omitting a healthy position to bias the result.

**Cross liquidation price for one position** (snapshot-based, holds every other position's PnL fixed at its current mark price):

```
requiredPnl = (otherMaintenance + targetMaintenance) - vaultBalance - otherPnl

Long:  liqPrice = entryPrice * (requiredPnl + targetSizeUsd) / targetSizeUsd
Short: liqPrice = entryPrice * (targetSizeUsd - requiredPnl) / targetSizeUsd
```

This number is only valid until another position in the same cross book moves, it's a point-in-time estimate, not a standing guarantee. See [Margin Modes](/margin-modes).

## Funding (FundingManager)

```
premium = (markPrice - indexPrice) / indexPrice
fundingRate = clamp(premium, -maxFundingRateBps, +maxFundingRateBps)

indexDelta = fundingRate * timeDelta / fundingInterval
cumulativeIndex += indexDelta
```

**Funding owed by a position:**

```
rawFunding = sizeUsd * (currentIndex - positionFundingIndex) / RATE_PRECISION

Long:  fundingOwed = rawFunding
Short: fundingOwed = -rawFunding
```

Positive means the position owes funding. See [Funding Rate](/funding-rate).

## Fees (FeeCollector)

**Trading fee:**

```
fee = sizeUsd * feeBps / 10_000
```

`feeBps` is `takerFeeBps` for market orders, `makerFeeBps` for limit orders.

**Liquidation fee:**

```
fee = sizeUsd * liquidationFeeBps / 10_000
```

**Fee distribution split:**

```
insuranceAmount = pendingFees * insuranceFundBps / 10_000
treasuryAmount = pendingFees - insuranceAmount
```

See [Fees](/fees).

## Liquidation settlement (Liquidator)

```
effectiveCollateral = collateralAmount + unrealizedPnl - fundingOwed
```

That value is split against the keeper fee three ways:

```
effectiveCollateral >= keeperFee:        keeper gets keeperFee, remainder returned to trader
0 < effectiveCollateral < keeperFee:      keeper gets effectiveCollateral, Insurance Fund covers the rest
effectiveCollateral <= 0:                 keeper gets 0, Insurance Fund covers the full keeperFee
```

See [Liquidation Engine](/liquidation-engine).

## Slippage (Matcher, off-chain)

```
diffBps = |oraclePrice - limitPrice| * 10_000 / limitPrice
reject if diffBps > maxSlippageBps
```

See [Slippage & Execution](/slippage-and-execution).

## Points (PointsSystem)

```
tradePointsEarned = volumeUSD * tradePointsPerUSD / 1e6
depositPointsEarned = amountUSD * depositPointsPerUSD / 1e6
referrerPointsEarned = baseEarned * referralBps / 10_000
```

`tradePointsEarned` accrues on both opening and closing a position, each counted separately. See [Points & Referrals](/funding-mechanism-tokenomics).
