Skip to content

Commit 4fc6eb1

Browse files
authored
Merge pull request lightningdevkit#26 from G8XSU/node-balances
Add implementation for GetBalances Api.
2 parents 29aa455 + 85ec0e6 commit 4fc6eb1

File tree

10 files changed

+797
-4
lines changed

10 files changed

+797
-4
lines changed

ldk-server/ldk-server-protos/src/api.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,50 @@ pub struct ListPaymentsResponse {
311311
#[prost(message, repeated, tag = "1")]
312312
pub payments: ::prost::alloc::vec::Vec<super::types::Payment>,
313313
}
314+
/// Retrieves an overview of all known balances.
315+
/// See more: <https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.list_balances>
316+
#[allow(clippy::derive_partial_eq_without_eq)]
317+
#[derive(Clone, PartialEq, ::prost::Message)]
318+
pub struct GetBalancesRequest {}
319+
#[allow(clippy::derive_partial_eq_without_eq)]
320+
#[derive(Clone, PartialEq, ::prost::Message)]
321+
pub struct GetBalancesResponse {
322+
/// The total balance of our on-chain wallet.
323+
#[prost(uint64, tag = "1")]
324+
pub total_onchain_balance_sats: u64,
325+
/// The currently spendable balance of our on-chain wallet.
326+
///
327+
/// This includes any sufficiently confirmed funds, minus `total_anchor_channels_reserve_sats`.
328+
#[prost(uint64, tag = "2")]
329+
pub spendable_onchain_balance_sats: u64,
330+
/// The share of our total balance that we retain as an emergency reserve to (hopefully) be
331+
/// able to spend the Anchor outputs when one of our channels is closed.
332+
#[prost(uint64, tag = "3")]
333+
pub total_anchor_channels_reserve_sats: u64,
334+
/// The total balance that we would be able to claim across all our Lightning channels.
335+
///
336+
/// Note this excludes balances that we are unsure if we are able to claim (e.g., as we are
337+
/// waiting for a preimage or for a timeout to expire). These balances will however be included
338+
/// as `MaybePreimageClaimableHTLC` and `MaybeTimeoutClaimableHTLC` in `lightning_balances`.
339+
#[prost(uint64, tag = "4")]
340+
pub total_lightning_balance_sats: u64,
341+
/// A detailed list of all known Lightning balances that would be claimable on channel closure.
342+
///
343+
/// Note that less than the listed amounts are spendable over lightning as further reserve
344+
/// restrictions apply. Please refer to `Channel::outbound_capacity_msat` and
345+
/// Channel::next_outbound_htlc_limit_msat as returned by `ListChannels`
346+
/// for a better approximation of the spendable amounts.
347+
#[prost(message, repeated, tag = "5")]
348+
pub lightning_balances: ::prost::alloc::vec::Vec<super::types::LightningBalance>,
349+
/// A detailed list of balances currently being swept from the Lightning to the on-chain
350+
/// wallet.
351+
///
352+
/// These are balances resulting from channel closures that may have been encumbered by a
353+
/// delay, but are now being claimed and useable once sufficiently confirmed on-chain.
354+
///
355+
/// Note that, depending on the sync status of the wallets, swept balances listed here might or
356+
/// might not already be accounted for in `total_onchain_balance_sats`.
357+
#[prost(message, repeated, tag = "6")]
358+
pub pending_balances_from_channel_closures:
359+
::prost::alloc::vec::Vec<super::types::PendingSweepBalance>,
360+
}

ldk-server/ldk-server-protos/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct ErrorResponse {
2020
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
2121
#[repr(i32)]
2222
pub enum ErrorCode {
23-
/// Will neve be used as `error_code` by ldk-server.
23+
/// Will never be used as `error_code` by server.
2424
///
2525
/// **Caution**: If a new type of `error_code` is introduced in the `ErrorCode` enum, `error_code` field will be set to
2626
/// `UnknownError`.
@@ -35,7 +35,7 @@ pub enum ErrorCode {
3535
AuthError = 2,
3636
/// Used to represent an error while doing a Lightning operation.
3737
LightningError = 3,
38-
/// Used when an internal ldk-server error occurred. The ldk-ldk-server-client is probably at no fault.
38+
/// Used when an internal server error occurred. The client is probably at no fault.
3939
InternalServerError = 4,
4040
}
4141
impl ErrorCode {

ldk-server/ldk-server-protos/src/proto/api.proto

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,46 @@ message ListPaymentsResponse {
293293
// List of payments.
294294
repeated types.Payment payments = 1;
295295
}
296+
297+
// Retrieves an overview of all known balances.
298+
// See more: https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.list_balances
299+
message GetBalancesRequest {}
300+
301+
message GetBalancesResponse {
302+
// The total balance of our on-chain wallet.
303+
uint64 total_onchain_balance_sats = 1;
304+
305+
// The currently spendable balance of our on-chain wallet.
306+
//
307+
// This includes any sufficiently confirmed funds, minus `total_anchor_channels_reserve_sats`.
308+
uint64 spendable_onchain_balance_sats = 2;
309+
310+
// The share of our total balance that we retain as an emergency reserve to (hopefully) be
311+
// able to spend the Anchor outputs when one of our channels is closed.
312+
uint64 total_anchor_channels_reserve_sats = 3;
313+
314+
// The total balance that we would be able to claim across all our Lightning channels.
315+
//
316+
// Note this excludes balances that we are unsure if we are able to claim (e.g., as we are
317+
// waiting for a preimage or for a timeout to expire). These balances will however be included
318+
// as `MaybePreimageClaimableHTLC` and `MaybeTimeoutClaimableHTLC` in `lightning_balances`.
319+
uint64 total_lightning_balance_sats = 4;
320+
321+
// A detailed list of all known Lightning balances that would be claimable on channel closure.
322+
//
323+
// Note that less than the listed amounts are spendable over lightning as further reserve
324+
// restrictions apply. Please refer to `Channel::outbound_capacity_msat` and
325+
// Channel::next_outbound_htlc_limit_msat as returned by `ListChannels`
326+
// for a better approximation of the spendable amounts.
327+
repeated types.LightningBalance lightning_balances = 5;
328+
329+
// A detailed list of balances currently being swept from the Lightning to the on-chain
330+
// wallet.
331+
//
332+
// These are balances resulting from channel closures that may have been encumbered by a
333+
// delay, but are now being claimed and useable once sufficiently confirmed on-chain.
334+
//
335+
// Note that, depending on the sync status of the wallets, swept balances listed here might or
336+
// might not already be accounted for in `total_onchain_balance_sats`.
337+
repeated types.PendingSweepBalance pending_balances_from_channel_closures = 6;
338+
}

ldk-server/ldk-server-protos/src/proto/error.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ message ErrorResponse {
2121

2222
enum ErrorCode {
2323

24-
// Will neve be used as `error_code` by server.
24+
// Will never be used as `error_code` by server.
2525
//
2626
// **Caution**: If a new type of `error_code` is introduced in the `ErrorCode` enum, `error_code` field will be set to
2727
// `UnknownError`.

ldk-server/ldk-server-protos/src/proto/types.proto

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,232 @@ message BestBlock {
350350
// The height at which the block was confirmed.
351351
uint32 height = 2;
352352
}
353+
354+
// Details about the status of a known Lightning balance.
355+
message LightningBalance {
356+
oneof balance_type {
357+
ClaimableOnChannelClose claimable_on_channel_close = 1;
358+
ClaimableAwaitingConfirmations claimable_awaiting_confirmations = 2;
359+
ContentiousClaimable contentious_claimable = 3;
360+
MaybeTimeoutClaimableHTLC maybe_timeout_claimable_htlc = 4;
361+
MaybePreimageClaimableHTLC maybe_preimage_claimable_htlc = 5;
362+
CounterpartyRevokedOutputClaimable counterparty_revoked_output_claimable = 6;
363+
}
364+
}
365+
366+
// The channel is not yet closed (or the commitment or closing transaction has not yet appeared in a block).
367+
// The given balance is claimable (less on-chain fees) if the channel is force-closed now.
368+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.LightningBalance.html#variant.ClaimableOnChannelClose
369+
message ClaimableOnChannelClose {
370+
// The identifier of the channel this balance belongs to.
371+
string channel_id = 1;
372+
373+
// The identifier of our channel counterparty.
374+
string counterparty_node_id = 2;
375+
376+
// The amount available to claim, in satoshis, excluding the on-chain fees which will be required to do so.
377+
uint64 amount_satoshis = 3;
378+
379+
// The transaction fee we pay for the closing commitment transaction.
380+
// This amount is not included in the `amount_satoshis` value.
381+
//
382+
// Note that if this channel is inbound (and thus our counterparty pays the commitment transaction fee) this value
383+
// will be zero.
384+
uint64 transaction_fee_satoshis = 4;
385+
386+
// The amount of millisatoshis which has been burned to fees from HTLCs which are outbound from us and are related to
387+
// a payment which was sent by us. This is the sum of the millisatoshis part of all HTLCs which are otherwise
388+
// represented by `LightningBalance::MaybeTimeoutClaimableHTLC` with their
389+
// `LightningBalance::MaybeTimeoutClaimableHTLC::outbound_payment` flag set, as well as any dust HTLCs which would
390+
// otherwise be represented the same.
391+
//
392+
// This amount (rounded up to a whole satoshi value) will not be included in `amount_satoshis`.
393+
uint64 outbound_payment_htlc_rounded_msat = 5;
394+
395+
// The amount of millisatoshis which has been burned to fees from HTLCs which are outbound from us and are related to
396+
// a forwarded HTLC. This is the sum of the millisatoshis part of all HTLCs which are otherwise represented by
397+
// `LightningBalance::MaybeTimeoutClaimableHTLC` with their `LightningBalance::MaybeTimeoutClaimableHTLC::outbound_payment`
398+
// flag not set, as well as any dust HTLCs which would otherwise be represented the same.
399+
//
400+
// This amount (rounded up to a whole satoshi value) will not be included in `amount_satoshis`.
401+
uint64 outbound_forwarded_htlc_rounded_msat = 6;
402+
403+
// The amount of millisatoshis which has been burned to fees from HTLCs which are inbound to us and for which we know
404+
// the preimage. This is the sum of the millisatoshis part of all HTLCs which would be represented by
405+
// `LightningBalance::ContentiousClaimable` on channel close, but whose current value is included in `amount_satoshis`,
406+
// as well as any dust HTLCs which would otherwise be represented the same.
407+
//
408+
// This amount (rounded up to a whole satoshi value) will not be included in `amount_satoshis`.
409+
uint64 inbound_claiming_htlc_rounded_msat = 7;
410+
411+
// The amount of millisatoshis which has been burned to fees from HTLCs which are inbound to us and for which we do
412+
// not know the preimage. This is the sum of the millisatoshis part of all HTLCs which would be represented by
413+
// `LightningBalance::MaybePreimageClaimableHTLC` on channel close, as well as any dust HTLCs which would otherwise be
414+
// represented the same.
415+
//
416+
// This amount (rounded up to a whole satoshi value) will not be included in the counterparty’s `amount_satoshis`.
417+
uint64 inbound_htlc_rounded_msat = 8;
418+
}
419+
420+
// The channel has been closed, and the given balance is ours but awaiting confirmations until we consider it spendable.
421+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.LightningBalance.html#variant.ClaimableAwaitingConfirmations
422+
message ClaimableAwaitingConfirmations {
423+
// The identifier of the channel this balance belongs to.
424+
string channel_id = 1;
425+
426+
// The identifier of our channel counterparty.
427+
string counterparty_node_id = 2;
428+
429+
// The amount available to claim, in satoshis, possibly excluding the on-chain fees which were spent in broadcasting
430+
// the transaction.
431+
uint64 amount_satoshis = 3;
432+
433+
// The height at which we start tracking it as `SpendableOutput`.
434+
uint32 confirmation_height = 4;
435+
}
436+
437+
// The channel has been closed, and the given balance should be ours but awaiting spending transaction confirmation.
438+
// If the spending transaction does not confirm in time, it is possible our counterparty can take the funds by
439+
// broadcasting an HTLC timeout on-chain.
440+
//
441+
// Once the spending transaction confirms, before it has reached enough confirmations to be considered safe from chain
442+
// reorganizations, the balance will instead be provided via `LightningBalance::ClaimableAwaitingConfirmations`.
443+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.LightningBalance.html#variant.ContentiousClaimable
444+
message ContentiousClaimable {
445+
// The identifier of the channel this balance belongs to.
446+
string channel_id = 1;
447+
448+
// The identifier of our channel counterparty.
449+
string counterparty_node_id = 2;
450+
451+
// The amount available to claim, in satoshis, excluding the on-chain fees which were spent in broadcasting
452+
// the transaction.
453+
uint64 amount_satoshis = 3;
454+
455+
// The height at which the counterparty may be able to claim the balance if we have not done so.
456+
uint32 timeout_height = 4;
457+
458+
// The payment hash that locks this HTLC.
459+
string payment_hash = 5;
460+
461+
// The preimage that can be used to claim this HTLC.
462+
string payment_preimage = 6;
463+
}
464+
465+
// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain fees) if the counterparty
466+
// does not know the preimage for the HTLCs. These are somewhat likely to be claimed by our counterparty before we do.
467+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.LightningBalance.html#variant.MaybeTimeoutClaimableHTLC
468+
message MaybeTimeoutClaimableHTLC {
469+
// The identifier of the channel this balance belongs to.
470+
string channel_id = 1;
471+
472+
// The identifier of our channel counterparty.
473+
string counterparty_node_id = 2;
474+
475+
// The amount available to claim, in satoshis, excluding the on-chain fees which were spent in broadcasting
476+
// the transaction.
477+
uint64 amount_satoshis = 3;
478+
479+
// The height at which we will be able to claim the balance if our counterparty has not done so.
480+
uint32 claimable_height = 4;
481+
482+
// The payment hash whose preimage our counterparty needs to claim this HTLC.
483+
string payment_hash = 5;
484+
485+
// Indicates whether this HTLC represents a payment which was sent outbound from us.
486+
bool outbound_payment = 6;
487+
}
488+
489+
// HTLCs which we received from our counterparty which are claimable with a preimage which we do not currently have.
490+
// This will only be claimable if we receive the preimage from the node to which we forwarded this HTLC before the
491+
// timeout.
492+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.LightningBalance.html#variant.MaybePreimageClaimableHTLC
493+
message MaybePreimageClaimableHTLC {
494+
// The identifier of the channel this balance belongs to.
495+
string channel_id = 1;
496+
497+
// The identifier of our channel counterparty.
498+
string counterparty_node_id = 2;
499+
500+
// The amount available to claim, in satoshis, excluding the on-chain fees which were spent in broadcasting
501+
// the transaction.
502+
uint64 amount_satoshis = 3;
503+
504+
// The height at which our counterparty will be able to claim the balance if we have not yet received the preimage and
505+
// claimed it ourselves.
506+
uint32 expiry_height = 4;
507+
508+
// The payment hash whose preimage we need to claim this HTLC.
509+
string payment_hash = 5;
510+
}
511+
// The channel has been closed, and our counterparty broadcasted a revoked commitment transaction.
512+
//
513+
// Thus, we’re able to claim all outputs in the commitment transaction, one of which has the following amount.
514+
//
515+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.LightningBalance.html#variant.CounterpartyRevokedOutputClaimable
516+
message CounterpartyRevokedOutputClaimable {
517+
// The identifier of the channel this balance belongs to.
518+
string channel_id = 1;
519+
520+
// The identifier of our channel counterparty.
521+
string counterparty_node_id = 2;
522+
523+
// The amount, in satoshis, of the output which we can claim.
524+
uint64 amount_satoshis = 3;
525+
}
526+
527+
// Details about the status of a known balance currently being swept to our on-chain wallet.
528+
message PendingSweepBalance {
529+
oneof balance_type {
530+
PendingBroadcast pending_broadcast = 1;
531+
BroadcastAwaitingConfirmation broadcast_awaiting_confirmation = 2;
532+
AwaitingThresholdConfirmations awaiting_threshold_confirmations = 3;
533+
}
534+
}
535+
536+
// The spendable output is about to be swept, but a spending transaction has yet to be generated and broadcast.
537+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.PendingSweepBalance.html#variant.PendingBroadcast
538+
message PendingBroadcast {
539+
// The identifier of the channel this balance belongs to.
540+
optional string channel_id = 1;
541+
542+
// The amount, in satoshis, of the output being swept.
543+
uint64 amount_satoshis = 2;
544+
}
545+
546+
// A spending transaction has been generated and broadcast and is awaiting confirmation on-chain.
547+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.PendingSweepBalance.html#variant.BroadcastAwaitingConfirmation
548+
message BroadcastAwaitingConfirmation {
549+
// The identifier of the channel this balance belongs to.
550+
optional string channel_id = 1;
551+
552+
// The best height when we last broadcast a transaction spending the output being swept.
553+
uint32 latest_broadcast_height = 2;
554+
555+
// The identifier of the transaction spending the swept output we last broadcast.
556+
string latest_spending_txid = 3;
557+
558+
// The amount, in satoshis, of the output being swept.
559+
uint64 amount_satoshis = 4;
560+
}
561+
562+
// A spending transaction has been confirmed on-chain and is awaiting threshold confirmations.
563+
//
564+
// It will be considered irrevocably confirmed after reaching `ANTI_REORG_DELAY`.
565+
// See more: https://docs.rs/ldk-node/latest/ldk_node/enum.PendingSweepBalance.html#variant.AwaitingThresholdConfirmations
566+
message AwaitingThresholdConfirmations {
567+
// The identifier of the channel this balance belongs to.
568+
optional string channel_id = 1;
569+
570+
// The identifier of the confirmed transaction spending the swept output.
571+
string latest_spending_txid = 2;
572+
573+
// The hash of the block in which the spending transaction was confirmed.
574+
string confirmation_hash = 3;
575+
576+
// The height at which the spending transaction was confirmed.
577+
uint32 confirmation_height = 4;
578+
579+
// The amount, in satoshis, of the output being swept.
580+
uint64 amount_satoshis = 5;
581+
}

0 commit comments

Comments
 (0)