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