Native Swap Flow
Following the flow of a native Chainflip cross-chain swap
Let's look at how native cross-chain swaps are done from end-to-end in the Chainflip protocol. Chainflip swaps are fire-and-forget, meaning once the deposit is made, the user doesn't need to do anything for the swap to take place.
Either through the frontend, Chainflip SDK, or directly using the chainflip-CLI client, there are multiple methods of starting a Chainflip Swap.
- Calling the swap function in Chainflip Vault contracts, which can either be done by the user or by other contracts for maximum composability (EVM chains only)
- Directly submitting a swap Deposit to a Vault using special transaction metadata (Not yet built)
Funds sent straight to Chainflip vaults without initiating it using one of these methods will result in the loss of funds, as the network won't know what to do with it.
All of the above methods will register the swap and essential information, such as:
- Destination Asset
- Destination Chain
- Destination Address
- Any call data for a follow-on Cross-Chain Message (such as a Uniswap or external Bridge call)
Once a method is chosen, the user can send their funds on the external chain (BTC, Ethereum, to be swapped in accordance with their chosen method.
Deposit Channels remain open for 24 hours. After that time, the network may no longer recognize late deposits. Users should always open a new Deposit Channel and send their deposit immediately every time they swap.
The network will monitor open Deposit Channels for transactions and the Vault Contracts for swap calls. Upon seeing a deposit for a Swap, it will be Witnessed by the network and registered on the State Chain.
The delay between the deposit transaction being made on the source chain and its confirmation on the State Chain through Witnessing depends mainly on the source chain. On Ethereum, the threshold is currently 6 blocks (~90 seconds), whereas Bitcoin is 3 blocks (~30 minutes).
Pre-witnessing will allow the network to confirm transactions faster or slower depending on the value of that deposit. This will cut down the confirmation window for small deposits, and increase the security for larger transfers. This feature is currently under active development.
Once the swap is Witnessed it is ready to be swapped in the JIT AMM.
At the end of each block, Swaps are automatically bundled and processed. This is so that liquidity providers can bid on swaps in the same block where they occur. Liquidity providers monitor incoming deposits to bid on them at their best price.
- For each asset in each pool, swaps that take place in the same block are bundled together and swapped in one step to eliminate frontrunning and MEV potential. This is done separately for each direction of trade.
- For swap routes that require multiple swaps through pools, for example, BTC -> USDC -> ETH, each swap is conducted sequentially, usually in the same block.
- Whenever the swap input is first denominated in USD, the Chainflip Network fee is deducted. This could be at the start, middle, or end of swap processing depending on the route. This always occurs at some stage in the swapping process as a function of having$USDC Denominated Pools.
- For each swap conducted, liquidity fees are collected and the output is forwarded to the next step.
Once all required swaps have been completed, the swap output is ready for the final stage of processing.
Once a swap has been automatically executed on the State Chain, the assets to be paid out to the user are listed as a Pending Egress Transaction.
Using the Destination Address specified in the user’s Swap Initiation step, and bundling the Pending Egress Transaction with others into a single batch, estimated gas fees are deducted from the final Swap Output amount to compensate the network for making the Broadcast.
If the user has included external call data in their swap request, that transaction will be broadcast separately to account for additional gas costs incurred at the user's request that shouldn't be socialised, but the egress is executed using the same TSS and broadcast method.
The user now has their shiny new native assets sitting peacefully in their wallet (or anywhere else that they specified).