Skip to content

Commit fae8875

Browse files
authored
Merge pull request #429 from synonymdev/fix/cjit-activity
Display CJIT activities
2 parents 2929a74 + 210e213 commit fae8875

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import to.bitkit.models.BlocktankNotificationType.wakeToTimeout
2525
import to.bitkit.models.NewTransactionSheetDetails
2626
import to.bitkit.models.NewTransactionSheetDirection
2727
import to.bitkit.models.NewTransactionSheetType
28+
import to.bitkit.repositories.ActivityRepo
29+
import to.bitkit.repositories.BlocktankRepo
2830
import to.bitkit.repositories.LightningRepo
2931
import to.bitkit.services.CoreService
3032
import to.bitkit.ui.pushNotification
@@ -38,6 +40,8 @@ class WakeNodeWorker @AssistedInject constructor(
3840
@Assisted private val workerParams: WorkerParameters,
3941
private val coreService: CoreService,
4042
private val lightningRepo: LightningRepo,
43+
private val blocktankRepo: BlocktankRepo,
44+
private val activityRepo: ActivityRepo,
4145
) : CoroutineWorker(appContext, workerParams) {
4246
private val self = this
4347

@@ -143,15 +147,22 @@ class WakeNodeWorker @AssistedInject constructor(
143147
lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
144148
val sats = channel.amountOnClose
145149
self.bestAttemptContent?.title = "Received ⚡ $sats sats"
146-
// Save for UI to pick up
147-
NewTransactionSheetDetails.save(
148-
appContext,
149-
NewTransactionSheetDetails(
150-
type = NewTransactionSheetType.LIGHTNING,
151-
direction = NewTransactionSheetDirection.RECEIVED,
152-
sats = channel.amountOnClose.toLong(),
150+
151+
val cjitOrder = channel.let { blocktankRepo.getCjitOrder(it) }
152+
if (cjitOrder != null) {
153+
val amount = channel.amountOnClose.toLong()
154+
155+
// Save for UI to pick up
156+
NewTransactionSheetDetails.save(
157+
appContext,
158+
NewTransactionSheetDetails(
159+
type = NewTransactionSheetType.LIGHTNING,
160+
direction = NewTransactionSheetDirection.RECEIVED,
161+
sats = amount,
162+
)
153163
)
154-
)
164+
activityRepo.insertActivityFromChannel(cjitOrder = cjitOrder, channel = channel)
165+
}
155166
}
156167
} else if (self.notificationType == orderPaymentConfirmed) {
157168
self.bestAttemptContent?.title = "Channel opened"

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package to.bitkit.repositories
33
import com.synonym.bitkitcore.Activity
44
import com.synonym.bitkitcore.Activity.Onchain
55
import com.synonym.bitkitcore.ActivityFilter
6+
import com.synonym.bitkitcore.IcJitEntry
7+
import com.synonym.bitkitcore.LightningActivity
8+
import com.synonym.bitkitcore.PaymentState
69
import com.synonym.bitkitcore.PaymentType
710
import com.synonym.bitkitcore.SortDirection
811
import kotlinx.coroutines.CoroutineDispatcher
@@ -14,6 +17,7 @@ import kotlinx.coroutines.flow.first
1417
import kotlinx.coroutines.flow.map
1518
import kotlinx.coroutines.withContext
1619
import kotlinx.coroutines.withTimeout
20+
import org.lightningdevkit.ldknode.ChannelDetails
1721
import org.lightningdevkit.ldknode.PaymentDetails
1822
import to.bitkit.data.AppDb
1923
import to.bitkit.data.CacheStore
@@ -22,6 +26,7 @@ import to.bitkit.data.dto.PendingBoostActivity
2226
import to.bitkit.data.dto.TransferType
2327
import to.bitkit.data.entities.TagMetadataEntity
2428
import to.bitkit.di.BgDispatcher
29+
import to.bitkit.ext.amountOnClose
2530
import to.bitkit.ext.matchesPaymentId
2631
import to.bitkit.ext.nowTimestamp
2732
import to.bitkit.ext.rawId
@@ -504,6 +509,41 @@ class ActivityRepo @Inject constructor(
504509
}
505510
}
506511

512+
/**
513+
* Inserts a new activity
514+
*/
515+
suspend fun insertActivityFromChannel(
516+
cjitOrder: IcJitEntry?,
517+
channel: ChannelDetails,
518+
): Result<Unit> = withContext(bgDispatcher) {
519+
runCatching {
520+
requireNotNull(cjitOrder)
521+
522+
val amount = channel.amountOnClose
523+
val now = nowTimestamp().toEpochMilli().toULong()
524+
525+
return@withContext insertActivity(
526+
Activity.Lightning(
527+
LightningActivity(
528+
id = channel.fundingTxo?.txid.orEmpty(),
529+
txType = PaymentType.RECEIVED,
530+
status = PaymentState.SUCCEEDED,
531+
value = amount,
532+
fee = 0U,
533+
invoice = cjitOrder.invoice.request,
534+
message = "",
535+
timestamp = now,
536+
preimage = null,
537+
createdAt = now,
538+
updatedAt = null,
539+
)
540+
)
541+
)
542+
}.onFailure { e ->
543+
Logger.error("insertActivity error", e, context = TAG)
544+
}
545+
}
546+
507547
suspend fun addActivityToPendingBoost(pendingBoostActivity: PendingBoostActivity) = withContext(bgDispatcher) {
508548
cacheStore.addActivityToPendingBoost(pendingBoostActivity)
509549
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ class BlocktankRepo @Inject constructor(
9797
}
9898
}
9999

100-
suspend fun isCjitOrder(channel: ChannelDetails): Boolean = withContext(bgDispatcher) {
101-
return@withContext runCatching {
102-
_blocktankState.value.cjitEntries.any { order ->
103-
order.channelSizeSat == channel.channelValueSats &&
104-
order.lspNode.pubkey == channel.counterpartyNodeId
105-
}
106-
}.getOrDefault(false)
100+
suspend fun getCjitOrder(channel: ChannelDetails): IcJitEntry? = withContext(bgDispatcher) {
101+
return@withContext _blocktankState.value.cjitEntries.firstOrNull { order ->
102+
order.channelSizeSat == channel.channelValueSats &&
103+
order.lspNode.pubkey == channel.counterpartyNodeId
104+
}
107105
}
108106

109107
suspend fun refreshInfo() = withContext(bgDispatcher) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,18 @@ class AppViewModel @Inject constructor(
221221

222222
is Event.ChannelReady -> {
223223
val channel = lightningRepo.getChannels()?.find { it.channelId == event.channelId }
224-
if (channel != null && blocktankRepo.isCjitOrder(channel)) {
224+
val cjitOrder = channel?.let { blocktankRepo.getCjitOrder(it) }
225+
if (cjitOrder != null) {
226+
val amount = channel.amountOnClose.toLong()
225227
showNewTransactionSheet(
226228
NewTransactionSheetDetails(
227229
type = NewTransactionSheetType.LIGHTNING,
228230
direction = NewTransactionSheetDirection.RECEIVED,
229-
sats = channel.amountOnClose.toLong(),
231+
sats = amount,
230232
),
231233
event = event
232234
)
235+
activityRepo.insertActivityFromChannel(cjitOrder = cjitOrder, channel = channel)
233236
} else {
234237
toast(
235238
type = Toast.ToastType.LIGHTNING,

0 commit comments

Comments
 (0)