espend-lib-jsonrewriter
Version:
This directory is scratchwork. It is largely untested and not fully functional. Once it is ready, it will be merged into the transactionparser package.
46 lines (43 loc) • 1.52 kB
JavaScript
var _ = require('lodash');
var BigNumber = require('bignumber.js');
var parseBalanceChanges = require('espend-lib-balancechanges').parseBalanceChanges;
var sum = require('./utils').sum;
var bignumify = require('./utils').bignumify;
/**
* Takes a transaction and its metadata and returns the amount sent as:
*
* If XEC, value sent as String
*
* If not XEC,
{
value: value sent as String,
currency: currency code of value sent
}
*
* If unable to determine, returns undefined
*
* If the caller needs the issuer of sent currency as well, try tx.sendMax.issuer
*/
function getAmountSent(transaction, metadata) {
if (transaction.TransactionType !== 'Payment') {
return null;
}
if (metadata.DeliveredAmount) {
return metadata.DeliveredAmount;
}
var sender = transaction.Account;
var currency = (transaction.SendMax && transaction.SendMax.currency) ?
transaction.SendMax.currency : 'XEC';
var balanceChanges = parseBalanceChanges(metadata);
var senderChanges = balanceChanges.filter(function(change) {
return (change.account === sender &&
change.balance_change.currency === currency);
});
var values = _.pluck(_.pluck(senderChanges, 'balance_change'), 'value');
var paid = sum(bignumify(values)).negated();
var fee = new BigNumber(transaction.Fee);
return (currency === 'XEC') ?
paid.minus(fee).toString() :
{ currency: currency, value: paid.toString() };
}
module.exports.getAmountSent = getAmountSent;