diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 63f78041..d295a7af 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -613,6 +613,8 @@ Received %1$s sats Sent %1$s sats, with a fee of %2$s sats Couldn't send %1$s sats + A channel closed: %1$s sats to the onchain wallet + A channel is closing: %1$s sats to the onchain wallet Receive What it's for Leave the amount empty to let the payer choose. diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt index 3fa21198..838da650 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt @@ -41,6 +41,16 @@ object GroupLightningReports { val agrees: Boolean? get() = signedOursMsat?.let { it == channel.oursMsat } } + /** + * Whether a payment the node reports is a channel's close. lightning-kmp files a close as an + * outgoing payment, and gives it no payment hash: every Lightning payment has one. Read here + * for the wallet's payment list and the room's lines alike, so the two cannot disagree. + */ + fun isChannelClose(incoming: Boolean, paymentHash: String?): Boolean = !incoming && paymentHash == null + + /** What reached an outgoing payment's recipient: lightning-kmp reports its amount with the fee in it. */ + fun recipientMsat(amountMsat: Long, feesMsat: Long): Long = amountMsat - feesMsat + /** Sends [snapshot] to the room, and files it here as every member would. */ suspend fun publishSnapshot(database: BennuDatabase, localChatRoom: LocalChatRoom, userPublicKey: HexKey, snapshot: WalletWire.Snapshot) { GroupLightningWalletManager.queue(database, localChatRoom, userPublicKey, GroupLightningEvents.SNAPSHOT, CommonJson.encodeToString(WalletWire.Snapshot.serializer(), snapshot)) @@ -150,7 +160,7 @@ object GroupLightningReports { // lightning-kmp files a channel's close as an outgoing payment, with no payment hash: every // Lightning payment has one. The channel's closed line already says it, and a second line // would read as the group having paid the money away. Found by the regtest run. - if (!payment.incoming && payment.paymentHash == null) return@forEach + if (isChannelClose(payment.incoming, payment.paymentHash)) return@forEach val (type, content) = when { payment.incoming && payment.succeeded -> ChatMessage.TYPE_LIGHTNING_PAYMENT_RECEIVED to buildString { append("The group's Lightning wallet received ${sats(payment.amountMsat)}") @@ -161,12 +171,12 @@ object GroupLightningReports { // An outgoing payment's amount is what left the wallet, its fee included; the line // says what reached the recipient and the fee beside it. payment.succeeded -> ChatMessage.TYPE_LIGHTNING_PAYMENT_SENT to buildString { - append("The group's Lightning wallet sent ${sats(payment.amountMsat - payment.feesMsat)}") + append("The group's Lightning wallet sent ${sats(recipientMsat(payment.amountMsat, payment.feesMsat))}") if (payment.feesMsat > 0) append(", with ${Satoshi((payment.feesMsat + 999) / 1000).grouped()} sats in fees") append(".") } else -> ChatMessage.TYPE_LIGHTNING_PAYMENT_FAILED to - "A Lightning payment of ${sats(payment.amountMsat)} failed. Nothing left the wallet." + "A Lightning payment of ${sats(recipientMsat(payment.amountMsat, payment.feesMsat))} failed. Nothing left the wallet." } GroupLightningLines.writeFact(database, chatRoomId, type, payment.id, sentBy, content, completedAt) } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupLightningWalletScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupLightningWalletScreen.kt index 4083df4e..cf1da4e4 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupLightningWalletScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupLightningWalletScreen.kt @@ -1,5 +1,8 @@ package com.machankura.bennu.compose.ui.composable +import bennu.composeapp.generated.resources.a_channel_closed_s_sats_to_the_onchain_wallet +import bennu.composeapp.generated.resources.a_channel_is_closing_s_sats_to_the_onchain_wallet +import androidx.compose.material.icons.filled.LinkOff import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -418,15 +421,36 @@ private fun ChannelRow(check: GroupLightningReports.ChannelCheck, onClose: ((for ) } +/** + * One payment the operator's node reported. A channel's close comes in this list too -- lightning-kmp + * files it as an outgoing payment -- and is said as a close, since the money went to the group's own + * onchain wallet rather than to anybody. A payment sent is said as what reached the recipient, with the + * fee beside it, not the two added together. + */ @Composable -private fun PaymentRow(payment: GroupLightningPaymentRow) { - val amount = Satoshi(payment.amountMsat / 1000).grouped() +internal fun PaymentRow(payment: GroupLightningPaymentRow) { + val isClose = GroupLightningReports.isChannelClose(payment.incoming, payment.paymentHash) + val amount = Satoshi( + if (payment.incoming || isClose) payment.amountMsat / 1000 + else GroupLightningReports.recipientMsat(payment.amountMsat, payment.feesMsat) / 1000 + ).grouped() ListItem( - leadingContent = { Icon(if (payment.incoming) Icons.AutoMirrored.Filled.CallReceived else Icons.AutoMirrored.Filled.CallMade, contentDescription = Decorative) }, + leadingContent = { + Icon( + when { + isClose -> Icons.Default.LinkOff + payment.incoming -> Icons.AutoMirrored.Filled.CallReceived + else -> Icons.AutoMirrored.Filled.CallMade + }, + contentDescription = Decorative, + ) + }, headlineContent = { Text( when { payment.incoming -> stringResource(Res.string.received_s_sats, amount) + isClose && payment.succeeded -> stringResource(Res.string.a_channel_closed_s_sats_to_the_onchain_wallet, amount) + isClose -> stringResource(Res.string.a_channel_is_closing_s_sats_to_the_onchain_wallet, amount) payment.succeeded -> stringResource(Res.string.sent_s_sats_with_a_fee_of_s_sats, amount, Satoshi(payment.feesMsat / 1000).grouped()) else -> stringResource(Res.string.couldn_t_send_s_sats, amount) } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/LightningPaymentRowJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/LightningPaymentRowJvmTest.kt new file mode 100644 index 00000000..2c695171 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/LightningPaymentRowJvmTest.kt @@ -0,0 +1,41 @@ +package com.machankura.bennu.compose.ui.composable + +import androidx.compose.foundation.layout.Column +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.runDesktopComposeUiTest +import com.machankura.bennu.compose.database.model.GroupLightningPaymentRow +import com.machankura.bennu.compose.ui.theme.BennuTheme +import kotlin.test.Test + +/** + * The Lightning wallet's payment list says what the node's payments were, as lightning-kmp reports + * them: a sent amount with its fee already in it, and a channel's close filed as an outgoing payment + * with no payment hash. Found by the regtest run, which listed a close as "sent" and 20,000 sats with + * an 84-sat fee as 20,084 sent. docs/group-lightning-transcript.md. + */ +@OptIn(ExperimentalTestApi::class) +class LightningPaymentRowJvmTest { + + private fun row(id: String, incoming: Boolean, amountMsat: Long, feesMsat: Long, paymentHash: String?, succeeded: Boolean) = + GroupLightningPaymentRow("w", id, incoming, amountMsat, feesMsat, paymentHash, description = null, completedAt = null, succeeded = succeeded) + + @Test + fun `a payment sent is what reached the recipient, and a close is said as a close`() = runDesktopComposeUiTest { + setContent { + BennuTheme { + Column { + PaymentRow(row("in", incoming = true, amountMsat = 96_900_000, feesMsat = 0, paymentHash = "aa".repeat(32), succeeded = true)) + PaymentRow(row("out", incoming = false, amountMsat = 20_084_000, feesMsat = 84_000, paymentHash = "bb".repeat(32), succeeded = true)) + PaymentRow(row("closed", incoming = false, amountMsat = 76_816_000, feesMsat = 0, paymentHash = null, succeeded = true)) + PaymentRow(row("closing", incoming = false, amountMsat = 50_000_000, feesMsat = 0, paymentHash = null, succeeded = false)) + } + } + } + onNodeWithText("Received 96,900 sats").assertIsDisplayed() + onNodeWithText("Sent 20,000 sats, with a fee of 84 sats").assertIsDisplayed() + onNodeWithText("A channel closed: 76,816 sats to the onchain wallet").assertIsDisplayed() + onNodeWithText("A channel is closing: 50,000 sats to the onchain wallet").assertIsDisplayed() + } +} diff --git a/docs/group-lightning-transcript.md b/docs/group-lightning-transcript.md index cd465737..3e2abf2d 100644 --- a/docs/group-lightning-transcript.md +++ b/docs/group-lightning-transcript.md @@ -33,8 +33,10 @@ unit test had not: lightning-kmp files a channel's close as an outgoing payment payment hash, which wrote a "sent" line beside each closed one, and an outgoing payment's amount includes its fee, so 20,000 sats with an 84-sat fee read as 20,084 sent. Both are fixed, and the unit test's payments are now shaped as lightning-kmp reports them. The -wallet screen's payment list reads the same two fields the same way and still shows both; -that is the screen's, and not changed here. A look at a room on a device has not been done. +wallet screen's payment list read the same two fields the same way, and now reads them +through the same two rules, `GroupLightningReports.isChannelClose` and `recipientMsat`: a +close is listed as a close, or as closing until it confirms, and a sent payment as what +reached the recipient with the fee beside it. A look at a room on a device has not been done. Read [group-lightning-wallet.md](./group-lightning-wallet.md) for the wallet itself and