feat: new version of galoy API

This commit is contained in:
Rafael Taranto 2024-03-11 10:47:31 +00:00
parent 508adb573f
commit 8b4e34644b

View file

@ -3,14 +3,14 @@ const axios = require('axios')
const { utils: coinUtils } = require('@lamassu/coins') const { utils: coinUtils } = require('@lamassu/coins')
const NAME = 'LN' const NAME = 'LN'
const SUPPORTED_COINS = ['LN', 'BTC'] const SUPPORTED_COINS = ['LN']
const BN = require('../../../bn') const BN = require('../../../bn')
function request (graphqlQuery, token, endpoint) { function request (graphqlQuery, token, endpoint) {
const headers = { const headers = {
'content-type': 'application/json', 'content-type': 'application/json',
'Authorization': `Bearer ${token}` 'X-API-KEY': token
} }
return axios({ return axios({
method: 'post', method: 'post',
@ -38,12 +38,11 @@ function checkCryptoCode (cryptoCode) {
function getTransactionsByAddress (token, endpoint, walletId, address) { function getTransactionsByAddress (token, endpoint, walletId, address) {
const accountInfo = { const accountInfo = {
'operationName': 'me', 'operationName': 'me',
'query': `query me { 'query': `query me($walletId: WalletId!, , $address: OnChainAddress!) {
me { me {
defaultAccount { defaultAccount {
wallets { walletById(walletId: $walletId) {
id transactionsByAddress (address: $address) {
transactionsByAddress (address: "${address}") {
edges { edges {
node { node {
direction direction
@ -56,11 +55,11 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
} }
} }
}`, }`,
'variables': {} 'variables': { walletId, address }
} }
return request(accountInfo, token, endpoint) return request(accountInfo, token, endpoint)
.then(r => { .then(r => {
return _.find(it => it.id === walletId, r.data.me.defaultAccount.wallets).transactionsByAddress return r.data.me.defaultAccount.walletById.transactionsByAddress
}) })
.catch(err => { .catch(err => {
throw new Error(err) throw new Error(err)
@ -70,10 +69,10 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
function getGaloyWallet (token, endpoint, walletId) { function getGaloyWallet (token, endpoint, walletId) {
const accountInfo = { const accountInfo = {
'operationName': 'me', 'operationName': 'me',
'query': `query me { 'query': `query me($walletId: WalletId!) {
me { me {
defaultAccount { defaultAccount {
wallets { walletById(walletId: $walletId) {
id id
walletCurrency walletCurrency
balance balance
@ -81,11 +80,11 @@ function getGaloyWallet (token, endpoint, walletId) {
} }
} }
}`, }`,
'variables': {} 'variables': { walletId }
} }
return request(accountInfo, token, endpoint) return request(accountInfo, token, endpoint)
.then(r => { .then(r => {
return _.find(it => it.id === walletId, r.data.me.defaultAccount.wallets) return r.data.me.defaultAccount.walletById
}) })
.catch(err => { .catch(err => {
throw new Error(err) throw new Error(err)
@ -112,7 +111,7 @@ function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
status status
} }
}`, }`,
'variables': { 'input': { 'address': `${address}`, 'amount': `${cryptoAtoms}`, 'walletId': `${walletId}` } } 'variables': { 'input': { address, amount: cryptoAtoms.toString(), walletId } }
} }
return request(sendOnChain, token, endpoint) return request(sendOnChain, token, endpoint)
.then(result => { .then(result => {
@ -149,7 +148,7 @@ function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
status status
} }
}`, }`,
'variables': { 'input': { 'paymentRequest': `${invoice}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } } 'variables': { 'input': { 'paymentRequest': invoice, walletId, amount: cryptoAtoms.toString() } }
} }
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoicePaymentSend) return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoicePaymentSend)
} }
@ -166,7 +165,7 @@ function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
} }
} }
}`, }`,
'variables': { 'input': { 'paymentRequest': `${invoice}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } } 'variables': { 'input': { paymentRequest: invoice, walletId, amount: cryptoAtoms.toString() } }
} }
return request(sendProbeNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoiceFeeProbe) return request(sendProbeNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoiceFeeProbe)
} }
@ -174,15 +173,14 @@ function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
function sendCoins (account, tx, settings, operatorId) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId)) .then(() => {
.then(wallet => {
if (isLnInvoice(toAddress)) { if (isLnInvoice(toAddress)) {
return sendFundsLN(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint) return sendFundsLN(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
} }
if (isLnurl(toAddress)) { if (isLnurl(toAddress)) {
return sendFundsLNURL(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint) return sendFundsLNURL(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
} }
return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint) return sendFundsOnChain(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
}) })
.then(result => { .then(result => {
switch (result.status) { switch (result.status) {
@ -222,7 +220,7 @@ function newOnChainAddress (walletId, token, endpoint) {
} }
} }
}`, }`,
'variables': { 'input': { 'walletId': `${walletId}` } } 'variables': { 'input': { walletId } }
} }
return request(createOnChainAddress, token, endpoint) return request(createOnChainAddress, token, endpoint)
.then(result => { .then(result => {
@ -230,6 +228,29 @@ function newOnChainAddress (walletId, token, endpoint) {
}) })
} }
function newNoAmountInvoice (walletId, token, endpoint) {
const createInvoice = {
'operationName': 'lnNoAmountInvoiceCreate',
'query': `mutation lnNoAmountInvoiceCreate($input: LnNoAmountInvoiceCreateInput!) {
lnNoAmountInvoiceCreate(input: $input) {
errors {
message
path
}
invoice {
paymentRequest
}
}
}`,
'variables': { 'input': { walletId } }
}
return request(createInvoice, token, endpoint)
.then(result => {
return result.data.lnNoAmountInvoiceCreate.invoice.paymentRequest
})
}
function newInvoice (walletId, cryptoAtoms, token, endpoint) { function newInvoice (walletId, cryptoAtoms, token, endpoint) {
const createInvoice = { const createInvoice = {
'operationName': 'lnInvoiceCreate', 'operationName': 'lnInvoiceCreate',
@ -244,7 +265,7 @@ function newInvoice (walletId, cryptoAtoms, token, endpoint) {
} }
} }
}`, }`,
'variables': { 'input': { 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } } 'variables': { 'input': { walletId, amount: cryptoAtoms.toString() } }
} }
return request(createInvoice, token, endpoint) return request(createInvoice, token, endpoint)
.then(result => { .then(result => {
@ -263,8 +284,7 @@ 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(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId)) .then(() => newInvoice(account.walletId, cryptoAtoms, account.apiSecret, account.endpoint))
.then(wallet => newInvoice(wallet.id, cryptoAtoms, account.apiSecret, account.endpoint))
} }
function getInvoiceStatus (token, endpoint, address) { function getInvoiceStatus (token, endpoint, address) {
@ -275,7 +295,7 @@ function getInvoiceStatus (token, endpoint, address) {
status status
} }
}`, }`,
'variables': {"input": {"paymentRequest": address}} 'variables': { input: { paymentRequest: address } }
} }
return request(query, token, endpoint) return request(query, token, endpoint)
.then(r => { .then(r => {
@ -307,8 +327,7 @@ function getStatus (account, tx, requested, settings, operatorId) {
// On-chain and intra-ledger transactions // On-chain and intra-ledger transactions
return getTransactionsByAddress(account.apiSecret, account.endpoint, account.walletId, address) return getTransactionsByAddress(account.apiSecret, account.endpoint, account.walletId, address)
.then(transactions => { .then(transactions => {
const txEdges = transactions.edges const { SUCCESS: confirmed, PENDING: pending } = getBalance(transactions.edges)
const { SUCCESS: confirmed, PENDING: pending } = getBalance(txEdges)
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' }
@ -322,7 +341,7 @@ function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId)) .then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(wallet => { .then(wallet => {
return newOnChainAddress(wallet.id, account.apiSecret, account.endpoint) return newOnChainAddress(account.walletId, account.apiSecret, account.endpoint)
.then(onChainAddress => [onChainAddress, wallet.balance]) .then(onChainAddress => [onChainAddress, wallet.balance])
}) })
.then(([onChainAddress, balance]) => { .then(([onChainAddress, balance]) => {