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,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
}