Query Prices
There are multiple ways to query token prices on Kroko DEX, depending on your use case.
Method 1: Quote API (Recommended)
The most accurate way to get a price is to request a swap quote. This considers all available routes and gives you the actual execution price.
const params = new URLSearchParams({
tokenIn: '0xTokenA',
tokenOut: '0xTokenB',
amountIn: '1000000000000000000', // 1 token
tradeType: '0'
});
const res = await fetch(`https://dex.kasplex.org/swap-api/api/v1/quote?${params}`);
const quote = await res.json();
console.log(`Price: 1 TokenA = ${quote.executionPrice} TokenB`);
console.log(`Price impact: ${quote.priceImpact}%`);
Best for: Real-time swap pricing, showing users what they’ll receive.
The execution price varies with trade size due to price impact. For reference prices, use a small amount.
Method 2: Price API
Get the price directly without specifying an amount:
// Single token price
const res = await fetch('https://dex.kasplex.org/swap-api/api/v1/price/0xTokenAddress');
// Pair price
const res = await fetch('https://dex.kasplex.org/swap-api/api/v1/price/pair/0xToken0/0xToken1');
Best for: Dashboard displays, portfolio valuation.
Method 3: On-Chain (V2)
Read pool reserves directly for V2 pools:
const PAIR_ABI = [
'function getReserves() view returns (uint112, uint112, uint32)',
'function token0() view returns (address)'
];
const pair = new ethers.Contract(pairAddress, PAIR_ABI, provider);
const [reserve0, reserve1] = await pair.getReserves();
const token0 = await pair.token0();
// Price of token0 in terms of token1
const price = Number(reserve1) / Number(reserve0);
Best for: Trustless on-chain price reads, oracle-like use cases.
Method 4: On-Chain (V3)
Read the current price from a V3 pool:
const POOL_ABI = [
'function slot0() view returns (uint160, int24, uint16, uint16, uint16, uint8, bool)'
];
const pool = new ethers.Contract(poolAddress, POOL_ABI, provider);
const [sqrtPriceX96, tick] = await pool.slot0();
// Convert sqrtPriceX96 to price
const price = (Number(sqrtPriceX96) / 2 ** 96) ** 2;
// Adjust for decimals if tokens have different decimal places
const adjustedPrice = price * (10 ** (decimals0 - decimals1));
Best for: V3-specific price feeds, tick-level precision.
Historical Prices
For historical K-line data:
const res = await fetch(
'https://dex.kasplex.org/swap-api/api/v1/price/history/0xPoolAddress?limit=100'
);
Comparison
| Method | Accuracy | Latency | Trust Model | Best For |
|---|
| Quote API | Highest (considers routing) | ~100ms | Centralized API | Swap UIs |
| Price API | High | ~50ms | Centralized API | Dashboards |
| On-chain V2 | Pool-level | ~2s (RPC) | Trustless | Oracles |
| On-chain V3 | Pool-level | ~2s (RPC) | Trustless | Advanced analytics |