2022-01-17 12:59:09 +01:00
|
|
|
//! This is a test plugin used to verify that we can compile and run
|
2022-04-06 07:09:48 +02:00
|
|
|
//! plugins using the Rust API against Core Lightning.
|
2022-02-21 18:31:29 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
|
|
|
use cln_plugin::{options, Builder, Error, Plugin};
|
2022-01-17 12:59:09 +01:00
|
|
|
use tokio;
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2022-04-10 16:56:13 +02:00
|
|
|
if let Some(plugin) = Builder::new((), tokio::io::stdin(), tokio::io::stdout())
|
2022-02-07 11:00:08 +01:00
|
|
|
.option(options::ConfigOption::new(
|
|
|
|
"test-option",
|
|
|
|
options::Value::Integer(42),
|
|
|
|
"a test-option with default 42",
|
|
|
|
))
|
2022-02-25 13:59:24 +01:00
|
|
|
.rpcmethod("testmethod", "This is a test", testmethod)
|
2022-02-25 14:48:32 +01:00
|
|
|
.subscribe("connect", connect_handler)
|
2022-02-25 14:36:39 +01:00
|
|
|
.hook("peer_connected", peer_connected_handler)
|
2022-02-15 16:24:15 +01:00
|
|
|
.start()
|
2022-04-10 16:56:13 +02:00
|
|
|
.await?
|
|
|
|
{
|
|
|
|
plugin.join().await
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-17 12:59:09 +01:00
|
|
|
}
|
2022-02-21 18:31:29 +01:00
|
|
|
|
2022-02-25 13:59:24 +01:00
|
|
|
async fn testmethod(_p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json::Value, Error> {
|
2022-02-21 18:31:29 +01:00
|
|
|
Ok(json!("Hello"))
|
|
|
|
}
|
2022-02-23 19:00:25 +01:00
|
|
|
|
2022-02-25 14:48:32 +01:00
|
|
|
async fn connect_handler(_p: Plugin<()>, v: serde_json::Value) -> Result<(), Error> {
|
2022-02-23 19:00:25 +01:00
|
|
|
log::info!("Got a connect notification: {}", v);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:56:13 +02:00
|
|
|
async fn peer_connected_handler(
|
|
|
|
_p: Plugin<()>,
|
|
|
|
v: serde_json::Value,
|
|
|
|
) -> Result<serde_json::Value, Error> {
|
2022-02-23 19:00:25 +01:00
|
|
|
log::info!("Got a connect hook call: {}", v);
|
|
|
|
Ok(json!({"result": "continue"}))
|
|
|
|
}
|