#lsp #payment #lightning #bitcoin #http-api #blocktank

rust-blocktank-client

A Rust client for the Blocktank LSP HTTP API

2 releases

new 0.0.2 Jan 17, 2025
0.0.1 Jan 14, 2025

#330 in Magic Beans

Download history 177/week @ 2025-01-12

177 downloads per month

MIT license

40KB
857 lines

rust-blocktank-client

A Rust client for the Blocktank LSP HTTP API.

Installation

Add this to your Cargo.toml:

[dependencies]
rust-blocktank-client = "0.0.1"

By default, the client uses rustls as the TLS backend. If you prefer to use the native TLS implementation:

[dependencies]
rust-blocktank-client = { version = "0.1.0", default-features = false, features = ["native-tls"] }

Usage

use rust_blocktank_client::{BlocktankClient, CreateOrderOptions, CreateCjitOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize the client
    let client = BlocktankClient::new("https://api1.blocktank.to/api")?;

    // Get service information
    let info = client.get_info().await?;
    println!("Service version: {}", info.version);
    println!("Network: {:?}", info.onchain.network);

    // Estimate channel order fee
    let fee_estimate = client.estimate_order_fee(
        100_000,     // lsp_balance_sat
        4,           // channel_expiry_weeks
        Some(CreateOrderOptions {
            client_balance_sat: Some(20_000),
            source: Some("example".to_string()),
            ..Default::default()
        }),
    ).await?;
    println!("Estimated fee: {} sats", fee_estimate.fee_sat);

    // Create a channel order
    let order = client.create_order(
        100_000,     // lsp_balance_sat
        4,           // channel_expiry_weeks
        Some(CreateOrderOptions {
            client_balance_sat: Some(20_000),
            turbo_channel: true,  // Will be converted to zeroConf in the API
            source: Some("example".to_string()),
            ..Default::default()
        }),
    ).await?;
    println!("Created order: {}", order.id);

    // Get existing order
    let order = client.get_order(&order.id).await?;
    println!("Order state: {:?}", order.state);

    // Open channel to a node
    let connection_string = "02eadbd9e7557375161df8b646776a547c5cbc2e95b3071ec81553f8ec2cea3b8c@127.0.0.1:9735";
    let updated_order = client.open_channel(&order.id, connection_string).await?;
    println!("Channel opening: {:?}", updated_order.channel);

    // Create a CJIT entry
    let cjit = client.create_cjit_entry(
        200_000,            // channel_size_sat
        50_000,            // invoice_sat
        "Test invoice",     // invoice_description
        "node_pubkey_hex", // node_id
        4,                 // channel_expiry_weeks
        Some(CreateCjitOptions {
            source: Some("example".to_string()),
            ..Default::default()
        }),
    ).await?;
    println!("Created CJIT entry: {}", cjit.id);

    Ok(())
}

Regtest Examples

use blocktank_client::BlocktankClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = BlocktankClient::new("http://localhost:3000/api")?;

    // Mine some blocks
    client.regtest_mine(Some(6)).await?;

    // Deposit to an address
    let tx_id = client.regtest_deposit(
        "bcrt1q94n9ekw8v3g7ksk76kq8k9kn4n7tqx0vfqva0w",
        Some(100_000)
    ).await?;
    println!("Deposit transaction: {}", tx_id);

    // Pay an invoice
    let payment_id = client.regtest_pay(
        "lnbcrt1...",  // Invoice string
        Some(50_000)   // Optional amount for zero-amount invoices
    ).await?;
    println!("Payment ID: {}", payment_id);

    // Close a channel
    let closing_tx = client.regtest_close_channel(
        "txid",     // funding_tx_id
        0,          // vout
        Some(3600), // force close after 1 hour
    ).await?;
    println!("Closing transaction: {}", closing_tx);

    Ok(())
}

Running Tests

To run all tests:

cargo test

To run tests with output (including println! statements):

cargo test -- --nocapture

To run a specific test:

cargo test <test_name>

Dependencies

~8–19MB
~254K SLoC