Merge pull request #1640 from RafaelTaranto/fix/galoy-multiple-fixes

chore: use old galoy api
This commit is contained in:
Rafael Taranto 2024-01-10 23:12:32 +00:00 committed by GitHub
commit f029018077
3 changed files with 32 additions and 42 deletions

View file

@ -35,17 +35,15 @@ function checkCryptoCode (cryptoCode) {
return Promise.resolve() return Promise.resolve()
} }
function getTransactionsByAddress (token, endpoint, address) { function getTransactionsByAddress (token, endpoint, walletId, address) {
const accountInfo = { const accountInfo = {
'operationName': 'me', 'operationName': 'me',
'query': `query me { 'query': `query me {
me { me {
defaultAccount { defaultAccount {
defaultWalletId
wallets { wallets {
id id
walletCurrency transactionsByAddress (address: "${address}") {
transactionsByAddress (address: "${address}")
edges { edges {
node { node {
direction direction
@ -62,33 +60,32 @@ function getTransactionsByAddress (token, endpoint, address) {
} }
return request(accountInfo, token, endpoint) return request(accountInfo, token, endpoint)
.then(r => { .then(r => {
return r.data.me.defaultAccount return _.find(it => it.id === walletId, r.data.me.defaultAccount.wallets).transactionsByAddress
}) })
.catch(err => { .catch(err => {
throw new Error(err) throw new Error(err)
}) })
} }
function getGaloyAccount (token, endpoint, walletId) { function getGaloyWallet (token, endpoint, walletId) {
const accountInfo = { const accountInfo = {
'operationName': 'me', 'operationName': 'me',
'query': `query me($walletId: WalletId!) { 'query': `query me {
me { me {
defaultAccount { defaultAccount {
walletById(walletId: $walletId) { wallets {
id id
walletCurrency walletCurrency
balance balance
pendingIncomingBalance
} }
} }
} }
}`, }`,
'variables': { 'walletId': `${walletId}` } 'variables': {}
} }
return request(accountInfo, token, endpoint) return request(accountInfo, token, endpoint)
.then(r => { .then(r => {
return r.data.me.defaultAccount return _.find(it => it.id === walletId, r.data.me.defaultAccount.wallets)
}) })
.catch(err => { .catch(err => {
throw new Error(err) throw new Error(err)
@ -157,9 +154,8 @@ function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx const { toAddress, cryptoAtoms, cryptoCode } = tx
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode) const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getGaloyAccount(account.apiSecret, account.endpoint, account.walletId)) .then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(galoyAccount => { .then(wallet => {
const wallet = galoyAccount.walletById
if (isLightning(toAddress)) { if (isLightning(toAddress)) {
return sendFundsLN(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint) return sendFundsLN(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
} }
@ -235,9 +231,8 @@ function newInvoice (walletId, cryptoAtoms, token, endpoint) {
function balance (account, cryptoCode, settings, operatorId) { function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getGaloyAccount(account.apiSecret, account.endpoint)) .then(() => getGaloyWallet(account.apiSecret, account.endpoint))
.then(galoyAccount => { .then(wallet => {
const wallet = galoyAccount.walletById
return new BN(wallet.balance || 0) return new BN(wallet.balance || 0)
}) })
} }
@ -245,9 +240,8 @@ function balance (account, cryptoCode, settings, operatorId) {
function newAddress (account, info, tx, settings, operatorId) { function newAddress (account, info, tx, settings, operatorId) {
const { cryptoAtoms, cryptoCode } = tx const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getGaloyAccount(account.apiSecret, account.endpoint)) .then(() => getGaloyWallet(account.apiSecret, account.endpoint))
.then(galoyAccount => { .then(wallet => {
const wallet = galoyAccount.walletById
const promises = [ const promises = [
newOnChainAddress(wallet.id, account.apiSecret, account.endpoint), newOnChainAddress(wallet.id, account.apiSecret, account.endpoint),
newInvoice(wallet.id, cryptoAtoms, account.apiSecret, account.endpoint) newInvoice(wallet.id, cryptoAtoms, account.apiSecret, account.endpoint)
@ -275,13 +269,10 @@ function getStatus (account, tx, requested, settings, operatorId) {
return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' } return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
} }
// On-chain and intra-ledger transactions // On-chain and intra-ledger transactions
return getTransactionsByAddress(account.apiSecret, account.endpoint, address) return getTransactionsByAddress(account.apiSecret, account.endpoint, account.walletId, address)
.then(accountInfo => { .then(transactions => {
const transactions = const txEdges = transactions.edges
_.find(wallet => wallet.walletCurrency === externalCryptoCode && const { SUCCESS: confirmed, PENDING: pending } = getBalance(txEdges)
wallet.id === accountInfo.defaultWalletId &&
wallet.id === account.walletId)(accountInfo.wallets).transactions.edges
const { SUCCESS: confirmed, PENDING: pending } = getBalance(transactions)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' } if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' } if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
@ -294,15 +285,15 @@ function newFunding (account, cryptoCode, settings, operatorId) {
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode) const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
// Regular BTC address // Regular BTC address
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getGaloyAccount(account.apiSecret, account.endpoint)) .then(() => getGaloyWallet(account.apiSecret, account.endpoint))
.then(galoyAccount => { .then(wallet => {
const wallet = galoyAccount.walletById
return newOnChainAddress(wallet.id, account.apiSecret, account.endpoint) return newOnChainAddress(wallet.id, account.apiSecret, account.endpoint)
.then(onChainAddress => [onChainAddress, wallet.balance, wallet.pendingIncomingBalance]) .then(onChainAddress => [onChainAddress, wallet.balance])
}) })
.then(([onChainAddress, balance, pendingIncomingBalance]) => { .then(([onChainAddress, balance]) => {
return { return {
fundingPendingBalance: new BN(pendingIncomingBalance), // with the old api is not possible to get pending balance
fundingPendingBalance: new BN(0),
fundingConfirmedBalance: new BN(balance), fundingConfirmedBalance: new BN(balance),
fundingAddress: onChainAddress fundingAddress: onChainAddress
} }
@ -328,6 +319,5 @@ module.exports = {
newFunding, newFunding,
cryptoNetwork, cryptoNetwork,
checkBlockchainStatus, checkBlockchainStatus,
sendProbeRequest,
probeLN probeLN
} }

14
package-lock.json generated
View file

@ -4040,9 +4040,9 @@
} }
}, },
"@lamassu/bolt11": { "@lamassu/bolt11": {
"version": "1.5.0", "version": "1.5.1",
"resolved": "https://registry.npmjs.org/@lamassu/bolt11/-/bolt11-1.5.0.tgz", "resolved": "https://registry.npmjs.org/@lamassu/bolt11/-/bolt11-1.5.1.tgz",
"integrity": "sha512-zvPbYuWuTJmvlU4nUjYBY/OtLFczHm5v/yNS9PTvFQJGSIQ2/7K3OEwl8FtTnV9KhIojoR7ue6NMSK2VHHqACA==", "integrity": "sha512-uP1FyG2cGUveX4tpcbpfs+px0aty0/cJ0O+v+yro24sSYOUvWT/N89yhZBOo/dOGEW+S3J45vzBoeH/OnK6jgA==",
"requires": { "requires": {
"bech32": "^1.1.2", "bech32": "^1.1.2",
"bitcoinjs-lib": "4.0.3", "bitcoinjs-lib": "4.0.3",
@ -4065,11 +4065,11 @@
} }
}, },
"@lamassu/coins": { "@lamassu/coins": {
"version": "1.4.0-beta.4", "version": "1.4.2",
"resolved": "https://registry.npmjs.org/@lamassu/coins/-/coins-1.4.0-beta.4.tgz", "resolved": "https://registry.npmjs.org/@lamassu/coins/-/coins-1.4.2.tgz",
"integrity": "sha512-Tjy3g1FwMJWGNe8pSxSG6ZeETF70ekzTSmNQX/v5BE3l5vsCcmIyug65KU265eyHQT6/IwzUNL8p7NE6VAKakQ==", "integrity": "sha512-eMDpKn/PEUVNsQ31ErLnHxObp7Wrab2g5YANnavUlfD5IUnmmlPGBTfYwDLWy9SHB1P46WsZJPncR12EcnrGqA==",
"requires": { "requires": {
"@lamassu/bolt11": "1.5.0", "@lamassu/bolt11": "1.5.1",
"bech32": "2.0.0", "bech32": "2.0.0",
"big-integer": "^1.6.48", "big-integer": "^1.6.48",
"bignumber.js": "^9.0.0", "bignumber.js": "^9.0.0",

View file

@ -15,7 +15,7 @@
"@ethereumjs/common": "^2.6.4", "@ethereumjs/common": "^2.6.4",
"@ethereumjs/tx": "^3.5.1", "@ethereumjs/tx": "^3.5.1",
"@graphql-tools/merge": "^6.2.5", "@graphql-tools/merge": "^6.2.5",
"@lamassu/coins": "v1.4.0-beta.4", "@lamassu/coins": "v1.4.2",
"@simplewebauthn/server": "^3.0.0", "@simplewebauthn/server": "^3.0.0",
"@vonage/auth": "^1.5.0", "@vonage/auth": "^1.5.0",
"@vonage/sms": "^1.7.0", "@vonage/sms": "^1.7.0",