Atomic Swap
- Atomic swap is a feature of blockchains that enables trades in a trustless manner.
- With atomic swap, two parties can exchange their assets without the need to trust in each other or the assistance of a mutually-trusted third party, commonly known as an 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.
- In most blockchains, atomic swap is implemented via smart contracts, whereas in 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 within the blockchain, and involve a high number of participants restricted only by the upper limit of inputs and outputs on our blockchain transactions — namely, 255 inputs and 255 outputs.
This article talks about atomic swap. We will cover:
- what atomic swap is;
- what problem it solves;
- how it works on other blockchains;
- why it is different on Hathor;
- what are its capabilities on Hathor;
- which use cases can benefit from it on Hathor;
- how it works on Hathor; and
- how to use it on Hathor.
These terms are used throughout this article:
- Transfer is a process by which ownership of assets is reassigned to a new owner.
- Participant is an individual or organization participating in a transfer, either as a remitter or a recipient.
- 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.
In most blockchains, transactions can have only one wallet transferring one asset (i.e., remitter wallet). Such kind of blockchain transaction isn't 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.
Thus, for a trade to be successfully registered into 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.
A better alternative is the implementation on a blockchain of a feature called atomic swap. Atomic swap is a feature of blockchains that enables trades in a trustless manner. In the blockchain jargon, such trades are atomic swaps.
A blockchain 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 into the blockchain. Once a valid and signed trade proposal is registered into the blockchain, both transfers (from Bob to Alice and from Alice to Bob) happen irreversibly.
Most blockchains use smart contracts to implement atomic swap. 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.
In Hathor, we have a more sophisticated implementation for atomic swap. In Hathor, blockchain transactions can have more than one remitter wallet and more than one asset, and atomic swap is a built-in feature. This allows the performance of trades in Hathor in 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.
In terms of assets, an atomic swap transaction may happen among any asset within Hathor, whether HTR (Hathor native token), custom fungibles, 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.
The synergy between atomic swap and the easy creation of custom tokens in 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.
As already said, atomic swap in Hathor is way more sofisticated 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 next picture shows how a single Hathor blockchain transaction acommodates both transfers:

As in any regular transaction, the sum of the inputs amounts must match the sum of the outputs amounts — thus:
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 pushes 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.
To use atomic swaps, users have two requirements. The first is that all users must have wallets applications supporting the feature.
At the moment, from our three official wallet applications — desktop, mobile and headless —, atomic swap is already supported only in Hathor wallet headless. So, products integrating or building upon Hathor can already use it.
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.
Note: nothing prevents third-party wallet applications — of course, that already connect with the Hathor Network — from adding atomic swap. This is because the library used to connect with Hathor Network and develop Hathor official wallets Hathor wallet library is already implementing it.
In this article, we explained that:
- Atomic swap is a feature of blockchains that enables trades of multiple assets, among multiple parties, in a trustless manner.
- In Hathor, atomic swap 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 push the transaction.
- Get started with atomic swap in Hathor wallet headless: tutorial to assist developers who use Hathor wallet headless to get started with atomic swap.
- Hathor wallet headless API reference: to consult while adding atomic swap transactions into your use case.
- If you are interested in seeing the process of development of atomic swap in Hathor, you can see this pull request, where we added support to this feature in wallet headless.
Last modified 8mo ago