Skip to main content

Cross-chain Swaps

Swap scenarios

Let's consider the following scenario: Alice has 100 MVRK and wants to exchange them for 0.1 BTC from Bob. How does she do it? There are multiple ways:

Using a centralized exchange

One way is to use a centralized exchange like Binance. This means that you must trust them to store your tokens and process the transaction correctly and securely. But this way relies on a single point of failure that could, potentially, fail at any time. Binance was hacked in 2019, and 7,000 bitcoins were stolen. In addition, exchanges usually take a fee for the service they offer. Does a better solution exist? Could we use a DEX instead?

swap-cex

FIGURE 1: Illustration of non-atomic swap on a centralized exchange

Using a DEX

Using a DEX is a great solution for making trustless token swaps. However, they only work within the same blockchain network. Normal DEX's live solely on their chain and can handle their chain's tokens. That wouldn't work if you want to exchange MVRK against BTC! One way would be to use wrapped assets as seen in the previous chapter and exchange wrapped MVRK (wMVRK) against wrapped BTC (wBTC) on a Mavryk DEX. However, this increases the difficulty of the process, as you must wrap and unwrap the tokens, which requires a third party for BTC. Additionally, you have to trust that the smart contract that allows you to unwrap your wBTC for actual BTC is secured. So, does a fully trustless solution exist?

swap-dex

FIGURE 2: Illustration of non-atomic swap on a decentralized exchange

Cross chain swaps

A cross-chain swap (also referred to as an Atomic Swap) refers to the exchanging of two different cryptocurrencies, on two different blockchains in a peer-to-peer fashion, i.e. without using a third party. This is possible thanks to a code locking mechanisms known as Hash Time Locked Contracts (HTLCs) doable into blockchain transactions. For this, Alice and Bob have to proceed as follows:

  • Alice must deposit her MVRK into an HTLC smart contract which acts like a safe and locks the funds. When this safe is created, Alice also generates a key to access it.

  • Alice shares a cryptographic hash of this key with Bob. Note that Bob can't access the MVRK yet because he only has the key's hash and not the key itself.

  • Bob uses the hash provided by Alice to create another HTLC smart contract, in which he deposits his BTC (Bitcoin is indeed capable of running basic smart contracts such as HTLC).

  • To claim the BTC, Alice is required to use that same key, and by doing so, she reveals it to Bob (thanks to a particular function called hashlock from HTLC).

  • This means that as soon as Alice claims the BTC, Bob can claim the MVRK and the swap is complete.

The term atomic relates to the fact that these transactions either happen entirely or not at all. If any of the parties give up or fails to do what they are supposed to, the contract is canceled, and the funds are automatically returned to their owners.

atomic-swap

FIGURE 3: Illustration of an atomic swap.

Alice's HTLC will timeout and refund the funds to Alice automatically if Bob never sends the funds.

atomic-swap-fail

FIGURE 4: Illustration of a failed atomic swap (Bob did not send the funds).

Hash Time Lock Contracts

Hash Timelock Contracts (HTLC) are one of the critical components that make atomic swaps possible. As the name suggests, they are based on two essential functions:

  • a hashlock, which prevents funds from being spent unless a piece of data is revealed (Alice's key in the previous example).
  • a timelock, which ensures the contract can only be executed within a predefined timeframe.

Consequently, the need for trust between the individuals is removed as the use of HTLCs creates a specific set of rules that prevent the atomic swaps from only executing partially.

To learn more about HTLCs, you can read this article.

Cross-chain swaps on Mavryk

References

[2] https://liquality.io/blog/atomic-swaps-explained/

[3] https://academy.binance.com/en/articles/atomic-swaps-explained