fix payment flow

This commit is contained in:
boufni95 2025-11-12 15:40:23 +00:00
parent 9b5d5e4105
commit 2b18f69873
2 changed files with 33 additions and 22 deletions

View file

@ -256,16 +256,15 @@ export class ReverseSwaps {
}
}
SubscribeToTransactionSwap = async (data: TransactionSwapData) => {
SubscribeToTransactionSwap = async (data: TransactionSwapData, swapDone: (result: { ok: true, txId: string } | { ok: false, error: string }) => void) => {
const webSocket = new ws(`${this.settings.getSettings().swapsSettings.boltzWebSocketUrl}/v2/ws`)
const subReq = { op: 'subscribe', channel: 'swap.update', args: [data.createdResponse.id] }
webSocket.on('open', () => {
webSocket.send(JSON.stringify(subReq))
})
return new Promise<string>((resolve, reject) => {
const done = (txId: string) => {
webSocket.close()
resolve(txId)
swapDone({ ok: true, txId })
}
webSocket.on('message', async (rawMsg) => {
try {
@ -273,12 +272,10 @@ export class ReverseSwaps {
} catch (err: any) {
this.log(ERROR, 'Error handling transaction WebSocket message', err.message)
webSocket.close()
reject(err)
swapDone({ ok: false, error: err.message })
return
}
})
})
}
handleSwapTransactionMessage = async (rawMsg: ws.RawData, data: TransactionSwapData, done: (txId: string) => void) => {

View file

@ -451,19 +451,33 @@ export default class {
preimage: Buffer.from(txSwap.preimage, 'hex'),
}
}
const swapPromise = this.swaps.reverseSwaps.SubscribeToTransactionSwap(data)
const payment = await this.PayInvoice(ctx.user_id, { amount: 0, invoice: txSwap.invoice }, app, req.swap_operation_id)
let txId = ""
let swapResult = { ok: false, error: "swap never completed" } as { ok: true, txId: string } | { ok: false, error: string }
this.swaps.reverseSwaps.SubscribeToTransactionSwap(data, result => {
swapResult = result
})
let payment: Types.PayInvoiceResponse
try {
txId = await swapPromise
await this.storage.paymentStorage.FinalizeTransactionSwap(req.swap_operation_id, txId)
payment = await this.PayInvoice(ctx.user_id, { amount: 0, invoice: txSwap.invoice }, app, req.swap_operation_id)
if (!swapResult.ok) {
this.log("invoice payment successful, but swap failed")
await this.storage.paymentStorage.FailTransactionSwap(req.swap_operation_id, swapResult.error)
throw new Error(swapResult.error)
}
this.log("swap completed successfully")
await this.storage.paymentStorage.FinalizeTransactionSwap(req.swap_operation_id, swapResult.txId)
} catch (err: any) {
if (swapResult.ok) {
this.log("failed to pay swap invoice, but swap completed successfully", swapResult.txId)
await this.storage.paymentStorage.FailTransactionSwap(req.swap_operation_id, err.message)
} else {
this.log("failed to pay swap invoice and swap failed", swapResult.error)
await this.storage.paymentStorage.FailTransactionSwap(req.swap_operation_id, swapResult.error)
}
throw err
}
const networkFeesTotal = txSwap.chain_fee_sats + txSwap.swap_fee_sats + payment.network_fee
return {
txId: txId,
txId: swapResult.txId,
network_fee: networkFeesTotal,
service_fee: payment.service_fee,
operation_id: payment.operation_id,