Swap HBAR for Tokens
Swap HBAR for HTS fungible tokens.
Last updated
Swap HBAR for HTS fungible tokens.
Last updated
Below are two methods available to swap HBAR for tokens:
Swap an exact amount of HBAR for a minimum token amount
Solidity function name: exactInput
bytes path
A bytes array representing a route path including fees data
recipient
EVM address of the token recipient
deadline
Deadline in Unix seconds
amountIn
The exact input token amount in its smallest unit
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.
Resources:
Note: The Hedera Typescript 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 a maximum amount of HBAR to receive an exact tokens amount.
Solidity function name: exactOutput
bytes path
A bytes array representing a route path including fees data
recipient
EVM address of the token recipient
deadline
Deadline in Unix seconds
amountOut
The exact input token amount in its smallest unit
amountInMaximum
The minimum token amount to receive in its smallest unit
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.
Resources: