fix: check for invoice fulfillment

This commit is contained in:
Rafael Taranto 2024-02-09 19:05:25 +00:00
parent 17701ecd3c
commit 3465173e1a

View file

@ -152,7 +152,6 @@ function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(wallet => {
@ -166,7 +165,7 @@ function sendCoins (account, tx, settings, operatorId) {
case 'ALREADY_PAID':
throw new Error('Transaction already exists!')
case 'FAILURE':
throw new Error('Transaction failed!')
throw new Error('Transaction failed!', JSON.stringify(result.errors))
case 'SUCCESS':
return '<galoy transaction>'
case 'PENDING':
@ -253,6 +252,25 @@ function newAddress (account, info, tx, settings, operatorId) {
})
}
function getInvoiceStatus (token, endpoint, address) {
const query = {
'operationName': 'lnInvoicePaymentStatus',
'query': `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
lnInvoicePaymentStatus(input: $input) {
status
}
}`,
'variables': {"input": {"paymentRequest": address}}
}
return request(query, token, endpoint)
.then(r => {
return r?.data?.lnInvoicePaymentStatus?.status
})
.catch(err => {
throw new Error(err)
})
}
function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const getBalance = _.reduce((acc, value) => {
@ -260,13 +278,16 @@ function getStatus (account, tx, requested, settings, operatorId) {
return acc
}, { SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) })
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
return checkCryptoCode(cryptoCode)
.then(() => {
const address = coinUtils.parseUrl(cryptoCode, account.environment, toAddress, false)
// Consider all LN transactions successful
if (isLightning(address)) {
return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
return getInvoiceStatus(account.apiSecret, account.endpoint, address)
.then(it => {
const isPaid = it === 'PAID'
if (isPaid) return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
return { receivedCryptoAtoms: BN(0), status: 'notSeen' }
})
}
// On-chain and intra-ledger transactions
return getTransactionsByAddress(account.apiSecret, account.endpoint, account.walletId, address)
@ -282,7 +303,6 @@ function getStatus (account, tx, requested, settings, operatorId) {
}
function newFunding (account, cryptoCode, settings, operatorId) {
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
// Regular BTC address
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))