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 = start_send(client, gateway, invoice).await?;
34
35    await_send(client, send_op).await
36}
37
38/// Funds a send through `gateway` without waiting for its outcome.
39pub async fn start_send(
40    client: &Client,
41    gateway: &str,
42    invoice: &str,
43) -> anyhow::Result<OperationId> {
44    Ok(serde_json::from_value::<OperationId>(
45        cmd!(
46            client,
47            "module",
48            "lnv2",
49            "send",
50            invoice,
51            "--gateway",
52            gateway
53        )
54        .out_json()
55        .await?,
56    )?)
57}
58
59/// Run `await-send` and parse the JSON, tolerating the pre-0.12 CLI output
60/// shape so this works across the backwards-compatibility test matrix.
61///
62/// Pre-0.12 binaries serialize `FinalSendOperationState::Success` as the bare
63/// string `"Success"` (unit variant); 0.12+ serializes it as `{"Success":
64/// "<hex preimage>"}` (tuple variant carrying the preimage). Coerce the old
65/// shape to a synthetic `Success(zero-preimage)` so callers can match
66/// uniformly regardless of which CLI produced the output.
67pub async fn await_send(
68    client: &Client,
69    send_op: OperationId,
70) -> anyhow::Result<FinalSendOperationState> {
71    let raw = cmd!(
72        client,
73        "module",
74        "lnv2",
75        "await-send",
76        serde_json::to_string(&send_op)?.substring(1, 65)
77    )
78    .out_json()
79    .await?;
80
81    Ok(if raw.as_str() == Some("Success") {
82        FinalSendOperationState::Success([0; 32])
83    } else {
84        serde_json::from_value(raw)?
85    })
86}
87
88pub async fn await_receive_claimed(
89    client: &Client,
90    operation_id: OperationId,
91) -> anyhow::Result<()> {
92    assert_eq!(
93        cmd!(
94            client,
95            "module",
96            "lnv2",
97            "await-receive",
98            serde_json::to_string(&operation_id)?.substring(1, 65)
99        )
100        .out_json()
101        .await?,
102        serde_json::to_value(FinalReceiveOperationState::Claimed)
103            .expect("JSON serialization failed"),
104    );
105
106    Ok(())
107}