Below are the two methods to get a quote for a swap from a given route:

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 Output Quote from Exact Input Amount

Get the output amount from a given exact input amount and a swap route.

Function name: quoteExactInput โ›ฝ No gas cost

Parameter NameDescription
bytes pathRoute path containing pair swap fees
uint256 amountInExact input amount in tokenโ€™s smallest unit
/// @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 consumes
function 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.

Code Overview

Resources:

Typescript
import * as ethers from 'ethers'; //V6

//Set one of Hedera's JSON RPC Relay as the provider
const provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', {
  batchMaxCount: 1, //workaround for V6
});

//load ABI data containing QuoterV2 functions
const abiInterfaces = new ethers.Interface(abi);

//QuoterV2.sol contract
const quoterEvmAddress = `0x${ContractId.fromString(quoterContractId).toSolidityAddress()}`;  

//swap path
const 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 directly
const result = await provider.call({
  to: quoterEvmAddress,
  data: data,
});

const decoded = abiInterfaces.decodeFunctionResult('quoteExactInput', result); 
const finalOutputAmount = decoded.amountOut; //in token's smallest unit


Get Input Quote from Exact Output Amount

Get the input amount from a given exact output amount and a swap route.

Function name: quoteExactOutput โ›ฝ No gas cost

Parameter NameDescription
bytes pathRoute path containing pair swap fees, reversed
uint256 amountOutExact output amount in tokenโ€™s smallest unit
/// @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 consumes
function 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.

Code Overview

Resources:

Typescript
import * as ethers from 'ethers'; //V6

//Set one of Hedera's JSON RPC Relay as the provider
const provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', {
  batchMaxCount: 1, //workaround for V6
});

//load ABI data containing QuoterV2 functions
const abiInterfaces = new ethers.Interface(abi);

//QuoterV2.sol contract
const quoterEvmAddress = `0x${ContractId.fromString(quoterContractId).toSolidityAddress()}`;  

//swap path
const pathData:string[] = [];
pathData.push(inputToken.toSolidityAddress());  
pathData.push(feeHexStr);
pathData.push(outputToken.toSolidityAddress());

//reverse the path
pathData.reverse();

//get encoded Uint8Array data for path hex
const encodedPathData = hexToUint8Array(pathData.join(''));

const data = abiInterfaces.encodeFunctionData('quoteExactOutput', [
  encodedPathData, 
  outputAmountInSmallestUnit
]);

//Send a call to the JSON-RPC provider directly
const result = await provider.call({
  to: quoterEvmAddress,
  data: data,
});

const decoded = abiInterfaces.decodeFunctionResult('quoteExactOutput', result); 
const finalAmountIn = decoded.amountIn; //in token's smallest unit