@@ -425,7 +425,15 @@ class BlocktankRepo @Inject constructor(
425425 ): Result <GiftClaimResult > = withContext(bgDispatcher) {
426426 runCatching {
427427 require(code.isNotBlank()) { " Gift code cannot be blank" }
428- require(amount > 0u ) { " Gift amount must be positive" }
428+
429+ if (amount == 0uL ) {
430+ Logger .warn(
431+ " Gift amount is 0 - proceeding anyway as backend may provide actual amount" ,
432+ context = TAG
433+ )
434+ }
435+
436+ Logger .debug(" Starting gift code claim: amount=$amount , timeout=$waitTimeout " , context = TAG )
429437
430438 lightningRepo.executeWhenNodeRunning(
431439 operationName = " claimGiftCode" ,
@@ -436,14 +444,25 @@ class BlocktankRepo @Inject constructor(
436444 val channels = lightningRepo.getChannelsAsync().getOrThrow()
437445 val maxInboundCapacity = channels.calculateRemoteBalance()
438446
439- if (maxInboundCapacity >= amount) {
447+ Logger .debug(
448+ " Liquidity check: maxInbound=$maxInboundCapacity , required=$amount " ,
449+ context = TAG
450+ )
451+
452+ if (amount > 0uL && maxInboundCapacity >= amount) {
453+ Logger .debug(" Sufficient liquidity available, claiming with existing channel" , context = TAG )
440454 Result .success(claimGiftCodeWithLiquidity(code))
441455 } else {
456+ if (amount == 0uL ) {
457+ Logger .debug(" Amount unknown (0), defaulting to channel opening path" , context = TAG )
458+ } else {
459+ Logger .debug(" Insufficient liquidity, opening new channel" , context = TAG )
460+ }
442461 Result .success(claimGiftCodeWithoutLiquidity(code, amount))
443462 }
444463 }.getOrThrow()
445464 }.onFailure {
446- Logger .error(" Failed to claim gift code" , it, context = TAG )
465+ Logger .error(" Failed to claim gift code: ${it.message} " , it, context = TAG )
447466 }
448467 }
449468
@@ -454,26 +473,39 @@ class BlocktankRepo @Inject constructor(
454473 expirySeconds = 3600u ,
455474 ).getOrThrow()
456475
457- ServiceQueue .CORE .background {
476+ Logger .debug(" Created invoice for gift code, requesting payment from LSP" , context = TAG )
477+
478+ val result = ServiceQueue .CORE .background {
458479 giftPay(invoice = invoice)
459480 }
460481
482+ Logger .debug(" Gift payment request completed: $result " , context = TAG )
483+
461484 return GiftClaimResult .SuccessWithLiquidity
462485 }
463486
464487 private suspend fun claimGiftCodeWithoutLiquidity (code : String , amount : ULong ): GiftClaimResult {
465488 val nodeId = lightningService.nodeId ? : throw ServiceError .NodeNotStarted ()
466489
490+ Logger .debug(" Creating gift order for code (insufficient liquidity)" , context = TAG )
491+
467492 val order = ServiceQueue .CORE .background {
468493 giftOrder(clientNodeId = nodeId, code = " blocktank-gift-code:$code " )
469494 }
470495
471- val orderId = checkNotNull(order.orderId) { " Order ID is null" }
496+ val orderId = checkNotNull(order.orderId) { " Order ID is null after gift order creation" }
497+ Logger .debug(" Gift order created: $orderId " , context = TAG )
472498
473499 val openedOrder = openChannel(orderId).getOrThrow()
500+ Logger .debug(" Channel opened for gift order: ${openedOrder.id} " , context = TAG )
501+
502+ val fundingTxId = openedOrder.channel?.fundingTx?.id
503+ if (fundingTxId == null ) {
504+ Logger .warn(" Channel opened but funding transaction ID is null" , context = TAG )
505+ }
474506
475507 return GiftClaimResult .SuccessWithoutLiquidity (
476- paymentHashOrTxId = openedOrder.channel?.fundingTx?.id ? : orderId,
508+ paymentHashOrTxId = fundingTxId ? : orderId,
477509 sats = amount.toLong(),
478510 invoice = openedOrder.payment?.bolt11Invoice?.request ? : " " ,
479511 code = code,
0 commit comments