Atomic Swaps

Portals AMM based DEX relies on atomic swaps between counterparties to achieve trustlessness and avoid attack vectors from validators. Much of the earlier sections discussed how the network works, in this section we will describe basics of atomicity that is integral to Portal Network security.

Atomic Swaps represent peer-to-peer trading methods employed to transfer cryptocurrencies between distinct blockchains without bridges, wrapping, or relying on trusted third parties.

An Atomic Swap protocol ensures the following:

  • When all parties adhere to the protocol, the swap occurs as intended.

  • If any party deviates from the protocol, no compliant participant suffers negative consequences.

  • No group has economic incentives to deviate from the protocol.

Atomic Swaps make use of Hashed Time Lock Contracts (HTLCs), which are a category of smart contracts designed to facilitate a trustless exchange of digital assets. Smart contracts employ an automated process that self-executes once all predefined conditions embedded within the contract are met.

Bitcoin Atomic Swaps are possible thanks to two key components encoded in Bitcoin’s HTLCs:

Hashlock

The hashlock mechanism enables the contract to be secured with a distinct cryptographic key that can solely be created by the depositor of the cryptocurrency. This unique key serves as a guarantee that the swap is completed when the party with the key (secret, pre-image) gives their consent to the transaction.

Timelocks

The timelock mechanism can be likened to a time limit for the swap. It guarantees that the transaction is finalized within a predefined time frame, and if this condition is not met, it makes possible the depositor to reclaim funds. Timelock plays a crucial role in ensuring security of swap transactions. It mandates that both parties must execute the swap within the stipulated time window for a successful swap.

Atomic Swaps vs. Bridges & Synthetic Assets

Atomic Swaps enable peer-to-peer exchanges, allowing users to swap cryptocurrencies directly without the need for intermediaries. On the other hand, cross-chain bridges establish a link between different blockchain networks, enabling the seamless transfer of digital assets through tokenized representations.

While both cross-chain bridges and Atomic Swaps contribute to improving blockchain interoperability and facilitate the movement of cryptocurrencies across various blockchain platforms, they operate differently. Cross-chain bridges act as connectors or intermediaries that facilitate asset transfers among multiple blockchain networks.

To use cross-chain bridges, a token must be locked on its original blockchain, after which a corresponding wrapped token or synthetic asset is generated on the target chain. This wrapped token is then deposited into a liquidity pool on the target blockchain, making it accessible for trading, transfer, or redemption for the corresponding underlying asset from the source blockchain. Wrapping generally incurs the expense of two gas fees/mining fees/transaction fees; one on each chain. Unwrapping, or redemption, again incurs the same.

Note that the two blockchains are independent, internally consistent systems that do not depend on external data to maintain or alter their contract states or order of transactions i.e, there is no enforcement of the peg or redeemability at the protocol level on either chain. Usually, either a single entity or a group of entities “guarantees the trust” in the peg between the wrapped token and the original token and its redeemability. There have been several instances of wrapped token attacks and failures and there is a need for atomicity as opposed to bridges, which is why Portal chose the more difficult but correct method of cross-chain swaps.

Example of a simple atomic swap flow

Atomic, non-custodial swaps are implemented using Hashed Time-Locked Contracts (HTLCs) residing on the L1’s or L2’s supporting the respective currencies being exchanged, i.e. there is an HTLC on each participating L1/L2. HTLCs contain the hash of a secret bit string (the “secret hash”) and are unlocked either when presented with the secret that originated the hash or after a time limit has expired.

When creating a swap order, each party supplies a secret hash and the node selects one of these for constructing both HTLCs. The party whose secret is chosen is the “secret holder” and the other party is the “secret seeker”. The secret holder is responsible for activating the HTLC using their in-the-clear, i.e. un-hashed, secret. Doing so reveals the secret to the secret seeker which may then activate their HTLC.

HTLCs are also constructed with a time limit by which they must be activated. After the time limit expires, the HTLC can no longer be unlocked with a secret and each party may retrieve their original currency any time thereafter.

Simple Swap Flow

  1. SecretKnower/Maker initiates the process by generating a random 32-byte secret, which is then hashed to create hashOfSecret. An order is subsequently generated using this hashOfSecret.

  2. SecretSeeker/Taker enters the picture by matching the order. Following this, SecretKnower/Maker establishes a locked deposit for SecretSeeker, utilizing the same hashOfSecret.

  3. SecretSeeker/Taker acknowledges the deposit and, in response, generates a counter-deposit on the opposing chain, also utilizing the hashOfSecret lock.

  4. Upon detecting an incoming payment on the opposing chain, SecretKnower/Maker proceeds to claim the original payment from SecretSeeker/Taker. Simultaneously, SecretKnower/Maker reveals the secret to SecretSeeker/Taker via chain data.

  5. Subsequently, SecretSeeker/Taker employs this same secret to claim the funds on the original chain.

A complete description of how atomic swaps work between different blockchains is presented in detail in this paper.

Last updated