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;
|
2024-02-06 10:52:50 +01:00
|
|
|
use cln_plugin::options::{DefaultIntegerConfigOption, IntegerConfigOption};
|
|
|
|
use cln_plugin::{messages, Builder, Error, Plugin};
|
2022-01-17 12:59:09 +01:00
|
|
|
use tokio;
|
2023-09-15 01:34:13 +02:00
|
|
|
|
|
|
|
const TEST_NOTIF_TAG: &str = "test_custom_notification";
|
|
|
|
|
2024-02-06 10:52:50 +01:00
|
|
|
const TEST_OPTION: DefaultIntegerConfigOption = DefaultIntegerConfigOption::new_i64_with_default(
|
|
|
|
"test-option",
|
|
|
|
42,
|
|
|
|
"a test-option with default 42",
|
|
|
|
);
|
|
|
|
|
|
|
|
const TEST_OPTION_NO_DEFAULT: IntegerConfigOption =
|
|
|
|
IntegerConfigOption::new_i64_no_default("opt-option", "An option without a default");
|
|
|
|
|
2022-01-17 12:59:09 +01:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2022-07-20 16:56:27 +02:00
|
|
|
let state = ();
|
|
|
|
|
|
|
|
if let Some(plugin) = Builder::new(tokio::io::stdin(), tokio::io::stdout())
|
2024-02-06 10:52:50 +01:00
|
|
|
.option(TEST_OPTION)
|
|
|
|
.option(TEST_OPTION_NO_DEFAULT)
|
2022-02-25 13:59:24 +01:00
|
|
|
.rpcmethod("testmethod", "This is a test", testmethod)
|
2022-10-26 13:10:19 +02:00
|
|
|
.rpcmethod(
|
|
|
|
"testoptions",
|
|
|
|
"Retrieve options from this plugin",
|
|
|
|
testoptions,
|
|
|
|
)
|
2023-09-15 01:34:13 +02:00
|
|
|
.rpcmethod(
|
|
|
|
"test-custom-notification",
|
|
|
|
"send a test_custom_notification event",
|
|
|
|
test_send_custom_notification,
|
|
|
|
)
|
2022-02-25 14:48:32 +01:00
|
|
|
.subscribe("connect", connect_handler)
|
2023-09-15 01:34:13 +02:00
|
|
|
.subscribe("test_custom_notification", test_receive_custom_notification)
|
2022-02-25 14:36:39 +01:00
|
|
|
.hook("peer_connected", peer_connected_handler)
|
2023-09-15 01:34:13 +02:00
|
|
|
.notification(messages::NotificationTopic::new(TEST_NOTIF_TAG))
|
2022-07-20 16:56:27 +02:00
|
|
|
.start(state)
|
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-10-26 13:10:19 +02:00
|
|
|
async fn testoptions(p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json::Value, Error> {
|
2024-02-06 10:52:50 +01:00
|
|
|
let test_option = p.option(&TEST_OPTION)?;
|
|
|
|
let test_option_no_default = p
|
|
|
|
.option(&TEST_OPTION_NO_DEFAULT)?;
|
|
|
|
|
2022-10-26 13:10:19 +02:00
|
|
|
Ok(json!({
|
2024-02-06 10:52:50 +01:00
|
|
|
"test-option": test_option,
|
|
|
|
"opt-option" : test_option_no_default
|
2022-10-26 13:10:19 +02: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
|
|
|
|
2023-09-15 01:34:13 +02:00
|
|
|
async fn test_send_custom_notification(
|
|
|
|
p: Plugin<()>,
|
|
|
|
_v: serde_json::Value,
|
|
|
|
) -> Result<serde_json::Value, Error> {
|
|
|
|
let custom_notification = json!({
|
|
|
|
"test": "test",
|
|
|
|
});
|
|
|
|
p.send_custom_notification(TEST_NOTIF_TAG.to_string(), custom_notification)
|
|
|
|
.await?;
|
|
|
|
Ok(json!("Notification sent"))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_receive_custom_notification(
|
|
|
|
_p: Plugin<()>,
|
|
|
|
v: serde_json::Value,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
log::info!("Received a test_custom_notification: {}", v);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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"}))
|
|
|
|
}
|