gateway_cli/
ecash_commands.rs

1use bitcoin::Address;
2use bitcoin::address::NetworkUnchecked;
3use clap::Subcommand;
4use fedimint_core::config::FederationId;
5use fedimint_core::util::SafeUrl;
6use fedimint_core::{Amount, BitcoinAmountOrAll};
7use fedimint_gateway_client::{
8    backup, get_deposit_address, receive_ecash, recheck_address, spend_ecash, withdraw,
9};
10use fedimint_gateway_common::{
11    BackupPayload, DepositAddressPayload, DepositAddressRecheckPayload, ReceiveEcashPayload,
12    SpendEcashPayload, WithdrawPayload,
13};
14use fedimint_ln_common::client::GatewayApi;
15use fedimint_mint_client::OOBNotes;
16
17use crate::print_response;
18
19#[derive(Subcommand)]
20pub enum EcashCommands {
21    /// Make a backup of snapshot of all e-cash.
22    Backup {
23        #[clap(long)]
24        federation_id: FederationId,
25    },
26    /// Generate a new peg-in address to a federation that the gateway can claim
27    /// e-cash for later.
28    Pegin {
29        #[clap(long)]
30        federation_id: FederationId,
31    },
32    /// Trigger a recheck for deposits on a deposit address
33    PeginRecheck {
34        #[clap(long)]
35        address: bitcoin::Address<NetworkUnchecked>,
36        #[clap(long)]
37        federation_id: FederationId,
38    },
39    /// Claim funds from a gateway federation to an on-chain address.
40    Pegout {
41        #[clap(long)]
42        federation_id: FederationId,
43        /// The amount to withdraw
44        #[clap(long)]
45        amount: BitcoinAmountOrAll,
46        /// The address to send the funds to
47        #[clap(long)]
48        address: Address<NetworkUnchecked>,
49    },
50    /// Send e-cash out of band
51    Send {
52        #[clap(long)]
53        federation_id: FederationId,
54        amount: Amount,
55        #[clap(long)]
56        allow_overpay: bool,
57        #[clap(long, default_value_t = 60 * 60 * 24 * 7)]
58        timeout: u64,
59        #[clap(long)]
60        include_invite: bool,
61    },
62    /// Receive e-cash out of band
63    Receive {
64        #[clap(long)]
65        notes: OOBNotes,
66        #[arg(long = "no-wait", action = clap::ArgAction::SetFalse)]
67        wait: bool,
68    },
69}
70
71impl EcashCommands {
72    pub async fn handle(self, client: &GatewayApi, base_url: &SafeUrl) -> anyhow::Result<()> {
73        match self {
74            Self::Backup { federation_id } => {
75                backup(client, base_url, BackupPayload { federation_id }).await?;
76            }
77            Self::Pegin { federation_id } => {
78                let response =
79                    get_deposit_address(client, base_url, DepositAddressPayload { federation_id })
80                        .await?;
81
82                print_response(response);
83            }
84            Self::PeginRecheck {
85                address,
86                federation_id,
87            } => {
88                let response = recheck_address(
89                    client,
90                    base_url,
91                    DepositAddressRecheckPayload {
92                        address,
93                        federation_id,
94                    },
95                )
96                .await?;
97                print_response(response);
98            }
99            Self::Pegout {
100                federation_id,
101                amount,
102                address,
103            } => {
104                let response = withdraw(
105                    client,
106                    base_url,
107                    WithdrawPayload {
108                        federation_id,
109                        amount,
110                        address,
111                    },
112                )
113                .await?;
114
115                print_response(response);
116            }
117            Self::Send {
118                federation_id,
119                amount,
120                allow_overpay,
121                timeout,
122                include_invite,
123            } => {
124                let response = spend_ecash(
125                    client,
126                    base_url,
127                    SpendEcashPayload {
128                        federation_id,
129                        amount,
130                        allow_overpay,
131                        timeout,
132                        include_invite,
133                    },
134                )
135                .await?;
136
137                print_response(response);
138            }
139            Self::Receive { notes, wait } => {
140                let response =
141                    receive_ecash(client, base_url, ReceiveEcashPayload { notes, wait }).await?;
142                print_response(response);
143            }
144        }
145
146        Ok(())
147    }
148}