The swapExactETHForTokens and swapETHForExactTokens function trades in HBAR but derives its name from Uniswap on Ethereum. This name was kept to simplify integration for developers versed in Uniswap tools.
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).
Swap Exact HBAR for Tokens
Swap an exact amount of HBAR for a minimum token amount.
Solidity function name:swapExactETHForTokens
Parameter name
Description
uint amountOutMin
The minimum token amount to receive in its smallest unit
Set the minimum output token amount (amountOutMin) 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.
Set the maximum HBAR amount (payable) 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 HBAR than expected.
Set the minimum output token amount (amountOutMin) 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.