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 a 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.
Swap exact tokens for HBAR
Swap an exact amount of tokens for a minimum HBAR amount. Solidity function name:exactInput
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.
Code overview
- JavaScript SDK
Resources: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 HBAR
Swap a maximum amount of tokens to receive an exact HBAR amount. Solidity function name:exactOutput
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.
Code overview
- JavaScript SDK
Resources: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.
Next steps
Swap quote (V2)
Fetch expected amounts from QuoterV2 first.
Swap tokens for tokens (V2)
Swap between two HTS tokens with the same router.
Contract deployments
SwapRouter and WHBAR IDs for mainnet and testnet.