Exchange

The Exchange contract is a factory that processes user swaps with a particular exchange adapter. In general, an exchange call looks like:

Drawing

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:

    1. SaucerSwapV1

    2. SaucerSwapV2

    3. Pangolin

    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.

  • amountFrom and amountTo - 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 provide amountFrom=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 calculate deadline:

  • 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 of tokenFrom for a variable amount of tokenTo), you need to pass false. Otherwise (meaning you want to receive an exact amount of tokenTo using a variable amount of tokenFrom), pass true. For more explanation about AMM basics and slippage, see: https://academy.binance.com/en/articles/what-is-an-automated-market-maker-ammarrow-up-right

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