handle failure cases

This commit is contained in:
boufni95 2026-02-04 18:31:37 +00:00
parent f262d46d1f
commit 23156986b1
2 changed files with 33 additions and 8 deletions

View file

@ -121,10 +121,14 @@ export class ReverseSwaps {
webSocket.send(JSON.stringify(subReq)) webSocket.send(JSON.stringify(subReq))
}) })
let txId = "", isDone = false let txId = "", isDone = false
const done = () => { const done = (failureReason?: string) => {
isDone = true isDone = true
webSocket.close() webSocket.close()
swapDone({ ok: true, txId }) if (failureReason) {
swapDone({ ok: false, error: failureReason })
} else {
swapDone({ ok: true, txId })
}
} }
webSocket.on('error', (err) => { webSocket.on('error', (err) => {
this.log(ERROR, 'Error in WebSocket', err.message) this.log(ERROR, 'Error in WebSocket', err.message)
@ -132,7 +136,7 @@ export class ReverseSwaps {
webSocket.on('close', () => { webSocket.on('close', () => {
if (!isDone) { if (!isDone) {
this.log(ERROR, 'WebSocket closed before swap was done'); 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) => { 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')); const msg = JSON.parse(rawMsg.toString('utf-8'));
if (msg.event !== 'update') { if (msg.event !== 'update') {
return; return;
@ -176,6 +180,15 @@ export class ReverseSwaps {
this.log('Transaction swap successful'); this.log('Transaction swap successful');
done() done()
return; 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;
} }
} }

View file

@ -375,10 +375,14 @@ export class SubmarineSwaps {
webSocket.send(JSON.stringify(subReq)) webSocket.send(JSON.stringify(subReq))
}) })
let isDone = false let isDone = false
const done = () => { const done = (failureReason?: string) => {
isDone = true isDone = true
webSocket.close() webSocket.close()
swapDone({ ok: true }) if (failureReason) {
swapDone({ ok: false, error: failureReason })
} else {
swapDone({ ok: true })
}
} }
webSocket.on('error', (err) => { webSocket.on('error', (err) => {
this.log(ERROR, 'Error in WebSocket', err.message) this.log(ERROR, 'Error in WebSocket', err.message)
@ -386,7 +390,7 @@ export class SubmarineSwaps {
webSocket.on('close', () => { webSocket.on('close', () => {
if (!isDone) { if (!isDone) {
this.log(ERROR, 'WebSocket closed before swap was done'); 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) => { 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')); const msg = JSON.parse(rawMsg.toString('utf-8'));
if (msg.event !== 'update') { if (msg.event !== 'update') {
return; return;
@ -426,6 +430,14 @@ export class SubmarineSwaps {
this.log('Invoice swap successful'); this.log('Invoice swap successful');
closeWebSocket() closeWebSocket()
return; 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;
} }
} }