fix: usage of .plus function in BN calculation

fix: move batching database writing to transactions
This commit is contained in:
Sérgio Salgado 2021-11-24 15:34:00 +00:00
parent bef24cf3a3
commit f6e793ea4d
2 changed files with 46 additions and 41 deletions

View file

@ -65,7 +65,7 @@ function sendCoinsBatch (account, txs, cryptoCode) {
sendCount = sendCount + txs.length
return new Promise((resolve, reject) => {
setTimeout(() => {
const cryptoSum = _.reduce((acc, value) => acc.add(value.crypto_atoms), BN(0), txs)
const cryptoSum = _.reduce((acc, value) => acc.plus(value.crypto_atoms), BN(0), txs)
if (isInsufficient(cryptoSum, cryptoCode)) {
console.log('[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode, cryptoSum.toString())

View file

@ -12,27 +12,6 @@ function createTransactionBatch (cryptoCode) {
return db.one(sql, [uuid.v4(), cryptoCode])
}
function closeTransactionBatch (batch) {
const sql = `UPDATE transaction_batches SET status='ready', closed_at=now() WHERE id=$1`
return db.none(sql, [batch.id])
}
function confirmSentBatch (batch, tx) {
return db.tx(t => {
const q1 = t.none(`UPDATE transaction_batches SET status='sent', error_message=NULL WHERE id=$1`, [batch.id])
const q2 = t.none(`UPDATE cash_in_txs SET tx_hash=$1, fee=$2, send=true, send_confirmed=true, send_time=now(), send_pending=false, error=NULL, error_code=NULL WHERE batch_id=$3`, [tx.txid, tx.fee.toString(), batch.id])
return t.batch([q1, q2])
})
}
function setErroredBatch (batch, errorMsg) {
const sql = `UPDATE transaction_batches SET status='failed', error_message=$1 WHERE id=$2`
return db.none(sql, [errorMsg, batch.id])
}
function addTransactionToBatch (tx) {
const sql = `SELECT * FROM transaction_batches WHERE crypto_code=$1 AND status='open' ORDER BY created_at DESC LIMIT 1`
const sql2 = `UPDATE cash_in_txs SET batch_id=$1 WHERE id=$2`
@ -46,38 +25,64 @@ function addTransactionToBatch (tx) {
.then(batch => db.none(sql2, [batch.id, tx.id]))
}
function getBatchTransactions (batch) {
const sql = `SELECT * FROM cash_in_txs WHERE batch_id=$1`
return db.manyOrNone(sql, [batch.id])
function closeTransactionBatch (t, batch) {
const sql = `UPDATE transaction_batches SET status='ready', closed_at=now() WHERE id=$1`
return t.none(sql, [batch.id])
}
function getBatchesByStatus (statuses) {
function confirmSentBatch (t, batch, tx) {
return t.none(`UPDATE transaction_batches SET status='sent', error_message=NULL WHERE id=$1`, [batch.id])
.then(() =>
t.none(`UPDATE cash_in_txs SET tx_hash=$1, fee=$2, send=true, send_confirmed=true, send_time=now(), send_pending=false, error=NULL, error_code=NULL WHERE batch_id=$3`, [tx.txid, tx.fee.toString(), batch.id])
)
}
function setErroredBatch (t, batch, errorMsg) {
const sql = `UPDATE transaction_batches SET status='failed', error_message=$1 WHERE id=$2`
return t.none(sql, [errorMsg, batch.id])
}
function getBatchTransactions (t, batch) {
const sql = `SELECT * FROM cash_in_txs WHERE batch_id=$1`
return t.manyOrNone(sql, [batch.id])
}
function getBatchesByStatus (t, statuses) {
const sql = `SELECT *, EXTRACT(EPOCH FROM (now() - created_at)) as time_elapsed FROM transaction_batches WHERE status in ($1^)`
return db.manyOrNone(sql, [_.map(pgp.as.text, statuses).join(',')])
return t.manyOrNone(sql, [_.map(pgp.as.text, statuses).join(',')])
}
function submitBatch (settings, batch) {
getBatchTransactions(batch)
function submitBatch (t, settings, batch) {
getBatchTransactions(t, batch)
.then(txs => {
wallet.sendCoinsBatch(settings, txs, batch.crypto_code)
.then(res => confirmSentBatch(batch, res))
.catch(err => setErroredBatch(batch, err.message))
.then(res => confirmSentBatch(t, batch, res))
.catch(err => setErroredBatch(t, batch, err.message))
})
}
function processBatches (settings, lifecycle) {
getBatchesByStatus(['open', 'failed'])
const transaction = t => {
getBatchesByStatus(t, ['open', 'failed', 'ready'])
.then(batches => {
_.each(batch => {
const elapsedMS = batch.time_elapsed * 1000
if (elapsedMS >= lifecycle) {
return closeTransactionBatch(batch)
.then(() => submitBatch(settings, batch))
return closeTransactionBatch(t, batch)
.then(() => submitBatch(t, settings, batch))
}
}, batches)
})
}
return db.tx(
new pgp.txMode.TransactionMode({ tiLevel: pgp.txMode.isolationLevel }),
transaction
)
}
module.exports = {