Home
Blog
Decentralized Trading - Atomic Swaps and Submarine Swaps
X Min read

Decentralized Trading - Atomic Swaps and Submarine Swaps

Blockchain networks don’t communicate well with one another. This model siloes digital assets and can make it difficult to conduct decentralized trading across networks. Fortunately, atomic swaps and submarine swaps are two methods of crypto trading that allow for trades across blockchains and their layers. 

Table of contents
Share this article
This is some text inside of a div block.

Written by: LNSwap team

Posted on: Feb 17, 2023

Updated on: Feb 17, 2023

Blockchain networks don’t communicate well with one another. This model siloes digital assets and can make it difficult to conduct decentralized trading across networks. Fortunately, atomic swaps and submarine swaps are two methods of crypto trading that allow for trades across blockchains and their layers. 

What is an atomic swap?

This is a crypto swap between two digital assets across different blockchains without the use of a trusted intermediary. This allows users to trade across blockchain networks in a non-custodial, completely decentralized way that doesn’t compromise their identities or other personal information. 

The name “atomic swaps” comes from the idea that these transactions are either completely fulfilled based on all of their conditions, or are canceled entirely. It’s widely believed the first swap was completed in 2017 between the Decred and Litecoin blockchains. Since then, these swaps have grown to be used on many different decentralized networks.

Atomic swaps use a type of smart contract known as a hashed time-locked contract (HTLC) to allow token swaps off-chain. These HTLCs lock digital assets securely until both sides of the transaction are completed. However, as per their name, HTLCs also include a timelock which sets a deadline for the transaction. If the transaction doesn’t complete before the deadline, any assets locked into the contract are returned to their original owners.

An atomic swap example

Here’s how a swap might work between Alice using Blockchain A and Bob using Blockchain B:

  1. Alice receives a public key from Bob and generates a secret key, known as a preimage.
  2. Alice hashes the preimage on Blockchain A (to encrypt the preimage) and creates an HTLC contract claimable only by Bob.
  3. Alice deposits her cryptocurrency into the contract on Blockchain A (this deposit is refundable after a prespecified amount of blocks if the transaction does not complete).
  4. Bob uses the same hash as Alice to lock his funds into an HTLC contract on Blockchain B.
  5. Alice claims the funds deposited by Bob from Blockchain B using the preimage and reveals the preimage to Bob.
  6. Bob uses the preimage to claim the deposit from Bob on Blockchain A where Alice made her deposit.

Pros of atomic swaps

  • Creates decentralized trading compatibility across blockchain networks
  • Allows for non-custodial trading and removes the need for centralized intermediaries when conducting cross-chain transactions
  • Eliminates the need for two trading parties to have each other's personal information

Cons of atomic swaps

  • Are more complex to execute than centralized trading
  • Cannot do these types of swaps from crypto-to-fiat
  • Transactions are completed on public blockchains, which can reveal information about the traders
  • Liquidity is not used as efficiently when compared to centralized swap contracts

What is a submarine swap?

A submarine swap is a type of atomic swap designed specifically to move assets from a bitcoin address to an off-chain address, and vice versa. Submarine swaps utilize HTLCs to ensure the transaction is either completed or reverted, allowing for trustless swaps without custody or counterparty risk. The most common use for this is a swap between the Bitcoin blockchain and Lightning Network. 

The concept began with Lightning Labs, who was seeking a way to transfer on-chain bitcoin to off-chain channels on the Lightning Network. Today, submarine swaps are used not just for the trustless movement of digital assets on and off-chain, but also any other token tied to Bitcoin or its layers.

Applications of submarine swaps

There are many use cases for submarine swaps. One area where they are useful is for Lightning node owners. When a Lightning channel is first opened, a pre-set amount of bitcoin is sent to that channel. If it ever runs out, there is no direct way to refill that channel. Instead, another channel must be opened.

Submarine swaps make it easy to send on-chain bitcoin directly to refill Lightning payment channels to add liquidity to a Lightning node. The same is true in the reverse: funds from a node can be moved on-chain to close a Lightning channel or send digital assets to cold storage. 

Another use case is for trading tokens on Lightning. One example of this is Stacks. The Stacks blockchain runs in parallel to Bitcoin, allowing digital assets to be built with smart contracts. With submarine swaps, users can trade STX and BTC directly via a bitcoin address.

A submarine swap example

Let’s look at another example between Alice and Bob:

  1. Alice generates an HTLC address and deposits her cryptocurrency into the contract. 
  2. Alice generates a Lightning Network invoice and sends it to Bob.
  3. Bob pays the Lightning invoice and receives Alice’s preimage.
  4. Bob uses a preimage to verify the invoice was paid and unlocks the on-chain funds from the HTLC.

Pros of submarine swaps

  • Allows users to transfer funds off-chain and on-chain without closing Lightning Network payment channels 
  • Through Lightning, one side of the swap is near-instant and free.

Cons of submarine swaps

  • Can lack the liquidity of centralized trading
  • The user experience can be more complex for the average user

Secure swaps with LNSwap

The growth of the Lightning Network has created the need for compatible swaps between bitcoin on-chain transactions and Lightning transactions. Fortunately, LNSwap, a Trust Machines product, uses submarine swap technology and HTLCs to allow for secure swaps through Bitcoin at near-instant speed with low transaction fees. 

LNSwap uses Clarity smart contracts through the Stacks network to generate HTLCs that anyone can use to swap bitcoin for other digital assets on-chain and off-chain. The LNSwap platform provides a place where submarine swaps can be made in a completely non-custodial manner, meaning, the platform never controls user funds. This doesn’t only go for on-chain and off-chain bitcoin, but also trades between other Layer 2 digital assets like STX and XUSD.

Whether you need to transfer bitcoin to your Lightning channel, trade BTC for STX, or any other non-custodial Bitcoin network trade, LNSwap offers an easy-to-use solution.

Start swapping Bitcoin for Stacks

LNSwap is a non custodian crypto currency swap protocol that provides a fast, private way of swapping Bitcoin for Stacks and vice versa.

Table of contents