Consider the tokenโs decimal places when determining input and output values.Input and output amounts passed to the solidity function should all be in the tokenโs smallest unit. For the SAUCE token, which has 6 decimal places, an input of 123.45 SAUCE should be entered as 123450000 (123.45 multiplied by 10^6).
Granting an spender allowance to the router contract is required when the input token is not native HBAR for security reasons enforced at the native code layer. Ensure that the allowance amount is in tokenโs smallest unit.
When providing HBAR in the path array, use the Wrapped HBAR token ID (WHBAR).
Set the minimum output token amount (amountOutMinimum) with caution.A high minimum might lead to a swap failure due to insufficient liquidity or rapid price movements. Conversely, setting the minimum too low can expose you to significant slippage, potentially resulting in a financial loss as you might receive far fewer tokens than expected.
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.
Include unwrapWHBAR function in your call to convert the Wrapped HBAR (WHBAR) output token back into the native HBAR cryptocurrency following the swap.
For the ExactInputParams recipient, use the SwapRouter contract address. This is required for unwrapWHBAR to function correctly. The unwrapped HBAR will then be sent to the userโs recipient address.When the output token is not HBAR, use the userโs address as the recipient instead.
Note: The Hedera JavaScript SDK currently does not support passing complex contract function parameters. Instead, use Ethers.js or Web3.js to obtain the encoded function data and pass that data as a function parameter.
Typescript
Copy
Ask AI
import * as ethers from 'ethers'; //V6//import {..} from @hashgraph/sdk//Client pre-checks:// - Router contract has spender allowance for the input token//load ABI data containing SwapRouter, PeripheryPayments and Multicall functionsconst abiInterfaces = new ethers.Interface(abi); //ExactInputParamsconst params = { path: routeDataWithFee, //'0x...' recipient: swapRouterAddress, //'0x...' - use the SwapRouter id here for unwrapWHBAR to work deadline: deadline, //Unix seconds amountIn: inputAmount, //in token's smallest unit amountOutMinimum: outputTinybarMin //in Tinybar};//encode each function individuallyconst swapEncoded = abiInterfaces.encodeFunctionData('exactInput', [params]);//Provide the user's address to receive the unwrapped HBARconst unwrapEncoded = abiInterfaces.encodeFunctionData('unwrapWHBAR', [0, recipient]); //multi-call parameter: bytes[]const multiCallParam = [swapEncoded, unwrapEncoded];//get encoded data for the multicall involving both functionsconst encodedData = abiInterfaces.encodeFunctionData('multicall', [multiCallParam]); //get encoded data as Uint8Arrayconst encodedDataAsUint8Array = hexToUint8Array(encodedData);const response = await new ContractExecuteTransaction() .setContractId(swapRouterContractId) .setGas(gasLim) .setFunctionParameters(encodedDataAsUint8Array) .execute(client);const record = await response.getRecord(client);const result = record.contractFunctionResult!;const values = result.getResult(['uint256']);const amountOut = values[0]; //uint256 amountOut - Tinybar
Swap a maximum amount of tokens to receive an exact HBAR amount.Solidity function name: exactOutput
Schema Parameter Name
Description
bytes path
A bytes array representing a route path including fees data
address recipient
EVM address for the token recipient
uint256 deadline
Deadline in Unix seconds
uint256 amountOut
The exact output amount to receive in its smallest unit
uint256 amountInMaximum
The maximum allowed input amount in its smallest unit
Copy
Ask AI
struct ExactOutputParams { bytes path; address recipient; uint256 deadline; uint256 amountOut; uint256 amountInMaximum;}/// @notice Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed)/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata/// @return amountIn The amount of the input tokenfunction exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
Set the maximum input token amount (amountInMaximum) with caution.A low maximum might lead to a swap failure if the required liquidity surpasses this limit or due to rapid price movements. Conversely, setting it too high can expose you to significant slippage, potentially leading to a financial loss as you might spend far more tokens than expected.
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 (3000) for a 0.30% fee.
Include unwrapWHBAR function in your call to convert the Wrapped HBAR output token back into the native HBAR cryptocurrency following the swap.
For the ExactInputParams recipient, use the SwapRouter contract address. This is required for unwrapWHBAR to function correctly. The unwrapped HBAR will then be sent to the userโs recipient address.When the output token is not HBAR, use the userโs address as the recipient instead.
Note: The Hedera JavaScript SDK currently does not support passing complex contract function parameters. Instead, use Ethers.js or Web3.js to obtain the encoded function data and pass that data as a function parameter.
Typescript
Copy
Ask AI
import * as ethers from 'ethers'; //V6//import {..} from @hashgraph/sdk//Client pre-checks:// - Router contract has spender allowance for the input token//load ABI data containing SwapRouter, PeripheryPayments and Multicall functionsconst abiInterfaces= new ethers.Interface(abi); //ExactOutputParamsconst params = { path: routeDataWithFee, //'0x...' (reversed route path) recipient: swapRouterAddress, //'0x...' - use the SwapRouter id here for unwrapWHBAR to work deadline: deadline, //Unix seconds amountOut: outputTinybar, //in Tinybar amountInMaximum: inputAmountMax //in token's smallest unit};//encode each function individuallyconst swapEncoded = abiInterfaces.encodeFunctionData('exactOutput', [params]);//Provide the user's address to receive the unwrapped HBARconst unwrapEncoded = abiInterfaces.encodeFunctionData('unwrapWHBAR', [0, recipient]);//multi-call parameter: bytes[]const multiCallParam = [swapEncoded, unwrapEncoded];//get encoded data for the multicall involving both functionsconst encodedData = abiInterfaces.encodeFunctionData('multicall', [multiCallParam]); //get encoded data as Uint8Arrayconst encodedDataAsUint8Array = hexToUint8Array(encodedData);const response = await new ContractExecuteTransaction() .setContractId(swapRouterContractId) .setGas(gasLim) .setFunctionParameters(encodedDataAsUint8Array) .execute(client);const record = await response.getRecord(client);const result = record.contractFunctionResult!;const values = result.getResult(['uint256']);const amountIn = values[0]; //uint256 amountIn