9 releases

0.1.8 Mar 8, 2024
0.1.7 Nov 22, 2023
0.1.6 Sep 26, 2023
0.1.4 Aug 28, 2023
0.1.0 Apr 20, 2022

#109 in Magic Beans

Download history 77/week @ 2023-12-22 109/week @ 2023-12-29 545/week @ 2024-01-05 155/week @ 2024-01-12 205/week @ 2024-01-19 149/week @ 2024-01-26 256/week @ 2024-02-02 342/week @ 2024-02-09 198/week @ 2024-02-16 524/week @ 2024-02-23 801/week @ 2024-03-01 738/week @ 2024-03-08 855/week @ 2024-03-15 429/week @ 2024-03-22 567/week @ 2024-03-29 496/week @ 2024-04-05

2,396 downloads per month
Used in 4 crates

MIT license

255KB
6.5K SLoC

cln-rpc: Talk to Core Lightning


lib.rs:

A Core Lightning RPC-client

Core Lightning exposes a JSON-RPC interface over unix-domain sockets. The unix-domain socket appears like file and located by default in ~/.lightning/<network>/lightning-rpc.

This crate contains an RPC-client called [ClnRpc] and models for most requests and responses.

The example below shows how to initiate the client and celss the getinfo-rpc method.

use std::path::Path;
use tokio_test;
use cln_rpc::{ClnRpc, TypedRequest};
use cln_rpc::model::requests::GetinfoRequest;
use cln_rpc::model::responses::GetinfoResponse;

tokio_test::block_on( async {
    let path = Path::new("path_to_lightning_dir");
    let mut rpc = ClnRpc::new(path).await.unwrap();
    let request = GetinfoRequest {};
    let response : GetinfoResponse = rpc.call_typed(&request).await.unwrap();
});

If the required model is not available you can implement TypedRequest and use ClnRpc::call_typed without a problem.

use std::path::Path;
use tokio_test;
use cln_rpc::{ClnRpc, TypedRequest};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Debug)]
struct CustomMethodRequest {
    param_a : String
};
#[derive(Deserialize, Debug)]
struct CustomMethodResponse {
    field_a : String
};

impl TypedRequest for CustomMethodRequest {
    type Response = CustomMethodResponse;

    fn method(&self) -> &str {
        "custommethod"
    }
}

tokio_test::block_on( async {
    let path = Path::new("path_to_lightning_dir");
    let mut rpc = ClnRpc::new(path).await.unwrap();

    let request = CustomMethodRequest { param_a : String::from("example")};
    let response = rpc.call_typed(&request).await.unwrap();
})

An alternative is to use ClnRpc::call_raw.

use std::path::Path;
use tokio_test;
use cln_rpc::{ClnRpc, TypedRequest};

tokio_test::block_on( async {
    let path = Path::new("path_to_lightning_dir");
    let mut rpc = ClnRpc::new(path).await.unwrap();
    let method = "custommethod";
    let request = serde_json::json!({"param_a" : "example"});
    let response : serde_json::Value = rpc.call_raw(method, &request).await.unwrap();
})

Dependencies

~11–23MB
~250K SLoC