Skip to main content

Swap

The following example demonstrates how InterSwap's cross-chain liquidity transfer works:

To describe how cross-chain swaps work, let's use an example:

User wants to exchange 5 ETH to equivalent amount of BNB.

  1. User calls swap() function on ChildRouter.sol on source chain (Ethereum) with the following parameters:
{
SwapObj, //
bytes calldata data //
}

The SwapObj interface looks like this:

struct SwapObj {
bytes pool;
bytes srcToken;
bytes dstToken;
string srcChain;
string dstChain;
uint256 amountIn;
bytes sender;
bytes receiver;
}
  1. swap() function locks ETH, then sends Axelar message to MasterRouter.sol on masterchain with the following payload:
{
uint8 method, // SWAP_TYPE = 1
uint256 poolId, // id of the pool, genesis pool = 0
bytes tokenA, // source token
bytes tokenB, // dest token
uint16 chainA, // source chainId (axelar)
uint16 chainB, // destination chainId (axelar)
uint256 amountA, // amount of source token
uint256 amountOutMin, // amountOutMin of dest token
address from, // address of sender
address to, // address of receiver, LP tokens will be sended who
bytes memory data // arbitrary data for post-action integrations
}
  1. MasterRouter.sol receives payload, gets InterswapPool.sol by poolID and calls Pool.swap(). It changes reserves of the pool, adds amountOut to the payload and sends second chained Axelar message to destination chain (Binance Smart Chain).

  2. ChildRouter.sol receives a message, get the address of Lock.sol contract and calls unlock() function to unlock BNB

Slippage Control

Undesirable slippage is the only possible reason why order execution can fail. Before swap execution, InterSwap UI queries current expected amountOut from MasterRouter.sol and adjust amountOutMin value according to slippage settings %.

Refund

Undesirable slippage will trigger a refund message from masterchain to the source chain to unlock user's assets.

If a "refund transaction" will fail because lack of gas - user will be able to re-execute it from his end.

InterSwap provides explorer UI to enable control over all transaction history and refunds.