11package to.bitkit.repositories
22
3+ import androidx.lifecycle.viewModelScope
4+ import com.synonym.bitkitcore.BtOrderState2
35import com.synonym.bitkitcore.CreateCjitOptions
46import com.synonym.bitkitcore.CreateOrderOptions
57import com.synonym.bitkitcore.IBtEstimateFeeResponse2
@@ -26,13 +28,15 @@ import kotlinx.coroutines.isActive
2628import kotlinx.coroutines.launch
2729import kotlinx.coroutines.withContext
2830import to.bitkit.data.CacheStore
31+ import to.bitkit.data.SettingsStore
2932import to.bitkit.di.BgDispatcher
3033import to.bitkit.env.Env
3134import to.bitkit.ext.nowTimestamp
3235import to.bitkit.services.CoreService
3336import to.bitkit.services.LightningService
3437import to.bitkit.utils.Logger
3538import to.bitkit.utils.ServiceError
39+ import to.bitkit.viewmodels.TransferViewModel
3640import java.math.BigDecimal
3741import javax.inject.Inject
3842import javax.inject.Named
@@ -47,6 +51,7 @@ class BlocktankRepo @Inject constructor(
4751 private val lightningService : LightningService ,
4852 private val currencyRepo : CurrencyRepo ,
4953 private val cacheStore : CacheStore ,
54+ private val settingsStore : SettingsStore ,
5055 @Named(" enablePolling" ) private val enablePolling : Boolean ,
5156) {
5257 private val repoScope = CoroutineScope (bgDispatcher + SupervisorJob ())
@@ -287,6 +292,75 @@ class BlocktankRepo @Inject constructor(
287292 }
288293 }
289294
295+ suspend fun watchOrder (orderId : String , frequencyMs : Long = 2_500) = withContext(bgDispatcher) {
296+ var isSettled = false
297+ var error: Throwable ? = null
298+
299+ Logger .debug(" Started to watch order '$orderId '" , context = TAG )
300+
301+ while (! isSettled && error == null ) {
302+ try {
303+ Logger .debug(" Refreshing order '$orderId '" )
304+ val order = getOrder(orderId, refresh = true ).getOrNull()
305+ if (order == null ) {
306+ error = Exception (" Order not found '$orderId '" )
307+ Logger .error(" Order not found '$orderId '" , context = TAG )
308+ break
309+ }
310+
311+ val step = updateOrder(order)
312+ settingsStore.update { it.copy(lightningSetupStep = step) }
313+ Logger .debug(" LN setup step: $step " )
314+
315+ if (order.state2 == BtOrderState2 .EXPIRED ) {
316+ error = Exception (" Order expired '$orderId '" )
317+ Logger .error(" Order expired '$orderId '" , context = TAG )
318+ break
319+ }
320+ if (step > 2 ) {
321+ Logger .debug(
322+ " Order settled, stopping watch order '$orderId '" ,
323+ context = TAG
324+ )
325+ isSettled = true
326+ break
327+ }
328+ } catch (e: Throwable ) {
329+ Logger .error(" Failed to watch order '$orderId '" , e, context = TAG )
330+ error = e
331+ break
332+ }
333+ delay(frequencyMs)
334+ }
335+ Logger .debug(" Stopped watching order '$orderId '" , context = TAG )
336+
337+ }
338+
339+ private suspend fun updateOrder (order : IBtOrder ): Int {
340+ var currentStep = 0
341+ if (order.channel != null ) {
342+ return 3
343+ }
344+
345+ when (order.state2) {
346+ BtOrderState2 .CREATED -> {
347+ currentStep = 0
348+ }
349+
350+ BtOrderState2 .PAID -> {
351+ currentStep = 1
352+ openChannel(order.id)
353+ }
354+
355+ BtOrderState2 .EXECUTED -> {
356+ currentStep = 2
357+ }
358+
359+ else -> Unit
360+ }
361+ return currentStep
362+ }
363+
290364 private suspend fun defaultCreateOrderOptions (clientBalanceSat : ULong ): CreateOrderOptions {
291365 val nodeId = lightningService.nodeId ? : throw ServiceError .NodeNotStarted
292366 val timestamp = nowTimestamp().toString()
0 commit comments