diff --git a/src/services/lnd/swaps.ts b/src/services/lnd/swaps.ts index ab87309c..7b585f52 100644 --- a/src/services/lnd/swaps.ts +++ b/src/services/lnd/swaps.ts @@ -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 subReq = { op: 'subscribe', channel: 'swap.update', args: [data.createdResponse.id] } webSocket.on('open', () => { webSocket.send(JSON.stringify(subReq)) }) - return new Promise((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() - 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) => { diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 78d780ba..77740d57 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -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) { - 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 } 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,