@@ -8,6 +8,7 @@ import androidx.datastore.preferences.core.stringPreferencesKey
88import androidx.datastore.preferences.preferencesDataStore
99import com.synonym.bitkitcore.Activity
1010import com.synonym.bitkitcore.ActivityTags
11+ import com.synonym.bitkitcore.BtOrderState2
1112import com.synonym.bitkitcore.ClosedChannelDetails
1213import com.synonym.bitkitcore.LightningActivity
1314import com.synonym.bitkitcore.OnchainActivity
@@ -55,7 +56,6 @@ import to.bitkit.models.TransactionSpeed
5556import to.bitkit.models.TransferType
5657import to.bitkit.models.WidgetType
5758import to.bitkit.models.WidgetWithPosition
58- import to.bitkit.models.safe
5959import to.bitkit.models.toSettingsString
6060import to.bitkit.models.widget.BlocksPreferences
6161import to.bitkit.models.widget.FactsPreferences
@@ -97,6 +97,7 @@ class MigrationService @Inject constructor(
9797 private const val RN_PENDING_BOOSTS_KEY = " rnPendingBoosts"
9898 private const val RN_CHANNEL_RECOVERY_CHECKED_KEY = " rnChannelRecoveryChecked"
9999 private const val RN_DID_ATTEMPT_PEER_RECOVERY_KEY = " rnDidAttemptMigrationPeerRecovery"
100+ private const val RN_DID_CLEANUP_INVALID_TRANSFERS_KEY = " didCleanupInvalidMigrationTransfers"
100101 private const val OPENING_CURLY_BRACE = " {"
101102 private const val MMKV_ROOT = " persist:root"
102103 private const val RN_WALLET_NAME = " wallet0"
@@ -1384,6 +1385,43 @@ class MigrationService @Inject constructor(
13841385 }.getOrDefault(emptyList())
13851386 }
13861387
1388+ suspend fun cleanupInvalidMigrationTransfers () {
1389+ val key = stringPreferencesKey(RN_DID_CLEANUP_INVALID_TRANSFERS_KEY )
1390+ if (rnMigrationStore.data.first()[key] == " true" ) return
1391+ if (! isRnMigrationCompleted()) return
1392+
1393+ val transfers = transferDao.getActiveTransfers().first()
1394+ .filter { it.type == TransferType .TO_SPENDING && it.lspOrderId != null }
1395+
1396+ if (transfers.isEmpty()) {
1397+ rnMigrationStore.edit { it[key] = " true" }
1398+ return
1399+ }
1400+
1401+ val orderIds = transfers.mapNotNull { it.lspOrderId }
1402+ val orders = runCatching {
1403+ coreService.blocktank.orders(orderIds = orderIds, filter = null , refresh = true )
1404+ }.onFailure {
1405+ Logger .warn(" Cannot cleanup migration transfers: Blocktank unreachable" , it, context = TAG )
1406+ }.getOrNull() ? : return
1407+
1408+ val now = System .currentTimeMillis() / MS_PER_SEC
1409+ for (transfer in transfers) {
1410+ val order = orders.find { it.id == transfer.lspOrderId } ? : continue
1411+ if (order.state2 != BtOrderState2 .PAID ) {
1412+ transferDao.markSettled(transfer.id, now)
1413+ Logger .info(
1414+ " Cleanup: settled invalid migration transfer ${transfer.id} " +
1415+ " for order ${transfer.lspOrderId} (state: ${order.state2} )" ,
1416+ context = TAG ,
1417+ )
1418+ }
1419+ }
1420+
1421+ rnMigrationStore.edit { it[key] = " true" }
1422+ Logger .info(" Migration transfer cleanup completed" , context = TAG )
1423+ }
1424+
13871425 suspend fun cleanupAfterMigration () {
13881426 clearPersistedMigrationData()
13891427 setNeedsPostMigrationSync(false )
@@ -1598,11 +1636,17 @@ class MigrationService @Inject constructor(
15981636 null
15991637 }
16001638
1601- order.state2 == com.synonym.bitkitcore.BtOrderState2 .EXECUTED -> null
1639+ order.state2 != BtOrderState2 .PAID -> {
1640+ Logger .debug(
1641+ " Skipping order $orderId with state ${order.state2} for transfer creation" ,
1642+ context = TAG ,
1643+ )
1644+ null
1645+ }
16021646 else -> TransferEntity (
16031647 id = txId,
16041648 type = TransferType .TO_SPENDING ,
1605- amountSats = ( order.clientBalanceSat.safe() + order.feeSat.safe()) .toLong(),
1649+ amountSats = order.clientBalanceSat.toLong(),
16061650 channelId = null ,
16071651 fundingTxId = null ,
16081652 lspOrderId = orderId,
0 commit comments