Blog

|

engineering

Don't invest unless you're prepared to lose all the money you invest. This is a high-risk investment and you should not expect to be protected if something goes wrong. Take 2 mins to learn more.

Peerswaps

A Lightning channel rebalancing mechanism

Tom Kirkpatrick

Dec 06, 2023

Peerswaps enable atomic swaps between Lightning Network and Bitcoin (BTC) or Liquid Bitcoin (L-BTC). This can enable a cost effective and capital efficient way to maintain well balanced Lightning channels.

We’ll take a practical look at how to go about performing a peerswap, and how you could add peerswaps to your channel rebalancing strategy.

Run the Daemons

Alongside your Lightning node (peerswap currently supports LND or CLN), run the Peerswap daemon, and optionally the Elements daemon if you want to use L-BTC.

The Peerswap node will act as the swap coordinator.

The Elements node will act as a Liquid Bitcoin wallet which you can use to swap funds into and out of your Lighting channels. If you want to use regular BTC instead of L-BTC, you can use the on-chain bitcoin wallet that’s integrated with your Lightning node instead.

When you want to rebalance the channel, use the Peerswap daemon to initiate a peerswap. The peerswap daemon will coordinate the transactions with your channel peer. After initiating the swap, you just need to wait for 2 confirmations on the Liquid network for everything to complete. The whole process takes about 2 minutes (the time it takes for 2 blocks to confirm on the Liquid blockchain).

Whitelist Peers

Peerswaps are direct swaps with direct channel partners. To use Peerswap your channel partner must also be running Peerswaps and must have whitelisted you in their peerswap config file (or set the accept_all_peers option). You must also whitelist them in yours. You can run pcli listpeers to check which of your connected channel peers support peerswaps and which chains they support (BTC and/or L-BTC).

Why the whitelist? Consider that a peerswap is an operation that has a cost (on-chain fees), a time opportunity cost (funds locked during the swap operation), and an effect on your liquidity profile (swapping capital between Lightning and BTC or L-BTC). It’s probably not something that you want to blindly allow anybody to do. However, it is something that you could reasonably enable between proven node operators within your own network of channel peers, or even your own nodes if you run multiple.

Rebalancing Use Bitcoin (BTC) or Liquid Bitcoin (L-BTC)

Peerswaps supports swapping using both the Bitcoin base chain, and the Liquid Bitcoin federated sidechain. Each has their pros and cons, but for the purposes of rebalancing channels, the Liquid Network has a distinct advantage in two important areas - speed, and cost.

Speed

Blocks on the Liquid network are produced every minute and only 2 confirmations are required to complete a peerswap. So it takes ~2+ minutes to finalize a peerswap.

On the Bitcoin network blocks are produced on average every 10 minutes and it will take 3 confirmations to complete a peerswap. So it takes ~30+ minutes to finalize a peerswap.

Cost

The average transaction fee on the Liquid network is currently 0.1 sat/vbyte to get into the next block (although this may change over time). Given an average transaction size of ~2000 vbytes it would cost you about 200 sats to perform a swap. That is about $0.025 by today's exchange rates.

On the Bitcoin base chain, fees are variable but generally much higher. At the time of writing, it will cost about 50 sat/vbyte to get into the next block. Given an average transaction size of ~300 vbytes it would cost you about 12,000 sats to perform a swap. That is about $5.62 by today's exchange rates. In other words, about 225 times more expensive than L-BTC, and a material consideration if you are a Lightning routing node operator or are running a business in which channels need to be regularly rebalanced.

Other Considerations

Of course, cost and speed aren’t the only considerations. To use Peerswaps you will also have some additional overhead.

Running peerswaps itself is pretty straightforward. It’s only acting as a coordinator so it doesn’t directly hold funds, although it does need access to your nodes, and it does need to maintain state so you will need to ensure you have robust secrets management and backup solutions in place.

If you want to take advantage of Liquid you also need to run the Elements daemon where there is more to consider, as the Elements wallet will be holding an L-BTC balance. And there is also the different trust model of the Liquid federation itself to consider.

BTC vs L-BTC

All in all, if you're looking for a fast, very cost effective way to maintain balanced channels between semi-trusted direct channel peers, L-BTC is a good option. Although using BTC for peerswaps may also be useful and preferable in certain situations. This comparison from the Peerswaps team shows some of those considerations:

Performing Swaps

Depending on the state of your channel, you’ll perform a swap-in or a swap-out to rebalance it:

The Swap-Out

When most of the liquidity is at your end of the channel (you are short of inbound liquidity) you swap some of your Lightning Bitcoin with your peer’s L-BTC (or BTC); you perform a swap-out.

In the swap-out, you will generate an L-BTC (or BTC) on-chain address, your peer will generate a Lightning invoice for the same amount, and you will then pay this invoice using the unbalanced outbound heavy channel. This pushes Lighting Bitcoin along the channel, to your peer, rebalancing the channel. As part of the same process your peer will send the required BTC amount to your on-chain address.

The Swap-In

Conversely, when most of the liquidity is at your peers end of the channel (you are short of outbound liquidity) you swap your L-BTC (or BTC) with your peers Lightning Bitcoin; you perform a swap-in.

In the swap-in, you will generate a Lightning invoice, your peer will generate an L-BTC (or BTC) on-chain address, and they will then pay your invoice using the unbalanced inbound heavy channel, pushing their Lighting Bitcoin along the channel to your side of the channel making it yours, and in the process rebalancing the channel. As part of the same process you will pay the required amount of BTC to your peer’s on-chain address.

The end result - ~2 minutes later your channel will be back at your preferred liquidity profile for a cost of less than $0.03, if you used liquid. You could do this dozens of times in a single day and only pay $1 total at today’s rates.

Automation to keep the channel balanced

With both yourself and your peer running peerswaps and elements, you now have the ability to keep channels between each other balanced for a very low cost.

One way to do this would be to maintain a pool of working capital in the form of L-BTC, which you peg into the Liquid sidechain from the Bitcoin chain when on-chain fee rates are relatively low. You could then automate things such that if all the liquidity in the channel moves towards 100% in either direction you can use peerswap to bring it back towards 50% by swapping into or out from the channel balance to and from your L-BTC working capital pool.

This can enable you to maintain fewer, smaller, well balanced channels with a very small on-chain footprint.

Testing with Peerswap Playground

We put together this simple PeerSwap Playground which you can use to try it out. It provides a Docker stack that comprises bitcoind (running on regtest), elementsd, 2x lnd nodes, 2x peerswap clients, and 2x L-BTC wallets. It connects everything together, initializes the wallets, and creates a channel between the two nodes.

You can use this to perform test swaps or just practice your skills with lnd, elementsd, and peerswapd! There are some simple instructions in the project’s readme which will walk you through the setup and the process of performing a swap.

Rebalancing channels with Strike using Peerswaps

If you support Peerswaps and are one of our existing channel partners or run a reliable routing node and would like to maintain a balanced channel with the Strike routing node, reach to our liquidity team at liquidity@strike.me.

Other Rebalancing Tools and Techniques

Peerswaps is a relatively new rebalancing technique. This comparison from the Peerswaps team conveys some of the key properties and tradeoffs of peerswaps as compared to other approaches to rebalancing channels.

All in all, Peerswaps between Lightning and L-BTC can be a reliable, cost effective, and capital efficient way to keep channels between semi-trusted peers well balanced.

More About Peerswaps

Peerswaps was created by the team at Blockstream. It's in its early stages and under active development. Peerswaps currently supports LND and CLN. There is only a command line interface at the moment, although a GUI integration with Ride the Lightning is in the works. The Peerswaps team has an active Discord where they engage with the community so if you need help or would like to help with the project you can head over to there.

announcements

Strike infrastructure update

We now serve customers on our own infrastructure

07 Sep, 2023

© 2024 Strike

This financial promotion was approved by Englebert LTD (FRN 1001386) on Nov 19, 2024, 3:07:43 PM

Get off zero.

Strike

BitcoinPaymentsSend GloballyBusinessAPI

Platform