fedimint_gateway_client/
lib.rs

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