@@ -263,19 +263,40 @@ class TransferViewModel @Inject constructor(
263263 }
264264
265265 private suspend fun pollUntil (orderId : String , condition : (IBtOrder ) -> Boolean ): IBtOrder ? {
266+ var consecutiveErrors = 0
267+
266268 while (true ) {
267- val order = blocktankRepo.getOrder(orderId, refresh = true ).getOrNull()
268- if (order == null ) {
269- Logger .error(" Order not found: '$orderId '" , context = TAG )
270- return null
271- }
272- if (order.state2 == BtOrderState2 .EXPIRED ) {
273- Logger .error(" Order expired: '$orderId '" , context = TAG )
274- return null
275- }
276- if (condition(order)) {
277- return order
278- }
269+ blocktankRepo.getOrder(orderId, refresh = true ).fold(
270+ onSuccess = { order ->
271+ consecutiveErrors = 0
272+
273+ if (order == null ) {
274+ Logger .error(" Order not found: '$orderId '" , context = TAG )
275+ return null
276+ }
277+ if (order.state2 == BtOrderState2 .EXPIRED ) {
278+ Logger .error(" Order expired: '$orderId '" , context = TAG )
279+ return null
280+ }
281+ if (condition(order)) {
282+ return order
283+ }
284+ },
285+ onFailure = {
286+ consecutiveErrors++
287+ Logger .warn(
288+ " Failed to fetch order '$orderId ' (attempt $consecutiveErrors /$MAX_CONSECUTIVE_ERRORS )" ,
289+ it,
290+ context = TAG
291+ )
292+
293+ if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS ) {
294+ Logger .error(" Too many consecutive errors polling order '$orderId ', giving up" , context = TAG )
295+ return null
296+ }
297+ }
298+ )
299+
279300 delay(POLL_INTERVAL_MS )
280301 }
281302 }
@@ -539,6 +560,7 @@ class TransferViewModel @Inject constructor(
539560 private const val TAG = " TransferViewModel"
540561 private const val MIN_STEP_DELAY_MS = 500L
541562 private const val POLL_INTERVAL_MS = 2_500L
563+ private const val MAX_CONSECUTIVE_ERRORS = 5
542564 const val LN_SETUP_STEP_0 = 0
543565 const val LN_SETUP_STEP_1 = 1
544566 const val LN_SETUP_STEP_2 = 2
0 commit comments