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, pegin_from_onchain, receive_ecash, recheck_address, spend_ecash,
9 withdraw, withdraw_to_onchain,
10};
11use fedimint_gateway_common::{
12 BackupPayload, DepositAddressPayload, DepositAddressRecheckPayload, PeginFromOnchainPayload,
13 ReceiveEcashPayload, SpendEcashPayload, WithdrawPayload, WithdrawToOnchainPayload,
14};
15use fedimint_ln_common::client::GatewayApi;
16
17use crate::{CliOutput, CliOutputResult};
18
19#[derive(Subcommand)]
22pub enum EcashCommands {
23 Backup {
25 #[clap(long)]
26 federation_id: FederationId,
27 },
28 Pegin {
31 #[clap(long)]
32 federation_id: FederationId,
33 },
34 PeginRecheck {
36 #[clap(long)]
37 address: bitcoin::Address<NetworkUnchecked>,
38 #[clap(long)]
39 federation_id: FederationId,
40 },
41 PeginFromOnchain {
44 #[clap(long)]
45 federation_id: FederationId,
46 #[clap(long)]
48 amount: BitcoinAmountOrAll,
49 #[clap(long)]
51 fee_rate_sats_per_vbyte: u64,
52 },
53 Pegout {
55 #[clap(long)]
56 federation_id: FederationId,
57 #[clap(long)]
59 amount: BitcoinAmountOrAll,
60 #[clap(long)]
62 address: Address<NetworkUnchecked>,
63 },
64 PegoutToOnchain {
66 #[clap(long)]
67 federation_id: FederationId,
68 #[clap(long)]
70 amount: BitcoinAmountOrAll,
71 },
72 Send {
74 #[clap(long)]
75 federation_id: FederationId,
76 amount: Amount,
77 },
78 Receive {
80 #[clap(long, required_unless_present = "notes_file")]
83 notes: Option<String>,
84 #[clap(long)]
86 notes_file: Option<std::path::PathBuf>,
87 },
88}
89
90impl EcashCommands {
91 pub async fn handle(self, client: &GatewayApi, base_url: &SafeUrl) -> CliOutputResult {
92 match self {
93 Self::Backup { federation_id } => {
94 backup(client, base_url, BackupPayload { federation_id }).await?;
95 Ok(CliOutput::Empty)
96 }
97 Self::Pegin { federation_id } => {
98 let address =
99 get_deposit_address(client, base_url, DepositAddressPayload { federation_id })
100 .await?;
101
102 Ok(CliOutput::DepositAddress { address })
103 }
104 Self::PeginRecheck {
105 address,
106 federation_id,
107 } => {
108 let response = recheck_address(
109 client,
110 base_url,
111 DepositAddressRecheckPayload {
112 address,
113 federation_id,
114 },
115 )
116 .await?;
117 Ok(CliOutput::DepositRecheck(response))
118 }
119 Self::PeginFromOnchain {
120 federation_id,
121 amount,
122 fee_rate_sats_per_vbyte,
123 } => {
124 let txid = pegin_from_onchain(
125 client,
126 base_url,
127 PeginFromOnchainPayload {
128 federation_id,
129 amount,
130 fee_rate_sats_per_vbyte,
131 },
132 )
133 .await?;
134
135 Ok(CliOutput::PeginTxid { txid })
136 }
137 Self::Pegout {
138 federation_id,
139 amount,
140 address,
141 } => {
142 let response = withdraw(
143 client,
144 base_url,
145 WithdrawPayload {
146 federation_id,
147 amount,
148 address,
149 quoted_fees: None,
150 },
151 )
152 .await?;
153
154 Ok(CliOutput::Withdraw(response))
155 }
156 Self::PegoutToOnchain {
157 federation_id,
158 amount,
159 } => {
160 let response = withdraw_to_onchain(
161 client,
162 base_url,
163 WithdrawToOnchainPayload {
164 federation_id,
165 amount,
166 },
167 )
168 .await?;
169
170 Ok(CliOutput::Withdraw(response))
171 }
172 Self::Send {
173 federation_id,
174 amount,
175 } => {
176 let response = spend_ecash(
177 client,
178 base_url,
179 SpendEcashPayload {
180 federation_id,
181 amount,
182 },
183 )
184 .await?;
185
186 Ok(CliOutput::SpendEcash(response))
187 }
188 Self::Receive { notes, .. } => {
189 let notes = notes.expect("required notes source resolved before handling");
190 let response =
191 receive_ecash(client, base_url, ReceiveEcashPayload { notes }).await?;
192 Ok(CliOutput::ReceiveEcash(response))
193 }
194 }
195 }
196}