> For the complete documentation index, see [llms.txt](https://interswap.gitbook.io/interswap-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://interswap.gitbook.io/interswap-docs/protocol-level/swap.md).

# 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:&#x20;

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

1. User calls **swap()** function on **ChildCommunicator.sol** on source chain (Ethereum) with the following parameters `messageObj, lzGasObj`:

```
struct messageObj {
    bytes tokenA; // source token
    bytes tokenB; // destination token
    uint16 chainA; // source chainId (layerzero)
    uint16 chainB; // destination chainId (layerzero)
    uint256 amountA; // amount of source token 
    uint256 amountB; // amountOutMin of dest token
    bytes to; // receiver on destination chain
    bytes from; // spender on source chain
    bytes data; // data for intergration layer
}
```

```
struct lzGasObj {
    uint256 dstGasForCall; // gasLimit to use for second chained transaction, by default 200 000
    uint256 dstNativeAmount; // amount of gas to be airdropped from Relyaer to dstNativeAddr, if 0 - no airdrop
    bytes dstNativeAddr; // receiver of airdropped gas
}
```

2\. **swap()** function locks ETH, then sends LayerZero message to **MasterCommunicator.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 (layerzero)
    uint16 chainB, // destination chainId (layerzero)
    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
}
```

3\. **MasterCommunicator.sol** receives payload, gets **Pool.sol** by poolID and calls **Pool.swap().** It  changes reserves of the pool, adds amountOut to the payload and sends second chained LayerZero message to destination chain (Binance Smart Chain).

4\. **ChildCommunicator.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 **MasterCommunicator.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.&#x20;

InterSwap provides explorer UI to enable control over all transaction history and refunds.&#x20;
