gateway_cli/
ecash_commands.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use bitcoin::address::NetworkUnchecked;
use bitcoin::Address;
use clap::Subcommand;
use fedimint_core::config::FederationId;
use fedimint_core::{Amount, BitcoinAmountOrAll};
use fedimint_mint_client::OOBNotes;
use ln_gateway::rpc::rpc_client::GatewayRpcClient;
use ln_gateway::rpc::{
    BackupPayload, DepositAddressPayload, DepositAddressRecheckPayload, ReceiveEcashPayload,
    SpendEcashPayload, WithdrawPayload,
};

use crate::print_response;

#[derive(Subcommand)]
pub enum EcashCommands {
    /// Make a backup of snapshot of all e-cash.
    Backup {
        #[clap(long)]
        federation_id: FederationId,
    },
    /// Generate a new peg-in address to a federation that the gateway can claim
    /// e-cash for later.
    Pegin {
        #[clap(long)]
        federation_id: FederationId,
    },
    /// Trigger a recheck for deposits on a deposit address
    PeginRecheck {
        #[clap(long)]
        address: bitcoin::Address<NetworkUnchecked>,
        #[clap(long)]
        federation_id: FederationId,
    },
    /// Claim funds from a gateway federation to an on-chain address.
    Pegout {
        #[clap(long)]
        federation_id: FederationId,
        /// The amount to withdraw
        #[clap(long)]
        amount: BitcoinAmountOrAll,
        /// The address to send the funds to
        #[clap(long)]
        address: Address<NetworkUnchecked>,
    },
    /// Send e-cash out of band
    Send {
        #[clap(long)]
        federation_id: FederationId,
        amount: Amount,
        #[clap(long)]
        allow_overpay: bool,
        #[clap(long, default_value_t = 60 * 60 * 24 * 7)]
        timeout: u64,
        #[clap(long)]
        include_invite: bool,
    },
    /// Receive e-cash out of band
    Receive {
        #[clap(long)]
        notes: OOBNotes,
        #[arg(long = "no-wait", action = clap::ArgAction::SetFalse)]
        wait: bool,
    },
}

impl EcashCommands {
    pub async fn handle(
        self,
        create_client: impl Fn() -> GatewayRpcClient + Send + Sync,
    ) -> anyhow::Result<()> {
        match self {
            Self::Backup { federation_id } => {
                create_client()
                    .backup(BackupPayload { federation_id })
                    .await?;
            }
            Self::Pegin { federation_id } => {
                let response = create_client()
                    .get_deposit_address(DepositAddressPayload { federation_id })
                    .await?;

                print_response(response);
            }
            Self::PeginRecheck {
                address,
                federation_id,
            } => {
                let response = create_client()
                    .recheck_address(DepositAddressRecheckPayload {
                        address,
                        federation_id,
                    })
                    .await?;
                print_response(response);
            }
            Self::Pegout {
                federation_id,
                amount,
                address,
            } => {
                let response = create_client()
                    .withdraw(WithdrawPayload {
                        federation_id,
                        amount,
                        address,
                    })
                    .await?;

                print_response(response);
            }
            Self::Send {
                federation_id,
                amount,
                allow_overpay,
                timeout,
                include_invite,
            } => {
                let response = create_client()
                    .spend_ecash(SpendEcashPayload {
                        federation_id,
                        amount,
                        allow_overpay,
                        timeout,
                        include_invite,
                    })
                    .await?;

                print_response(response);
            }
            Self::Receive { notes, wait } => {
                let response = create_client()
                    .receive_ecash(ReceiveEcashPayload { notes, wait })
                    .await?;
                print_response(response);
            }
        }

        Ok(())
    }
}