@@ -1239,10 +1239,6 @@ class MigrationService @Inject constructor(
12391239 if (hasRNMmkvData()) {
12401240 val mmkvData = loadRNMmkvData() ? : return
12411241
1242- extractRNMetadata(mmkvData)?.let { metadata ->
1243- applyRNMetadata(metadata)
1244- }
1245-
12461242 extractRNActivities(mmkvData)?.let { activities ->
12471243 applyOnchainMetadata(activities)
12481244 }
@@ -1257,6 +1253,10 @@ class MigrationService @Inject constructor(
12571253 applyBoostTransactions(boosts)
12581254 }
12591255 }
1256+
1257+ extractRNMetadata(mmkvData)?.let { metadata ->
1258+ applyRNMetadata(metadata)
1259+ }
12601260 }
12611261
12621262 pendingRemoteActivityData?.let { remoteActivities ->
@@ -1450,6 +1450,19 @@ class MigrationService @Inject constructor(
14501450 }
14511451 }
14521452
1453+ val backupValue = item.value.toULong()
1454+ if (backupValue > updated.value) {
1455+ updated = updated.copy(value = backupValue)
1456+ wasUpdated = true
1457+ }
1458+
1459+ item.fee?.let { backupFee ->
1460+ if (backupFee.toULong() > updated.fee) {
1461+ updated = updated.copy(fee = backupFee.toULong())
1462+ wasUpdated = true
1463+ }
1464+ }
1465+
14531466 item.address?.let { address ->
14541467 if (address.isNotEmpty() && updated.address != address) {
14551468 updated = updated.copy(address = address)
@@ -1460,30 +1473,80 @@ class MigrationService @Inject constructor(
14601473 return if (wasUpdated) updated else null
14611474 }
14621475
1463- @Suppress(" CyclomaticComplexMethod" , " NestedBlockDepth" )
1476+ @Suppress(" CyclomaticComplexMethod" , " NestedBlockDepth" , " LongMethod " )
14641477 private suspend fun applyOnchainMetadata (items : List <RNActivityItem >) {
14651478 val onchainItems = items.filter { it.activityType == " onchain" }
1479+ var updatedCount = 0
1480+ var createdCount = 0
14661481
14671482 onchainItems.forEach { item ->
14681483 val txId = item.txId ? : item.id.takeIf { it.isNotEmpty() } ? : return @forEach
14691484
14701485 val onchain = activityRepo.getOnchainActivityByTxId(txId)
1471- if (onchain == null ) {
1472- Logger .warn(" Onchain activity not found for txId: $txId " , context = TAG )
1473- return @forEach
1474- }
1486+ if (onchain != null ) {
1487+ updateOnchainActivityMetadata(item, onchain)?.let { updated ->
1488+ activityRepo.updateActivity(updated.id, Activity .Onchain (updated))
1489+ .onSuccess { updatedCount++ }
1490+ .onFailure { e ->
1491+ Logger .error(
1492+ " Failed to update onchain activity metadata for $txId : $e " ,
1493+ e,
1494+ context = TAG
1495+ )
1496+ }
1497+ }
1498+ } else {
1499+ val timestampSecs = (item.timestamp / MS_PER_SEC ).toULong()
1500+ val now = (System .currentTimeMillis() / MS_PER_SEC ).toULong()
14751501
1476- updateOnchainActivityMetadata(item, onchain)?.let { updated ->
1477- activityRepo.updateActivity(updated.id, Activity .Onchain (updated))
1502+ val activityTimestamp = if (timestampSecs > 0u ) timestampSecs else now
1503+
1504+ val newOnchain = OnchainActivity (
1505+ id = item.id,
1506+ txType = if (item.txType == " sent" ) PaymentType .SENT else PaymentType .RECEIVED ,
1507+ txId = txId,
1508+ value = item.value.toULong(),
1509+ fee = (item.fee ? : 0 ).toULong(),
1510+ feeRate = (item.feeRate ? : 1 ).toULong(),
1511+ address = item.address ? : " " ,
1512+ timestamp = activityTimestamp,
1513+ confirmed = item.confirmed ? : false ,
1514+ isBoosted = item.isBoosted ? : false ,
1515+ boostTxIds = emptyList(),
1516+ isTransfer = item.isTransfer ? : false ,
1517+ confirmTimestamp = item.confirmTimestamp?.let { (it / MS_PER_SEC ).toULong() },
1518+ channelId = item.channelId,
1519+ transferTxId = item.transferTxId,
1520+ doesExist = item.exists ? : true ,
1521+ createdAt = activityTimestamp,
1522+ updatedAt = activityTimestamp,
1523+ seenAt = now,
1524+ )
1525+
1526+ activityRepo.upsertActivity(Activity .Onchain (newOnchain))
1527+ .onSuccess {
1528+ createdCount++
1529+
1530+ item.boostedParents?.takeIf { it.isNotEmpty() }?.let { parents ->
1531+ applyBoostedParents(parents, txId)
1532+ }
1533+ }
14781534 .onFailure { e ->
14791535 Logger .error(
1480- " Failed to update onchain activity metadata for $txId : $e " ,
1536+ " Failed to create onchain activity for unsupported address $txId : $e " ,
14811537 e,
14821538 context = TAG
14831539 )
14841540 }
14851541 }
14861542 }
1543+
1544+ if (updatedCount > 0 || createdCount > 0 ) {
1545+ Logger .info(
1546+ " Applied metadata to $updatedCount onchain activities, created $createdCount for unsupported addresses" ,
1547+ context = TAG
1548+ )
1549+ }
14871550 }
14881551
14891552 @Suppress(" LongMethod" , " CyclomaticComplexMethod" , " NestedBlockDepth" )
0 commit comments