This is the part 3 of Uniswap V3 SDK Swap Tutorial, full source code.
In this part we’ll call Quoter interface with ethers.js to get quote for a swap.
First we format inAmountStr according to decimals of this token. ethers.js has very handy functions for it. Always use the number of decimals after the decimal point you got from the chain (tokenIn.decimals) since on some test networks I’ve encountered unusual values (let’s say 6 instead of 18 widely used on mainnet):
const amountIn = ethers.utils.parseUnits(inAmountStr, tokenIn.decimals);
Next step is to actually call the quoter and get quotedAmountOut. Uniswap quoter address is the same on all chains, here you can find all addresses of Uniswap smart contracts.
const UNISWAP_QUOTER_ADDRESS = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6'
const quoterContract = new ethers.Contract(UNISWAP_QUOTER_ADDRESS, QuoterABI.abi, provider);
const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(
tokenIn.address,
tokenOut.address,
pool.fee,
amountIn,
0
);
There are few nuances tho.
We can then print the quote using ethers.utils.formatUnits to format quote according to decimals of tokenOut.
console.log(`You'll get approximately ${ethers.utils.formatUnits(quotedAmountOut, tokenOut.decimals)} ${tokenOut.symbol} for ${inAmountStr} ${tokenIn.symbol}`);
That’s it, we successfully got a swap quote. The next part is about getting a route for a swap.