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