Calculate Swap Output
The calculateRates
function is used to estimate the result of a token swap within Ferra’s Discrete Liquidity Market Maker (DLMM) system. It calculates how much output you would receive from swapping a given amount, along with estimated fees and liquidity availability.
Prerequisites
Pair object with current state
Input amount and direction
Basic Output Calculation
// Calculate output for a swap
const result = sdk.Swap.calculateRates(pair, {
amount: bigint,
swapBins: LbPairBinData[],
xtoy: boolean,
});
Example
async function estimateSwap() {
const pair = await sdk.Pair.getPair(pairId);
const bins = await sdk.Pair.getPairBinsData(pairId);
const decimals = a2b ? coinA.decimals : coinB.decimals;
const parsedAmount = parseUnits(amount.toString(), decimals);
const result = sdk.Swap.calculateRates(pair, {
amount: parsedAmount,
swapBins: bins,
xtoy: a2b,
});
}
pair
DLMM.Pair
The liquidity pair object, retrieved via getPair(pairId)
amount
bigint
The amount to swap, expressed in smallest units (e.g., wei). Use parseUnits()
to convert from human-readable numbers
swapBins
LbPairBinData[]
The list of liquidity bins for the pair, retrieved via getPairBinsData(pairId)
xtoy
boolean
Direction of the swap. true
means swap token X to Y (A to B), false
for the reverse
amount
bigint
Input amount (as provided)
estimatedAmountIn
bigint
Estimated actual input required to complete the swap
estimatedAmountOut
bigint
Estimated amount received from the swap
estimatedFeeAmount
bigint
Estimated fee deducted during the swap
isExceed
boolean
Indicates whether the requested amount exceeds available liquidity
estimatedEndBinId
bigint
The estimated ending square root price
isMaxLoop
boolean
Indicates if the estimated amount exceeds the limit
priceImpactPct
number
The price impact percentage
Important Notes
Output depends on current bin liquidity
Large swaps may have significant impact
Always include fees in calculations
Related Topics
Execute Swap - Perform the swap
Price Impact - Calculate market impact
Slippage Protection - Set safety limits
Get Pair Bins - Check liquidity
Last updated