@@ -3039,82 +3039,15 @@ class LiquidRepository(
30393039 getCachedLightningPaymentResolution(executionPlan.paymentInput, executionPlan.requestedAmountSats)
30403040 ? : resolveBoltzLightningPaymentInput(executionPlan.paymentInput, executionPlan.requestedAmountSats)
30413041 try {
3042- if (resolvedPayment.fetchedInvoice != null ) {
3043- val refundDetails = allocateBoltzSubmarineRefundDetails()
3044- logBoltzTrace(
3045- " prepare_rest_submarine" ,
3046- trace.copy(backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE .name, source = " rest" ),
3047- " invoice" to summarizeValue(resolvedPayment.fetchedInvoice),
3048- " refundAddress" to summarizeValue(refundDetails.refundAddress),
3049- )
3050- logDebug(
3051- " prepareLightningPaymentExecutionPlan using cached resolution " +
3052- " requestKey=$requestKey amount=${executionPlan.paymentAmountSats} " +
3053- " refund=${summarizeValue(refundDetails.refundAddress)} " ,
3054- )
3055- val response =
3056- boltzProvider.createLegacySubmarineSwap(
3057- invoice = resolvedPayment.fetchedInvoice,
3058- refundPublicKey = refundDetails.refundPublicKey,
3059- )
3060- val paymentAmountSats =
3061- executionPlan.requestedAmountSats.takeIf { it != null && it > 0L } ? : executionPlan.paymentAmountSats
3062- val lockupAmountSats = maxOf(response.expectedAmount, safeAddSats(paymentAmountSats, 0L ))
3063- val swapFeeSats = (lockupAmountSats - paymentAmountSats).coerceAtLeast(0L )
3064- val session =
3065- PendingLightningPaymentSession (
3066- swapId = response.id,
3067- backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE ,
3068- requestKey = requestKey,
3069- paymentInput = executionPlan.paymentInput,
3070- lockupAddress = response.address,
3071- lockupAmountSats = lockupAmountSats,
3072- refundAddress = refundDetails.refundAddress,
3073- requestedAmountSats = executionPlan.requestedAmountSats,
3074- resolvedPaymentInput = resolvedPayment.paymentInput,
3075- fetchedInvoice = resolvedPayment.fetchedInvoice,
3076- refundPublicKey = refundDetails.refundPublicKey,
3077- paymentAmountSats = paymentAmountSats,
3078- swapFeeSats = swapFeeSats,
3079- phase = PendingLightningPaymentPhase .PREPARED ,
3080- status = " Prepared Lightning payment. Awaiting funding." ,
3081- )
3082- persistPreparedLightningPaymentSession(session)
3083- logBoltzTrace(
3084- " prepared" ,
3085- trace.copy(
3086- swapId = response.id,
3087- backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE .name,
3088- source = " rest" ,
3089- ),
3090- " elapsedMs" to boltzElapsedMs(traceStartedAt),
3091- " lockupAmountSats" to lockupAmountSats,
3092- " paymentAmountSats" to paymentAmountSats,
3093- " swapFeeSats" to swapFeeSats,
3094- )
3095- return @withContext LightningPaymentExecutionPlan .BoltzSwap (
3096- paymentInput = executionPlan.paymentInput,
3097- backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE ,
3098- requestKey = requestKey,
3099- resolvedPaymentInput = resolvedPayment.paymentInput,
3100- requestedAmountSats = executionPlan.requestedAmountSats,
3101- swapId = response.id,
3102- lockupAddress = response.address,
3103- lockupAmountSats = lockupAmountSats,
3104- refundAddress = refundDetails.refundAddress,
3105- fetchedInvoice = resolvedPayment.fetchedInvoice,
3106- refundPublicKey = refundDetails.refundPublicKey,
3107- paymentAmountSats = paymentAmountSats,
3108- swapFeeSats = swapFeeSats,
3109- )
3110- }
3042+ val lwkPaymentInput = resolvedPayment.fetchedInvoice ? : executionPlan.resolvedPaymentInput
31113043 val refundAddress = getNewAddress() ? : throw Exception (" No Liquid refund address available" )
3112- val payment = createLightningPayment(executionPlan.resolvedPaymentInput )
3044+ val payment = createLightningPayment(lwkPaymentInput )
31133045 logBoltzTrace(
31143046 " prepare_lwk" ,
31153047 trace.copy(backend = LightningPaymentBackend .LWK_PREPARE_PAY .name, source = " lwk" ),
31163048 " refundAddress" to summarizeValue(refundAddress),
3117- " payment" to summarizeValue(executionPlan.resolvedPaymentInput),
3049+ " payment" to summarizeValue(lwkPaymentInput),
3050+ " hasFetchedInvoice" to (resolvedPayment.fetchedInvoice != null ),
31183051 )
31193052 return @withContext withBoltzOperationLock(operation = " prepareLightningPaymentExecutionPlan" ) {
31203053 val walletId = currentWalletId ? : throw Exception (" Wallet not loaded" )
@@ -3143,6 +3076,7 @@ class LiquidRepository(
31433076 snapshot = snapshot,
31443077 requestedAmountSats = executionPlan.requestedAmountSats,
31453078 resolvedPaymentInput = executionPlan.resolvedPaymentInput,
3079+ fetchedInvoice = resolvedPayment.fetchedInvoice,
31463080 paymentAmountSats = paymentAmountSats,
31473081 swapFeeSats = effectiveSwapFeeSats,
31483082 phase = PendingLightningPaymentPhase .PREPARED ,
@@ -3160,6 +3094,7 @@ class LiquidRepository(
31603094 " lockupAmountSats" to lockupAmountSats,
31613095 " paymentAmountSats" to paymentAmountSats,
31623096 " swapFeeSats" to effectiveSwapFeeSats,
3097+ " hasFetchedInvoice" to (resolvedPayment.fetchedInvoice != null ),
31633098 )
31643099 LightningPaymentExecutionPlan .BoltzSwap (
31653100 paymentInput = executionPlan.paymentInput,
@@ -3171,7 +3106,7 @@ class LiquidRepository(
31713106 lockupAddress = response.uriAddress().toString(),
31723107 lockupAmountSats = lockupAmountSats,
31733108 refundAddress = refundAddress,
3174- fetchedInvoice = null ,
3109+ fetchedInvoice = resolvedPayment.fetchedInvoice ,
31753110 refundPublicKey = null ,
31763111 paymentAmountSats = paymentAmountSats,
31773112 swapFeeSats = effectiveSwapFeeSats,
@@ -3192,6 +3127,22 @@ class LiquidRepository(
31923127 amountSats = direct.amountSats,
31933128 )
31943129 } catch (e: Exception ) {
3130+ if (resolvedPayment.fetchedInvoice != null && e !is LwkException .MagicRoutingHint ) {
3131+ logBoltzTrace(
3132+ " lwk_failed_falling_back_to_rest" ,
3133+ trace,
3134+ level = BoltzTraceLevel .WARN ,
3135+ throwable = e,
3136+ " elapsedMs" to boltzElapsedMs(traceStartedAt),
3137+ )
3138+ return @withContext prepareRestSubmarineSwapFallback(
3139+ executionPlan = executionPlan,
3140+ resolvedPayment = resolvedPayment,
3141+ requestKey = requestKey,
3142+ trace = trace,
3143+ traceStartedAt = traceStartedAt,
3144+ )
3145+ }
31953146 logBoltzTrace(
31963147 " failed" ,
31973148 trace,
@@ -3205,6 +3156,85 @@ class LiquidRepository(
32053156 }
32063157 }
32073158
3159+ private suspend fun prepareRestSubmarineSwapFallback (
3160+ executionPlan : LightningPaymentExecutionPlan .BoltzQuote ,
3161+ resolvedPayment : ResolvedLightningPaymentInput ,
3162+ requestKey : String ,
3163+ trace : BoltzTraceContext ,
3164+ traceStartedAt : Long ,
3165+ ): LightningPaymentExecutionPlan .BoltzSwap {
3166+ val fetchedInvoice = resolvedPayment.fetchedInvoice
3167+ ? : throw Exception (" REST fallback requires a fetched invoice" )
3168+ val refundDetails = allocateBoltzSubmarineRefundDetails()
3169+ logBoltzTrace(
3170+ " prepare_rest_submarine_fallback" ,
3171+ trace.copy(backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE .name, source = " rest" ),
3172+ " invoice" to summarizeValue(fetchedInvoice),
3173+ " refundAddress" to summarizeValue(refundDetails.refundAddress),
3174+ )
3175+ val response =
3176+ boltzProvider.createLegacySubmarineSwap(
3177+ invoice = fetchedInvoice,
3178+ refundPublicKey = refundDetails.refundPublicKey,
3179+ )
3180+ val paymentAmountSats =
3181+ executionPlan.requestedAmountSats.takeIf { it != null && it > 0L }
3182+ ? : executionPlan.paymentAmountSats
3183+ val lockupAmountSats = maxOf(response.expectedAmount, safeAddSats(paymentAmountSats, 0L ))
3184+ val swapFeeSats = (lockupAmountSats - paymentAmountSats).coerceAtLeast(0L )
3185+ val session =
3186+ PendingLightningPaymentSession (
3187+ swapId = response.id,
3188+ backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE ,
3189+ requestKey = requestKey,
3190+ paymentInput = executionPlan.paymentInput,
3191+ lockupAddress = response.address,
3192+ lockupAmountSats = lockupAmountSats,
3193+ refundAddress = refundDetails.refundAddress,
3194+ requestedAmountSats = executionPlan.requestedAmountSats,
3195+ resolvedPaymentInput = resolvedPayment.paymentInput,
3196+ fetchedInvoice = fetchedInvoice,
3197+ refundPublicKey = refundDetails.refundPublicKey,
3198+ paymentAmountSats = paymentAmountSats,
3199+ swapFeeSats = swapFeeSats,
3200+ phase = PendingLightningPaymentPhase .PREPARED ,
3201+ status = " Prepared Lightning payment. Awaiting funding." ,
3202+ boltzClaimPublicKey = response.claimPublicKey,
3203+ timeoutBlockHeight = response.timeoutBlockHeight,
3204+ swapTree = response.swapTree,
3205+ blindingKey = response.blindingKey,
3206+ )
3207+ persistPreparedLightningPaymentSession(session)
3208+ logBoltzTrace(
3209+ " prepared" ,
3210+ trace.copy(
3211+ swapId = response.id,
3212+ backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE .name,
3213+ source = " rest" ,
3214+ ),
3215+ " elapsedMs" to boltzElapsedMs(traceStartedAt),
3216+ " lockupAmountSats" to lockupAmountSats,
3217+ " paymentAmountSats" to paymentAmountSats,
3218+ " swapFeeSats" to swapFeeSats,
3219+ " timeoutBlockHeight" to response.timeoutBlockHeight,
3220+ )
3221+ return LightningPaymentExecutionPlan .BoltzSwap (
3222+ paymentInput = executionPlan.paymentInput,
3223+ backend = LightningPaymentBackend .BOLTZ_REST_SUBMARINE ,
3224+ requestKey = requestKey,
3225+ resolvedPaymentInput = resolvedPayment.paymentInput,
3226+ requestedAmountSats = executionPlan.requestedAmountSats,
3227+ swapId = response.id,
3228+ lockupAddress = response.address,
3229+ lockupAmountSats = lockupAmountSats,
3230+ refundAddress = refundDetails.refundAddress,
3231+ fetchedInvoice = fetchedInvoice,
3232+ refundPublicKey = refundDetails.refundPublicKey,
3233+ paymentAmountSats = paymentAmountSats,
3234+ swapFeeSats = swapFeeSats,
3235+ )
3236+ }
3237+
32083238 suspend fun resolveLightningPaymentPreview (
32093239 paymentInput : String ,
32103240 requestedAmountSats : Long? ,
@@ -3226,6 +3256,7 @@ class LiquidRepository(
32263256 estimatedLockupAmountSats = context.estimatedLockupAmountSats,
32273257 paymentAmountSats = context.paymentAmountSats,
32283258 swapFeeSats = context.estimatedSwapFeeSats,
3259+ refundAddress = _liquidState .value.currentAddress,
32293260 )
32303261 val preview =
32313262 buildLightningPreviewFromExecutionPlan(
0 commit comments