Consider the tokenโs decimal places when determining the output amount.
The output values should 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).
Ensure that the โtoโ account has the output token id associated prior to executing the swap. Failure to do so will result in a TOKEN_NOT_ASSOCIATED_TO_ACCOUNT error.
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.
It is recommended to include refundETH in the multicall in case excess HBAR was sent to the contract.
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 (500) for a 0.05% fee.
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'; //V6import { ContractExecuteTransaction, TokenAssociateTransaction, .. } from '@hashgraph/sdk';//Client pre-checks:// - Output token is associated//load ABI data containing SwapRouter, PeripheryPayments and Multicall functionsconst abiInterfaces = new ethers.Interface(abi); //ExactInputParamsconst params = { path: routeDataWithFee, //'0x...' recipient: recipientAddress, //'0x...' - user's recipient address deadline: deadline, //Unix seconds amountIn: inputTinybar, //in Tinybar amountOutMinimum: outputAmountMin//in token's smallest unit};//encode each function individuallyconst swapEncoded = abiInterfaces.encodeFunctionData('exactInput', [params]);const refundHBAREncoded = abiInterfaces.encodeFunctionData('refundETH');//multi-call parameter: bytes[]const multiCallParam = [swapEncoded, refundHBAREncoded];//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() .setPayableAmount(Hbar.from(inputTinybar, HbarUnit.Tinybar)) .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
Ensure that the refundETH is included in the multicall so that any excess payable HBAR amount, up to the maximum amount, is refunded to the sender.
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.