Quoting prices
Approach
When to use
Trade-off
Option 1: QuoterV2 directly
import { createPublicClient, http, parseUnits } from 'viem'
const QUOTER_V2 = '0x1F1a8dC7E138C34b503Ca080962aC10B75384a27' // mainnet
const RPC = 'https://mainnet.megaeth.com/rpc'
const quoterAbi = [{
type: 'function',
name: 'quoteExactInputSingle',
stateMutability: 'nonpayable',
inputs: [{ type: 'tuple', name: 'params', components: [
{ type: 'address', name: 'tokenIn' },
{ type: 'address', name: 'tokenOut' },
{ type: 'uint256', name: 'amountIn' },
{ type: 'uint24', name: 'fee' },
{ type: 'uint160', name: 'sqrtPriceLimitX96' },
]}],
outputs: [
{ type: 'uint256', name: 'amountOut' },
{ type: 'uint160', name: 'sqrtPriceX96After' },
{ type: 'uint32', name: 'initializedTicksCrossed' },
{ type: 'uint256', name: 'gasEstimate' },
],
}] as const
const client = createPublicClient({ transport: http(RPC) })
const quote = await client.simulateContract({
address: QUOTER_V2,
abi: quoterAbi,
functionName: 'quoteExactInputSingle',
args: [{
tokenIn: '0x4200000000000000000000000000000000000006', // WETH
tokenOut: '0xYOUR_TOKEN',
amountIn: parseUnits('1', 18),
fee: 3000, // 0.3% - pick the right tier
sqrtPriceLimitX96: 0n,
}],
})
console.log('Out:', quote.result[0])
console.log('Gas estimate:', quote.result[3])Option 2: Smart Order Router (client-side)
Option 3: Exchange API
Picking between them
Last updated