Merge remote-tracking branch 'origin/release-9.0' into chore/merge-9-into-10-20240314

This commit is contained in:
Rafael Taranto 2024-03-14 21:58:30 +00:00
commit 196483fd6e
10 changed files with 148 additions and 605 deletions

View file

@ -21,7 +21,7 @@ const logger = new winston.Logger({
})
],
rewriters: [
(...[,, meta]) => meta instanceof Error ? { message: meta.message, stack: meta.stack } : meta
(...[,, meta]) => meta instanceof Error ? { message: meta.message, stack: meta.stack, meta } : meta
],
exitOnError: false
})

View file

@ -32,10 +32,12 @@ const mapLanguage = lang => {
}
const massageCryptos = cryptos => {
const betaList = ['LN']
const convert = crypto => ({
code: crypto['cryptoCode'],
display: crypto['display'],
codeDisplay: crypto['cryptoCodeDisplay'] ?? crypto['cryptoCode']
codeDisplay: crypto['cryptoCodeDisplay'] ?? crypto['cryptoCode'],
isBeta: betaList.includes(crypto.cryptoCode)
})
return _.map(convert, cryptos)

View file

@ -10,6 +10,7 @@ const typeDef = gql`
code: String!
display: String!
codeDisplay: String!
isBeta: Boolean
}
type Query {

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,19 +80,23 @@ 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)
})
}
function isLightning (address) {
return address.substr(0, 2) === 'ln'
function isLnInvoice (address) {
return address.toLowerCase().startsWith('lnbc')
}
function isLnurl (address) {
return address.toLowerCase().startsWith('lnurl')
}
function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
@ -108,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 => {
@ -116,6 +119,23 @@ function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
})
}
function sendFundsLNURL (walletId, lnurl, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnurlPaymentSend',
'query': `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
lnurlPaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
'variables': { 'input': { 'lnurl': `${lnurl}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
}
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnurlPaymentSend)
}
function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnNoAmountInvoicePaymentSend',
@ -128,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)
}
@ -145,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)
}
@ -153,12 +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 => {
if (isLightning(toAddress)) {
return sendFundsLN(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
.then(() => {
if (isLnInvoice(toAddress)) {
return sendFundsLN(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
}
return sendFundsOnChain(wallet.id, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
if (isLnurl(toAddress)) {
return sendFundsLNURL(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
}
return sendFundsOnChain(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
})
.then(result => {
switch (result.status) {
@ -198,7 +220,7 @@ function newOnChainAddress (walletId, token, endpoint) {
}
}
}`,
'variables': { 'input': { 'walletId': `${walletId}` } }
'variables': { 'input': { walletId } }
}
return request(createOnChainAddress, token, endpoint)
.then(result => {
@ -206,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',
@ -220,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 => {
@ -239,17 +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 => {
const promises = [
newOnChainAddress(wallet.id, account.apiSecret, account.endpoint),
newInvoice(wallet.id, cryptoAtoms, account.apiSecret, account.endpoint)
]
return Promise.all(promises)
})
.then(([onChainAddress, invoice]) => {
return `bitcoin:${onChainAddress}?amount=${cryptoAtoms}&lightning=${invoice}`
})
.then(() => newInvoice(account.walletId, cryptoAtoms, account.apiSecret, account.endpoint))
}
function getInvoiceStatus (token, endpoint, address) {
@ -260,7 +295,7 @@ function getInvoiceStatus (token, endpoint, address) {
status
}
}`,
'variables': {"input": {"paymentRequest": address}}
'variables': { input: { paymentRequest: address } }
}
return request(query, token, endpoint)
.then(r => {
@ -281,7 +316,7 @@ function getStatus (account, tx, requested, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => {
const address = coinUtils.parseUrl(cryptoCode, account.environment, toAddress, false)
if (isLightning(address)) {
if (isLnInvoice(address)) {
return getInvoiceStatus(account.apiSecret, account.endpoint, address)
.then(it => {
const isPaid = it === 'PAID'
@ -292,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' }
@ -307,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]) => {