@@ -427,6 +427,8 @@ class BlocktankRepo @Inject constructor(
427427 require(code.isNotBlank()) { " Gift code cannot be blank" }
428428 require(amount > 0u ) { " Gift amount must be positive" }
429429
430+ Logger .debug(" Starting gift code claim: amount=$amount , timeout=$waitTimeout " , context = TAG )
431+
430432 lightningRepo.executeWhenNodeRunning(
431433 operationName = " claimGiftCode" ,
432434 waitTimeout = waitTimeout,
@@ -436,9 +438,16 @@ class BlocktankRepo @Inject constructor(
436438 val channels = lightningRepo.getChannelsAsync().getOrThrow()
437439 val maxInboundCapacity = channels.calculateRemoteBalance()
438440
441+ Logger .debug(
442+ " Liquidity check: maxInbound=$maxInboundCapacity , required=$amount " ,
443+ context = TAG
444+ )
445+
439446 if (maxInboundCapacity >= amount) {
440- Result .success(claimGiftCodeWithLiquidity(code))
447+ Logger .debug(" Sufficient liquidity available, claiming with existing channel" , context = TAG )
448+ Result .success(claimGiftCodeWithLiquidity(code, amount))
441449 } else {
450+ Logger .debug(" Insufficient liquidity, opening new channel" , context = TAG )
442451 Result .success(claimGiftCodeWithoutLiquidity(code, amount))
443452 }
444453 }.getOrThrow()
@@ -447,33 +456,53 @@ class BlocktankRepo @Inject constructor(
447456 }
448457 }
449458
450- private suspend fun claimGiftCodeWithLiquidity (code : String ): GiftClaimResult {
459+ private suspend fun claimGiftCodeWithLiquidity (code : String , amount : ULong ): GiftClaimResult {
451460 val invoice = lightningRepo.createInvoice(
452461 amountSats = null ,
453462 description = " blocktank-gift-code:$code " ,
454463 expirySeconds = 3600u ,
455464 ).getOrThrow()
456465
457- ServiceQueue .CORE .background {
466+ Logger .debug(" Created invoice for gift code, requesting payment from LSP" , context = TAG )
467+
468+ val giftResponse = ServiceQueue .CORE .background {
458469 giftPay(invoice = invoice)
459470 }
460471
461- return GiftClaimResult .SuccessWithLiquidity
472+ Logger .debug(" Gift payment request completed: id=${giftResponse.id} " , context = TAG )
473+
474+ return GiftClaimResult .SuccessWithLiquidity (
475+ paymentHashOrTxId = giftResponse.bolt11PaymentId ? : giftResponse.id,
476+ sats = giftResponse.bolt11Payment?.paidSat?.toLong()
477+ ? : giftResponse.appliedGiftCode?.giftSat?.toLong()
478+ ? : amount.toLong(),
479+ invoice = invoice,
480+ code = code,
481+ )
462482 }
463483
464484 private suspend fun claimGiftCodeWithoutLiquidity (code : String , amount : ULong ): GiftClaimResult {
465485 val nodeId = lightningService.nodeId ? : throw ServiceError .NodeNotStarted ()
466486
487+ Logger .debug(" Creating gift order for code (insufficient liquidity)" , context = TAG )
488+
467489 val order = ServiceQueue .CORE .background {
468490 giftOrder(clientNodeId = nodeId, code = " blocktank-gift-code:$code " )
469491 }
470492
471- val orderId = checkNotNull(order.orderId) { " Order ID is null" }
493+ val orderId = checkNotNull(order.orderId) { " Order ID is null after gift order creation" }
494+ Logger .debug(" Gift order created: $orderId " , context = TAG )
472495
473496 val openedOrder = openChannel(orderId).getOrThrow()
497+ Logger .debug(" Channel opened for gift order: ${openedOrder.id} " , context = TAG )
498+
499+ val fundingTxId = openedOrder.channel?.fundingTx?.id
500+ if (fundingTxId == null ) {
501+ Logger .warn(" Channel opened but funding transaction ID is null" , context = TAG )
502+ }
474503
475504 return GiftClaimResult .SuccessWithoutLiquidity (
476- paymentHashOrTxId = openedOrder.channel?.fundingTx?.id ? : orderId,
505+ paymentHashOrTxId = fundingTxId ? : orderId,
477506 sats = amount.toLong(),
478507 invoice = openedOrder.payment?.bolt11Invoice?.request ? : " " ,
479508 code = code,
@@ -498,11 +527,22 @@ data class BlocktankState(
498527)
499528
500529sealed class GiftClaimResult {
501- object SuccessWithLiquidity : GiftClaimResult()
530+ abstract val paymentHashOrTxId: String
531+ abstract val sats: Long
532+ abstract val invoice: String
533+ abstract val code: String
534+
535+ data class SuccessWithLiquidity (
536+ override val paymentHashOrTxId : String ,
537+ override val sats : Long ,
538+ override val invoice : String ,
539+ override val code : String ,
540+ ) : GiftClaimResult()
541+
502542 data class SuccessWithoutLiquidity (
503- val paymentHashOrTxId : String ,
504- val sats : Long ,
505- val invoice : String ,
506- val code : String ,
543+ override val paymentHashOrTxId : String ,
544+ override val sats : Long ,
545+ override val invoice : String ,
546+ override val code : String ,
507547 ) : GiftClaimResult()
508548}
0 commit comments