🔬
InterSwap Docs
  • Welcome
  • Protocol Level
    • Liquidity Pools
      • Weighted Pools
      • Stable Pools
      • Dynamic Weights
      • Gas Tokens Index(GTI)
    • Add/Remove Liquidity
      • New Pool Creation
      • Add Liquidity
      • Remove Liquidity
    • Swap
  • Integration Level
    • You Integrate us
    • We Integrate you
    • Cross-Chain Smart Routing
  • Supported networks
  • How To
    • Setting Up a Wallet
      • Creating a New Wallet
      • Adding Networks to Metamask
      • Connecting Your Wallet
    • Swap
    • Add Liquidity
    • Remove Liquidity
  • Security
    • Audits
    • Bug Bounty
    • Deployed Contracts
  • TOKENOMICS & GOVERNANCE
    • Allocation & Lockups
    • Token Value Accrual
    • Fees
    • Governance Model
    • Proposals & Voting
  • Information
    • FAQ
Powered by GitBook
On this page
  • Slippage Control
  • Refund
  1. Protocol Level

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 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.

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

PreviousRemove LiquidityNextIntegration Level

Last updated 2 years ago