Skip to content

Commit 3649737

Browse files
committed
feat: onchain received sheet
1 parent 773d1ba commit 3649737

File tree

7 files changed

+67
-106
lines changed

7 files changed

+67
-106
lines changed

app/src/main/java/to/bitkit/fcm/WakeNodeWorker.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ class WakeNodeWorker @AssistedInject constructor(
132132
is Event.ChannelReady -> onChannelReady(event, showDetails, openBitkitMessage)
133133
is Event.ChannelClosed -> onChannelClosed(event)
134134

135-
is Event.PaymentSuccessful -> Unit
136-
is Event.PaymentClaimable -> Unit
137-
is Event.PaymentForwarded -> Unit
138-
139135
is Event.PaymentFailed -> {
140136
self.bestAttemptContent?.title = "Payment failed"
141137
self.bestAttemptContent?.body = "${event.reason}"
@@ -144,6 +140,27 @@ class WakeNodeWorker @AssistedInject constructor(
144140
self.deliver()
145141
}
146142
}
143+
144+
is Event.OnchainTransactionReceived -> {
145+
// TODO handle like onPaymentReceived but for onchain
146+
}
147+
148+
is Event.OnchainTransactionConfirmed -> Unit
149+
150+
is Event.PaymentSuccessful -> Unit
151+
is Event.PaymentClaimable -> Unit
152+
is Event.PaymentForwarded -> Unit
153+
154+
is Event.SyncProgress -> Unit
155+
is Event.SyncCompleted -> Unit
156+
is Event.BalanceChanged -> Unit
157+
158+
is Event.OnchainTransactionEvicted,
159+
is Event.OnchainTransactionReorged,
160+
is Event.OnchainTransactionReplaced,
161+
-> {
162+
// TODO handle activity removed from mempool UI & toast
163+
}
147164
}
148165
}
149166

app/src/main/java/to/bitkit/models/NewTransactionSheetDetails.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ data class NewTransactionSheetDetails(
1616
val type: NewTransactionSheetType,
1717
val direction: NewTransactionSheetDirection,
1818
val paymentHashOrTxId: String? = null,
19-
val sats: Long,
20-
val isLoadingDetails: Boolean = false
19+
val sats: Long = 0,
20+
val isLoadingDetails: Boolean = false,
2121
) {
2222
companion object {
2323
private const val BACKGROUND_TRANSACTION_KEY = "backgroundTransaction"

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class LightningRepo @Inject constructor(
368368
}
369369

370370
val channelName = channel.inboundScidAlias?.toString()
371-
?: channel.channelId.take(CHANNEL_ID_PREVIEW_LENGTH) + ""
371+
?: (channel.channelId.take(CHANNEL_ID_PREVIEW_LENGTH) + "")
372372

373373
val closedAt = (System.currentTimeMillis() / 1000L).toULong()
374374

@@ -656,14 +656,13 @@ class LightningRepo @Inject constructor(
656656
isMaxAmount = isMaxAmount
657657
)
658658

659-
val addressString = address.toString()
660659
val preActivityMetadata = PreActivityMetadata(
661660
paymentId = txId,
662661
createdAt = nowTimestamp().toEpochMilli().toULong(),
663662
tags = tags,
664663
paymentHash = null,
665664
txId = txId,
666-
address = addressString,
665+
address = address,
667666
isReceive = false,
668667
feeRate = satsPerVByte.toULong(),
669668
isTransfer = isTransfer,

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

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import to.bitkit.utils.LdkError
4949
import to.bitkit.utils.LdkLogWriter
5050
import to.bitkit.utils.Logger
5151
import to.bitkit.utils.ServiceError
52+
import to.bitkit.utils.jsonLogOf
5253
import javax.inject.Inject
5354
import javax.inject.Singleton
5455
import kotlin.io.path.Path
@@ -680,88 +681,16 @@ class LightningService @Inject constructor(
680681
return
681682
}
682683
val event = node.nextEventAsync()
683-
684+
Logger.debug("LDK-node event fired: ${jsonLogOf(event)}")
684685
try {
685686
node.eventHandled()
686-
Logger.debug("LDK eventHandled: $event")
687+
Logger.verbose("LDK-node eventHandled: $event")
687688
} catch (e: NodeException) {
688-
Logger.error("LDK eventHandled error", LdkError(e))
689+
Logger.verbose("LDK eventHandled error: $event", LdkError(e))
689690
}
690-
691-
logEvent(event)
692691
onEvent?.invoke(event)
693692
}
694693
}
695-
696-
private fun logEvent(event: Event) {
697-
when (event) {
698-
is Event.PaymentSuccessful -> {
699-
val paymentId = event.paymentId ?: "?"
700-
val paymentHash = event.paymentHash
701-
val feePaidMsat = event.feePaidMsat ?: 0
702-
Logger.info(
703-
"✅ Payment successful: paymentId: $paymentId paymentHash: $paymentHash feePaidMsat: $feePaidMsat"
704-
)
705-
}
706-
707-
is Event.PaymentFailed -> {
708-
val paymentId = event.paymentId ?: "?"
709-
val paymentHash = event.paymentHash
710-
val reason = event.reason
711-
Logger.info("❌ Payment failed: paymentId: $paymentId paymentHash: $paymentHash reason: $reason")
712-
}
713-
714-
is Event.PaymentReceived -> {
715-
val paymentId = event.paymentId ?: "?"
716-
val paymentHash = event.paymentHash
717-
val amountMsat = event.amountMsat
718-
Logger.info(
719-
"🤑 Payment received: paymentId: $paymentId paymentHash: $paymentHash amountMsat: $amountMsat"
720-
)
721-
}
722-
723-
is Event.PaymentClaimable -> {
724-
val paymentId = event.paymentId
725-
val paymentHash = event.paymentHash
726-
val claimableAmountMsat = event.claimableAmountMsat
727-
Logger.info(
728-
"🫰 Payment claimable: paymentId: $paymentId paymentHash: $paymentHash claimableAmountMsat: $claimableAmountMsat"
729-
)
730-
}
731-
732-
is Event.PaymentForwarded -> Unit
733-
734-
is Event.ChannelPending -> {
735-
val channelId = event.channelId
736-
val userChannelId = event.userChannelId
737-
val formerTemporaryChannelId = event.formerTemporaryChannelId
738-
val counterpartyNodeId = event.counterpartyNodeId
739-
val fundingTxo = event.fundingTxo
740-
Logger.info(
741-
"⏳ Channel pending: channelId: $channelId userChannelId: $userChannelId formerTemporaryChannelId: $formerTemporaryChannelId counterpartyNodeId: $counterpartyNodeId fundingTxo: $fundingTxo"
742-
)
743-
}
744-
745-
is Event.ChannelReady -> {
746-
val channelId = event.channelId
747-
val userChannelId = event.userChannelId
748-
val counterpartyNodeId = event.counterpartyNodeId ?: "?"
749-
Logger.info(
750-
"👐 Channel ready: channelId: $channelId userChannelId: $userChannelId counterpartyNodeId: $counterpartyNodeId"
751-
)
752-
}
753-
754-
is Event.ChannelClosed -> {
755-
val channelId = event.channelId
756-
val userChannelId = event.userChannelId
757-
val counterpartyNodeId = event.counterpartyNodeId ?: "?"
758-
val reason = event.reason?.toString() ?: ""
759-
Logger.info(
760-
"⛔ Channel closed: channelId: $channelId userChannelId: $userChannelId counterpartyNodeId: $counterpartyNodeId reason: $reason"
761-
)
762-
}
763-
}
764-
}
765694
// endregion
766695

767696
// region state

app/src/main/java/to/bitkit/ui/ContentView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fun ContentView(
205205

206206
val pendingTransaction = NewTransactionSheetDetails.load(context)
207207
if (pendingTransaction != null) {
208-
appViewModel.showNewTransactionSheet(details = pendingTransaction, event = null)
208+
appViewModel.showNewTransactionSheet(details = pendingTransaction)
209209
NewTransactionSheetDetails.clear(context)
210210
}
211211

app/src/main/java/to/bitkit/ui/components/Money.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ fun MoneyMSB(
7474
@Composable
7575
fun MoneyCaptionB(
7676
sats: Long,
77+
modifier: Modifier = Modifier,
7778
color: Color = MaterialTheme.colorScheme.primary,
7879
symbol: Boolean = false,
7980
symbolColor: Color = Colors.White64,
80-
modifier: Modifier = Modifier,
8181
) {
8282
val isPreview = LocalInspectionMode.current
8383
if (isPreview) {

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class AppViewModel @Inject constructor(
226226

227227
launch(bgDispatcher) { walletRepo.syncNodeAndWallet() }
228228
runCatching {
229-
when (event) { // TODO Create individual sheet for each type of event
229+
when (event) {
230230
is Event.PaymentReceived -> {
231231
showNewTransactionSheet(
232232
NewTransactionSheetDetails(
@@ -235,7 +235,7 @@ class AppViewModel @Inject constructor(
235235
paymentHashOrTxId = event.paymentHash,
236236
sats = (event.amountMsat / 1000u).toLong(),
237237
),
238-
event = event
238+
event,
239239
)
240240
}
241241

@@ -250,7 +250,7 @@ class AppViewModel @Inject constructor(
250250
direction = NewTransactionSheetDirection.RECEIVED,
251251
sats = amount,
252252
),
253-
event = event
253+
event,
254254
)
255255
activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel)
256256
} else {
@@ -267,7 +267,7 @@ class AppViewModel @Inject constructor(
267267

268268
is Event.PaymentSuccessful -> {
269269
val paymentHash = event.paymentHash
270-
// TODO Temporary solution while LDK node don't returns the sent value in the event
270+
// TODO Temporary solution while LDK node doesn't return the sent value in the event
271271
activityRepo.findActivityByPaymentId(
272272
paymentHashOrTxId = paymentHash,
273273
type = ActivityFilter.LIGHTNING,
@@ -283,13 +283,37 @@ class AppViewModel @Inject constructor(
283283
),
284284
)
285285
}.onFailure { e ->
286-
Logger.warn("Failed displaying sheet for event: $Event", e)
286+
Logger.warn("Failed displaying sheet for event: $event", e)
287287
}
288288
}
289289

290290
is Event.PaymentClaimable -> Unit
291291
is Event.PaymentFailed -> Unit
292292
is Event.PaymentForwarded -> Unit
293+
294+
is Event.OnchainTransactionReceived -> {
295+
showNewTransactionSheet(
296+
NewTransactionSheetDetails(
297+
type = NewTransactionSheetType.ONCHAIN,
298+
direction = NewTransactionSheetDirection.RECEIVED,
299+
paymentHashOrTxId = event.txid,
300+
sats = event.details.amountSats.toLong(),
301+
),
302+
event,
303+
)
304+
}
305+
306+
is Event.OnchainTransactionConfirmed -> Unit
307+
is Event.SyncProgress -> Unit
308+
is Event.SyncCompleted -> Unit
309+
is Event.BalanceChanged -> Unit
310+
311+
is Event.OnchainTransactionEvicted,
312+
is Event.OnchainTransactionReorged,
313+
is Event.OnchainTransactionReplaced,
314+
-> {
315+
// TODO handle activity removed from mempool UI & toast
316+
}
293317
}
294318
}.onFailure { e ->
295319
Logger.error("LDK event handler error", e, context = TAG)
@@ -1324,18 +1348,14 @@ class AppViewModel @Inject constructor(
13241348
// endregion
13251349

13261350
// region TxSheet
1327-
var isNewTransactionSheetEnabled = true
1328-
private set
1329-
1351+
private var _isNewTransactionSheetEnabled = true
13301352
private val _showNewTransaction = MutableStateFlow(false)
13311353
val showNewTransaction: StateFlow<Boolean> = _showNewTransaction.asStateFlow()
13321354

13331355
private val _newTransaction = MutableStateFlow(
13341356
NewTransactionSheetDetails(
13351357
type = NewTransactionSheetType.LIGHTNING,
13361358
direction = NewTransactionSheetDirection.RECEIVED,
1337-
paymentHashOrTxId = null,
1338-
sats = 0
13391359
)
13401360
)
13411361

@@ -1345,25 +1365,23 @@ class AppViewModel @Inject constructor(
13451365
NewTransactionSheetDetails(
13461366
type = NewTransactionSheetType.LIGHTNING,
13471367
direction = NewTransactionSheetDirection.SENT,
1348-
paymentHashOrTxId = null,
1349-
sats = 0
13501368
)
13511369
)
13521370

13531371
val successSendUiState = _successSendUiState.asStateFlow()
13541372

13551373
fun setNewTransactionSheetEnabled(enabled: Boolean) {
1356-
isNewTransactionSheetEnabled = enabled
1374+
_isNewTransactionSheetEnabled = enabled
13571375
}
13581376

13591377
fun showNewTransactionSheet(
13601378
details: NewTransactionSheetDetails,
1361-
event: Event?,
1379+
event: Event? = null,
13621380
) = viewModelScope.launch {
13631381
if (backupRepo.isRestoring.value) return@launch
13641382

1365-
if (!isNewTransactionSheetEnabled) {
1366-
Logger.debug("NewTransactionSheet display blocked by isNewTransactionSheetEnabled=false", context = TAG)
1383+
if (!_isNewTransactionSheetEnabled) {
1384+
Logger.verbose("NewTransactionSheet blocked by isNewTransactionSheetEnabled=false", context = TAG)
13671385
return@launch
13681386
}
13691387

@@ -1385,13 +1403,11 @@ class AppViewModel @Inject constructor(
13851403

13861404
hideSheet()
13871405

1406+
_showNewTransaction.update { true }
13881407
_newTransaction.update { details }
1389-
_showNewTransaction.value = true
13901408
}
13911409

1392-
fun hideNewTransactionSheet() {
1393-
_showNewTransaction.value = false
1394-
}
1410+
fun hideNewTransactionSheet() = _showNewTransaction.update { false }
13951411
// endregion
13961412

13971413
// region Sheets

0 commit comments

Comments
 (0)