When providing HBAR in the path array, use the wrapped HBAR token ID (WHBAR).
For production environments, itโs highly recommended to use a paid Mirror Node provider for commercial and high-traffic purposes. While Hederaโs public mirror node offers free REST API and JSON API endpoints, they have global rate limits. These are best suited for development or low rate usage scenarios.
Get the output amount from a given exact input amount and a swap route.Function name: quoteExactInput โฝ No gas cost
Parameter Name
Description
bytes path
Route path containing pair swap fees
uint256 amountIn
Exact input amount in tokenโs smallest unit
Copy
Ask AI
/// @notice Returns the amount out received for a given exact input swap without executing the swap/// @param path The path of the swap, i.e. each token pair and the pool fee/// @param amountIn The amount of the first token to swap/// @return amountOut The amount of the last token that would be received/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each pool in the path/// @return initializedTicksCrossedList List of the initialized ticks that the swap crossed for each pool in the path/// @return gasEstimate The estimate of the gas that the swap consumesfunction quoteExactInput(bytes memory path, uint256 amountIn) external returns ( uint256 amountOut, uint160[] memory sqrtPriceX96AfterList, uint32[] memory initializedTicksCrossedList, uint256 gasEstimate );
The data passed to the โpathโ parameter follows this format: [token, fee, token, fee, token, โฆ], with each โtokenโ in the route being 20 bytes long and each โfeeโ being 3 bytes long. Example, 0x0001F4 for a 0.05% fee.
import * as ethers from 'ethers'; //V6//Set one of Hedera's JSON RPC Relay as the providerconst provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', { batchMaxCount: 1, //workaround for V6});//load ABI data containing QuoterV2 functionsconst abiInterfaces = new ethers.Interface(abi);//QuoterV2.sol contractconst quoterEvmAddress = `0x${ContractId.fromString(quoterContractId).toSolidityAddress()}`; //swap pathconst pathData:string[] = [];pathData.push(inputToken.toSolidityAddress()); pathData.push(feeHexStr);pathData.push(outputToken.toSolidityAddress());const encodedPathData = hexToUint8Array(pathData.join(''));const data = abiInterfaces.encodeFunctionData('quoteExactInput', [ encodedPathData, inputAmountInSmallestUnit.toString()]);//Send a call to the JSON-RPC provider directlyconst result = await provider.call({ to: quoterEvmAddress, data: data,});const decoded = abiInterfaces.decodeFunctionResult('quoteExactInput', result); const finalOutputAmount = decoded.amountOut; //in token's smallest unit
Get the input amount from a given exact output amount and a swap route.Function name: quoteExactOutput โฝ No gas cost
Parameter Name
Description
bytes path
Route path containing pair swap fees, reversed
uint256 amountOut
Exact output amount in tokenโs smallest unit
Copy
Ask AI
/// @notice Returns the amount in required for a given exact output swap without executing the swap/// @param path The path of the swap, i.e. each token pair and the pool fee. Path must be provided in reverse order/// @param amountOut The amount of the last token to receive/// @return amountIn The amount of first token required to be paid/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each pool in the path/// @return initializedTicksCrossedList List of the initialized ticks that the swap crossed for each pool in the path/// @return gasEstimate The estimate of the gas that the swap consumesfunction quoteExactOutput(bytes memory path, uint256 amountOut) external returns ( uint256 amountIn, uint160[] memory sqrtPriceX96AfterList, uint32[] memory initializedTicksCrossedList, uint256 gasEstimate ); struct QuoteExactOutputSingleParams { address tokenIn; address tokenOut; uint256 amount; uint24 fee; uint160 sqrtPriceLimitX96; }
The data passed to the โpathโ parameter follows this format: [token, fee, token, fee, token, โฆ], but reversed (i.e. the first token in the array should be output token), with each โtokenโ in the route being 20 bytes long and each โfeeโ being 3 bytes long. Example, 0x000BB8 for a 0.30% fee.
import * as ethers from 'ethers'; //V6//Set one of Hedera's JSON RPC Relay as the providerconst provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', { batchMaxCount: 1, //workaround for V6});//load ABI data containing QuoterV2 functionsconst abiInterfaces = new ethers.Interface(abi);//QuoterV2.sol contractconst quoterEvmAddress = `0x${ContractId.fromString(quoterContractId).toSolidityAddress()}`; //swap pathconst pathData:string[] = [];pathData.push(inputToken.toSolidityAddress()); pathData.push(feeHexStr);pathData.push(outputToken.toSolidityAddress());//reverse the pathpathData.reverse();//get encoded Uint8Array data for path hexconst encodedPathData = hexToUint8Array(pathData.join(''));const data = abiInterfaces.encodeFunctionData('quoteExactOutput', [ encodedPathData, outputAmountInSmallestUnit]);//Send a call to the JSON-RPC provider directlyconst result = await provider.call({ to: quoterEvmAddress, data: data,});const decoded = abiInterfaces.decodeFunctionResult('quoteExactOutput', result); const finalAmountIn = decoded.amountIn; //in token's smallest unit