Cross-chain swaps with state channels

This repo contains scripts that run atomic asset swapping between two local blockchains, using a hashlock approach. See this presentation for an overview.

To install dependencies, run yarn (install if necessary).

In common/two-chain-setup.ts, there are functions that can be used to start two blockchains, and an Executor and Responder class representing two actors, each having an externally owned account on each chain and owning some tokens on just one chain or the other (initially). These actors will track the amout of gas expended during the swaps, and their token balances on ecah chain.

The two chains are called left and right, and "belong to" the Executor and Responder, respectively (meaning that chain is where that actor's assets start out). As you may expect, the Executor is the actor who sets up the swap and has the power to execute it.

The approach taken here is to bypass the state channel wallets entirely; this avoids the need to manage any runtime dependencies such as a database or messaging system.

Vector protocol

Connext have made cross-chain swaps their main focus, and are rolling out a network of liquidity pools to facilitate that.

In vector-atomic-swap/happy-case.ts, there is a typescript script which will setup the two chains, deploy the relevant contacts to both chains, and fund/defund channels to execute the swap. Note that the state channel message passing part, plus any checks that should be done in a real application, is currently incomplete.

Helper functions (specific to vector protocol) are located in helpers.ts.

Executing yarn go-vector will execute the script with ts-node. You should see color-coded output detailing the steps that happen and the gas expended.

The happy path looks like this:

Note how the responder submits 3 of the 4 transactions. This matches the experience at spacefold.io, and the demo in this repository shows very similar gas costs.

Note also that much of the off-chain messaging is not well understood just now (hence the ?s). This is a TODO :-)

Nitro protocol

Nitro protocol also supports atomic swaps.

In nitro-atomic-swap/happy-case.ts, there is a script which will setup the two chains, deploy the relevant contacts to both chains, and fund/defund channels to execute the swap. Note that the state channel message passing part, plus any checks that should be done in a real application, is only partially complete.

Helper functions (specific to nitro protocol) are located in helpers.ts.

Executing yarn go-nitro will execute the script with ts-node. You should see color-coded output detailing the steps that happen and the gas expended.

The happy path looks like this:

The gas costs are evenly split between Executor and Responder.

Future directions

  • Complete the off-chain part of the swap in both protocols
  • Explore unhappy paths where one or the other actor backs out of the swap
  • Explore the security of the swap by trying to have one actor steal the other's coins.