Atomic swaps
TL;DR
- Atomic swaps is a feature of blockchains that enables trades in a trustless manner.
- With atomic swaps, two parties can exchange their assets without needing to trust in each other or the assistance of a mutually-trusted third party, commonly known as escrow.
- In an atomic swap, transfers of assets from different participants are bound together in such a manner that either all transfers happen or none.
- On most blockchains, atomic swaps is implemented via smart contracts, whereas on Hathor, it is a built-in feature, and is performed using a single blockchain transaction.
- On Hathor, one can perform an atomic swap among any asset on the blockchain, and involve a high number of participants restricted only by the upper limit of inputs and outputs in our blockchain transactions — namely, 255 inputs and 255 outputs.
Introduction
This article talks about atomic swaps. We will cover:
- what atomic swaps is;
- what problem it solves;
- how it works on other blockchains;
- why it is different on Hathor;
- what its capabilities are on Hathor;
- which use cases can benefit from it on Hathor;
- how it works on Hathor; and
- how to use it on Hathor.
Glossary
These terms are used throughout this article:
-
Transfer is a process by which ownership of assets is reassigned to a new owner.
-
Participant is a person or entity participating in a blockchain transaction.
-
Remitter is a participant of a transfer from which assets are transferred.
-
Recipient is a participant of a transfer to which assets are transferred.
-
Trade is an exchange of predefined and agreed amounts of multiple assets among multiple parties.
For example, Alice and Bob are participants in a transfer. Alice, the remitter, will transfer X amount of token A to Bob, the recipient.
Contextualization: the need for trades on blockchains
In most blockchains, transactions can have only one wallet transferring one asset (i.e., remitter wallet). Such kind of blockchain transaction is not enough for a trade to happen. A trade is an exchange of predefined and agreed amounts of multiple assets among multiple parties.
This implies the occurrence of at least two transfers of assets between at least two parties. For example, when you go to a bakery to buy bread, this event is a trade between you and the shop, composed of two transfers of assets, where you transfer cash to it, and it transfers bread to you.
Contextualization: the problem with trades on blockchains
Thus, for a trade to be successfully registered onto such blockchains, one must use at least two transactions. But, once using such ordinary blockchain transactions, the first party to have their transaction registered into the blockchain has no guarantee that their counterparty will honor its end in the trade. For example, suppose Alice want to buy an NFT using some cryptocurrency directly from Bob, a stranger to her. After Alice transfers Bob her cryptocurrency, what guarantees that Bob will transfer her the purchased NFT?
The first solution that surged to address this problem was using a mutually-trusted third party — commonly known as an escrow — as an intermediary agent in the trade. This is what we have, for example, in centralized cryptocurrency exchanges, centralized NFT marketplaces, and over-the-counter cryptocurrency negotiations assisted by escrow. However, this solution doesn't allow the performance of trades in a 100% trustless manner.
What is atomic swaps?
A better alternative is the implementation on a blockchain of a feature called atomic swaps. Atomic swaps is the feature of blockchains that enables trades in a trustless manner. In the blockchain jargon, such trades are atomic swaps.
A blockchain technology can implement atomic swaps with different mechanisms, but in all of them the principle is the same. Transfers of assets from different participants are bound together in such a manner that, either all transfers happen or none.
Let's take again as an example the trade between Alice and Bob. But this time, they will use atomic swap. To buy the NFT, Alice needs to transfer an amount of some cryptocurrency to Bob's wallet. To sell the NFT, Bob in turn needs to transfer it to Alice's wallet. Atomic swap binds transfers to form a trade proposal. They both need to sign the trade proposal that is (or will be) somehow registered onto the blockchain. Once a valid and signed trade proposal is registered onto the blockchain, both transfers (from Bob to Alice and from Alice to Bob) happen irreversibly.
Atomic swaps in other blockchains
Most blockchains use smart contracts to implement atomic swaps. In this case, smart contracts replace the mutually-trusted third party as an intermediary agent, and since it (the smart contract) runs over the blockchain, the trade happens in a trustless manner.
Despite being a viable solution, it adds a layer of complexity. This was needed because, as explained in the beginning, most blockchains do not support transactions with more than one remitter wallet. Enters Hathor.
Atomic swaps in Hathor
On Hathor, we have a more sophisticated (yet simpler) implementation for atomic swaps. In Hathor, blockchain transactions can have more than one remitter wallet and more than one asset, and atomic swaps is a built-in feature. This allows the performance of trades on Hathor on a single transaction — namely, an atomic swap transaction.
Hence, unlike most blockchains, Hathor does not need smart contracts to enable trustless trades. Such approach removes the inherent complexity and risks of smart contracts for developers and users. Also, this is one of the differentials of Hathor, upon which we build our ethos of blockchain made easy.
Capabilities of atomic swaps in Hathor
In terms of types of assets, an atomic swap transaction may happen among any asset within Hathor, whether HTR (Hathor native token), custom fungible, and non-fungible tokens.
In terms of quantity of assets, it supports 16 different tokens besides HTR. Thus, if one of the tokens one wants to transfer is HTR, one may transfer up to 17 different assets.
In terms of participant wallets, atomic swap transactions allow a high number of remitters and recipient wallets, restricted only by the upper limit of inputs and outputs on our blockchain transactions — namely, 255 inputs and 255 outputs.
Which use cases can benefit?
Atomic swap allows use cases to employ sophisticated arrangements, such circular trades — e.g., Alice pays Bob if and only if Bob pays Carl if and only if Carl pays Alice — and group payments — i.e., everyone pays or no one pays.
Furthermore, the synergy between atomic swaps and the easy creation of custom tokens on Hathor, makes trades straightforward events. Therefore, where trades exist, benefits can be perceived. It may be the primary case of two strangers exchanging assets without requiring an escrow, until its extrapolation for marketplaces. In the case of marketplaces, they stop acting as a mutually-trusted third party while keeping their roles as a meeting and negotiation point among buyers, sellers, customers and merchants.
How it works: the anatomy of an atomic swap transaction
As already said, atomic swaps in Hathor is way more sophisticated than that made with smart contracts. In most blockchains, all remitter wallets need to transfer their assets to the smart contract, that acts as an escrow for the trade. A complete trade takes at least four transactions to happen.
In Hathor, a blockchain transaction can have multiple remitter wallets transferring multiple assets to multiple recipient wallets. For example, let's suppose the case where we have the following trade: Alice will transfer to Bob X amount of token A, while Bob will transfer to Alice Y amount of token B. The following diagram shows how a single Hathor blockchain transaction accommodates both transfers:
As in any regular transaction, the sum of the inputs amounts must match the sum of the outputs amounts — thus, , and .
How it works: the need for off-chain communication
Beyond having inputs and outputs from Alice and Bob, the transaction discussed in the previous section also has the signatures from both of them. Alice and Bob must sign the transaction before any one submits it to Hathor Network.
It requires that Alice and Bob establish an off-chain communication channel between them to exchange transaction data — namely, transaction proposal partial and signatures.
This communication channel may use any medium Alice and Bob believe that best suits their needs. They may use, for example, email, Telegram, or discord. Take notice that the need for an off-chain communication channel is not a shortcoming of Hathor but a design decision to provide users the maximum level of privacy and choice concerning how they handle their negotiations.
How to use it?
To use atomic swaps, users have two requirements. The first is that all users must have wallet applications supporting the feature.
At the moment, from our three official wallet applications — desktop, mobile, and headless —, atomic swaps feature is supported only in Hathor headless wallet v0.18.0 or later.
Regarding third-party wallets integrated with Hathor Network, they may add support to this feature using Hathor wallet library v0.40.0 or later.
The second requirement is that, as discussed in the prior section, participants must establish an off-chain communication channel — e.g., email, Telegram, Discord, etc. — to exchange transaction data.
Key takeaways
In this article, we explained that:
- Atomic swaps is a feature of blockchains that enables trades of multiple assets, among multiple parties, in a trustless manner.
- On Hathor, atomic swaps is a built-in feature, performed with a single blockchain transaction.
- To use atomic swap transactions, all engaged remitters of assets need to have wallet applications with support to the feature, and additionally, establish a communication channel among them, to negotiate, assemble, sign, and submit the transaction.
What's next?
-
Life cycle of atomic swap transactions: to understand the conceptual implementation of the atomic swaps feature into a use case.
-
Create an atomic swap transaction with Hathor headless wallet: hands-on tutorial to assist developers to implement atomic swaps in their use cases.
-
Hathor headless wallet HTTP API reference: for developers to consult while implementing atomic swaps into their use cases.