Atomic Swap & Non-Fungible Token Demo App

Improving Linera Example Applications

We’re excited to share two recent updates on the example applications of Linera!

  1. We added support for Atomic Swaps, that is, trustless exchanges of assets between microchains.
  2. We created a new demo app to illustrate Non-Fungible Tokens (NFTs) and how to transfer them between microchains.

:atom_symbol: Adding Support for Atomic Swaps

Microchains allow Linera users to store assets in their own chain of blocks, called a user chain. User chains allow quickly receiving and transferring assets, but how about swapping assets with another user chain?

Ideally, to make a swap, you’d like to simply move your assets to the other user’s chain while retaining ownership. Then, the other user would create a block to execute the swap—or cancel the operation altogether. Unfortunately, this solution does not work because nothing forces the other user to quickly (or ever) produce a block in their chain. When they don’t, the first user may find themselves in the unpleasant situation where the assets have not been exchanged, yet they are unavailable and cannot be recovered until the second user creates a block. In theory, we could use another type of microchains where block production is assumed by validators—known as public chains in the Linera whitepaper. However, public chains are expensive and not ideal for a one-time operation.

The solution to this problem uses a temporary chain, where both users can create blocks but with limited permissions. We extended the matching-engine example application to illustrate this. Now, if you and I want to swap tokens, we can create a chain co-owned by both of us, and set it up so that at any point, any owner can create a block to cancel the operation or (ideally) complete the swap. In that sense, the swap is atomic: It either happens completely or not at all, and at any point in time you can, without having to wait for me, either get your tokens back or get the tokens you bought from me.

This is possible because the protocol was extended:

  • A chain can be restricted to allow only operations of a particular application.
  • It can be configured so that its owners can only close the chain by calling that application.
  • Closed chains can now still reject messages. So, if you send assets to a closed chain, they can bounce back to you.

You can read about atomic swaps further in the matching-engine README.

:framed_picture: NFT Demo App

The repository now includes an NFT demo application—a testing environment for creating, managing, and transferring Non-Fungible Tokens within Linera’s microchain architecture:

Video

In the video above, we start out with two separate browser tabs, each representing a different account on the same chain. The first account starts empty, with no NFTs in its possession, leading to the creation of a new NFT named ‘nft6’—complete with a unique image. ‘nft6’ is transferred from the first account to the second, which is already populated with a variety of NFTs.

Similar to the existing ‘fungible’ app demo, initiating an NFT transfer in the UI of the sender creates a block in the sender’s microchain. This sends a cross-chain message to the receiver’s chain. If connected, the wallet of the receiver gets a notification from validators and notifies the web UI of the receiver, causing data to be refreshed.

Try both out on our devnet and let us know your thoughts. Questions or insights? Reply below.

6 Likes