Exchange
The Exchange contract is a factory that processes user swaps with a particular exchange adapter. In general, an exchange call looks like:
Each adapter has its own implementation of swap (depending on DEX interfaces), but all of them implement the same interface for the factory.
To swap tokens through the factory, we should call the swap() method of the Exchange contract:
Parameters:
aggregatorId- for now it's 3 possible options for this field:SaucerSwapV1SaucerSwapV2Pangolin
Choose the one which DEX you want to use for swap.
path- specially encoded swap route. Can be different depending on the particular provider. The value can be retrieved from the API.amountFromandamountTo- amounts of tokens to swap in smallest units. Each token has a different number of decimals. For example, if you want to swap 1 GRELF to your account and GRELF has 8 decimals, you need to provideamountFrom=100000000.deadline- this is a remnant from porting DEXes from the Ethereum network, because on Ethereum transactions can wait a long time before execution (depending on gas strategy). On Hedera, most transactions are processed within 4 seconds. The value of this field should be a 10-digit timestamp. Just make sure this timestamp > current time. For example, in JavaScript you can use this snippet to calculatedeadline:
isTokenFromHBAR- true if you are swapping HBAR to something, otherwise false.feeOnTransfer- this parameter determines which token to apply slippage to. If you want to apply slippage to tokenTo (meaning you swap an exact amount oftokenFromfor a variable amount oftokenTo), you need to passfalse. Otherwise (meaning you want to receive an exact amount oftokenTousing a variable amount oftokenFrom), passtrue. For more explanation about AMM basics and slippage, see: https://academy.binance.com/en/articles/what-is-an-automated-market-maker-amm
There is also one more useful read-only function on the Exchange smart contract:
This function returns the current EtaSwap fee for a particular aggregator. The returned value is in per mille (promille). For example, if the function returns 1, it means the EtaSwap fee is 0.1%.
Last updated