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,18 +121,22 @@ export class ReverseSwaps {
webSocket.send(JSON.stringify(subReq))
})
let txId = "", isDone = false
const done = () => {
const done = (failureReason?: string) => {
isDone = true
webSocket.close()
if (failureReason) {
swapDone({ ok: false, error: failureReason })
} else {
swapDone({ ok: true, txId })
}
}
webSocket.on('error', (err) => {
this.log(ERROR, 'Error in WebSocket', err.message)
})
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;
}
}

View file

@ -375,18 +375,22 @@ export class SubmarineSwaps {
webSocket.send(JSON.stringify(subReq))
})
let isDone = false
const done = () => {
const done = (failureReason?: string) => {
isDone = true
webSocket.close()
if (failureReason) {
swapDone({ ok: false, error: failureReason })
} else {
swapDone({ ok: true })
}
}
webSocket.on('error', (err) => {
this.log(ERROR, 'Error in WebSocket', err.message)
})
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;
}
}