Skip to content

About

Minimal LDK Node client for Megalith's mainnet LSPS2 service: get a just-in-time (JIT) Lightning channel on the first payment.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

Megalith LSPS2 client example

A minimal LDK Node client that gets a just-in-time (JIT) channel from Megalith's mainnet LSPS2 service.

Full guide: LSPS2: Get a just-in-time channel for mobile clients

Get a channel

This is the command to run:

cargo run --bin megalith_lsps2

It prints a JIT invoice for 10,000 sats. Paying that invoice is how you get a channel. When the payment arrives, Megalith's LSP opens a 110,000 sat channel to your node (the invoice amount plus 100,000 sats) and forwards the payment into it, minus its opening fee. The program prints the exact fee; at low on-chain fee rates it is 2,500 sats, so you receive 7,500.

  • Pay it from any Lightning wallet (MPP works).
  • Leave the process running. It waits for the payment and the channel, then keeps going (below).
  • Press Ctrl-C to stop. Press it again to quit without waiting for a clean shutdown.

After the channel opens

The same process keeps going. Don't quit it yet: quitting takes your node offline, and an offline node can't send or receive payments. It prints your spendable balance and asks two questions.

1. Would you like to make a test payment? We recommend it. It shows the channel can send, not only receive. Make an invoice for 1,000 sats in another Lightning wallet, then paste it in. Before it asks for the invoice, the program downloads the public channel graph from a Rapid Gossip Sync server (a few seconds the first time); without the graph your node can't find a route past the LSP. After each payment it shows your new balance and asks for another invoice. Press Enter on an empty line when you're done.

2. Would you like to close this channel and sweep the funds on-chain? If you say yes, it:

  1. Closes the channel cooperatively. If the LSP hasn't agreed within 60s, it offers a force close, which leaves your funds locked by a timelock for up to ~2 weeks.
  2. Waits for the closing transaction to confirm (~10 minutes).
  3. Asks for a Bitcoin address and sends everything there.

You can press Ctrl-C while it waits and finish later with cargo run --bin close_and_sweep. If you say no, the channel stays open and the program exits.

If you run cargo run --bin megalith_lsps2 again while you have a channel, it skips the invoice and goes straight to these questions.

After you have a channel

Command What it does
cargo run --bin node_info Public key, balances, peers, channels, recent payments
cargo run --bin make_invoice Prints a regular 5-sat invoice to receive into
cargo run --bin pay_invoice Shows your spendable balance, then pays invoices you paste in
cargo run --bin sweep_onchain [address] [--no-reserves] Sweeps on-chain funds to an address (asks for one if not given)
cargo run --bin close_and_sweep Closes all channels, waits for the funds to land on-chain, then asks for an address and sweeps everything to it

Node state and logs are stored in tmp/. Run one command at a time.

Invoice amount and fees

The LSP keeps its opening fee out of the payment, so the invoice has to be larger than the fee. Megalith's fee is the larger of 1.4% of the payment and a minimum that depends on on-chain fees and on how many channels your node has had recently:

  • 2,500 sats at 1 sat/vB for your first and second channel (5,000 for the 3rd and 4th, 10,000 after that)
  • plus 507 sats for each sat/vB of on-chain fee above 1

The smallest invoice the LSP accepts is that fee plus 1 sat (2,501 sats today for a new node). 10,000 sats leaves room for on-chain fees up to about 15 sat/vB. If the amount is too small, no invoice is made and nothing is paid; raise JIT_INVOICE_AMOUNT_SATS in .env.

How it works

The Megalith LSP is configured in the committed .env:

Node ID 034066e29e402d9cf55af1ae1026cc5adf92eed1e0e421785442f53717ad1453b0
Address 64.23.159.177:9735
Token any string works

The whole integration is two calls:

builder.set_liquidity_source_lsps2(
    "034066e29e402d9cf55af1ae1026cc5adf92eed1e0e421785442f53717ad1453b0".parse().unwrap(),
    "64.23.159.177:9735".parse().unwrap(),
    Some("use any token here".to_string()),
);

let desc = Bolt11InvoiceDescription::Direct(Description::new("Payment".to_string()).unwrap());
let invoice = node.bolt11_payment().receive_via_jit_channel(10_000_000, &desc, 3_600, None).unwrap();

Configuration

Settings live in the committed .env (nothing in it is secret). Environment variables override it:

Variable Description Value
LSP_PUBKEY LSP node ID 034066e2…1453b0
LSP_ADDRESS LSP host and port 64.23.159.177:9735
LSP_TOKEN LSPS2 token (any string works) lsps2-client-example
JIT_INVOICE_AMOUNT_SATS Amount of the JIT invoice, in sats 10000
NETWORK Must be bitcoin for the mainnet LSP bitcoin
ESPLORA_API_URL Esplora API endpoint https://blockstream.info/api
RGS_SERVER_URL Rapid Gossip Sync server for the channel graph (needed to pay) https://rapidsync.lightningdevkit.org/snapshot
LOG_LEVEL Trace, Debug, Info, Warn, Error Debug

About

Minimal LDK Node client for Megalith's mainnet LSPS2 service: get a just-in-time (JIT) Lightning channel on the first payment.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages