Skip to content

Commit 1065fac

Browse files
committed
fix: move regtest mining to viewmodel
1 parent fd95f09 commit 1065fac

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

app/src/main/java/to/bitkit/ui/screens/transfer/SettingUpScreen.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ import androidx.compose.ui.res.stringResource
2020
import androidx.compose.ui.tooling.preview.Devices.NEXUS_5
2121
import androidx.compose.ui.tooling.preview.Preview
2222
import androidx.compose.ui.unit.dp
23-
import com.synonym.bitkitcore.regtestMine
24-
import kotlinx.coroutines.delay
25-
import org.lightningdevkit.ldknode.Network
2623
import to.bitkit.R
27-
import to.bitkit.env.Env
2824
import to.bitkit.ui.appViewModel
2925
import to.bitkit.ui.components.BodyM
3026
import to.bitkit.ui.components.Display
@@ -51,20 +47,7 @@ fun SettingUpScreen(
5147
val lightningSetupStep by viewModel.lightningSetupStep.collectAsState()
5248

5349
LaunchedEffect(Unit) {
54-
Logger.debug("SettingUp view appeared - TransferViewModel is handling order updates")
55-
56-
// Auto-mine a block on regtest after a 5-seconds delay
57-
if (Env.network == Network.REGTEST) {
58-
delay(5000)
59-
60-
try {
61-
Logger.debug("Auto-mining a block", context = "SettingUpScreen")
62-
regtestMine(1u)
63-
Logger.debug("Successfully mined a block", context = "SettingUpScreen")
64-
} catch (e: Throwable) {
65-
Logger.error("Failed to mine block: $e", context = "SettingUpScreen")
66-
}
67-
}
50+
Logger.debug("SettingUp screen appeared - TransferViewModel is handling order updates")
6851
}
6952

7053
// Effect to disable new transaction sheet for channel purchase

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import com.synonym.bitkitcore.BtOrderState2
77
import com.synonym.bitkitcore.IBtOrder
8+
import com.synonym.bitkitcore.regtestMine
89
import dagger.hilt.android.lifecycle.HiltViewModel
910
import dagger.hilt.android.qualifiers.ApplicationContext
1011
import kotlinx.coroutines.Job
@@ -25,9 +26,11 @@ import kotlinx.coroutines.isActive
2526
import kotlinx.coroutines.launch
2627
import kotlinx.coroutines.withTimeoutOrNull
2728
import org.lightningdevkit.ldknode.ChannelDetails
29+
import org.lightningdevkit.ldknode.Network
2830
import to.bitkit.R
2931
import to.bitkit.data.CacheStore
3032
import to.bitkit.data.SettingsStore
33+
import to.bitkit.env.Env
3134
import to.bitkit.ext.amountOnClose
3235
import to.bitkit.models.Toast
3336
import to.bitkit.models.TransactionSpeed
@@ -224,7 +227,19 @@ class TransferViewModel @Inject constructor(
224227
// Step 0: Starting
225228
settingsStore.update { it.copy(lightningSetupStep = LN_SETUP_STEP_0) }
226229
Logger.debug("LN setup step: $LN_SETUP_STEP_0", context = TAG)
227-
delay(MIN_STEP_DELAY_MS)
230+
delay(MS_DELAY_STEP)
231+
232+
// Auto-mine on regtest: delay to let tx propagate, then mine
233+
if (Env.network == Network.REGTEST) {
234+
delay(MS_DELAY_REGTEST_MINE)
235+
try {
236+
Logger.debug("Auto-mining a block for order: '$orderId'", context = TAG)
237+
regtestMine(1u)
238+
Logger.debug("Successfully mined a block", context = TAG)
239+
} catch (e: Throwable) {
240+
Logger.warn("Failed to mine block", e, context = TAG)
241+
}
242+
}
228243

229244
// Poll until payment is confirmed (order state becomes PAID or EXECUTED)
230245
val paidOrder = pollUntil(orderId) { order ->
@@ -234,15 +249,15 @@ class TransferViewModel @Inject constructor(
234249
// Step 1: Payment confirmed
235250
settingsStore.update { it.copy(lightningSetupStep = LN_SETUP_STEP_1) }
236251
Logger.debug("LN setup step: $LN_SETUP_STEP_1", context = TAG)
237-
delay(MIN_STEP_DELAY_MS)
252+
delay(MS_DELAY_STEP)
238253

239254
// Try to open channel (idempotent - safe to call multiple times)
240255
blocktankRepo.openChannel(paidOrder.id)
241256

242257
// Step 2: Channel opening requested
243258
settingsStore.update { it.copy(lightningSetupStep = LN_SETUP_STEP_2) }
244259
Logger.debug("LN setup step: $LN_SETUP_STEP_2", context = TAG)
245-
delay(MIN_STEP_DELAY_MS)
260+
delay(MS_DELAY_STEP)
246261

247262
// Poll until channel is ready (EXECUTED state or channel has state)
248263
pollUntil(orderId) { order ->
@@ -278,7 +293,7 @@ class TransferViewModel @Inject constructor(
278293
if (condition(order)) {
279294
return order
280295
}
281-
delay(POLL_INTERVAL_MS)
296+
delay(MS_INTERVAL_POLL)
282297
}
283298
}
284299

@@ -492,8 +507,9 @@ class TransferViewModel @Inject constructor(
492507

493508
companion object {
494509
private const val TAG = "TransferViewModel"
495-
private const val MIN_STEP_DELAY_MS = 500L
496-
private const val POLL_INTERVAL_MS = 2_500L
510+
private const val MS_DELAY_STEP = 500L
511+
private const val MS_INTERVAL_POLL = 2_500L
512+
private const val MS_DELAY_REGTEST_MINE = 5_000L
497513
const val LN_SETUP_STEP_0 = 0
498514
const val LN_SETUP_STEP_1 = 1
499515
const val LN_SETUP_STEP_2 = 2

0 commit comments

Comments
 (0)