Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions app/src/main/java/to/bitkit/fcm/WakeNodeWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import to.bitkit.models.BlocktankNotificationType.wakeToTimeout
import to.bitkit.models.NewTransactionSheetDetails
import to.bitkit.models.NewTransactionSheetDirection
import to.bitkit.models.NewTransactionSheetType
import to.bitkit.repositories.ActivityRepo
import to.bitkit.repositories.BlocktankRepo
import to.bitkit.repositories.LightningRepo
import to.bitkit.services.CoreService
import to.bitkit.ui.pushNotification
Expand All @@ -38,6 +40,8 @@ class WakeNodeWorker @AssistedInject constructor(
@Assisted private val workerParams: WorkerParameters,
private val coreService: CoreService,
private val lightningRepo: LightningRepo,
private val blocktankRepo: BlocktankRepo,
private val activityRepo: ActivityRepo,
) : CoroutineWorker(appContext, workerParams) {
private val self = this

Expand Down Expand Up @@ -143,15 +147,22 @@ class WakeNodeWorker @AssistedInject constructor(
lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
val sats = channel.amountOnClose
self.bestAttemptContent?.title = "Received ⚡ $sats sats"
// Save for UI to pick up
NewTransactionSheetDetails.save(
appContext,
NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
sats = channel.amountOnClose.toLong(),

val cjitOrder = channel.let { blocktankRepo.getCjitOrder(it) }
if (cjitOrder != null) {
val amount = channel.amountOnClose.toLong()

// Save for UI to pick up
NewTransactionSheetDetails.save(
appContext,
NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
sats = amount,
)
)
)
activityRepo.insertActivityFromChannel(cjitOrder = cjitOrder, channel = channel)
}
}
} else if (self.notificationType == orderPaymentConfirmed) {
self.bestAttemptContent?.title = "Channel opened"
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/to/bitkit/repositories/ActivityRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package to.bitkit.repositories
import com.synonym.bitkitcore.Activity
import com.synonym.bitkitcore.Activity.Onchain
import com.synonym.bitkitcore.ActivityFilter
import com.synonym.bitkitcore.IcJitEntry
import com.synonym.bitkitcore.LightningActivity
import com.synonym.bitkitcore.PaymentState
import com.synonym.bitkitcore.PaymentType
import com.synonym.bitkitcore.SortDirection
import kotlinx.coroutines.CoroutineDispatcher
Expand All @@ -14,6 +17,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import org.lightningdevkit.ldknode.ChannelDetails
import org.lightningdevkit.ldknode.PaymentDetails
import to.bitkit.data.AppDb
import to.bitkit.data.CacheStore
Expand All @@ -22,6 +26,7 @@ import to.bitkit.data.dto.PendingBoostActivity
import to.bitkit.data.dto.TransferType
import to.bitkit.data.entities.TagMetadataEntity
import to.bitkit.di.BgDispatcher
import to.bitkit.ext.amountOnClose
import to.bitkit.ext.matchesPaymentId
import to.bitkit.ext.nowTimestamp
import to.bitkit.ext.rawId
Expand Down Expand Up @@ -504,6 +509,41 @@ class ActivityRepo @Inject constructor(
}
}

/**
* Inserts a new activity
*/
suspend fun insertActivityFromChannel(
cjitOrder: IcJitEntry?,
channel: ChannelDetails,
): Result<Unit> = withContext(bgDispatcher) {
runCatching {
requireNotNull(cjitOrder)

val amount = channel.amountOnClose
val now = nowTimestamp().toEpochMilli().toULong()

return@withContext insertActivity(
Activity.Lightning(
LightningActivity(
id = channel.fundingTxo?.txid.orEmpty(),
txType = PaymentType.RECEIVED,
status = PaymentState.SUCCEEDED,
value = amount,
fee = 0U,
invoice = cjitOrder.invoice.request,
message = "",
timestamp = now,
preimage = null,
createdAt = now,
updatedAt = null,
)
)
)
}.onFailure { e ->
Logger.error("insertActivity error", e, context = TAG)
}
}

suspend fun addActivityToPendingBoost(pendingBoostActivity: PendingBoostActivity) = withContext(bgDispatcher) {
cacheStore.addActivityToPendingBoost(pendingBoostActivity)
}
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/to/bitkit/repositories/BlocktankRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ class BlocktankRepo @Inject constructor(
}
}

suspend fun isCjitOrder(channel: ChannelDetails): Boolean = withContext(bgDispatcher) {
return@withContext runCatching {
_blocktankState.value.cjitEntries.any { order ->
order.channelSizeSat == channel.channelValueSats &&
order.lspNode.pubkey == channel.counterpartyNodeId
}
}.getOrDefault(false)
suspend fun getCjitOrder(channel: ChannelDetails): IcJitEntry? = withContext(bgDispatcher) {
return@withContext _blocktankState.value.cjitEntries.firstOrNull { order ->
order.channelSizeSat == channel.channelValueSats &&
order.lspNode.pubkey == channel.counterpartyNodeId
}
}

suspend fun refreshInfo() = withContext(bgDispatcher) {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,18 @@ class AppViewModel @Inject constructor(

is Event.ChannelReady -> {
val channel = lightningRepo.getChannels()?.find { it.channelId == event.channelId }
if (channel != null && blocktankRepo.isCjitOrder(channel)) {
val cjitOrder = channel?.let { blocktankRepo.getCjitOrder(it) }
if (cjitOrder != null) {
val amount = channel.amountOnClose.toLong()
showNewTransactionSheet(
NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
sats = channel.amountOnClose.toLong(),
sats = amount,
),
event = event
)
activityRepo.insertActivityFromChannel(cjitOrder = cjitOrder, channel = channel)
} else {
toast(
type = Toast.ToastType.LIGHTNING,
Expand Down
Loading