Skip to main content

lnv2_module_tests/
common.rs

1use 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
50/// Run `await-send` and parse the JSON, tolerating the pre-0.12 CLI output
51/// shape so this works across the backwards-compatibility test matrix.
52///
53/// Pre-0.12 binaries serialize `FinalSendOperationState::Success` as the bare
54/// string `"Success"` (unit variant); 0.12+ serializes it as `{"Success":
55/// "<hex preimage>"}` (tuple variant carrying the preimage). Coerce the old
56/// shape to a synthetic `Success(zero-preimage)` so callers can match
57/// uniformly regardless of which CLI produced the output.
58pub 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}