Our readers often ask us what exactly is Atomic Swap, and how does it guarantee a secured transaction.
Let us look into the technology in more detail.
In Crypto, the term “Atomic” was adopted from databases, where there is a number of database operations that either occur altogether or do not occur at all (or are brought back to their initial condition, if one of the operations is interrupted, or if something else goes wrong).
These operations are named Atomic Transactions.
Atomicity is one of the four characteristics of “ACID” transactions, with the remaining three being: Consistency; Isolation; Durability. All of these characteristics are necessary, in order to perform a transaction within the database.
The word Swap speaks for itself. It is the exchange of one cryptocurrency for another. By bringing these two meanings together, we get the following definition of an Atomic Swap – a series of operations with two blockchain systems, that lead to a successful exchange of two cryptocurrencies. If one of the conditions of the transaction is not met, the swap is not confirmed, excluding a situation where only one of the two participants receives their cryptocurrency.
The principals of the atomic swap are used in a variety of decentralized exchange technologies. For example, in all sorts for onchain DEXax ( idex, waves, etherdelta, bitshares) and Cross-Chain exchanges (komodo, hodlhodl, swap.online). In the near future we expect to see them be used in PCN (Payment Channel Networks), for instance – Lightning Network.
All of these technologies used differently and have various levels of decentralization. Nevertheless, they all solve the problem of creating a balance between decentralization and speed. The usage of atomic swaps is becoming more popular among Cross Chain exchanges based on HTLC technologies (Hash Time Lock Contract).
As you already know, Hash – is a one-way encryption, with a determined result. This means that for any given secret you will always get the same encrypted line that is difficult to reconstruct (if being precise – very-very hard to reconstruct).
HTLC is a smart contract for an agreed amount of cryptocurrency. The amount can only be sent to a predetermined wallet(address – account) after providing the secret to the hash junction within the given time frame. When creating a contract, you only know the hash function of the secret, keeping the secret itself unavailable to the public. By creating symmetric HTCL on two different block-chains, you get a simplified Atomic Swap mechanism.
Alice creates a smart contract on the Bitcoin blockchain, stating Bob’s address as the beneficiary. Bob creates the same exact contract, but only on the Ethereum blockchain, mentioning Alice’s address as the beneficiary. Both contracts use the same Hash function, but only one of the two parties knows the Hash functions secret.
There is no difference who knows the secret, as long as the party that does know it creates a contract with a more extended time frame.
Here is how the HTLC works. Both of the contracts are symmetric, and the first party to receive their crypto-currency is the one that holds the secret. For example, Alice is the holder of the secret. She provides the Secret to Bob’s smart contract on the Ethereum BlockChain. Once the smart contract receives the secret, it calculates its hash function, and if it matches the hash function in the smart contract, Alice will receive here Ethereum, that will be sent to her wallet.
As we know, all the operations in the blockchain are seen by the public, so when Alice reveals the secret – Bob will immediately receive it. Given that Alice’s Bitcoin Blockchain contract holds the exact same hash function when Bob provides it with the secret he automatically received his Bitcoins to his wallet.
If for some reason Alice does not reveal the secret, Bob will get his Ethereum back after his smart contract’s time frame runs out. In addition, if Alice provides an incorrect secret, the calculation will not match the hash function of Bob’s smart contract, not allowing the transfer of the funds.
It’s that simple.
You can find more details on HTLC on the internet with different examples of how there are used.
Every Atomic Swap goes through two stages.
The first stage – “Order Matching”. Reaching an agreement between two participants of the Swap. To be precise, this important stage has to be done before the atomic swap. How this stage is accomplished defines projects from one another. There are different “handshake” protocols that can be used, for example, some use centralized services or sidechains, others use decentralized order-books.
Swap.online in its initial protocol version uses ipfs pubsub – a fully decentralized protocol for sending messages. Each order that a Market Maker places is distributed through the message channels that the Market Takers are subscribed to. As if announcing through a loudspeaker they are willing to exchange, for example, 10 of their Bitcoins for 100 of your Ethereums.
The Market Takers will see all the new orders after subscribing to the message channels. In order to view the Market Makers order constantly – he or she should always send out messages about its relevance, meaning the Market Maker should always be online in order to confirm an exchange.
Nevertheless, this does not bring any operational inconvenience. Because in any case for an HTLC protocol exchange you must sign the transactions in real time, and a Market Taker can show an interest to your order at any time.
Once a Market Taker decides to accept one of the orders, he/she sends a message of confirmation, and both parties create an HTLC contract on their blockchains and finalize the exchange as shown in the above-mentioned protocol.