Wallet/src/js/services/txFormatService.js

231 lines
6.4 KiB
JavaScript
Raw Normal View History

2015-08-20 16:43:03 -03:00
'use strict';
2016-12-23 15:20:29 -03:00
angular.module('copayApp.services').factory('txFormatService', function($filter, 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) {
2017-08-31 12:11:33 -03:00
var config = configService.getDefaults().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
};
2017-08-28 15:51:13 -03:00
root.formatAmountStr = function(coin, satoshis) {
var defaults = configService.getDefaults();
var configCache = configService.getSync();
var c = coin == 'btc' ? (configCache.bitcoinAlias || defaults.bitcoinAlias)
: (configCache.bitcoinCashAlias || defaults.bitcoinCashAlias);
2017-01-24 10:45:43 -03:00
if (isNaN(satoshis)) return;
return root.formatAmount(satoshis) + ' ' + (c).toUpperCase();
2015-08-20 16:43:03 -03:00
};
2017-08-28 15:51:13 -03:00
root.toFiat = function(coin, satoshis, code, cb) {
2017-06-27 19:10:54 -03:00
if (isNaN(satoshis)) return;
var val = function() {
2017-08-28 15:51:13 -03:00
var v1 = rateService.toFiat(satoshis, code, coin);
2017-06-27 19:10:54 -03:00
if (!v1) return null;
return v1.toFixed(2);
};
// Async version
if (cb) {
rateService.whenAvailable(function() {
return cb(val());
});
} else {
if (!rateService.isAvailable()) return null;
return val();
};
};
2017-08-28 15:51:13 -03:00
root.formatToUSD = function(coin, satoshis, cb) {
2017-01-24 10:45:43 -03:00
if (isNaN(satoshis)) return;
2016-09-21 11:47:19 -03:00
var val = function() {
2017-08-28 15:51:13 -03:00
var v1 = rateService.toFiat(satoshis, 'USD', coin);
2016-09-21 11:47:19 -03:00
if (!v1) return null;
return v1.toFixed(2);
};
// Async version
if (cb) {
rateService.whenAvailable(function() {
return cb(val());
});
} else {
if (!rateService.isAvailable()) return null;
return val();
};
};
2017-08-28 15:51:13 -03:00
root.formatAlternativeStr = function(coin, satoshis, cb) {
2017-01-24 10:45:43 -03:00
if (isNaN(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() {
2017-08-28 15:51:13 -03:00
var v1 = parseFloat((rateService.toFiat(satoshis, config.alternativeIsoCode, coin)).toFixed(2));
2016-12-23 15:20:29 -03:00
v1 = $filter('formatFiatAmount')(v1);
2016-08-18 14:51:35 -03:00
if (!v1) return null;
2016-12-23 15:20:29 -03:00
return v1 + ' ' + config.alternativeIsoCode;
2016-08-18 14:51:35 -03:00
};
// 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
};
2017-08-28 15:51:13 -03:00
root.processTx = function(coin, 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) {
2017-08-28 15:51:13 -03:00
o.amountStr = root.formatAmountStr(coin, o.amount);
o.alternativeAmountStr = root.formatAlternativeStr(coin, 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
2017-08-28 15:51:13 -03:00
tx.amountStr = root.formatAmountStr(coin, tx.amount);
tx.alternativeAmountStr = root.formatAlternativeStr(coin, tx.amount);
tx.feeStr = root.formatAmountStr(coin, tx.fee || tx.fees);
2015-08-20 16:43:03 -03:00
2017-06-22 11:38:13 -03:00
if (tx.amountStr) {
tx.amountValueStr = tx.amountStr.split(' ')[0];
tx.amountUnitStr = tx.amountStr.split(' ')[1];
}
2015-08-20 16:43:03 -03:00
return tx;
};
2016-08-23 16:53:26 -03:00
root.formatPendingTxps = function(txps) {
2016-09-21 11:47:19 -03:00
$scope.pendingTxProposalsCountForUs = 0;
var now = Math.floor(Date.now() / 1000);
/* To test multiple outputs...
var txp = {
message: 'test multi-output',
fee: 1000,
createdOn: new Date() / 1000,
outputs: []
};
function addOutput(n) {
txp.outputs.push({
amount: 600,
toAddress: '2N8bhEwbKtMvR2jqMRcTCQqzHP6zXGToXcK',
message: 'output #' + (Number(n) + 1)
});
};
lodash.times(150, addOutput);
txps.push(txp);
*/
2016-08-23 16:53:26 -03:00
2016-09-21 11:47:19 -03:00
lodash.each(txps, function(tx) {
2016-08-23 16:53:26 -03:00
2016-09-21 11:47:19 -03:00
// no future transactions...
if (tx.createdOn > now)
tx.createdOn = now;
tx.wallet = profileService.getWallet(tx.walletId);
if (!tx.wallet) {
$log.error("no wallet at txp?");
return;
}
2016-08-23 16:53:26 -03:00
2017-08-28 15:51:13 -03:00
tx = txFormatService.processTx(tx.wallet.coin, tx);
2016-09-21 11:47:19 -03:00
var action = lodash.find(tx.actions, {
copayerId: tx.wallet.copayerId
2016-08-23 16:53:26 -03:00
});
2016-09-21 11:47:19 -03:00
if (!action && tx.status == 'pending') {
tx.pendingForUs = true;
}
if (action && action.type == 'accept') {
tx.statusForUs = 'accepted';
} else if (action && action.type == 'reject') {
tx.statusForUs = 'rejected';
} else {
tx.statusForUs = 'pending';
}
if (!tx.deleteLockTime)
tx.canBeRemoved = true;
});
return txps;
};
2016-08-23 16:53:26 -03:00
2017-08-28 15:51:13 -03:00
root.parseAmount = function(coin, amount, currency) {
var config = configService.getSync().wallet.settings;
var satToBtc = 1 / 100000000;
var unitToSatoshi = config.unitToSatoshi;
var amountUnitStr;
var amountSat;
var alternativeIsoCode = config.alternativeIsoCode;
// If fiat currency
if (currency != 'BCH' && currency != 'BTC' && currency != 'sat') {
amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency;
2017-08-28 15:51:13 -03:00
amountSat = rateService.fromFiat(amount, currency, coin).toFixed(0);
2017-06-28 10:21:53 -03:00
} else if (currency == 'sat') {
amountSat = amount;
2017-08-28 15:51:13 -03:00
amountUnitStr = root.formatAmountStr(coin, amountSat);
// convert sat to BTC or BCH
2017-06-28 10:21:53 -03:00
amount = (amountSat * satToBtc).toFixed(8);
2017-08-28 15:51:13 -03:00
currency = (coin).toUpperCase();
} else {
amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
2017-08-28 15:51:13 -03:00
amountUnitStr = root.formatAmountStr(coin, amountSat);
// convert unit to BTC or BCH
amount = (amountSat * satToBtc).toFixed(8);
2017-08-28 15:51:13 -03:00
currency = (coin).toUpperCase();
}
return {
2017-06-27 19:10:54 -03:00
amount: amount,
currency: currency,
alternativeIsoCode: alternativeIsoCode,
amountSat: amountSat,
amountUnitStr: amountUnitStr
};
};
2017-04-16 22:23:20 -03:00
root.satToUnit = function(amount) {
var config = configService.getSync().wallet.settings;
var unitToSatoshi = config.unitToSatoshi;
var satToUnit = 1 / unitToSatoshi;
var unitDecimals = config.unitDecimals;
return parseFloat((amount * satToUnit).toFixed(unitDecimals));
};
2015-08-20 16:43:03 -03:00
return root;
});