How to automatically rebalance your channels - profitably!

Note: This tutorial is made available free to the Umbrel community. If you’d like to tip me for my work in publishing this article, please consider purchasing it on Y’alls.

Greetings, all

I’m happy to report that thanks to generous help from Carsten Otto, I now have rebalance-lnd working smoothly on Umbrel, in a way that won’t get erased every time I update the OS (though not in a docker, for now).

I wanted to create another guide (like my guide for charge-lnd) for all of you who are non-technical (like me), so that you can simply follow along with the steps and get automatic, profit-calculated rebalancing working on your node. As you probably know, Umbrel is notoriously finicky about you playing with it’s core files, so my main objective here was to install all files either on the SSD, to prevent you from losing your configurations during updates, so that no core system functionality or apps are disturbed

0. What is Rebalance-LND, and why do you need it?

As you likely already know, rebalancing through Thunderhub or RTL is time consuming, and doesn’t work half the time. You have to try 20 different channels, at different prices, and different amounts, to get 1 rebalance to work.

There are lots of rebalancing scripts that will “probe” and try different routes for you, including Balance of Satoshis. But as far as I know, they don’t consider profitability!

Rebalance-LND is smarter. It takes into consideration both how much you’ll earn if the sats are re-routed, as well as how much you’re giving up in routing fees by sending the sats from another channel.


(Note the 2X profit rate on the route, within 1 hour)

This is a pretty big deal… it saves a ton of time, plus, it makes sure that you never rebalance in a way that would lose you money:

Alright, let’s begin:

1. Log in to your Umbrel’s ssh via a command line tool on your computer (Terminal on Mac, for example):

ssh -t umbrel@Umbrel.local

You’ll need your admin password, which is the one you set on the back end.

2. Create a directory on the SSD for the plugin files (so it doesn’t get erased upon updates or SD card reflashes)

sudo mkdir -p /mnt/data/upgrades/rebalance-lnd

3. Clone the Github Directory (The Files) into your new folder

cd /mnt/data/upgrades/rebalance-lnd && git clone https://github.com/C-Otto/rebalance-lnd.git

4. Install Python 3

pip3 install -r requirements.txt

(This may take a long time. Be patient)

5. Check that it’s working by listing your channels:

Run this command:

/mnt/data/upgrades/rebalance-lnd/rebalance.py -c

(-c is for listing channels)

It should list out all of your channels, in order of which ones need the most balancing.

6. Try a 100,000 sat rebalance

Take one of your channel ID’s that you’re sure will route a lot of sats at the price you currently have set. Copy/paste the long number on the left column:

Then, in the command line, type:

/mnt/data/upgrades/rebalance-lnd/rebalance.py -t [PASTE NUMBER HERE] -a 100000

Explanation:

  • -t tells the script which channel you want the sats to go to
  • -a tells it the amount. It’s good to try 100k or less - bigger rebalances are harder.

Hit “enter” and watch the script work:

Immediately, you will see it start trying routes, including calculating the profit or loss from each of the attempted routes. Brilliant!

If successful, you will see a lot of blue text which will tell you how much you rebalanced, and for how much, and through which route.

(By the way, you can also use --fee-factor to adjust the rate up or down. For a full list of commands/parameters/limits, check out the Github page where it’s all explained and pretty easy to understand.

7. Warning:

Don’t use this script to rebalance funds into channels that are dynamically repriced using charge-lnd. As you put more sats there, the price will go lower automatically, and you’ll end up charging less than you paid to rebalance. Carsten suggests disabling automatic fee policies for channels you use this script with, though you could also use “–fee-factor” and lower the fee so that the profit calculation is correct or at least wrong in your favor :wink:

Also, Carsten does not recommend you rebalance OUT of channels, b/c the profit calculation can’t possibly know which channel is going to use that new inbound capacity you’ve created, and that could mean losses.

Finally, as I said before, only use this on channels you know will route at the current price you’ve set. If a channel hasn’t “proven” it will route at that price, the script will calculate profit the wrong way, and you’ll lose money. Alternatively, once again, use --fee-factor of 0.5 or whatever to lower the price artificially so you don’t get screwed, for example if you want to rebalance a new channel that you don’t have any history for.

Other than that, keep it basic. Let the script do the heavy lifting.

7. Now, Automate it (Optional)

Once you’re happy with everything, you can optionally set up a “cron job” to automatically run this command every few hours.

First, structure the rebalance you want, with the right fees, the right “destination, etc… for example:

/mnt/data/upgrades/reblance-lnd/rebalance.py -t [CHANNEL-ID—HERE] -a 100000 --min-remote 2000000

Copy/paste that formatted command.

Then, from the command line, type:

crontab -e

The system will create a new crontab file, if none exists - or open your existing one if you’re created one.

In this file, you can put the following, all on one line:

30 */3 * * * /mnt/data/upgrades/reblance-lnd/rebalance.py -t [CHANNEL-ID—HERE] -a 100000 --min-remote 2000000

This will run your rebalance-lnd script once every 3 hours, at the 30 minute mark (I did this because some of you might be resetting fee policies on the 0 minute, and we want to stagger things). This is the maximum recommended frequency by Carsten. (It might take a long time to find routes, so you probably shouldn’t run it more than every 3 hours or so).

If you wish to run it more/less frequently, or change the timing, or add additional ones to go at different times (stagger them by the minute or by the hour), that’s up to you, and you can find the instructions for changing the 0 * * * * part of the command above here: https://crontab.guru

That’s all!

If you’ve followed the steps above correctly, everything should work just fine, and you should see rebalance transactions come through every 3 hours, assuming they are successful.

This guide was created with the help and support of a LOT of people MUCH smarter than me - and of course, it leverages the awesome (and free) script written by Carsten - so make sure to support their work.

Tip Carsten for building this script: 027ce055380348d7812d2ae7745701c9f93e70c1adeb2657f053f91df4f2843c71@157.90.112.145:9735 (keysend)

Tip The Lorax for writing this tutorial:

  • 032a54b1e9cd2ff8ec5f58915a749cf074e957006e8b4da9c8497fed4b6c6f88dc@r6uunzmfrn3jjcm7nytkenbzlq54fbfhfh27qctlqvkbscr76cdcuhqd.onion:9735 (keysend)
  • The_Lorax_ln@ln.tips

Thanks for reading, I hope you enjoyed this guide.

Happy routing!

5 Likes

Hi,
I’m getting;

mkdir: cannot create directory ‘/mnt/data/upgrades’: Permission denied

I tried;

chown -R $USER:$USER /mnt/data/upgrades

and I get;

chown: cannot access ‘/mnt/data/upgrades’: No such file or directory

I tried;

chown -R $USER:$USER /mnt/data/

and I get;

chown: changing ownership of ‘/mnt/data/’: Operation not permitted

I also tried;

chmod +rwx /mnt/data/

and I get;

chmod: changing permissions of ‘/mnt/data/’: Operation not permitted

finally I tried;

chmod -R a+x /mnt/data/

I still get;

chmod: changing permissions of ‘/mnt/data/’: Operation not permitted

!!!

any help would be appreciated.

Thanks,

add “sudo” before mkdir and you’ll be good.

Thanks for that!

that was a newbie error! lol

now at step 3 I get;

fatal: could not create work tree dir ‘rebalance-lnd’: Permission denied

I added sudo to that one also;
I think it works but I am getting this at step 4;

Could not open requirements file: [Errno 2] No such file or directory: ‘requirements.txt’

If I try and redo step 3 I get;

fatal: destination path ‘rebalance-lnd’ already exists and is not an empty directory.

the time of step 3, the directory structure is as follows.

/mnt/data/upgrades/rebalance-lnd/rebalance-lnd/requirements.txt

If you are following the steps, the following command should work.

pip3 install -r /rebalance-lnd/requirements.txt

no, I get this;

Could not open requirements file: [Errno 2] No such file or directory: ‘/rebalance-lnd/requirements.txt’

Great tutorial Thanks !
Had installed it previously but not in the same directory.

Maybe another guide for lntop ?

Cheers man.

1 Like

I had to run step 3 in 2 parts
a. cd /mnt/data/upgrades/rebalance-lnd
b. git clone https://github.com/C-Otto/rebalance-lnd.git

After that, used ls to see the folders in the directory using ls.
I had another re-balance folder as the only file in the directory
cd re-balance-lnd then ls again to see the files in the directory
I could now see requirements.txt as a file among other files in the folder

At this point you can run step 4 with no problems. Hope that helps

1 Like

Any idea where I’m going wrong, so far?

https://i.imgur.com/QqPc9q0.png

If you follow the guide, your directory structure at the time of step 4 will be as follows.

/mnt/data/upgrades/rebalance-lnd/rebalance-lnd/

“rebalance-lnd” is a duplicate. The guide assumes that there is no duplication, which causes confusion.

cd /mnt/data/upgrades/rebalance-lnd/rebalance-lnd/

The hierarchy needs to be moved or the directory configuration needs to be done correctly.

I got around the problem by not creating the /rebalance-lnd/ folder in step 2 and cloning it in the /mnt/data/upgrades/ folder in step 3.

cd /mnt/data/upgrades && git clone https://github.com/C-Otto/rebalance-lnd.git

If the “/mnt/data/upgrades” folder does not exist, you will need to create a new one.

Hey @The_Lorax , do I need to change the guide about this?
Thanks.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

An alternative is to use --fee-ppm-limit FEE_PPM_LIMIT as an argument to control the cost up to the given number of satoshis per 1M satoshis sent.

That way, no matter if you use proportional fees with a channel, you’re the one fixing the cost ceiling. You may have to tweak the numbers though.

This is a really slick piece of code you have here :star_struck:

I think a neat feature would be after it has run doing a rebalance and failed, it outputs the average/median ppm of all the routes that failed, along with the total amount of sats it was trying to route, so users get an idea of what the current market rate is for that amount of sats to help set their own channel fees within whats currently acceptable.

That’s because rebalance-lnd is installed in/mnt/data/upgrades/rebalance-lnd/rebalance-lnd and not in /mnt/data/upgrades/rebalance-lnd - You have to point pip3 to the correct path.

However, I don’t know if the installation path is intentionally “doubled” to …/rebalance-lnd/rebalance-lnd

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Ok i`m at step 6 now. And apparently the script is not finding any suitable route (see pic), any guess?

The node you are trying to rebalance is not well connected with your other nodes, so there is no route available as of now. It could change in the future depending how routes evolve.

Try to rebalance another node or open more channels.

1 Like

Thank you m8, im still an incredible noob on the subject , so is all good xp. My plan is indeed to add ASAP others 3-4 2Mil channels so i assume the more you have the higher are the chance of the rebalancing to go through. Im also doing triangles at Lightning Network + .