lnv2_module_tests/
common.rs1use devimint::cmd;
2use devimint::federation::Client;
3use fedimint_core::core::OperationId;
4use fedimint_lnv2_client::{FinalReceiveOperationState, FinalSendOperationState};
5use lightning_invoice::Bolt11Invoice;
6use substring::Substring;
7
8pub async fn receive(
9 client: &Client,
10 gateway: &str,
11 amount: u64,
12) -> anyhow::Result<(Bolt11Invoice, OperationId)> {
13 Ok(serde_json::from_value::<(Bolt11Invoice, OperationId)>(
14 cmd!(
15 client,
16 "module",
17 "lnv2",
18 "receive",
19 amount,
20 "--gateway",
21 gateway
22 )
23 .out_json()
24 .await?,
25 )?)
26}
27
28pub async fn send(
29 client: &Client,
30 gateway: &str,
31 invoice: &str,
32) -> anyhow::Result<FinalSendOperationState> {
33 let send_op = serde_json::from_value::<OperationId>(
34 cmd!(
35 client,
36 "module",
37 "lnv2",
38 "send",
39 invoice,
40 "--gateway",
41 gateway
42 )
43 .out_json()
44 .await?,
45 )?;
46
47 await_send(client, send_op).await
48}
49
50pub async fn await_send(
59 client: &Client,
60 send_op: OperationId,
61) -> anyhow::Result<FinalSendOperationState> {
62 let raw = cmd!(
63 client,
64 "module",
65 "lnv2",
66 "await-send",
67 serde_json::to_string(&send_op)?.substring(1, 65)
68 )
69 .out_json()
70 .await?;
71
72 Ok(if raw.as_str() == Some("Success") {
73 FinalSendOperationState::Success([0; 32])
74 } else {
75 serde_json::from_value(raw)?
76 })
77}
78
79pub async fn await_receive_claimed(
80 client: &Client,
81 operation_id: OperationId,
82) -> anyhow::Result<()> {
83 assert_eq!(
84 cmd!(
85 client,
86 "module",
87 "lnv2",
88 "await-receive",
89 serde_json::to_string(&operation_id)?.substring(1, 65)
90 )
91 .out_json()
92 .await?,
93 serde_json::to_value(FinalReceiveOperationState::Claimed)
94 .expect("JSON serialization failed"),
95 );
96
97 Ok(())
98}