Perfectly balanced as all things should be

Posted over 1 year ago by Cuaritas

I developed this script for fun to  pefectly balanced your LND node as all things should be.

Note: you do not need to rebalance your channels all time, it can be expensive to rebalance some channels read this:

Inspired by Igniter and Balance Of Satoshi. It is based on rebalance-lnd by Dr. C-Otto and uses it under the hood. It is pretty straightfoward, you can list your channels and rebalance them, some examples:

List all channels within tolerance 0.92:
./ --tolerance=0.92 list
./ -t=0.92 list

Default list within tolerance 0.95:
./ list

Unbalanced channels being rebalanced max fee 10 sats and tolerance 0.97:
./ --max-fee=10 --tolerance=0.97 rebalance
./ -m=10 -t=0.97 rebalance

Default max fee 50 sats and tolerance 0.95:
./ rebalance

Rebalance with max fee 10 sats and tolerance 0.98, ignoring channel id '761128128258703361':
./ --ignore=761128128258703361 -t=0.98 -m=10 rebalance

Feel free to check the code here: or keysend me 1 sat :)

lncli sendpayment --keysend 03e7299ced214b19b87ed87979462d9aee3ec07a42fe6e2211854bfa4cb32b0bb8 1 # sat



LN+ wrote over 1 year ago

That’s cool. I will try it out. Thanks!

Less Relevant

Less Relevant wrote over 1 year ago

Noob question: When/Why should this script be used vs the Loop In/Out feature within Lightning Terminal or the Circular Rebalance feature within Ride The Lightning? My guess is that your rebalance is (near?) perfect while the other tools result in a less balanced node but for the same amount of fees. Just trying to understand the nuances. That is for contributing another tool. 


Cuaritas wrote over 1 year ago

Relatively Irrelevant, Yes, the idea of this script is to perfectly balance your channels 1:1, this is possible thanks to the --reckless mode feature in rebalance-lnd tool, I just automated it by creating this script to iterate all your channels. To minimize the fees you can use --max-fee=1 for example to just spend 1 sat in rebalance or --tolerance=0.90 to only rebalance the channels under 90% side balance, also you can use --ignore to avoid rebalancing some channels :)

Less Relevant

Less Relevant wrote over 1 year ago

@Cuaritas Thank you for taking the time to explain; and for sharing what you've developed.


anon wrote over 1 year ago

Thank you. @Cuaritas how is it different from rebalance-lnd? I know it well, so curious what has changed.


Cuaritas wrote over 1 year ago

Hi anon, well it is not different, just a bash script to automate rebalance-lnd for small channels in reckless mode. It iterates all your channels and rebalance each of them using rebalance-lnd.

BJNode wrote over 1 year ago

I love it Cuaritas. Works really well!  I have a couple of questions. 

Is there a way I can target a channel to rebalance? 
Can I set a max fee rate as well as max fee?

BJNode wrote over 1 year ago

I just ask because in rebalance-lnd you can specify which node you want to use for in and out.   

I use this to completely fill my money making nodes that always drain and empty the nodes that always fill up.

Not all nodes should be 50/50  =) 


Cuaritas wrote over 1 year ago

Hi BJNode, this is just a script to automate rebalance-lnd a little bit. I use it to perfectly rebalance some of my channels, just for fun. I like to see perfect ammount of sats in each side of my node hehehe. 

Answering your questions:
1. Not yet, you can use --ignore to ignore some channels you do not want to include in the rebalance.
2. For now my script can just do max fee, max fee rate is not supported yet.

Like you said to do more things you can use rebalance-lnd itself :)

Btw, yeah I 100% agree not all channels should be 50/50



HODLmeTight wrote over 1 year ago

This is both excellent and a fun exercise for some of the channels, kudos Cuaritas!
I'll log a little suggestion in github, but also sharing here: What I'm missing from Carsten's script and adopting a little from BOS would be an iterative approach of amount to be send.

Steps like:
1) 500,000 sats to balance out > temporary channel failure 1, temporary channel failure 2, .... > Could not find any suitable route
resulting for the same channel into
2) 2 x 250,000 sats to balance out > temporary channel failure 1, temporary channel failure 2, .... > Could not find any suitable route
3) 4 x 125,000 ...

This obviously collides with your max-fee setting and should rather leverage rebalance-lnd --fee-factor, since you'll have many more single transactions. But it would be more economically feasible, since bigger chunks on boths sides in our network are (yet) so rare to see.

Less Relevant

Less Relevant wrote over 1 year ago

How do I use your script on an Umbrel node? 

  • I've installed python3 and bc. 
  • I used wget to download both perfectly-balanced and rebalance-lnd to /mnt/data/upgrades. 
  • I installed rebalance-lnd via "pip install -r requirements.txt"
  • I used chmod to get scripts to run as the umbrel:umbrel user

But I'm stuck at "FileNotFoundError: [Errno 2] No such file or directory: '/home/umbrel/.lnd//tls.cert'. And since LND is running in a Docker container on Umbrel, I'm having a hard time figuring out how to meet his requirement: " Make sure your LND path is located or linked to $HOME/.lnd"

Thanks for anyone's help.


Cuaritas wrote over 1 year ago

Hi Bro, I'm sorry I missed the notification for your comment. 

For the lnd path to work you can link the umbrel path where LND is located, use `docker container ls` and check where is the umbrel path for lnd. In my case I'm using a custom Ubuntu Server with Umbrel inside runing on an old laptop, so I linked my umbrel path  to home `sudo ln -s /umbrel/lnd $HOME/.lnd`

Please login to post comments.

Lightning Network Node
Rank: 5 / Titanium Node
Capacity: 2,720,000 SAT
Channels: 10