Merge branch 'release-10.0' into refactor/notifs-rework
* release-10.0: fix: recycler counts reset on cash unit update fix: profit display on admin chore: v9.0.0-rc.1 (#1661) chore: v10.0.0-rc.1 (#1660) chore: beta status on LN feat: new version of galoy API fix: case sensitivity chore: v9.0.0-beta.11 (#1656) feat: lnurl support chore: v9.0.0-beta.10 (#1655) fix: use simplified LN deposit URL chore: v10.0.0-beta.13 (#1654) chore: v9.0.0-beta.9 (#1650) chore: adjust probe numbers temporarily chore: v9.0.0-beta.8 (#1649) chore: declutter server logs fix: check for invoice fulfillment
This commit is contained in:
commit
dcfca2099d
19 changed files with 191 additions and 689 deletions
|
|
@ -49,7 +49,11 @@ function updateMachineWithBatch (machineContext, oldCashboxCount) {
|
|||
bills.cashbox_batch_id IS NULL`, [batchId, deviceId])
|
||||
const q3 = t.none(`UPDATE empty_unit_bills SET cashbox_batch_id=$1
|
||||
WHERE empty_unit_bills.device_id = $2 AND empty_unit_bills.cashbox_batch_id IS NULL`, [batchId, deviceId])
|
||||
const q4 = t.none(`UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4, recycler1=$5, recycler2=$6, recycler3=$7, recycler4=$8, recycler5=$9, recycler6=$10 WHERE device_id=$11`, [
|
||||
const q4 = t.none(`
|
||||
UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4,
|
||||
recycler1=coalesce($5, recycler1), recycler2=coalesce($6, recycler2), recycler3=coalesce($7, recycler3),
|
||||
recycler4=coalesce($8, recycler4), recycler5=coalesce($9, recycler5), recycler6=coalesce($10, recycler6) WHERE device_id=$11
|
||||
`, [
|
||||
cashUnits.cassette1,
|
||||
cashUnits.cassette2,
|
||||
cashUnits.cassette3,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
})
|
||||
|
|
|
|||
|
|
@ -183,7 +183,11 @@ function setCassetteBills (rec) {
|
|||
.then(machine => {
|
||||
const oldCashboxCount = machine?.cashUnits?.cashbox
|
||||
if (_.isNil(oldCashboxCount) || cashbox.toString() === oldCashboxCount.toString()) {
|
||||
const sql = 'UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4, recycler1=$5, recycler2=$6, recycler3=$7, recycler4=$8, recycler5=$9, recycler6=$10 WHERE device_id=$11'
|
||||
const sql = `
|
||||
UPDATE devices SET cassette1=$1, cassette2=$2, cassette3=$3, cassette4=$4,
|
||||
recycler1=coalesce($5, recycler1), recycler2=coalesce($6, recycler2), recycler3=coalesce($7, recycler3),
|
||||
recycler4=coalesce($8, recycler4), recycler5=coalesce($9, recycler5), recycler6=coalesce($10, recycler6)
|
||||
WHERE device_id=$11`
|
||||
return db.none(sql, [cassette1, cassette2, cassette3, cassette4, recycler1, recycler2, recycler3, recycler4, recycler5, recycler6, rec.deviceId])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ const crypto = require('crypto')
|
|||
const logger = require('../logger')
|
||||
|
||||
function sha256 (buf) {
|
||||
if (!buf) return null
|
||||
const hash = crypto.createHash('sha256')
|
||||
|
||||
hash.update(buf)
|
||||
|
|
@ -12,9 +13,10 @@ function sha256 (buf) {
|
|||
|
||||
const populateDeviceId = function (req, res, next) {
|
||||
const deviceId = _.isFunction(req.connection.getPeerCertificate)
|
||||
? sha256(req.connection.getPeerCertificate().raw)
|
||||
? sha256(req.connection.getPeerCertificate()?.raw)
|
||||
: null
|
||||
|
||||
if (!deviceId) return res.status(500).json({ error: 'Unable to find certificate' })
|
||||
req.deviceId = deviceId
|
||||
req.deviceTime = req.get('date')
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ const typeDef = gql`
|
|||
code: String!
|
||||
display: String!
|
||||
codeDisplay: String!
|
||||
isBeta: Boolean
|
||||
}
|
||||
|
||||
type Query {
|
||||
|
|
|
|||
|
|
@ -1,66 +0,0 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const argv = require('minimist')(process.argv.slice(2))
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
require('./environment-helper')
|
||||
|
||||
const DATABASE = process.env.LAMASSU_DB ?? 'PROD'
|
||||
const dbMapping = psqlConf => ({
|
||||
STRESS_TEST: _.replace('lamassu', 'lamassu_stress', psqlConf),
|
||||
PAZUZ: _.replace('lamassu', 'lamassu_pazuz', psqlConf),
|
||||
RELEASE: _.replace('lamassu', 'lamassu_release', psqlConf),
|
||||
DEV: _.replace('lamassu', 'lamassu', psqlConf),
|
||||
PROD: _.replace('lamassu', 'lamassu', psqlConf)
|
||||
})
|
||||
|
||||
/**
|
||||
* @return {{path: string, opts: any}}
|
||||
*/
|
||||
function load () {
|
||||
if (process.env.LAMASSU_CONFIG) {
|
||||
const configPath = process.env.LAMASSU_CONFIG
|
||||
return {
|
||||
path: configPath,
|
||||
opts: JSON.parse(fs.readFileSync(configPath))
|
||||
}
|
||||
}
|
||||
|
||||
if (argv.f) {
|
||||
const configPath = argv.f
|
||||
return {
|
||||
path: configPath,
|
||||
opts: JSON.parse(fs.readFileSync(configPath))
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const globalConfigPath = path.resolve('/etc', 'lamassu', 'lamassu.json')
|
||||
const config = {
|
||||
path: globalConfigPath,
|
||||
opts: JSON.parse(fs.readFileSync(globalConfigPath))
|
||||
}
|
||||
|
||||
config.opts.postgresql = dbMapping(config.opts.postgresql)[DATABASE]
|
||||
|
||||
return config
|
||||
} catch (_) {
|
||||
try {
|
||||
const homeConfigPath = path.resolve(os.homedir(), '.lamassu', 'lamassu.json')
|
||||
const config = {
|
||||
path: homeConfigPath,
|
||||
opts: JSON.parse(fs.readFileSync(homeConfigPath))
|
||||
}
|
||||
|
||||
config.opts.postgresql = dbMapping(config.opts.postgresql)[DATABASE]
|
||||
|
||||
return config
|
||||
} catch (_) {
|
||||
console.error("Couldn't open lamassu.json config file.")
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = load
|
||||
|
|
@ -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,28 +165,29 @@ 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)
|
||||
}
|
||||
|
||||
function sendCoins (account, tx, settings, operatorId) {
|
||||
const { toAddress, cryptoAtoms, cryptoCode } = tx
|
||||
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
|
||||
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) {
|
||||
case 'ALREADY_PAID':
|
||||
throw new Error('Transaction already exists!')
|
||||
case 'FAILURE':
|
||||
throw new Error('Transaction failed!')
|
||||
throw new Error('Transaction failed!', JSON.stringify(result.errors))
|
||||
case 'SUCCESS':
|
||||
return '<galoy transaction>'
|
||||
case 'PENDING':
|
||||
|
|
@ -178,7 +199,7 @@ function sendCoins (account, tx, settings, operatorId) {
|
|||
}
|
||||
|
||||
function probeLN (account, cryptoCode, invoice) {
|
||||
const probeHardLimits = [100, 500, 1000]
|
||||
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))
|
||||
|
|
@ -199,7 +220,7 @@ function newOnChainAddress (walletId, token, endpoint) {
|
|||
}
|
||||
}
|
||||
}`,
|
||||
'variables': { 'input': { 'walletId': `${walletId}` } }
|
||||
'variables': { 'input': { walletId } }
|
||||
}
|
||||
return request(createOnChainAddress, token, endpoint)
|
||||
.then(result => {
|
||||
|
|
@ -207,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',
|
||||
|
|
@ -221,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 => {
|
||||
|
|
@ -240,16 +284,25 @@ 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(() => newInvoice(account.walletId, cryptoAtoms, account.apiSecret, account.endpoint))
|
||||
}
|
||||
|
||||
function getInvoiceStatus (token, endpoint, address) {
|
||||
const query = {
|
||||
'operationName': 'lnInvoicePaymentStatus',
|
||||
'query': `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
|
||||
lnInvoicePaymentStatus(input: $input) {
|
||||
status
|
||||
}
|
||||
}`,
|
||||
'variables': { input: { paymentRequest: address } }
|
||||
}
|
||||
return request(query, token, endpoint)
|
||||
.then(r => {
|
||||
return r?.data?.lnInvoicePaymentStatus?.status
|
||||
})
|
||||
.then(([onChainAddress, invoice]) => {
|
||||
return `bitcoin:${onChainAddress}?amount=${cryptoAtoms}&lightning=${invoice}`
|
||||
.catch(err => {
|
||||
throw new Error(err)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -260,19 +313,21 @@ function getStatus (account, tx, requested, settings, operatorId) {
|
|||
return acc
|
||||
}, { SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) })
|
||||
|
||||
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const address = coinUtils.parseUrl(cryptoCode, account.environment, toAddress, false)
|
||||
// Consider all LN transactions successful
|
||||
if (isLightning(address)) {
|
||||
return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
|
||||
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 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' }
|
||||
|
|
@ -282,12 +337,11 @@ function getStatus (account, tx, requested, settings, operatorId) {
|
|||
}
|
||||
|
||||
function newFunding (account, cryptoCode, settings, operatorId) {
|
||||
const externalCryptoCode = coinUtils.getEquivalentCode(cryptoCode)
|
||||
// Regular BTC address
|
||||
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]) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue