@@ -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,21 @@ class MigrationService @Inject constructor(
14501450 }
14511451 }
14521452
1453+ // Preserve higher value from backup (handles mixed input txs with unsupported addresses)
1454+ val backupValue = item.value.toULong()
1455+ if (backupValue > updated.value) {
1456+ updated = updated.copy(value = backupValue)
1457+ wasUpdated = true
1458+ }
1459+
1460+ // Preserve higher fee from backup
1461+ item.fee?.let { backupFee ->
1462+ if (backupFee.toULong() > updated.fee) {
1463+ updated = updated.copy(fee = backupFee.toULong())
1464+ wasUpdated = true
1465+ }
1466+ }
1467+
14531468 item.address?.let { address ->
14541469 if (address.isNotEmpty() && updated.address != address) {
14551470 updated = updated.copy(address = address)
@@ -1460,30 +1475,80 @@ class MigrationService @Inject constructor(
14601475 return if (wasUpdated) updated else null
14611476 }
14621477
1463- @Suppress(" CyclomaticComplexMethod" , " NestedBlockDepth" )
1478+ @Suppress(" CyclomaticComplexMethod" , " NestedBlockDepth" , " LongMethod " )
14641479 private suspend fun applyOnchainMetadata (items : List <RNActivityItem >) {
14651480 val onchainItems = items.filter { it.activityType == " onchain" }
1481+ var updatedCount = 0
1482+ var createdCount = 0
14661483
14671484 onchainItems.forEach { item ->
14681485 val txId = item.txId ? : item.id.takeIf { it.isNotEmpty() } ? : return @forEach
14691486
14701487 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- }
1488+ if (onchain != null ) {
1489+ // Activity exists, update it with metadata from backup
1490+ updateOnchainActivityMetadata(item, onchain)?.let { updated ->
1491+ activityRepo.updateActivity(updated.id, Activity .Onchain (updated))
1492+ .onSuccess { updatedCount++ }
1493+ .onFailure { e ->
1494+ Logger .error(
1495+ " Failed to update onchain activity metadata for $txId : $e " ,
1496+ e,
1497+ context = TAG
1498+ )
1499+ }
1500+ }
1501+ } else {
1502+ val timestampSecs = (item.timestamp / MS_PER_SEC ).toULong()
1503+ val now = (System .currentTimeMillis() / MS_PER_SEC ).toULong()
14751504
1476- updateOnchainActivityMetadata(item, onchain)?.let { updated ->
1477- activityRepo.updateActivity(updated.id, Activity .Onchain (updated))
1478- .onFailure { e ->
1479- Logger .error(
1480- " Failed to update onchain activity metadata for $txId : $e " ,
1481- e,
1482- context = TAG
1483- )
1505+ val activityTimestamp = if (timestampSecs > 0u ) timestampSecs else now
1506+
1507+ val newOnchain = OnchainActivity (
1508+ id = item.id,
1509+ txType = if (item.txType == " sent" ) PaymentType .SENT else PaymentType .RECEIVED ,
1510+ txId = txId,
1511+ value = item.value.toULong(),
1512+ fee = (item.fee ? : 0 ).toULong(),
1513+ feeRate = (item.feeRate ? : 1 ).toULong(),
1514+ address = item.address ? : " " ,
1515+ timestamp = activityTimestamp,
1516+ confirmed = item.confirmed ? : false ,
1517+ isBoosted = item.isBoosted ? : false ,
1518+ boostTxIds = emptyList(),
1519+ isTransfer = item.isTransfer ? : false ,
1520+ confirmTimestamp = item.confirmTimestamp?.let { (it / MS_PER_SEC ).toULong() },
1521+ channelId = item.channelId,
1522+ transferTxId = item.transferTxId,
1523+ doesExist = item.exists ? : true ,
1524+ createdAt = activityTimestamp,
1525+ updatedAt = activityTimestamp,
1526+ seenAt = now,
1527+ )
1528+
1529+ runCatching {
1530+ activityRepo.upsertActivity(Activity .Onchain (newOnchain))
1531+ createdCount++
1532+
1533+ item.boostedParents?.takeIf { it.isNotEmpty() }?.let { parents ->
1534+ applyBoostedParents(parents, txId)
14841535 }
1536+ }.onFailure { e ->
1537+ Logger .error(
1538+ " Failed to create onchain activity for unsupported address $txId : $e " ,
1539+ e,
1540+ context = TAG
1541+ )
1542+ }
14851543 }
14861544 }
1545+
1546+ if (updatedCount > 0 || createdCount > 0 ) {
1547+ Logger .info(
1548+ " Applied metadata to $updatedCount onchain activities, created $createdCount for unsupported addresses" ,
1549+ context = TAG
1550+ )
1551+ }
14871552 }
14881553
14891554 @Suppress(" LongMethod" , " CyclomaticComplexMethod" , " NestedBlockDepth" )
0 commit comments