Wallet/src/js/services/txFormatService.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-08-20 16:43:03 -03:00
'use strict';
2016-08-18 11:45:30 -03:00
angular.module('copayApp.services').factory('txFormatService', function(bwcService, rateService, configService, lodash) {
2015-08-20 16:43:03 -03:00
var root = {};
2016-08-18 11:45:30 -03:00
// // RECEIVE
// // Check address
// root.isUsed(wallet.walletId, balance.byAddress, function(err, used) {
// if (used) {
// $log.debug('Address used. Creating new');
// $rootScope.$emit('Local/AddressIsUsed');
// }
// });
//
root.Utils = bwcService.getUtils();
root.formatAmount = function(amount, fullPrecision) {
var config = configService.getSync().wallet.settings;
if (config.unitCode == 'sat') return amount;
//TODO : now only works for english, specify opts to change thousand separator and decimal separator
var opts = {
fullPrecision: !!fullPrecision
};
return this.Utils.formatAmount(amount, config.unitCode, opts);
};
2015-08-20 16:43:03 -03:00
var formatAmountStr = function(amount) {
if (!amount) return;
var config = configService.getSync().wallet.settings;
2016-08-18 11:45:30 -03:00
return root.formatAmount(amount) + ' ' + config.unitName;
2015-08-20 16:43:03 -03:00
};
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;
2016-08-18 11:45:30 -03:00
return root.formatAmount(fee) + ' ' + config.unitName;
2015-08-20 16:43:03 -03:00
};
root.processTx = function(tx) {
2016-02-18 16:54:13 -03:00
if (!tx || tx.action == 'invalid')
return tx;
2015-08-20 16:43:03 -03:00
2016-02-11 13:56:57 -05:00
// New transaction output format
2016-02-29 16:34:57 -03:00
if (tx.outputs && tx.outputs.length) {
2016-02-11 13:56:57 -05:00
var outputsNr = tx.outputs.length;
if (tx.action != 'received') {
if (outputsNr > 1) {
tx.recipientCount = outputsNr;
tx.hasMultiplesOutputs = true;
}
tx.amount = lodash.reduce(tx.outputs, function(total, o) {
o.amountStr = formatAmountStr(o.amount);
o.alternativeAmountStr = formatAlternativeStr(o.amount);
return total + o.amount;
}, 0);
}
2016-02-11 13:56:57 -05:00
tx.toAddress = tx.outputs[0].toAddress;
}
2015-08-20 16:43:03 -03:00
tx.amountStr = formatAmountStr(tx.amount);
tx.alternativeAmountStr = formatAlternativeStr(tx.amount);
tx.feeStr = formatFeeStr(tx.fee || tx.fees);
return tx;
};
return root;
});