Skip to content

Commit f76fc40

Browse files
committed
Merge branch 'master' into feat/multiple-addresses-types
# Conflicts: # app/src/main/java/to/bitkit/services/MigrationService.kt
2 parents aad545d + 6b94cef commit f76fc40

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package to.bitkit.repositories
22

33
import com.synonym.bitkitcore.Activity
44
import com.synonym.bitkitcore.ActivityFilter
5+
import com.synonym.bitkitcore.BtOrderState2
56
import com.synonym.bitkitcore.SortDirection
67
import kotlinx.coroutines.CoroutineDispatcher
78
import kotlinx.coroutines.flow.Flow
@@ -111,6 +112,15 @@ class TransferRepo @Inject constructor(
111112
if (channel != null && channel.isChannelReady) {
112113
markSettled(transfer.id)
113114
Logger.debug("Channel $channelId ready, settled transfer: ${transfer.id}", context = TAG)
115+
} else if (channelId == null && transfer.lspOrderId != null) {
116+
val order = blocktankRepo.getOrder(transfer.lspOrderId, refresh = false).getOrNull()
117+
if (order?.state2 == BtOrderState2.EXPIRED) {
118+
markSettled(transfer.id)
119+
Logger.info(
120+
"Order ${transfer.lspOrderId} expired, settled transfer: ${transfer.id}",
121+
context = TAG,
122+
)
123+
}
114124
}
115125
}
116126

app/src/main/java/to/bitkit/services/MigrationService.kt

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.datastore.preferences.core.stringPreferencesKey
88
import androidx.datastore.preferences.preferencesDataStore
99
import com.synonym.bitkitcore.Activity
1010
import com.synonym.bitkitcore.ActivityTags
11+
import com.synonym.bitkitcore.BtOrderState2
1112
import com.synonym.bitkitcore.ClosedChannelDetails
1213
import com.synonym.bitkitcore.LightningActivity
1314
import com.synonym.bitkitcore.OnchainActivity
@@ -55,7 +56,6 @@ import to.bitkit.models.TransactionSpeed
5556
import to.bitkit.models.TransferType
5657
import to.bitkit.models.WidgetType
5758
import to.bitkit.models.WidgetWithPosition
58-
import to.bitkit.models.safe
5959
import to.bitkit.models.toSettingsString
6060
import to.bitkit.models.widget.BlocksPreferences
6161
import 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,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class WalletViewModel @Inject constructor(
301301
}
302302
walletRepo.setWalletExistsState()
303303
connectMigrationPeers()
304+
migrationService.cleanupInvalidMigrationTransfers()
304305
walletRepo.syncBalances()
305306
if (_restoreState.value.isIdle()) {
306307
walletRepo.refreshBip21()

0 commit comments

Comments
 (0)