Get Pair Reserves
Fetch complete liquidity distribution across all bins in a pair, including reserves and accumulated fees. This method provides a comprehensive view of the entire liquidity landscape.
Prerequisites
Before fetching reserves:
Have a valid LBPair object from
getPair()
Understand the difference between reserves and fees
Be prepared to handle potentially large datasets
Basic Usage
// Get pair first
const pair = await sdk.Pair.getPair(pairAddress);
// Fetch all bin reserves
const reserves = await sdk.Pair.getPairReserves(pair);
console.log(`Total bins with liquidity: ${reserves.length}`);
// Display first few bins
reserves.slice(0, 5).forEach(bin => {
console.log(`Bin ${bin.id}:`, {
reserveX: bin.reserve_x.toString(),
reserveY: bin.reserve_y.toString(),
feeX: bin.fee_x.toString(),
feeY: bin.fee_y.toString()
});
});
Method Signature
async getPairReserves(pair: LBPair): Promise<BinReserves[]>
Returns
Array of BinReserves
objects sorted by bin ID:
interface BinReserves {
id: number; // Bin ID
reserve_x: bigint; // Token X liquidity
reserve_y: bigint; // Token Y liquidity
fee_x: bigint; // Accumulated X fees
fee_y: bigint; // Accumulated Y fees
}
Understanding the Data
Reserves vs Fees
Reserves: Liquidity available for trading
Fees: Accumulated trading fees not yet collected
Total: reserves + fees = total bin value
const reserves = await sdk.Pair.getPairReserves(pair);
reserves.forEach(bin => {
const totalX = bin.reserve_x + bin.fee_x;
const totalY = bin.reserve_y + bin.fee_y;
console.log(`Bin ${bin.id} total value:`, { totalX, totalY });
});
Practical Examples
Calculate Total Value Locked (TVL)
const reserves = await sdk.Pair.getPairReserves(pair);
let totalReserveX = 0n;
let totalReserveY = 0n;
let totalFeesX = 0n;
let totalFeesY = 0n;
reserves.forEach(bin => {
totalReserveX += bin.reserve_x;
totalReserveY += bin.reserve_y;
totalFeesX += bin.fee_x;
totalFeesY += bin.fee_y;
});
console.log("Pair TVL:", {
reservesX: totalReserveX,
reservesY: totalReserveY,
uncollectedFeesX: totalFeesX,
uncollectedFeesY: totalFeesY
});
Find Liquidity Distribution
const reserves = await sdk.Pair.getPairReserves(pair);
const activeId = pair.parameters.active_id;
// Categorize bins by position relative to active
const distribution = {
below: reserves.filter(bin => bin.id < activeId),
active: reserves.filter(bin => bin.id === activeId),
above: reserves.filter(bin => bin.id > activeId)
};
console.log("Liquidity distribution:", {
binsBelow: distribution.below.length,
binsAbove: distribution.above.length,
hasActiveBin: distribution.active.length > 0
});
Analyze Fee Generation
const reserves = await sdk.Pair.getPairReserves(pair);
// Find bins with highest accumulated fees
const topFeeBins = reserves
.filter(bin => bin.fee_x > 0n || bin.fee_y > 0n)
.sort((a, b) => {
const totalFeesA = a.fee_x + a.fee_y;
const totalFeesB = b.fee_x + b.fee_y;
return Number(totalFeesB - totalFeesA);
})
.slice(0, 10);
console.log("Top 10 bins by accumulated fees:", topFeeBins);
Create Liquidity Heatmap Data
const reserves = await sdk.Pair.getPairReserves(pair);
const binStep = Number(pair.binStep);
const heatmapData = reserves.map(bin => ({
binId: bin.id,
price: getPriceFromBinId(bin.id, binStep),
liquidity: bin.reserve_x + bin.reserve_y,
fees: bin.fee_x + bin.fee_y
}));
// Use for visualization
console.log("Heatmap data ready:", heatmapData.length, "points");
Performance Considerations
Fetches all bins with liquidity (no range limit)
Uses pagination internally for large datasets
May return hundreds of bins for active pairs
Consider caching results for repeated analysis
Differences from getPairBins
Range
All bins with liquidity
Specified range only
Fees
Includes accumulated fees
Reserves only
Performance
Slower for large pairs
Faster for small ranges
Use Case
Complete analysis
Targeted inspection
Related Topics
Get Pair Bins - Query specific bin ranges
Calculate Position Fees - Fee analysis
Position Value - Calculate total worth
Last updated