With the introduction of BEP3, now it’s possible to use atomic swaps to trade tokens native to Binance Chain for tokens native to other chains in a totally trustless way.
How do atomic swaps work?
Atomic swaps work through a multi-stage mechanism that makes sure that either the whole trade is completed or no part of it is, making it so it’s impossible for one side to cheat by obtaining some funds without having honored their part of the trade. We’ll illustrate how these work using an example: Alice, who owns 1 BTC, will trade it with Bob for 1 BNB through an atomic swap:
- Alice locks her BTC into a smart contract that says “If Alice reveals her secret before X minutes, send this bitcoin to Bob, otherwise send it back to her”.
- Bob locks his BNB in the same way.
- Alice then proceeds to reveal her secret in order to claim Bob’s BNB.
- By revealing her secret, her BTC transaction is unlocked and Bob claims Alice’s BTC for himself.
While the trustlessness of this mechanism is perfect, everything isn’t rosy in atomic swap land, as these mechanisms have an important set of drawbacks that have prevented them from getting any real-world usage:
- They’re slow due to the fact that steps 2 and 3 must wait for the previous step to finalize, so there’s no skipping the waiting time of new transactions on both chains, which in Bitcoin is around 1 hour.
- They are always worse for one part of the deal, usually the party initiating the first step. This is because Bob can always wait some time and take the trade only if the price has moved in his favor, at the expense of Alice, who will be trading at a worse rate (another way of looking at it is that Alice is granting Bob an option on her BTC, at her expense).
- They are expensive, as they require 2 transactions on each chain for every single trade performed.
BEP3 atomic swaps
BEP3, an improvement proposal for Binance Chain that was deployed on main chain a few weeks ago, introduced three new transaction types that make atomic swaps viable:
- Hash Timer Locked Transfer (HTLT): Transaction used to lock tokens on Binance Chain, in the example provided before this transaction would be used in the 2nd step (Bob locks 1 BNB).
- Claim Hash Timer Locked Transfer: Used by the counterparty to claim the locked tokens after the secret is revealed, to be used in step 3 of the example flow (Alices claims 1 BNB).
- Refund Hash Timer Locked Transfer: Used when the atomic swap falls through because the secret was not revealed before the deadline expired, this transaction will just send the tokens back to their previous owner, thus going back to the initial state (minus some transaction fees). This is not used in the example, as it depicts a successful atomic swap and this transaction type would be used only in a failed one.
Furthermore, BEP3 also adds support for swaps inside Binance Chain (eg: a swap between two different BEP2s) through yet another transaction named Deposit Hash Timer Locked Transfer, which allows users to deposit BEP2 tokens to be locked along with an HTLT.