From 3465173e1a09a085d56c220a3380d57e657aac57 Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Fri, 9 Feb 2024 19:05:25 +0000 Subject: [PATCH] fix: check for invoice fulfillment --- lib/plugins/wallet/galoy/galoy.js | 32 +++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/plugins/wallet/galoy/galoy.js b/lib/plugins/wallet/galoy/galoy.js index 0299aab0..ec4e57c8 100644 --- a/lib/plugins/wallet/galoy/galoy.js +++ b/lib/plugins/wallet/galoy/galoy.js @@ -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 '' 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))