1use std::collections::BTreeMap;
2
3use bitcoin::address::NetworkUnchecked;
4use bitcoin::{Address, Txid};
5use fedimint_connectors::ServerResult;
6use fedimint_core::config::FederationId;
7use fedimint_core::invite_code::InviteCode;
8use fedimint_core::util::SafeUrl;
9use fedimint_gateway_common::{
10 ADDRESS_ENDPOINT, ADDRESS_RECHECK_ENDPOINT, BACKUP_ENDPOINT, BackupPayload,
11 CLOSE_CHANNELS_WITH_PEER_ENDPOINT, CONFIGURATION_ENDPOINT, CONNECT_FED_ENDPOINT,
12 CREATE_BOLT11_INVOICE_FOR_OPERATOR_ENDPOINT, CREATE_BOLT12_OFFER_FOR_OPERATOR_ENDPOINT,
13 ChannelInfo, CloseChannelsWithPeerRequest, CloseChannelsWithPeerResponse, ConfigPayload,
14 ConnectFedPayload, CreateInvoiceForOperatorPayload, CreateOfferPayload, CreateOfferResponse,
15 DepositAddressPayload, DepositAddressRecheckPayload, FederationInfo, GATEWAY_INFO_ENDPOINT,
16 GET_BALANCES_ENDPOINT, GET_INVOICE_ENDPOINT, GET_LN_ONCHAIN_ADDRESS_ENDPOINT, GatewayBalances,
17 GatewayFedConfig, GatewayInfo, GetInvoiceRequest, GetInvoiceResponse, INVITE_CODES_ENDPOINT,
18 LEAVE_FED_ENDPOINT, LIST_CHANNELS_ENDPOINT, LIST_TRANSACTIONS_ENDPOINT, LeaveFedPayload,
19 ListTransactionsPayload, ListTransactionsResponse, MNEMONIC_ENDPOINT, MnemonicResponse,
20 OPEN_CHANNEL_ENDPOINT, OpenChannelRequest, PAY_INVOICE_FOR_OPERATOR_ENDPOINT,
21 PAY_OFFER_FOR_OPERATOR_ENDPOINT, PAYMENT_LOG_ENDPOINT, PAYMENT_SUMMARY_ENDPOINT,
22 PayInvoiceForOperatorPayload, PayOfferPayload, PayOfferResponse, PaymentLogPayload,
23 PaymentLogResponse, PaymentSummaryPayload, PaymentSummaryResponse, RECEIVE_ECASH_ENDPOINT,
24 ReceiveEcashPayload, ReceiveEcashResponse, SEND_ONCHAIN_ENDPOINT, SET_FEES_ENDPOINT,
25 SPEND_ECASH_ENDPOINT, STOP_ENDPOINT, SendOnchainRequest, SetFeesPayload, SetMnemonicPayload,
26 SpendEcashPayload, SpendEcashResponse, WITHDRAW_ENDPOINT, WithdrawPayload, WithdrawResponse,
27};
28use fedimint_ln_common::Method;
29use fedimint_ln_common::client::GatewayApi;
30use lightning_invoice::Bolt11Invoice;
31
32pub async fn get_info(client: &GatewayApi, base_url: &SafeUrl) -> ServerResult<GatewayInfo> {
33 client
34 .request::<(), GatewayInfo>(base_url, Method::GET, GATEWAY_INFO_ENDPOINT, None)
35 .await
36}
37
38pub async fn get_config(
39 client: &GatewayApi,
40 base_url: &SafeUrl,
41 payload: ConfigPayload,
42) -> ServerResult<GatewayFedConfig> {
43 client
44 .request(
45 base_url,
46 Method::POST,
47 CONFIGURATION_ENDPOINT,
48 Some(payload),
49 )
50 .await
51}
52
53pub async fn get_deposit_address(
54 client: &GatewayApi,
55 base_url: &SafeUrl,
56 payload: DepositAddressPayload,
57) -> ServerResult<Address<NetworkUnchecked>> {
58 client
59 .request(base_url, Method::POST, ADDRESS_ENDPOINT, Some(payload))
60 .await
61}
62
63pub async fn withdraw(
64 client: &GatewayApi,
65 base_url: &SafeUrl,
66 payload: WithdrawPayload,
67) -> ServerResult<WithdrawResponse> {
68 client
69 .request(base_url, Method::POST, WITHDRAW_ENDPOINT, Some(payload))
70 .await
71}
72
73pub async fn connect_federation(
74 client: &GatewayApi,
75 base_url: &SafeUrl,
76 payload: ConnectFedPayload,
77) -> ServerResult<FederationInfo> {
78 client
79 .request(base_url, Method::POST, CONNECT_FED_ENDPOINT, Some(payload))
80 .await
81}
82
83pub async fn leave_federation(
84 client: &GatewayApi,
85 base_url: &SafeUrl,
86 payload: LeaveFedPayload,
87) -> ServerResult<FederationInfo> {
88 client
89 .request(base_url, Method::POST, LEAVE_FED_ENDPOINT, Some(payload))
90 .await
91}
92
93pub async fn backup(
94 client: &GatewayApi,
95 base_url: &SafeUrl,
96 payload: BackupPayload,
97) -> ServerResult<()> {
98 client
99 .request(base_url, Method::POST, BACKUP_ENDPOINT, Some(payload))
100 .await
101}
102
103pub async fn set_fees(
104 client: &GatewayApi,
105 base_url: &SafeUrl,
106 payload: SetFeesPayload,
107) -> ServerResult<()> {
108 client
109 .request(base_url, Method::POST, SET_FEES_ENDPOINT, Some(payload))
110 .await
111}
112
113pub async fn create_invoice_for_self(
114 client: &GatewayApi,
115 base_url: &SafeUrl,
116 payload: CreateInvoiceForOperatorPayload,
117) -> ServerResult<Bolt11Invoice> {
118 client
119 .request(
120 base_url,
121 Method::POST,
122 CREATE_BOLT11_INVOICE_FOR_OPERATOR_ENDPOINT,
123 Some(payload),
124 )
125 .await
126}
127
128pub async fn pay_invoice(
129 client: &GatewayApi,
130 base_url: &SafeUrl,
131 payload: PayInvoiceForOperatorPayload,
132) -> ServerResult<String> {
133 client
134 .request(
135 base_url,
136 Method::POST,
137 PAY_INVOICE_FOR_OPERATOR_ENDPOINT,
138 Some(payload),
139 )
140 .await
141}
142
143pub async fn get_ln_onchain_address(
144 client: &GatewayApi,
145 base_url: &SafeUrl,
146) -> ServerResult<Address<NetworkUnchecked>> {
147 client
148 .request::<(), Address<NetworkUnchecked>>(
149 base_url,
150 Method::GET,
151 GET_LN_ONCHAIN_ADDRESS_ENDPOINT,
152 None,
153 )
154 .await
155}
156
157pub async fn open_channel(
158 client: &GatewayApi,
159 base_url: &SafeUrl,
160 payload: OpenChannelRequest,
161) -> ServerResult<Txid> {
162 client
163 .request(base_url, Method::POST, OPEN_CHANNEL_ENDPOINT, Some(payload))
164 .await
165}
166
167pub async fn close_channels_with_peer(
168 client: &GatewayApi,
169 base_url: &SafeUrl,
170 payload: CloseChannelsWithPeerRequest,
171) -> ServerResult<CloseChannelsWithPeerResponse> {
172 client
173 .request(
174 base_url,
175 Method::POST,
176 CLOSE_CHANNELS_WITH_PEER_ENDPOINT,
177 Some(payload),
178 )
179 .await
180}
181
182pub async fn list_channels(
183 client: &GatewayApi,
184 base_url: &SafeUrl,
185) -> ServerResult<Vec<ChannelInfo>> {
186 client
187 .request::<(), Vec<ChannelInfo>>(base_url, Method::GET, LIST_CHANNELS_ENDPOINT, None)
188 .await
189}
190
191pub async fn send_onchain(
192 client: &GatewayApi,
193 base_url: &SafeUrl,
194 payload: SendOnchainRequest,
195) -> ServerResult<Txid> {
196 client
197 .request(base_url, Method::POST, SEND_ONCHAIN_ENDPOINT, Some(payload))
198 .await
199}
200
201pub async fn recheck_address(
202 client: &GatewayApi,
203 base_url: &SafeUrl,
204 payload: DepositAddressRecheckPayload,
205) -> ServerResult<serde_json::Value> {
206 client
207 .request(
208 base_url,
209 Method::POST,
210 ADDRESS_RECHECK_ENDPOINT,
211 Some(payload),
212 )
213 .await
214}
215
216pub async fn spend_ecash(
217 client: &GatewayApi,
218 base_url: &SafeUrl,
219 payload: SpendEcashPayload,
220) -> ServerResult<SpendEcashResponse> {
221 client
222 .request(base_url, Method::POST, SPEND_ECASH_ENDPOINT, Some(payload))
223 .await
224}
225
226pub async fn receive_ecash(
227 client: &GatewayApi,
228 base_url: &SafeUrl,
229 payload: ReceiveEcashPayload,
230) -> ServerResult<ReceiveEcashResponse> {
231 client
232 .request(
233 base_url,
234 Method::POST,
235 RECEIVE_ECASH_ENDPOINT,
236 Some(payload),
237 )
238 .await
239}
240
241pub async fn get_balances(
242 client: &GatewayApi,
243 base_url: &SafeUrl,
244) -> ServerResult<GatewayBalances> {
245 client
246 .request::<(), GatewayBalances>(base_url, Method::GET, GET_BALANCES_ENDPOINT, None)
247 .await
248}
249
250pub async fn get_mnemonic(
251 client: &GatewayApi,
252 base_url: &SafeUrl,
253) -> ServerResult<MnemonicResponse> {
254 client
255 .request::<(), MnemonicResponse>(base_url, Method::GET, MNEMONIC_ENDPOINT, None)
256 .await
257}
258
259pub async fn stop(client: &GatewayApi, base_url: &SafeUrl) -> ServerResult<()> {
260 client
261 .request::<(), ()>(base_url, Method::GET, STOP_ENDPOINT, None)
262 .await
263}
264
265pub async fn payment_log(
266 client: &GatewayApi,
267 base_url: &SafeUrl,
268 payload: PaymentLogPayload,
269) -> ServerResult<PaymentLogResponse> {
270 client
271 .request(base_url, Method::POST, PAYMENT_LOG_ENDPOINT, Some(payload))
272 .await
273}
274
275pub async fn payment_summary(
276 client: &GatewayApi,
277 base_url: &SafeUrl,
278 payload: PaymentSummaryPayload,
279) -> ServerResult<PaymentSummaryResponse> {
280 client
281 .request(
282 base_url,
283 Method::POST,
284 PAYMENT_SUMMARY_ENDPOINT,
285 Some(payload),
286 )
287 .await
288}
289
290pub async fn get_invoice(
291 client: &GatewayApi,
292 base_url: &SafeUrl,
293 payload: GetInvoiceRequest,
294) -> ServerResult<Option<GetInvoiceResponse>> {
295 client
296 .request(base_url, Method::POST, GET_INVOICE_ENDPOINT, Some(payload))
297 .await
298}
299
300pub async fn list_transactions(
301 client: &GatewayApi,
302 base_url: &SafeUrl,
303 payload: ListTransactionsPayload,
304) -> ServerResult<ListTransactionsResponse> {
305 client
306 .request(
307 base_url,
308 Method::POST,
309 LIST_TRANSACTIONS_ENDPOINT,
310 Some(payload),
311 )
312 .await
313}
314
315pub async fn create_offer(
316 client: &GatewayApi,
317 base_url: &SafeUrl,
318 payload: CreateOfferPayload,
319) -> ServerResult<CreateOfferResponse> {
320 client
321 .request(
322 base_url,
323 Method::POST,
324 CREATE_BOLT12_OFFER_FOR_OPERATOR_ENDPOINT,
325 Some(payload),
326 )
327 .await
328}
329
330pub async fn pay_offer(
331 client: &GatewayApi,
332 base_url: &SafeUrl,
333 payload: PayOfferPayload,
334) -> ServerResult<PayOfferResponse> {
335 client
336 .request(
337 base_url,
338 Method::POST,
339 PAY_OFFER_FOR_OPERATOR_ENDPOINT,
340 Some(payload),
341 )
342 .await
343}
344
345pub async fn set_mnemonic(
346 client: &GatewayApi,
347 base_url: &SafeUrl,
348 payload: SetMnemonicPayload,
349) -> ServerResult<()> {
350 client
351 .request(base_url, Method::POST, MNEMONIC_ENDPOINT, Some(payload))
352 .await
353}
354
355pub async fn get_invite_codes(
356 client: &GatewayApi,
357 base_url: &SafeUrl,
358) -> ServerResult<BTreeMap<FederationId, Vec<InviteCode>>> {
359 client
360 .request::<(), BTreeMap<FederationId, Vec<InviteCode>>>(
361 base_url,
362 Method::GET,
363 INVITE_CODES_ENDPOINT,
364 None,
365 )
366 .await
367}