-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChannelDetails.kt
More file actions
84 lines (76 loc) · 3.16 KB
/
ChannelDetails.kt
File metadata and controls
84 lines (76 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package to.bitkit.ext
import org.lightningdevkit.ldknode.ChannelConfig
import org.lightningdevkit.ldknode.ChannelDetails
import org.lightningdevkit.ldknode.MaxDustHtlcExposure
import to.bitkit.models.msatFloorOf
/**
* Calculates our total balance in the channel (see `value_to_self_msat` in rust-lightning).
*
* This represents the amount we would receive if the channel closes now (excluding fees).
* Approximates ldk-node's `ClaimableOnChannelClose.amountSatoshis` (excluding HTLCs).
*
* Formula: outbound_capacity + our_reserve
* - outbound_capacity: What we can spend now over Lightning
* - our_reserve: Our reserve that we get back on close
*/
val ChannelDetails.amountOnClose: ULong
@Suppress("ForbiddenComment")
get() {
// TODO: use channelDetails.claimableOnCloseSats
val outboundCapacitySat = msatFloorOf(this.outboundCapacityMsat)
val ourReserve = this.unspendablePunishmentReserve ?: 0u
return outboundCapacitySat + ourReserve
}
/** Returns only `open` channels, filtering out pending ones. */
fun List<ChannelDetails>.filterOpen(): List<ChannelDetails> = this.filter { it.isChannelReady }
/** Returns only `pending` channels. */
fun List<ChannelDetails>.filterPending(): List<ChannelDetails> = this.filterNot { it.isChannelReady }
/** Returns a limit in sats as close as possible to the HTLC limit we can currently send. */
fun List<ChannelDetails>?.totalNextOutboundHtlcLimitSats(): ULong = this?.filter { it.isUsable }
?.sumOf { msatFloorOf(it.nextOutboundHtlcLimitMsat) }
?: 0u
/** Calculates the total remote balance (inbound capacity) from open channels. */
fun List<ChannelDetails>.calculateRemoteBalance(): ULong = this
.filterOpen()
.sumOf { msatFloorOf(it.inboundCapacityMsat) }
fun createChannelDetails(): ChannelDetails = ChannelDetails(
channelId = "channelId",
counterpartyNodeId = "counterpartyNodeId",
fundingTxo = null,
shortChannelId = null,
outboundScidAlias = null,
inboundScidAlias = null,
channelValueSats = 0u,
unspendablePunishmentReserve = null,
userChannelId = "0",
feerateSatPer1000Weight = 0u,
outboundCapacityMsat = 0u,
inboundCapacityMsat = 0u,
confirmationsRequired = null,
confirmations = null,
isOutbound = false,
isChannelReady = false,
isUsable = false,
isAnnounced = false,
cltvExpiryDelta = null,
counterpartyUnspendablePunishmentReserve = 0u,
counterpartyOutboundHtlcMinimumMsat = null,
counterpartyOutboundHtlcMaximumMsat = null,
counterpartyForwardingInfoFeeBaseMsat = null,
counterpartyForwardingInfoFeeProportionalMillionths = null,
counterpartyForwardingInfoCltvExpiryDelta = null,
nextOutboundHtlcLimitMsat = 0u,
nextOutboundHtlcMinimumMsat = 0u,
forceCloseSpendDelay = null,
inboundHtlcMinimumMsat = 0u,
inboundHtlcMaximumMsat = null,
claimableOnCloseSats = 0u,
config = ChannelConfig(
forwardingFeeProportionalMillionths = 0u,
forwardingFeeBaseMsat = 0u,
cltvExpiryDelta = 0u,
maxDustHtlcExposure = MaxDustHtlcExposure.FixedLimit(limitMsat = 0u),
forceCloseAvoidanceMaxFeeSatoshis = 0u,
acceptUnderpayingHtlcs = false,
),
)