fix: reducer function to calculate per address crypto atoms

This commit is contained in:
Sérgio Salgado 2022-02-25 16:39:39 +00:00
parent 4339e58e6a
commit 450de3cc0b

View file

@ -91,10 +91,12 @@ function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
logger.info('** DEBUG MINERS FEE ** - Calculated fee discount: ', newFee) logger.info('** DEBUG MINERS FEE ** - Calculated fee discount: ', newFee)
return fetch('settxfee', [newFee]) return fetch('settxfee', [newFee])
}) })
.then(() => { .then(() => _.reduce((acc, value) => ({
const txAddressAmountPairs = _.map(tx => [tx.toAddress, BN(tx.cryptoAtoms).shiftedBy(-unitScale).toFixed(8)], txs) ...acc,
return _.fromPairs(txAddressAmountPairs) [value.toAddress]: _.isNil(acc[value.toAddress])
}) ? BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8)
: BN(acc[value.toAddress]).plus(BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8))
}), {}, txs))
.then((obj) => fetch('sendmany', ['', obj])) .then((obj) => fetch('sendmany', ['', obj]))
.then((txId) => fetch('gettransaction', [txId])) .then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res)) .then((res) => _.pick(['fee', 'txid'], res))