Sequentia docs
Search
K

5.1. Atomic swap

In general, an atomic swap transaction allows two users to exchange tokens between different blockchains (inter-chain) or even from the same blockchain (intra-chain) in case the blockchain supports multiple tokens. Atomic swaps are Hashed TimeLock Contracts (HTLC) using Bitcoin script. Sequentia wallets that are also Bitcoin wallets can manage an atomic swap both in the Bitcoin network and in the Sequentia network; therefore, it implements atomic swaps for both cross-chain DEX transactions (between BTC and RAS tokens) and intra-chain transactions (between RAS tokens).
Below is an example of how an atomic swap works in the case of an exchange of BTC against a generic stablecoin called S-USD. Note that the timeframe required for these swaps can be much more compacted than an atomic swap between other chains, thanks to the anchoring system between Sequentia and Bitcoin blockchains described in §3.4, §3.7 and Sequentia transactions’ immediate finality described in §3.5.
I. User A creates a transaction tx1 on the Bitcoin network, sending an amount of BTC to user B. The transaction output has some specific conditions:
  1. 1.
    B can grab the coins within a specific timeframe (e.g., 6 Bitcoin blocks). Otherwise, A can get the coins back.
  2. 2.
    To make sure A can get the coins back in case B does not claim the money and the exchange does not happen, a second transaction is created: before A broadcasts tx1, B signs a second transaction which would send back the coins to A after a larger time frame than the locktime of the tx1 (e.g., 9 Bitcoin blocks). B can technically sign this child transaction even if the parent is unknown.
  3. 3.
    A broadcasts tx1 (only after B signs the child transaction and passes that to A) which may be included in a forthcoming Bitcoin block.
  4. 4.
    The only condition for B to be able to spend the output of tx1 within 6 Bitcoin blocks is to claim them before A, which is possible only if B knows a “secret” X revealed by A himself.
II. B does not know the secret X, but A sends the result of a hash function of that secret h(x) to B.
III: User B creates a transaction tx2 on the Sequentia network, sending S-USD to user A in exchange for the BTC. The transaction output has some specific conditions
  1. 1.
    A can grab the output of tx2 within a specific timeframe (about 4 Bitcoin blocks, calculated on the Sequentia network thanks to the anchoring system). Otherwise, B can get his coins back.
  2. 2.
    Before B broadcasts tx2, A signs a child transaction that would send back the coins to B after a larger timeframe (e.g., 7 Bitcoin blocks).
  3. 3.
    The only condition that allows A to spend those coins within 3 Bitcoin blocks is to reveal the secret. The “key” to unlock the transaction’s output is the secret X.
A must reveal X to grab the coins sent by B and has about 3 Bitcoin blocks to do so. Once A grabs the coins, B knows the secret X and can take the BTC sent by A.
Even if A reveals the secret at the very last minute to be included in the 3rd Bitcoin block, B has an additional 3 Bitcoin blocks to claim the BTC sent by A since the locktime for the first transaction’s output is 6 Bitcoin blocks instead of 3.
If A does not reveal the coins, both users will get their assets back within 6 Bitcoin blocks (B) and 9 Bitcoin blocks (A). If everything goes as expected, both users will receive the coins in the first blocks validated on the respective chain (Bitcoin and Sequentia), which will happen on average 5-10 minutes after tx1 is broadcasted to the network.
The only flaw of a traditional cross-chain atomic swap is that the time lock cannot be reliable, as one of the blockchains may be lagging behind due to hashrate variability. Depending on specific blockchain structures and rules, possible reorganizations or other factors may also come into play. Therefore, the LockTime parameters used in the two chains are inadequate to represent frames in solar time, which, if broken, may allow malicious play by the parties exchanging tokens. For this reason, atomic swaps generally require very large time frames in the range of 12-24 hours. If something goes wrong (e.g., one of the parties disappears), recovering funds might require an entire day.
Sequentia bypasses these issues by introducing the first reliable timeframes for cross-chain Bitcoin atomic swaps. Since it is structured as a Bitcoin sidechain, each Sequentia block carries a reference to the Bitcoin blockchain. The nLockTime of the two transactions returning the coins to the sender is calculated in the exact number of Bitcoin blocks counted on both chains. Furthermore, thanks to the anchoring system between Sequentia blocks and Bitcoin blocks, cross-chain atomic swaps are secure even in case of reorg (chain reorganization): if a reorganization happens on Bitcoin, the Sequentia chain reorganizes, too, as part of the consensus mechanism.
Finally, the UTXO structure with Bitcoin script allows wallets to implement CPFP (child pays for parent) functionality, speeding up atomic swap transactions if they are stuck in the mempool due to too low fees. Wallets may even interrogate a watchtower (controlled by the user or by a service provider/oracle) to notify the user when the transaction spending output is late, suggesting a proper CPFP transaction fee amount.
The atomic swap smart contract performs the role of the trading engine in a DEX transaction and can be combined with any type of communication system to manage the orders and potentially even direct communication via private message: each party simply needs to specify the node id of their wallet to connect each other, initiating the swap. This solution is likely the best choice for OTC trades as it requires both parties to already be in contact and agree on the pair exchange details, like the precise amount of tokens and the relative price.
The difference with other types of OTC trades is that this procedure is entirely trustless. The communication is facilitated via standardized order messages (§5.3) that can be used to create the atomic swap transaction in the wallet automatically. This way, the trade can easily occur without requiring escrow or technical support.
Privacy is guaranteed because no one needs to know about the transaction except the two parties involved, whereas blockchain analysis cannot distinguish an atomic swap from any other transaction in the blockchain.
Atomic swaps can also be used for DEXs between unknown parties, either using intermediaries who manage an order book of orders submitted by users or a peer-to-peer software where users freely share their order packages. In the first case (with intermediation), the trading engine remains trustless, it being the atomic swap, but a third party is required to manage the communication between the exchanging parties. In the second case (p2p order book), both the trading engine and the communication system are trustless. There are also solutions in the middle, like centralized entities providing a ranking of the order packages exchanged via peer-to-peer, as it will be described in §5.6.