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,29 +256,26 @@ 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 webSocket = new ws(`${this.settings.getSettings().swapsSettings.boltzWebSocketUrl}/v2/ws`)
const subReq = { op: 'subscribe', channel: 'swap.update', args: [data.createdResponse.id] } const subReq = { op: 'subscribe', channel: 'swap.update', args: [data.createdResponse.id] }
webSocket.on('open', () => { webSocket.on('open', () => {
webSocket.send(JSON.stringify(subReq)) webSocket.send(JSON.stringify(subReq))
}) })
return new Promise<string>((resolve, reject) => { const done = (txId: string) => {
const done = (txId: string) => { webSocket.close()
swapDone({ ok: true, txId })
}
webSocket.on('message', async (rawMsg) => {
try {
await this.handleSwapTransactionMessage(rawMsg, data, done)
} catch (err: any) {
this.log(ERROR, 'Error handling transaction WebSocket message', err.message)
webSocket.close() webSocket.close()
resolve(txId) swapDone({ ok: false, error: err.message })
return
} }
webSocket.on('message', async (rawMsg) => {
try {
await this.handleSwapTransactionMessage(rawMsg, data, done)
} catch (err: any) {
this.log(ERROR, 'Error handling transaction WebSocket message', err.message)
webSocket.close()
reject(err)
return
}
})
}) })
} }
handleSwapTransactionMessage = async (rawMsg: ws.RawData, data: TransactionSwapData, done: (txId: string) => void) => { 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'), preimage: Buffer.from(txSwap.preimage, 'hex'),
} }
} }
const swapPromise = this.swaps.reverseSwaps.SubscribeToTransactionSwap(data) let swapResult = { ok: false, error: "swap never completed" } as { ok: true, txId: string } | { ok: false, error: string }
const payment = await this.PayInvoice(ctx.user_id, { amount: 0, invoice: txSwap.invoice }, app, req.swap_operation_id) this.swaps.reverseSwaps.SubscribeToTransactionSwap(data, result => {
let txId = "" swapResult = result
})
let payment: Types.PayInvoiceResponse
try { try {
txId = await swapPromise payment = await this.PayInvoice(ctx.user_id, { amount: 0, invoice: txSwap.invoice }, app, req.swap_operation_id)
await this.storage.paymentStorage.FinalizeTransactionSwap(req.swap_operation_id, txId) 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) { } catch (err: any) {
await this.storage.paymentStorage.FailTransactionSwap(req.swap_operation_id, err.message) 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 throw err
} }
const networkFeesTotal = txSwap.chain_fee_sats + txSwap.swap_fee_sats + payment.network_fee const networkFeesTotal = txSwap.chain_fee_sats + txSwap.swap_fee_sats + payment.network_fee
return { return {
txId: txId, txId: swapResult.txId,
network_fee: networkFeesTotal, network_fee: networkFeesTotal,
service_fee: payment.service_fee, service_fee: payment.service_fee,
operation_id: payment.operation_id, operation_id: payment.operation_id,