chore: server code formatting

This commit is contained in:
Rafael Taranto 2025-05-12 15:35:00 +01:00
parent aedabcbdee
commit 68517170e2
234 changed files with 9824 additions and 6195 deletions

View file

@ -7,16 +7,16 @@ const SUPPORTED_COINS = ['LN']
const BN = require('../../../bn')
function request (graphqlQuery, token, endpoint) {
function request(graphqlQuery, token, endpoint) {
const headers = {
'content-type': 'application/json',
'X-API-KEY': token
'X-API-KEY': token,
}
return axios({
method: 'post',
url: endpoint,
headers: headers,
data: graphqlQuery
data: graphqlQuery,
})
.then(r => {
if (r.error) throw r.error
@ -27,7 +27,7 @@ function request (graphqlQuery, token, endpoint) {
})
}
function checkCryptoCode (cryptoCode) {
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
@ -35,10 +35,10 @@ function checkCryptoCode (cryptoCode) {
return Promise.resolve()
}
function getTransactionsByAddress (token, endpoint, walletId, address) {
function getTransactionsByAddress(token, endpoint, walletId, address) {
const accountInfo = {
'operationName': 'me',
'query': `query me($walletId: WalletId!, , $address: OnChainAddress!) {
operationName: 'me',
query: `query me($walletId: WalletId!, , $address: OnChainAddress!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
@ -55,7 +55,7 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
}
}
}`,
'variables': { walletId, address }
variables: { walletId, address },
}
return request(accountInfo, token, endpoint)
.then(r => {
@ -66,10 +66,10 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
})
}
function getGaloyWallet (token, endpoint, walletId) {
function getGaloyWallet(token, endpoint, walletId) {
const accountInfo = {
'operationName': 'me',
'query': `query me($walletId: WalletId!) {
operationName: 'me',
query: `query me($walletId: WalletId!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
@ -80,7 +80,7 @@ function getGaloyWallet (token, endpoint, walletId) {
}
}
}`,
'variables': { walletId }
variables: { walletId },
}
return request(accountInfo, token, endpoint)
.then(r => {
@ -91,18 +91,18 @@ function getGaloyWallet (token, endpoint, walletId) {
})
}
function isLnInvoice (address) {
function isLnInvoice(address) {
return address.toLowerCase().startsWith('lnbc')
}
function isLnurl (address) {
function isLnurl(address) {
return address.toLowerCase().startsWith('lnurl')
}
function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
function sendFundsOnChain(walletId, address, cryptoAtoms, token, endpoint) {
const sendOnChain = {
'operationName': 'onChainPaymentSend',
'query': `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
operationName: 'onChainPaymentSend',
query: `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
onChainPaymentSend(input: $input) {
errors {
message
@ -111,18 +111,17 @@ function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { address, amount: cryptoAtoms.toString(), walletId } }
variables: { input: { address, amount: cryptoAtoms.toString(), walletId } },
}
return request(sendOnChain, token, endpoint)
.then(result => {
return result.data.onChainPaymentSend
})
return request(sendOnChain, token, endpoint).then(result => {
return result.data.onChainPaymentSend
})
}
function sendFundsLNURL (walletId, lnurl, cryptoAtoms, token, endpoint) {
function sendFundsLNURL(walletId, lnurl, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnurlPaymentSend',
'query': `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
operationName: 'lnurlPaymentSend',
query: `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
lnurlPaymentSend(input: $input) {
errors {
message
@ -131,15 +130,23 @@ function sendFundsLNURL (walletId, lnurl, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { 'lnurl': `${lnurl}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
variables: {
input: {
lnurl: `${lnurl}`,
walletId: `${walletId}`,
amount: `${cryptoAtoms}`,
},
},
}
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnurlPaymentSend)
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnurlPaymentSend,
)
}
function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
function sendFundsLN(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnNoAmountInvoicePaymentSend',
'query': `mutation lnNoAmountInvoicePaymentSend($input: LnNoAmountInvoicePaymentInput!) {
operationName: 'lnNoAmountInvoicePaymentSend',
query: `mutation lnNoAmountInvoicePaymentSend($input: LnNoAmountInvoicePaymentInput!) {
lnNoAmountInvoicePaymentSend(input: $input) {
errors {
message
@ -148,15 +155,23 @@ function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { 'paymentRequest': invoice, walletId, amount: cryptoAtoms.toString() } }
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,
)
}
function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
function sendProbeRequest(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendProbeNoAmount = {
'operationName': 'lnNoAmountInvoiceFeeProbe',
'query': `mutation lnNoAmountInvoiceFeeProbe($input: LnNoAmountInvoiceFeeProbeInput!) {
operationName: 'lnNoAmountInvoiceFeeProbe',
query: `mutation lnNoAmountInvoiceFeeProbe($input: LnNoAmountInvoiceFeeProbeInput!) {
lnNoAmountInvoiceFeeProbe(input: $input) {
amount
errors {
@ -165,22 +180,48 @@ function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
}
}
}`,
'variables': { 'input': { paymentRequest: invoice, walletId, amount: cryptoAtoms.toString() } }
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,
)
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => {
if (isLnInvoice(toAddress)) {
return sendFundsLN(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsLN(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
if (isLnurl(toAddress)) {
return sendFundsLNURL(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsLNURL(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
return sendFundsOnChain(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsOnChain(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
})
.then(result => {
switch (result.status) {
@ -193,25 +234,33 @@ function sendCoins (account, tx, settings, operatorId) {
case 'PENDING':
return '<galoy transaction>'
default:
throw new Error(`Transaction failed: ${_.head(result.errors).message}`)
throw new Error(
`Transaction failed: ${_.head(result.errors).message}`,
)
}
})
}
function probeLN (account, cryptoCode, invoice) {
function probeLN(account, cryptoCode, invoice) {
const probeHardLimits = [200000, 1000000, 2000000]
const promises = probeHardLimits.map(limit => {
return sendProbeRequest(account.walletId, invoice, limit, account.apiSecret, account.endpoint)
.then(r => _.isEmpty(r.errors))
return sendProbeRequest(
account.walletId,
invoice,
limit,
account.apiSecret,
account.endpoint,
).then(r => _.isEmpty(r.errors))
})
return Promise.all(promises)
.then(results => _.zipObject(probeHardLimits, results))
return Promise.all(promises).then(results =>
_.zipObject(probeHardLimits, results),
)
}
function newOnChainAddress (walletId, token, endpoint) {
function newOnChainAddress(walletId, token, endpoint) {
const createOnChainAddress = {
'operationName': 'onChainAddressCreate',
'query': `mutation onChainAddressCreate($input: OnChainAddressCreateInput!) {
operationName: 'onChainAddressCreate',
query: `mutation onChainAddressCreate($input: OnChainAddressCreateInput!) {
onChainAddressCreate(input: $input) {
address
errors {
@ -220,41 +269,17 @@ function newOnChainAddress (walletId, token, endpoint) {
}
}
}`,
'variables': { 'input': { walletId } }
variables: { input: { walletId } },
}
return request(createOnChainAddress, token, endpoint)
.then(result => {
return result.data.onChainAddressCreate.address
})
return request(createOnChainAddress, token, endpoint).then(result => {
return result.data.onChainAddressCreate.address
})
}
function newNoAmountInvoice (walletId, token, endpoint) {
function newInvoice(walletId, cryptoAtoms, 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',
'query': `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
operationName: 'lnInvoiceCreate',
query: `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
lnInvoiceCreate(input: $input) {
errors {
message
@ -265,37 +290,44 @@ function newInvoice (walletId, cryptoAtoms, token, endpoint) {
}
}
}`,
'variables': { 'input': { walletId, amount: cryptoAtoms.toString() } }
variables: { input: { walletId, amount: cryptoAtoms.toString() } },
}
return request(createInvoice, token, endpoint)
.then(result => {
return result.data.lnInvoiceCreate.invoice.paymentRequest
})
return request(createInvoice, token, endpoint).then(result => {
return result.data.lnInvoiceCreate.invoice.paymentRequest
})
}
function balance (account, cryptoCode, settings, operatorId) {
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return new BN(wallet.balance || 0)
})
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info, tx) {
const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => newInvoice(account.walletId, cryptoAtoms, account.apiSecret, account.endpoint))
return checkCryptoCode(cryptoCode).then(() =>
newInvoice(
account.walletId,
cryptoAtoms,
account.apiSecret,
account.endpoint,
),
)
}
function getInvoiceStatus (token, endpoint, address) {
function getInvoiceStatus(token, endpoint, address) {
const query = {
'operationName': 'lnInvoicePaymentStatus',
'query': `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
operationName: 'lnInvoicePaymentStatus',
query: `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
lnInvoicePaymentStatus(input: $input) {
status
}
}`,
'variables': { input: { paymentRequest: address } }
variables: { input: { paymentRequest: address } },
}
return request(query, token, endpoint)
.then(r => {
@ -306,62 +338,89 @@ function getInvoiceStatus (token, endpoint, address) {
})
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const getBalance = _.reduce((acc, value) => {
acc[value.node.status] = acc[value.node.status].plus(new BN(value.node.settlementAmount))
return acc
}, { SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) })
const getBalance = _.reduce(
(acc, value) => {
acc[value.node.status] = acc[value.node.status].plus(
new BN(value.node.settlementAmount),
)
return acc
},
{ SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) },
)
return checkCryptoCode(cryptoCode)
.then(() => {
const address = coinUtils.parseUrl(cryptoCode, account.environment, toAddress, false)
if (isLnInvoice(address)) {
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)
.then(transactions => {
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' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return checkCryptoCode(cryptoCode).then(() => {
const address = coinUtils.parseUrl(
cryptoCode,
account.environment,
toAddress,
false,
)
if (isLnInvoice(address)) {
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,
).then(transactions => {
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' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
// Regular BTC address
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return newOnChainAddress(account.walletId, account.apiSecret, account.endpoint)
.then(onChainAddress => [onChainAddress, wallet.balance])
return newOnChainAddress(
account.walletId,
account.apiSecret,
account.endpoint,
).then(onChainAddress => [onChainAddress, wallet.balance])
})
.then(([onChainAddress, balance]) => {
return {
// with the old api is not possible to get pending balance
fundingPendingBalance: new BN(0),
fundingConfirmedBalance: new BN(balance),
fundingAddress: onChainAddress
fundingAddress: onChainAddress,
}
})
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => account.environment === 'test' ? 'test' : 'main')
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => Promise.resolve('ready'))
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
@ -373,5 +432,5 @@ module.exports = {
newFunding,
cryptoNetwork,
checkBlockchainStatus,
probeLN
probeLN,
}