Swapping Basics
Chainflip swaps are fire-and-forget, meaning once a deposit is made, users don't need to do anything else for the swap to take place.
In a nutshell, the swapping process starts when a deposit is made to the State Chain through a process called Ingress.
The State Chain is an application-specific blockchain designed for the Chainflip protocol, making cross-chain swap transactions efficient. Whenever funds are deposited, the State Chain witnesses the deposit, where they are bundled and processed to be swapped in the Just-In-Time AMM protocol — all in the same block.
Once the funds are swapped, the new assets are ready to be sent to the destination address by a process called Egress.
Learn more about the Native Swap Flow.
Example: USDC (Ethereum) to BTC (Bitcoin)
Essentially, the process of JIT swaps on Chainflip looks like this for a user:
- A user initiates a swap by depositing 10,000 USDC to buy BTC. The deposit is made through a swap Deposit Channel linked to their BTC address.
- Market Makers monitor the Ethereum blockchain for the deposit and anticipate its execution on the Chainflip State Chain.
- After 7 Ethereum blocks, the deposit transaction is considered final in the State Chain, and is expected to be executed in about 48 seconds (8 Chainflip State Chain blocks).
- Market Makers now must include any limit or range order updates to compete for the trade on the BTC-USDC pool. They use risk model calculations and their capital on other exchanges to determine their best possible price for the trade.
- The swap deposit reaches the witness threshold and is executed. The swap consumes some range orders before the pool price matches the next best limit order.
- The Chainflip Validator network sends the swapped BTC funds to the user's native BTC wallet address.
Learn more about our novel Just In Time AMM Protocol.
Example: BTC (Bitcoin) to ETH (Ethereum)
The process is almost identical to the previous example, but in this case, there are two pools involved:
- A user initiates a swap by depositing 1 BTC to buy ETH. The deposit is made through a swap Deposit Channel linked to their ETH address.
- Market Makers monitor the Bitcoin blockchain for the deposit and anticipate its execution on the Chainflip State Chain.
- After 3 Bitcoin blocks (~30 minutes), the deposit transaction is considered final in the State Chain, it is expected to be executed in about 48 seconds (8 Chainflip State Chain blocks).
- Market Makers now must include any limit or range order updates to compete for the trade on the BTC-USDC and ETH-USDC pools. They use risk model calculations and their capital on other exchanges to determine their best possible price for the trade.
- When a swap route requires multiple swaps through pools, each swap is conducted sequentially, usually in the same block.
- The swap deposit reaches the witness threshold and is executed. Each swap consumes some range orders before the pool price matches the next best limit order.
- The Chainflip Validator network sends the swapped ETH funds to the user's ETH wallet address at the end.
Minimum accepted price: Slippage protection
Chainflip allows to set a minimum accepted price for a swap deposit channel. This allows to protect users against price changes between a quote and the execution of a swap (also known as slippage protection). When specifying a minimum accepted price, the protocol also requires a refund address and a retry duration.
If a swap is triggered through a deposit channel with a minimum accepted price, the Chainflip protocol will only swap the deposited assets if the available liquidity leads to a price that is equal or better than the specified minimum price. If this is not possible within the specified retry duration, the deposited assets will be refunded to the specified refund address on the source chain.
Minimum accepted prices are enforced on the AMM level and do not take deposit fees and broadcast fees into account. If a deposit is refunded, the Chainflip protocol deducts a deposit fee and a broadcast fee from the refund amount to pay for the transactions sent by the protocol. If the deposit address has a broker fee, it will be deducted from the refund amount.
USDC as an intermediate asset
It's important to note how Chainflip uses USD as the default pairing for all liquidity pools, with USDC (ERC20) as collateral. This design choice optimizes pricing while reducing liquidity fragmentation and aggregates slippage. USDC is chosen as the primary stablecoin base asset due to its high liquidity, adoption in DeFi, and secure structure compared to other options like USDT and algorithmic-stablecoins.
Learn more about the need of a common pair and the role of USDC in Chainflip's AMM.