Swap Tokens for Tokens
Swap HTS fungible tokens for HTS fungible tokens
Below are two methods available to swap tokens for tokens:
Contract ID: SaucerSwapV2SwapRouter
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.
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.
Swap Exact Tokens for Tokens
Swap an exact amount of tokens for a minimum token amount.
Solidity function name: exactInput
Struct Parameter Name | Description |
---|---|
bytes path | A bytes array representing a route path including fees data |
address recipient | EVM address of the token recipient |
uint256 deadline | Deadline in Unix seconds |
uint256 amountIn | The exact input token amount in its smallest unit |
uint256 amountOutMinimum | The minimum token amount to receive in its smallest unit |
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 (500) for a 0.05% fee.
Code Overview
Resources:
- SaucerSwap deployed contract IDs
- Ethers.js docs (v6)
- Hedera JavaScript SDK
- Token approve allowance
- Associate tokens to an account
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.
Resources:
- SaucerSwap deployed contract IDs
- Ethers.js docs (v6)
- Hedera JavaScript SDK
- Token approve allowance
- Associate tokens to an account
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.
Swap Tokens for Exact Tokens
Swap a maximum amount of tokens to receive an exact tokens 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 |
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.
Code Overview
Resources:
- SaucerSwap deployed contract IDs
- Ethers.js docs (v6)
- Hedera JavaScript SDK
- Token approve allowance
- Associate tokens to an account
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.
Resources:
- SaucerSwap deployed contract IDs
- Ethers.js docs (v6)
- Hedera JavaScript SDK
- Token approve allowance
- Associate tokens to an account
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.