Skip to main content

fedimint_gateway_client/
lib.rs

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