Skip to content

Commit 186ba4f

Browse files
committed
Add GetBalances API impl.
1 parent ccb75f3 commit 186ba4f

4 files changed

Lines changed: 202 additions & 1 deletion

File tree

ldk-server/src/api/get_balances.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::util::proto_adapter::{lightning_balance_to_proto, pending_sweep_balance_to_proto};
2+
use ldk_node::Node;
3+
use ldk_server_protos::api::{GetBalancesRequest, GetBalancesResponse};
4+
use std::sync::Arc;
5+
6+
pub(crate) const GET_BALANCES: &str = "GetBalances";
7+
8+
pub(crate) fn handle_get_balances_request(
9+
node: Arc<Node>, _request: GetBalancesRequest,
10+
) -> Result<GetBalancesResponse, ldk_node::NodeError> {
11+
let balance_details = node.list_balances();
12+
13+
let response = GetBalancesResponse {
14+
total_onchain_balance_sats: balance_details.total_onchain_balance_sats,
15+
spendable_onchain_balance_sats: balance_details.spendable_onchain_balance_sats,
16+
total_anchor_channels_reserve_sats: balance_details.total_anchor_channels_reserve_sats,
17+
total_lightning_balance_sats: balance_details.total_lightning_balance_sats,
18+
lightning_balances: balance_details
19+
.lightning_balances
20+
.into_iter()
21+
.map(lightning_balance_to_proto)
22+
.collect(),
23+
pending_balances_from_channel_closures: balance_details
24+
.pending_balances_from_channel_closures
25+
.into_iter()
26+
.map(pending_sweep_balance_to_proto)
27+
.collect(),
28+
};
29+
Ok(response)
30+
}

ldk-server/src/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) mod bolt12_receive;
44
pub(crate) mod bolt12_send;
55
pub(crate) mod close_channel;
66
pub(crate) mod error;
7+
pub(crate) mod get_balances;
78
pub(crate) mod get_node_info;
89
pub(crate) mod get_payment_details;
910
pub(crate) mod list_channels;

ldk-server/src/service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::api::bolt11_send::{handle_bolt11_send_request, BOLT11_SEND_PATH};
1616
use crate::api::bolt12_receive::{handle_bolt12_receive_request, BOLT12_RECEIVE_PATH};
1717
use crate::api::bolt12_send::{handle_bolt12_send_request, BOLT12_SEND_PATH};
1818
use crate::api::close_channel::{handle_close_channel_request, CLOSE_CHANNEL_PATH};
19+
use crate::api::get_balances::{handle_get_balances_request, GET_BALANCES};
1920
use crate::api::get_node_info::{handle_get_node_info_request, GET_NODE_INFO};
2021
use crate::api::get_payment_details::{
2122
handle_get_payment_details_request, GET_PAYMENT_DETAILS_PATH,
@@ -50,6 +51,7 @@ impl Service<Request<Incoming>> for NodeService {
5051
// Exclude '/' from path pattern matching.
5152
match &req.uri().path()[1..] {
5253
GET_NODE_INFO => Box::pin(handle_request(node, req, handle_get_node_info_request)),
54+
GET_BALANCES => Box::pin(handle_request(node, req, handle_get_balances_request)),
5355
ONCHAIN_RECEIVE_PATH => {
5456
Box::pin(handle_request(node, req, handle_onchain_receive_request))
5557
},

ldk-server/src/util/proto_adapter.rs

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ use bytes::Bytes;
22
use hex::prelude::*;
33
use ldk_node::config::{ChannelConfig, MaxDustHTLCExposure};
44
use ldk_node::payment::{PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus};
5-
use ldk_node::ChannelDetails;
5+
use ldk_node::{ChannelDetails, LightningBalance, PendingSweepBalance};
6+
use ldk_server_protos::types::lightning_balance::BalanceType::{
7+
ClaimableAwaitingConfirmations, ClaimableOnChannelClose, ContentiousClaimable,
8+
CounterpartyRevokedOutputClaimable, MaybePreimageClaimableHtlc, MaybeTimeoutClaimableHtlc,
9+
};
610
use ldk_server_protos::types::payment_kind::Kind::{
711
Bolt11, Bolt11Jit, Bolt12Offer, Bolt12Refund, Onchain, Spontaneous,
812
};
13+
use ldk_server_protos::types::pending_sweep_balance::BalanceType::{
14+
AwaitingThresholdConfirmations, BroadcastAwaitingConfirmation, PendingBroadcast,
15+
};
916
use ldk_server_protos::types::{Channel, LspFeeLimits, OutPoint, Payment};
1017

1118
pub(crate) fn channel_to_proto(channel: ChannelDetails) -> Channel {
@@ -152,3 +159,164 @@ pub(crate) fn payment_kind_to_proto(
152159
},
153160
}
154161
}
162+
163+
pub(crate) fn lightning_balance_to_proto(
164+
lightning_balance: LightningBalance,
165+
) -> ldk_server_protos::types::LightningBalance {
166+
match lightning_balance {
167+
LightningBalance::ClaimableOnChannelClose {
168+
channel_id,
169+
counterparty_node_id,
170+
amount_satoshis,
171+
transaction_fee_satoshis,
172+
outbound_payment_htlc_rounded_msat,
173+
outbound_forwarded_htlc_rounded_msat,
174+
inbound_claiming_htlc_rounded_msat,
175+
inbound_htlc_rounded_msat,
176+
} => ldk_server_protos::types::LightningBalance {
177+
balance_type: Some(ClaimableOnChannelClose(
178+
ldk_server_protos::types::ClaimableOnChannelClose {
179+
channel_id: channel_id.0.to_lower_hex_string(),
180+
counterparty_node_id: counterparty_node_id.to_string(),
181+
amount_satoshis,
182+
transaction_fee_satoshis,
183+
outbound_payment_htlc_rounded_msat,
184+
outbound_forwarded_htlc_rounded_msat,
185+
inbound_claiming_htlc_rounded_msat,
186+
inbound_htlc_rounded_msat,
187+
},
188+
)),
189+
},
190+
LightningBalance::ClaimableAwaitingConfirmations {
191+
channel_id,
192+
counterparty_node_id,
193+
amount_satoshis,
194+
confirmation_height,
195+
..
196+
} => ldk_server_protos::types::LightningBalance {
197+
balance_type: Some(ClaimableAwaitingConfirmations(
198+
ldk_server_protos::types::ClaimableAwaitingConfirmations {
199+
channel_id: channel_id.0.to_lower_hex_string(),
200+
counterparty_node_id: counterparty_node_id.to_string(),
201+
amount_satoshis,
202+
confirmation_height,
203+
},
204+
)),
205+
},
206+
LightningBalance::ContentiousClaimable {
207+
channel_id,
208+
counterparty_node_id,
209+
amount_satoshis,
210+
timeout_height,
211+
payment_hash,
212+
payment_preimage,
213+
} => ldk_server_protos::types::LightningBalance {
214+
balance_type: Some(ContentiousClaimable(
215+
ldk_server_protos::types::ContentiousClaimable {
216+
channel_id: channel_id.0.to_lower_hex_string(),
217+
counterparty_node_id: counterparty_node_id.to_string(),
218+
amount_satoshis,
219+
timeout_height,
220+
payment_hash: payment_hash.to_string(),
221+
payment_preimage: payment_preimage.to_string(),
222+
},
223+
)),
224+
},
225+
LightningBalance::MaybeTimeoutClaimableHTLC {
226+
channel_id,
227+
counterparty_node_id,
228+
amount_satoshis,
229+
claimable_height,
230+
payment_hash,
231+
outbound_payment,
232+
} => ldk_server_protos::types::LightningBalance {
233+
balance_type: Some(MaybeTimeoutClaimableHtlc(
234+
ldk_server_protos::types::MaybeTimeoutClaimableHtlc {
235+
channel_id: channel_id.0.to_lower_hex_string(),
236+
counterparty_node_id: counterparty_node_id.to_string(),
237+
amount_satoshis,
238+
claimable_height,
239+
payment_hash: payment_hash.to_string(),
240+
outbound_payment,
241+
},
242+
)),
243+
},
244+
LightningBalance::MaybePreimageClaimableHTLC {
245+
channel_id,
246+
counterparty_node_id,
247+
amount_satoshis,
248+
expiry_height,
249+
payment_hash,
250+
} => ldk_server_protos::types::LightningBalance {
251+
balance_type: Some(MaybePreimageClaimableHtlc(
252+
ldk_server_protos::types::MaybePreimageClaimableHtlc {
253+
channel_id: channel_id.0.to_lower_hex_string(),
254+
counterparty_node_id: counterparty_node_id.to_string(),
255+
amount_satoshis,
256+
expiry_height,
257+
payment_hash: payment_hash.to_string(),
258+
},
259+
)),
260+
},
261+
LightningBalance::CounterpartyRevokedOutputClaimable {
262+
channel_id,
263+
counterparty_node_id,
264+
amount_satoshis,
265+
} => ldk_server_protos::types::LightningBalance {
266+
balance_type: Some(CounterpartyRevokedOutputClaimable(
267+
ldk_server_protos::types::CounterpartyRevokedOutputClaimable {
268+
channel_id: channel_id.0.to_lower_hex_string(),
269+
counterparty_node_id: counterparty_node_id.to_string(),
270+
amount_satoshis,
271+
},
272+
)),
273+
},
274+
}
275+
}
276+
277+
pub(crate) fn pending_sweep_balance_to_proto(
278+
pending_sweep_balance: PendingSweepBalance,
279+
) -> ldk_server_protos::types::PendingSweepBalance {
280+
match pending_sweep_balance {
281+
PendingSweepBalance::PendingBroadcast { channel_id, amount_satoshis } => {
282+
ldk_server_protos::types::PendingSweepBalance {
283+
balance_type: Some(PendingBroadcast(ldk_server_protos::types::PendingBroadcast {
284+
channel_id: channel_id.map(|c| c.0.to_lower_hex_string()),
285+
amount_satoshis,
286+
})),
287+
}
288+
},
289+
PendingSweepBalance::BroadcastAwaitingConfirmation {
290+
channel_id,
291+
latest_broadcast_height,
292+
latest_spending_txid,
293+
amount_satoshis,
294+
} => ldk_server_protos::types::PendingSweepBalance {
295+
balance_type: Some(BroadcastAwaitingConfirmation(
296+
ldk_server_protos::types::BroadcastAwaitingConfirmation {
297+
channel_id: channel_id.map(|c| c.0.to_lower_hex_string()),
298+
latest_broadcast_height,
299+
latest_spending_txid: latest_spending_txid.to_string(),
300+
amount_satoshis,
301+
},
302+
)),
303+
},
304+
PendingSweepBalance::AwaitingThresholdConfirmations {
305+
channel_id,
306+
latest_spending_txid,
307+
confirmation_hash,
308+
confirmation_height,
309+
amount_satoshis,
310+
} => ldk_server_protos::types::PendingSweepBalance {
311+
balance_type: Some(AwaitingThresholdConfirmations(
312+
ldk_server_protos::types::AwaitingThresholdConfirmations {
313+
channel_id: channel_id.map(|c| c.0.to_lower_hex_string()),
314+
latest_spending_txid: latest_spending_txid.to_string(),
315+
confirmation_hash: confirmation_hash.to_string(),
316+
confirmation_height,
317+
amount_satoshis,
318+
},
319+
)),
320+
},
321+
}
322+
}

0 commit comments

Comments
 (0)