feat: add crypto reservation for batched transaction
This commit is contained in:
parent
0132227251
commit
008dd3e9f6
4 changed files with 49 additions and 24 deletions
|
|
@ -4,7 +4,6 @@ const uuid = require('uuid')
|
|||
|
||||
const BN = require('./bn')
|
||||
const db = require('./db')
|
||||
const wallet = require('./wallet')
|
||||
|
||||
function addTransactionToBatch (tx) {
|
||||
const sql = `SELECT * FROM transaction_batches WHERE crypto_code=$1 AND status='open' ORDER BY created_at DESC LIMIT 1`
|
||||
|
|
@ -57,30 +56,23 @@ function getBatchesByStatus (statuses) {
|
|||
return db.manyOrNone(sql, [_.map(pgp.as.text, statuses).join(',')])
|
||||
}
|
||||
|
||||
function submitBatch (settings, batch) {
|
||||
getBatchTransactions(batch)
|
||||
.then(txs => {
|
||||
wallet.sendCoinsBatch(settings, txs, batch.crypto_code)
|
||||
.then(res => confirmSentBatch(batch, res))
|
||||
.catch(err => setErroredBatch(batch, err.message))
|
||||
})
|
||||
}
|
||||
function getOpenBatchCryptoValue (cryptoCode) {
|
||||
const sql = `SELECT * FROM transaction_batches WHERE crypto_code=$1 AND status='open' ORDER BY created_at DESC LIMIT 1`
|
||||
|
||||
function processBatches (settings, lifecycle) {
|
||||
return getBatchesByStatus(['open'])
|
||||
.then(batches => {
|
||||
_.each(batch => {
|
||||
const elapsedMS = batch.time_elapsed * 1000
|
||||
|
||||
if (elapsedMS >= lifecycle) {
|
||||
return closeTransactionBatch(batch)
|
||||
.then(() => submitBatch(settings, batch))
|
||||
}
|
||||
}, batches)
|
||||
return db.oneOrNone(sql, [cryptoCode])
|
||||
.then(batch => {
|
||||
if (_.isNil(batch)) return Promise.resolve([])
|
||||
return db.any(`SELECT * FROM cash_in_txs WHERE batch_id=$1`, [batch.id])
|
||||
})
|
||||
.then(txs => _.reduce((acc, tx) => acc.plus(tx.cash_in_fee_crypto).plus(tx.crypto_atoms), BN(0), txs))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addTransactionToBatch,
|
||||
processBatches
|
||||
closeTransactionBatch,
|
||||
confirmSentBatch,
|
||||
setErroredBatch,
|
||||
getBatchTransactions,
|
||||
getBatchesByStatus,
|
||||
getOpenBatchCryptoValue
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue