handle failure cases
This commit is contained in:
parent
f262d46d1f
commit
23156986b1
2 changed files with 33 additions and 8 deletions
|
|
@ -121,18 +121,22 @@ 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()
|
||||||
|
if (failureReason) {
|
||||||
|
swapDone({ ok: false, error: failureReason })
|
||||||
|
} else {
|
||||||
swapDone({ ok: true, txId })
|
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)
|
||||||
})
|
})
|
||||||
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -375,18 +375,22 @@ 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()
|
||||||
|
if (failureReason) {
|
||||||
|
swapDone({ ok: false, error: failureReason })
|
||||||
|
} else {
|
||||||
swapDone({ ok: true })
|
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)
|
||||||
})
|
})
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue