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

@ -8,63 +8,73 @@ const cashInLow = require('./cash-in-low')
module.exports = { atomic }
function atomic (machineTx, pi) {
function atomic(machineTx) {
const TransactionMode = pgp.txMode.TransactionMode
const isolationLevel = pgp.txMode.isolationLevel
const mode = new TransactionMode({ tiLevel: isolationLevel.serializable })
function transaction (t) {
function transaction(t) {
const sql = 'select * from cash_in_txs where id=$1'
const sql2 = 'select * from bills where cash_in_txs_id=$1'
return t.oneOrNone(sql, [machineTx.id])
.then(row => {
if (row && row.tx_version >= machineTx.txVersion) throw new E.StaleTxError({ txId: machineTx.id })
return t.oneOrNone(sql, [machineTx.id]).then(row => {
if (row && row.tx_version >= machineTx.txVersion)
throw new E.StaleTxError({ txId: machineTx.id })
return t.any(sql2, [machineTx.id])
.then(billRows => {
const dbTx = cashInLow.toObj(row)
return t.any(sql2, [machineTx.id]).then(billRows => {
const dbTx = cashInLow.toObj(row)
return preProcess(dbTx, machineTx, pi)
.then(preProcessedTx => cashInLow.upsert(t, dbTx, preProcessedTx))
.then(r => {
return insertNewBills(t, billRows, machineTx)
.then(newBills => _.set('newBills', newBills, r))
})
return preProcess(dbTx, machineTx)
.then(preProcessedTx => cashInLow.upsert(t, dbTx, preProcessedTx))
.then(r => {
return insertNewBills(t, billRows, machineTx).then(newBills =>
_.set('newBills', newBills, r),
)
})
})
})
}
return db.tx({ mode }, transaction)
}
function insertNewBills (t, billRows, machineTx) {
function insertNewBills(t, billRows, machineTx) {
const bills = pullNewBills(billRows, machineTx)
if (_.isEmpty(bills)) return Promise.resolve([])
const dbBills = _.map(cashInLow.massage, bills)
const billsByDestination = _.countBy(_.get(['destination_unit']) ,dbBills)
const billsByDestination = _.countBy(_.get(['destination_unit']), dbBills)
const columns = ['id', 'fiat', 'fiat_code', 'crypto_code', 'cash_in_fee', 'cash_in_txs_id', 'device_time', 'destination_unit']
const columns = [
'id',
'fiat',
'fiat_code',
'crypto_code',
'cash_in_fee',
'cash_in_txs_id',
'device_time',
'destination_unit',
]
const sql = pgp.helpers.insert(dbBills, columns, 'bills')
const deviceID = machineTx.deviceId
const sql2 = `update devices set recycler1 = recycler1 + $2, recycler2 = recycler2 + $3, recycler3 = recycler3 + $4, recycler4 = recycler4 + $5, recycler5 = recycler5 + $6, recycler6 = recycler6 + $7
where device_id = $1`
return t.none(sql2, [
deviceID,
_.defaultTo(0, billsByDestination.recycler1),
_.defaultTo(0, billsByDestination.recycler2),
_.defaultTo(0, billsByDestination.recycler3),
_.defaultTo(0, billsByDestination.recycler4),
_.defaultTo(0, billsByDestination.recycler5),
_.defaultTo(0, billsByDestination.recycler6)
])
return t
.none(sql2, [
deviceID,
_.defaultTo(0, billsByDestination.recycler1),
_.defaultTo(0, billsByDestination.recycler2),
_.defaultTo(0, billsByDestination.recycler3),
_.defaultTo(0, billsByDestination.recycler4),
_.defaultTo(0, billsByDestination.recycler5),
_.defaultTo(0, billsByDestination.recycler6),
])
.then(() => {
return t.none(sql)
})
.then(() => bills)
}
function pullNewBills (billRows, machineTx) {
function pullNewBills(billRows, machineTx) {
if (_.isEmpty(machineTx.bills)) return []
const toBill = _.mapKeys(_.camelCase)
@ -73,7 +83,7 @@ function pullNewBills (billRows, machineTx) {
return _.differenceBy(_.get('id'), machineTx.bills, bills)
}
function preProcess (dbTx, machineTx, pi) {
function preProcess(dbTx, machineTx) {
// Note: The way this works is if we're clear to send,
// we mark the transaction as sendPending.
//

View file

@ -8,26 +8,40 @@ const E = require('../error')
const PENDING_INTERVAL_MS = 60 * T.minutes
const massageFields = ['direction', 'cryptoNetwork', 'bills', 'blacklisted', 'blacklistMessage', 'addressReuse', 'promoCodeApplied', 'validWalletScore', 'cashInFeeCrypto']
const massageFields = [
'direction',
'cryptoNetwork',
'bills',
'blacklisted',
'blacklistMessage',
'addressReuse',
'promoCodeApplied',
'validWalletScore',
'cashInFeeCrypto',
]
const massageUpdateFields = _.concat(massageFields, 'cryptoAtoms')
const massage = _.flow(_.omit(massageFields),
convertBigNumFields, _.mapKeys(_.snakeCase))
const massage = _.flow(
_.omit(massageFields),
convertBigNumFields,
_.mapKeys(_.snakeCase),
)
const massageUpdates = _.flow(_.omit(massageUpdateFields),
convertBigNumFields, _.mapKeys(_.snakeCase))
const massageUpdates = _.flow(
_.omit(massageUpdateFields),
convertBigNumFields,
_.mapKeys(_.snakeCase),
)
module.exports = {toObj, upsert, insert, update, massage, isClearToSend}
module.exports = { toObj, upsert, insert, update, massage, isClearToSend }
function convertBigNumFields (obj) {
function convertBigNumFields(obj) {
const convert = value =>
value && BN.isBigNumber(value)
? value.toString()
: value
value && BN.isBigNumber(value) ? value.toString() : value
return _.mapValues(convert, obj)
}
function toObj (row) {
function toObj(row) {
if (!row) return null
const keys = _.keys(row)
@ -35,7 +49,15 @@ function toObj (row) {
keys.forEach(key => {
const objKey = _.camelCase(key)
if (_.includes(key, ['crypto_atoms', 'fiat', 'cash_in_fee', 'commission_percentage', 'raw_ticker_price'])) {
if (
_.includes(key, [
'crypto_atoms',
'fiat',
'cash_in_fee',
'commission_percentage',
'raw_ticker_price',
])
) {
newObj[objKey] = new BN(row[key])
return
}
@ -48,35 +70,35 @@ function toObj (row) {
return newObj
}
function upsert (t, dbTx, preProcessedTx) {
function upsert(t, dbTx, preProcessedTx) {
if (!dbTx) {
return insert(t, preProcessedTx)
.then(tx => ({dbTx, tx}))
return insert(t, preProcessedTx).then(tx => ({ dbTx, tx }))
}
return update(t, dbTx, diff(dbTx, preProcessedTx))
.then(tx => ({dbTx, tx}))
return update(t, dbTx, diff(dbTx, preProcessedTx)).then(tx => ({ dbTx, tx }))
}
function insert (t, tx) {
function insert(t, tx) {
const dbTx = massage(tx)
const sql = pgp.helpers.insert(dbTx, null, 'cash_in_txs') + ' returning *'
return t.one(sql)
.then(toObj)
return t.one(sql).then(toObj)
}
function update (t, tx, changes) {
function update(t, tx, changes) {
if (_.isEmpty(changes)) return Promise.resolve(tx)
const dbChanges = isFinalTxStage(changes) ? massage(changes) : massageUpdates(changes)
const sql = pgp.helpers.update(dbChanges, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [tx.id]) + ' returning *'
const dbChanges = isFinalTxStage(changes)
? massage(changes)
: massageUpdates(changes)
const sql =
pgp.helpers.update(dbChanges, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [tx.id]) +
' returning *'
return t.one(sql)
.then(toObj)
return t.one(sql).then(toObj)
}
function diff (oldTx, newTx) {
function diff(oldTx, newTx) {
let updatedTx = {}
if (!oldTx) throw new Error('oldTx must not be null')
@ -89,10 +111,15 @@ function diff (oldTx, newTx) {
if (_.isEqualWith(nilEqual, oldField, newField)) return
if (!ensureRatchet(oldField, newField, fieldKey)) {
logger.warn('Value from lamassu-machine would violate ratchet [%s]', fieldKey)
logger.warn(
'Value from lamassu-machine would violate ratchet [%s]',
fieldKey,
)
logger.warn('Old tx: %j', oldTx)
logger.warn('New tx: %j', newTx)
throw new E.RatchetError('Value from lamassu-machine would violate ratchet')
throw new E.RatchetError(
'Value from lamassu-machine would violate ratchet',
)
}
updatedTx[fieldKey] = newField
@ -101,12 +128,29 @@ function diff (oldTx, newTx) {
return updatedTx
}
function ensureRatchet (oldField, newField, fieldKey) {
const monotonic = ['cryptoAtoms', 'fiat', 'send', 'sendConfirmed', 'operatorCompleted', 'timedout', 'txVersion', 'batched', 'discount']
const free = ['sendPending', 'error', 'errorCode', 'customerId', 'discountSource']
function ensureRatchet(oldField, newField, fieldKey) {
const monotonic = [
'cryptoAtoms',
'fiat',
'send',
'sendConfirmed',
'operatorCompleted',
'timedout',
'txVersion',
'batched',
'discount',
]
const free = [
'sendPending',
'error',
'errorCode',
'customerId',
'discountSource',
]
if (_.isNil(oldField)) return true
if (_.includes(fieldKey, monotonic)) return isMonotonic(oldField, newField, fieldKey)
if (_.includes(fieldKey, monotonic))
return isMonotonic(oldField, newField, fieldKey)
if (_.includes(fieldKey, free)) {
if (_.isNil(newField)) return false
@ -114,13 +158,14 @@ function ensureRatchet (oldField, newField, fieldKey) {
}
if (_.isNil(newField)) return false
if (BN.isBigNumber(oldField) && BN.isBigNumber(newField)) return new BN(oldField).eq(newField)
if (BN.isBigNumber(oldField) && BN.isBigNumber(newField))
return new BN(oldField).eq(newField)
if (oldField.toString() === newField.toString()) return true
return false
}
function isMonotonic (oldField, newField, fieldKey) {
function isMonotonic(oldField, newField, fieldKey) {
if (_.isNil(newField)) return false
if (_.isBoolean(oldField)) return oldField === newField || !oldField
if (BN.isBigNumber(oldField)) return oldField.lte(newField)
@ -129,20 +174,22 @@ function isMonotonic (oldField, newField, fieldKey) {
throw new Error(`Unexpected value [${fieldKey}]: ${oldField}, ${newField}`)
}
function nilEqual (a, b) {
function nilEqual(a, b) {
if (_.isNil(a) && _.isNil(b)) return true
return undefined
}
function isClearToSend (oldTx, newTx) {
function isClearToSend(oldTx, newTx) {
const now = Date.now()
return (newTx.send || newTx.batched) &&
return (
(newTx.send || newTx.batched) &&
(!oldTx || (!oldTx.sendPending && !oldTx.sendConfirmed)) &&
(newTx.created > now - PENDING_INTERVAL_MS)
newTx.created > now - PENDING_INTERVAL_MS
)
}
function isFinalTxStage (txChanges) {
function isFinalTxStage(txChanges) {
return txChanges.send || txChanges.batched
}

View file

@ -25,93 +25,114 @@ case
else 'Pending'
end`
module.exports = { post, monitorPending, cancel, PENDING_INTERVAL, TRANSACTION_STATES }
module.exports = {
post,
monitorPending,
cancel,
PENDING_INTERVAL,
TRANSACTION_STATES,
}
function post (machineTx, pi) {
function post(machineTx, pi) {
logger.silly('Updating cashin tx:', machineTx)
return cashInAtomic.atomic(machineTx, pi)
.then(r => {
const updatedTx = r.tx
let addressReuse = false
return cashInAtomic.atomic(machineTx).then(r => {
const updatedTx = r.tx
let addressReuse = false
const promises = [settingsLoader.loadLatestConfig()]
const promises = [settingsLoader.loadLatestConfig()]
const isFirstPost = !r.tx.fiat || r.tx.fiat.isZero()
if (isFirstPost) {
promises.push(
checkForBlacklisted(updatedTx),
doesTxReuseAddress(updatedTx),
getWalletScore(updatedTx, pi)
)
}
const isFirstPost = !r.tx.fiat || r.tx.fiat.isZero()
if (isFirstPost) {
promises.push(
checkForBlacklisted(updatedTx),
doesTxReuseAddress(updatedTx),
getWalletScore(updatedTx, pi),
)
}
return Promise.all(promises)
.then(([config, blacklisted = false, isReusedAddress = false, walletScore = null]) => {
const { rejectAddressReuse } = configManager.getCompliance(config)
const isBlacklisted = !!blacklisted
return Promise.all(promises).then(
([
config,
blacklisted = false,
isReusedAddress = false,
walletScore = null,
]) => {
const { rejectAddressReuse } = configManager.getCompliance(config)
const isBlacklisted = !!blacklisted
if (isBlacklisted) {
notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, false)
} else if (isReusedAddress && rejectAddressReuse) {
notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, true)
addressReuse = true
}
return postProcess(r, pi, isBlacklisted, addressReuse, walletScore)
.then(changes => _.set('walletScore', _.isNil(walletScore) ? null : walletScore.score, changes))
.then(changes => cashInLow.update(db, updatedTx, changes))
.then(_.flow(
if (isBlacklisted) {
notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, false)
} else if (isReusedAddress && rejectAddressReuse) {
notifier.notifyIfActive('compliance', 'blacklistNotify', r.tx, true)
addressReuse = true
}
return postProcess(r, pi, isBlacklisted, addressReuse, walletScore)
.then(changes =>
_.set(
'walletScore',
_.isNil(walletScore) ? null : walletScore.score,
changes,
),
)
.then(changes => cashInLow.update(db, updatedTx, changes))
.then(
_.flow(
_.set('bills', machineTx.bills),
_.set('blacklisted', isBlacklisted),
_.set('blacklistMessage', blacklisted?.content),
_.set('addressReuse', addressReuse),
_.set('validWalletScore', _.isNil(walletScore) || walletScore.isValid),
))
})
})
_.set(
'validWalletScore',
_.isNil(walletScore) || walletScore.isValid,
),
),
)
},
)
})
}
function registerTrades (pi, r) {
function registerTrades(pi, r) {
_.forEach(bill => pi.buy(bill, r.tx), r.newBills)
}
function logAction (rec, tx) {
function logAction(rec, tx) {
const action = {
tx_id: tx.id,
action: rec.action || (rec.sendConfirmed ? 'sendCoins' : 'sendCoinsError'),
error: rec.error,
error_code: rec.errorCode,
tx_hash: rec.txHash
tx_hash: rec.txHash,
}
const sql = pgp.helpers.insert(action, null, 'cash_in_actions')
return db.none(sql)
.then(_.constant(rec))
return db.none(sql).then(_.constant(rec))
}
function logActionById (action, _rec, txId) {
function logActionById(action, _rec, txId) {
const rec = _.assign(_rec, { action, tx_id: txId })
const sql = pgp.helpers.insert(rec, null, 'cash_in_actions')
return db.none(sql)
}
function checkForBlacklisted (tx) {
function checkForBlacklisted(tx) {
return blacklist.blocked(tx.toAddress)
}
function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
function postProcess(r, pi, isBlacklisted, addressReuse, walletScore) {
if (addressReuse) {
return Promise.resolve({
operatorCompleted: true,
error: 'Address Reused'
error: 'Address Reused',
})
}
if (isBlacklisted) {
return Promise.resolve({
operatorCompleted: true,
error: 'Blacklisted Address'
error: 'Blacklisted Address',
})
}
@ -120,7 +141,7 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
walletScore: walletScore.score,
operatorCompleted: true,
error: 'Chain analysis score is above defined threshold',
errorCode: 'scoreThresholdReached'
errorCode: 'scoreThresholdReached',
})
}
@ -128,7 +149,8 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
if (!cashInLow.isClearToSend(r.dbTx, r.tx)) return Promise.resolve({})
return pi.sendCoins(r.tx)
return pi
.sendCoins(r.tx)
.then(txObj => {
if (txObj.batched) {
return {
@ -136,7 +158,7 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
batchTime: 'now()^',
sendPending: true,
error: null,
errorCode: null
errorCode: null,
}
}
@ -147,7 +169,7 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
sendTime: 'now()^',
sendPending: false,
error: null,
errorCode: null
errorCode: null,
}
})
.catch(err => {
@ -161,17 +183,18 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
sendTime: 'now()^',
error: err.message,
errorCode: err.name,
sendPending: true
sendPending: true,
}
})
.then(sendRec => {
pi.notifyOperator(r.tx, sendRec)
.catch((err) => logger.error('Failure sending transaction notification', err))
pi.notifyOperator(r.tx, sendRec).catch(err =>
logger.error('Failure sending transaction notification', err),
)
return logAction(sendRec, r.tx)
})
}
function doesTxReuseAddress (tx) {
function doesTxReuseAddress(tx) {
const sql = `
SELECT EXISTS (
SELECT DISTINCT to_address FROM (
@ -181,15 +204,14 @@ function doesTxReuseAddress (tx) {
return db.one(sql, [tx.id, tx.toAddress]).then(({ exists }) => exists)
}
function getWalletScore (tx, pi) {
return pi.isWalletScoringEnabled(tx)
.then(isEnabled => {
if (!isEnabled) return null
return pi.rateAddress(tx.cryptoCode, tx.toAddress)
})
function getWalletScore(tx, pi) {
return pi.isWalletScoringEnabled(tx).then(isEnabled => {
if (!isEnabled) return null
return pi.rateAddress(tx.cryptoCode, tx.toAddress)
})
}
function monitorPending (settings) {
function monitorPending(settings) {
const sql = `select * from cash_in_txs
where created > now() - interval $1
and send
@ -203,27 +225,29 @@ function monitorPending (settings) {
const tx = cashInLow.toObj(row)
const pi = plugins(settings, tx.deviceId)
return post(tx, pi)
.catch(logger.error)
return post(tx, pi).catch(logger.error)
}
return db.any(sql, [PENDING_INTERVAL, MAX_PENDING])
return db
.any(sql, [PENDING_INTERVAL, MAX_PENDING])
.then(rows => pEachSeries(rows, row => processPending(row)))
.catch(logger.error)
}
function cancel (txId) {
function cancel(txId) {
const updateRec = {
error: 'Operator cancel',
error_code: 'operatorCancel',
operator_completed: true,
batch_id: null
batch_id: null,
}
return Promise.resolve()
.then(() => {
return pgp.helpers.update(updateRec, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [txId])
return (
pgp.helpers.update(updateRec, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [txId])
)
})
.then(sql => db.result(sql, false))
.then(res => {