Wallet/src/js/services/txFormatService.js

79 lines
2.2 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
root.Utils = bwcService.getUtils();
2016-08-18 14:51:35 -03:00
root.formatAmount = function(satoshis, fullPrecision) {
2016-08-18 11:45:30 -03:00
var config = configService.getSync().wallet.settings;
2016-08-18 14:51:35 -03:00
if (config.unitCode == 'sat') return satoshis;
2016-08-18 11:45:30 -03:00
//TODO : now only works for english, specify opts to change thousand separator and decimal separator
var opts = {
fullPrecision: !!fullPrecision
};
2016-08-18 14:51:35 -03:00
return this.Utils.formatAmount(satoshis, config.unitCode, opts);
2016-08-18 11:45:30 -03:00
};
2016-08-18 14:51:35 -03:00
root.formatAmountStr = function(satoshis) {
if (!satoshis) return;
2015-08-20 16:43:03 -03:00
var config = configService.getSync().wallet.settings;
2016-08-18 14:51:35 -03:00
return root.formatAmount(satoshis) + ' ' + config.unitName;
2015-08-20 16:43:03 -03:00
};
2016-08-18 14:51:35 -03:00
root.formatAlternativeStr = function(satoshis, cb) {
if (!satoshis) return;
2015-08-20 16:43:03 -03:00
var config = configService.getSync().wallet.settings;
2016-08-18 14:51:35 -03:00
var val = function() {
var v1 = rateService.toFiat(satoshis, config.alternativeIsoCode);
if (!v1) return null;
return v1.toFixed(2) + ' ' + config.alternativeIsoCode;
};
// Async version
if (cb) {
rateService.whenAvailable(function() {
return cb(val());
});
} else {
if (!rateService.isAvailable()) return null;
return val();
};
2015-08-20 16:43:03 -03:00
};
root.processTx = function(tx) {
2016-08-18 10:37:08 -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) {
2016-08-18 14:51:35 -03:00
o.amountStr = root.formatAmountStr(o.amount);
o.alternativeAmountStr = root.formatAlternativeStr(o.amount);
2016-02-11 13:56:57 -05:00
return total + o.amount;
}, 0);
}
2016-02-11 13:56:57 -05:00
tx.toAddress = tx.outputs[0].toAddress;
2016-08-18 10:37:08 -03:00
}
2016-02-11 13:56:57 -05:00
2016-08-18 14:51:35 -03:00
tx.amountStr = root.formatAmountStr(tx.amount);
tx.alternativeAmountStr = root.formatAlternativeStr(tx.amount);
tx.feeStr = root.formatAmountStr(tx.fee || tx.fees);
2015-08-20 16:43:03 -03:00
return tx;
};
return root;
});