Hathor Network
Search…
⌃K

Atomic Swap

TL;DR

  • 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.

Introduction

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;
  • what 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: process by which ownership of assets is reassigned to a new owner.
  • Participant: entity (person or organization) participating in a transfer, whether as a remitter or a recipient.
  • Remitter: participant of a transfer from which assets are transferred from.
  • Recipient: participant of a transfer to which assets are transferred to.
  • Trade: 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 into blockchain

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.

Contextualization: the problem with trades into blockchain

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.

What is atomic swap?

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.

Atomic swap in other blockchains

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.

Atomic swap in 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.

Capabilities of atomic swaps in Hathor

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.

What use cases can benefit?

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.

How it works: the anatomy of an atomic swap transaction

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:
x1+x2=X+x3,and y1+y2=Y+y3x_1+x_2=X+x_3, and \space y_1+y_2=Y+y_3

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 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.

How to use it?

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.

Key takeaways

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.

What's next?

References

  • 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 1mo ago