Calculating liquidation price
Learn how to calculate any trade's liquidation price
Note: This guide works on Arbitrum Sepolia and requires exact beta SDK version of 0.2.12-rc15
To calculate liquidation price, we need to use SDK's getLiquidationPrice()
function.
getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: InitialAccFees, context: GetLiquidationPriceContext) => number;
TradeContainer
To calculate trade's liquidation price, we need to find a trade and its TradeContainer first. TradeContainer is unique object per single trade and is containing all its information.
By inspecting allTrades
of backend's /trading-variables
response, we're able to find all open trades & orders (each represented by single, unique TradingContainerBackend object). Lets pick last market trade (trade.type
= 0 ) at time of writing this guide, which is:
This response is of TradeContainerBackend type, therefore we need to properly convert it to TradeContainer type first (SDK uses client types). Converted and normalised trade will look like this:
Respective trade
and initialAccFees
objects will be used as first and third arguments of SDK's getLiquidationPrice()
while liquidationParams
and tradeInfo
will be helpful when crafting fourth argument (context
).
Fee
Lets focus on second getLiquidationPrice()
argument now, which is Fee.
To find proper Fee object, we need to compute feeIndex
for given trade first. By calling backend's /trading-variables
we're getting pairs
and fees
arrays.
Remember that backend values should be normalised, see Pair vs PairBackend and Fee vs FeeBackend
With pairs
and trade's pairIndex
we compute feeIndex
:
pairs[trade.pairIndex].feeIndex
Now we are able to find our Fee by simply accessing fees[feeIndex]
. Resulting object should look similar to this:
Context
We've now covered 3 arguments and are left with last one, the GetLiquidationPriceContext
.
There are few context parameters that we can pass here. All of them are coming from backend's /trading-variables
endpoint or TradeContainer
discussed above.
currentBlock
Its current block of underlying network. Use any valid block or backend's /trading-variables
response:
tradingVariables.currentBlock
groups
and pairs
To get groups
and pairs
we need to inspect collaterals
backend /trading-variables
response. It is an array of objects corresponding to each supported collateral. Use trade.collateralIndex
to select proper collateral object, then access its borrowingFees:
tradingVariables.collaterals[trade.collateralIndex - 1].borrowingFees
Remember that backend values should be normalised, see PairParamsBorrowingFees vs PairParamsBorrowingFeesBackend
liquidationParams
Available directly on TradeContainer
pairSpreadP
To get pairSpreadP
inspect pairs
key of backends /trading-variables
response. It is an array of objects corresponding to each pair. Use trade.pairIndex
to select proper pair:
tradingVariables.pairs[trade.pairIndex].spreadP
Should be normalised, see Pair vs PairBackend
collateralPriceUsd
Its similar to getting groups
and pairs
but select prices
key after accessing proper collaterals
entry:
tradingVariables.collaterals[trade.collateralIndex - 1].prices.collateralPriceUsd
contractsVersion
Available on TradeContainer:
tradeInfo.contractsVersion
At the end, the context passed to getLiquidationPrice()
should look similar to this:
Calculate liquidation price
Wrapping all of the above, we should pass trade
, fee
, initialAccFees
and context
to SDK's getLiquidationPrice()
. This should return single, float number. Per example above the return liquidation price is:
4.702126243307295
which matches gTrade's UI:
Last updated