chore: use old galoy api

This commit is contained in:
Rafael Taranto 2024-01-10 23:11:21 +00:00
parent 462d3c72ed
commit 1cd1e1d9ab
3 changed files with 32 additions and 42 deletions

View file

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