diff --git a/src/services/lnd/swaps/reverseSwaps.ts b/src/services/lnd/swaps/reverseSwaps.ts index f0a5bc03..1486464a 100644 --- a/src/services/lnd/swaps/reverseSwaps.ts +++ b/src/services/lnd/swaps/reverseSwaps.ts @@ -121,10 +121,14 @@ export class ReverseSwaps { webSocket.send(JSON.stringify(subReq)) }) let txId = "", isDone = false - const done = () => { + const done = (failureReason?: string) => { isDone = true webSocket.close() - swapDone({ ok: true, txId }) + if (failureReason) { + swapDone({ ok: false, error: failureReason }) + } else { + swapDone({ ok: true, txId }) + } } webSocket.on('error', (err) => { this.log(ERROR, 'Error in WebSocket', err.message) @@ -132,7 +136,7 @@ export class ReverseSwaps { webSocket.on('close', () => { if (!isDone) { this.log(ERROR, 'WebSocket closed before swap was done'); - swapDone({ ok: false, error: 'WebSocket closed before swap was done' }) + done('WebSocket closed before swap was done') } }) webSocket.on('message', async (rawMsg) => { @@ -151,7 +155,7 @@ export class ReverseSwaps { }) } - handleSwapTransactionMessage = async (rawMsg: ws.RawData, data: TransactionSwapData, done: () => void) => { + handleSwapTransactionMessage = async (rawMsg: ws.RawData, data: TransactionSwapData, done: (failureReason?: string) => void) => { const msg = JSON.parse(rawMsg.toString('utf-8')); if (msg.event !== 'update') { return; @@ -176,6 +180,15 @@ export class ReverseSwaps { this.log('Transaction swap successful'); done() return; + case 'invoice.expired': + case 'swap.expired': + case 'transaction.failed': + done(`swap ${data.createdResponse.id} failed with status ${msg.args[0].status}`) + return; + default: + this.log('Unknown swap transaction WebSocket message', msg) + return; + } } diff --git a/src/services/lnd/swaps/submarineSwaps.ts b/src/services/lnd/swaps/submarineSwaps.ts index bf223b3e..760ca57e 100644 --- a/src/services/lnd/swaps/submarineSwaps.ts +++ b/src/services/lnd/swaps/submarineSwaps.ts @@ -375,10 +375,14 @@ export class SubmarineSwaps { webSocket.send(JSON.stringify(subReq)) }) let isDone = false - const done = () => { + const done = (failureReason?: string) => { isDone = true webSocket.close() - swapDone({ ok: true }) + if (failureReason) { + swapDone({ ok: false, error: failureReason }) + } else { + swapDone({ ok: true }) + } } webSocket.on('error', (err) => { this.log(ERROR, 'Error in WebSocket', err.message) @@ -386,7 +390,7 @@ export class SubmarineSwaps { webSocket.on('close', () => { if (!isDone) { this.log(ERROR, 'WebSocket closed before swap was done'); - swapDone({ ok: false, error: 'WebSocket closed before swap was done' }) + done('WebSocket closed before swap was done') } }) webSocket.on('message', async (rawMsg) => { @@ -403,7 +407,7 @@ export class SubmarineSwaps { } } - handleSwapInvoiceMessage = async (rawMsg: ws.RawData, data: InvoiceSwapData, closeWebSocket: () => void, waitingTx: () => void) => { + handleSwapInvoiceMessage = async (rawMsg: ws.RawData, data: InvoiceSwapData, closeWebSocket: (failureReason?: string) => void, waitingTx: () => void) => { const msg = JSON.parse(rawMsg.toString('utf-8')); if (msg.event !== 'update') { return; @@ -426,6 +430,14 @@ export class SubmarineSwaps { this.log('Invoice swap successful'); closeWebSocket() return; + case 'swap.expired': + case 'transaction.lockupFailed': + case 'invoice.failedToPay': + closeWebSocket(`swap ${data.createdResponse.id} failed with status ${msg.args[0].status}`) + return; + default: + this.log('Unknown swap invoice WebSocket message', msg) + return; } }