gateway_cli/
ecash_commands.rs

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