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