Wallet/src/js/services/txFormatService.js

45 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-08-20 16:43:03 -03:00
'use strict';
2015-09-09 16:17:08 -03:00
angular.module('copayApp.services').factory('txFormatService', function(profileService, rateService, configService, lodash) {
2015-08-20 16:43:03 -03:00
var root = {};
var formatAmountStr = function(amount) {
if (!amount) return;
var config = configService.getSync().wallet.settings;
return profileService.formatAmount(amount) + ' ' + config.unitName;
};
var formatAlternativeStr = function(amount) {
if (!amount) return;
var config = configService.getSync().wallet.settings;
return (rateService.toFiat(amount, config.alternativeIsoCode) ? rateService.toFiat(amount, config.alternativeIsoCode).toFixed(2) : 'N/A') + ' ' + config.alternativeIsoCode;
};
var formatFeeStr = function(fee) {
if (!fee) return;
var config = configService.getSync().wallet.settings;
return profileService.formatAmount(fee) + ' ' + config.unitName;
};
root.processTx = function(tx) {
if (!tx) return;
2015-12-16 16:40:02 -03:00
if (lodash.isArray(tx.outputs) && tx.outputs.length > 0 && tx.action != 'received') {
2015-08-20 16:43:03 -03:00
tx.hasMultiplesOutputs = true;
2015-12-16 16:22:40 -03:00
tx.recipientCount = tx.outputs.length;
2015-08-20 16:43:03 -03:00
tx.amount = lodash.reduce(tx.outputs, function(total, o) {
o.amountStr = formatAmountStr(o.amount);
o.alternativeAmountStr = formatAlternativeStr(o.amount);
return total + o.amount;
}, 0);
}
tx.amountStr = formatAmountStr(tx.amount);
tx.alternativeAmountStr = formatAlternativeStr(tx.amount);
tx.feeStr = formatFeeStr(tx.fee || tx.fees);
return tx;
};
return root;
});