Skip to content

Commit 1cc2e7b

Browse files
committed
refactor: Calculate amount received on close using channel only
1 parent 35e9b33 commit 1cc2e7b

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

app/src/main/java/to/bitkit/ext/ChannelDetails.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import org.lightningdevkit.ldknode.ChannelConfig
44
import org.lightningdevkit.ldknode.ChannelDetails
55
import org.lightningdevkit.ldknode.MaxDustHtlcExposure
66

7+
/**
8+
* Calculates the expected amount in sats that will be available upon channel closure.
9+
*/
10+
val ChannelDetails.amountOnClose: ULong
11+
get() {
12+
val outboundCapacitySat = this.outboundCapacityMsat / 1000u
13+
val reserveSats = this.unspendablePunishmentReserve ?: 0u
14+
15+
return outboundCapacitySat + reserveSats
16+
}
17+
718
fun mockChannelDetails(
819
channelId: String,
920
isChannelReady: Boolean = true,
@@ -13,7 +24,7 @@ fun mockChannelDetails(
1324
counterpartyNodeId = "counterpartyNodeId",
1425
fundingTxo = null,
1526
channelValueSats = 100_000uL,
16-
unspendablePunishmentReserve = 0uL,
27+
unspendablePunishmentReserve = 354uL,
1728
userChannelId = "userChannelId",
1829
feerateSatPer1000Weight = 5u,
1930
outboundCapacityMsat = 50_000uL,
@@ -25,7 +36,7 @@ fun mockChannelDetails(
2536
isUsable = true,
2637
isAnnounced = false,
2738
cltvExpiryDelta = null,
28-
counterpartyUnspendablePunishmentReserve = 354uL,
39+
counterpartyUnspendablePunishmentReserve = 0uL,
2940
counterpartyOutboundHtlcMinimumMsat = null,
3041
counterpartyOutboundHtlcMaximumMsat = null,
3142
counterpartyForwardingInfoFeeBaseMsat = null,

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,6 @@ class LightningService @Inject constructor(
271271
throw LdkError(e)
272272
}
273273
}
274-
275-
/** Calculates expected amount of funds received on channel close. */
276-
fun getChannelAmountOnClose(channelId: String): ULong {
277-
val channel = channels
278-
?.find { it.channelId == channelId }
279-
?.takeIf { it.isChannelReady } // only open channels
280-
?: return 0uL
281-
282-
val balance = balances?.lightningBalances
283-
?.filterIsInstance<LightningBalance.ClaimableOnChannelClose>()
284-
?.find { it.channelId == channel.channelId }
285-
?: return 0uL
286-
287-
return balance.amountSatoshis
288-
}
289274
// endregion
290275

291276
// region payments

app/src/main/java/to/bitkit/ui/screens/transfer/SavingsAdvancedScreen.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import androidx.compose.ui.tooling.preview.Preview
2929
import androidx.compose.ui.unit.dp
3030
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3131
import to.bitkit.R
32+
import to.bitkit.ext.amountOnClose
3233
import to.bitkit.services.filterOpen
3334
import to.bitkit.ui.components.BodyM
3435
import to.bitkit.ui.components.Caption13Up
@@ -78,7 +79,7 @@ fun SavingsAdvancedScreen(
7879
openChannels.map {
7980
TransferChannelUiState(
8081
channelId = it.channelId,
81-
balance = transfer.getChannelAmountOnClose(it.channelId),
82+
balance = it.amountOnClose,
8283
isSelected = selectedChannelIds.contains(it.channelId),
8384
)
8485
}

app/src/main/java/to/bitkit/ui/screens/transfer/SavingsConfirmScreen.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import kotlinx.coroutines.delay
3030
import kotlinx.coroutines.launch
3131
import org.lightningdevkit.ldknode.ChannelDetails
3232
import to.bitkit.R
33+
import to.bitkit.ext.amountOnClose
3334
import to.bitkit.services.filterOpen
3435
import to.bitkit.ui.components.ButtonSize
3536
import to.bitkit.ui.components.Caption13Up
@@ -71,7 +72,7 @@ fun SavingsConfirmScreen(
7172

7273
val channels = selectedChannels ?: openChannels
7374

74-
val amount = channels.sumOf { transfer.getChannelAmountOnClose(it.channelId) }
75+
val amount = channels.sumOf { it.amountOnClose }
7576

7677
SavingsConfirmContent(
7778
amount = amount,

app/src/main/java/to/bitkit/viewmodels/TransferViewModel.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ class TransferViewModel @Inject constructor(
144144

145145
// region Savings
146146

147-
fun getChannelAmountOnClose(channelId: String): ULong {
148-
return lightningService.getChannelAmountOnClose(channelId)
149-
}
150-
151147
private var channelsToClose = emptyList<ChannelDetails>()
152148

153149
fun setSelectedChannelIds(channelIds: Set<String>) {

0 commit comments

Comments
 (0)