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