add to low amount to incoming TXs

This commit is contained in:
Matias Alejo Garcia 2017-06-22 11:38:13 -03:00
commit c54b38a9a9
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
14 changed files with 110 additions and 37 deletions

View file

@ -4,6 +4,7 @@ angular.module('copayApp.services').factory('feeService', function($log, $timeou
var root = {};
var CACHE_TIME_TS = 60; // 1 min
var LOW_AMOUNT_RATIO = 0.15; //Ratio low amount warning (econ fee/amount)
// Constant fee options to translate
root.feeOpts = {
@ -43,7 +44,7 @@ angular.module('copayApp.services').factory('feeService', function($log, $timeou
var feeRate = feeLevelRate.feePerKB;
if (!fromCache) $log.debug('Dynamic fee: ' + feeLevel + '/' + network +' ' + (feeLevelRate.feePerKB / 1000).toFixed() + ' SAT/B');
if (!fromCache) $log.debug('Dynamic fee: ' + feeLevel + '/' + network + ' ' + (feeLevelRate.feePerKB / 1000).toFixed() + ' SAT/B');
return cb(null, feeRate);
});
@ -55,8 +56,8 @@ angular.module('copayApp.services').factory('feeService', function($log, $timeou
root.getFeeLevels = function(cb) {
if (cache.updateTs > Date.now() - CACHE_TIME_TS * 1000 ) {
$timeout( function() {
if (cache.updateTs > Date.now() - CACHE_TIME_TS * 1000) {
$timeout(function() {
return cb(null, cache.data, true);
}, 1);
}
@ -70,7 +71,7 @@ angular.module('copayApp.services').factory('feeService', function($log, $timeou
return cb(gettextCatalog.getString('Could not get dynamic fee'));
}
cache.updateTs =Date.now();
cache.updateTs = Date.now();
cache.data = {
'livenet': levelsLivenet,
'testnet': levelsTestnet
@ -81,5 +82,52 @@ angular.module('copayApp.services').factory('feeService', function($log, $timeou
});
};
// These 2 functions were taken from
// https://github.com/bitpay/bitcore-wallet-service/blob/master/lib/model/txproposal.js#L243
function getEstimatedSizeForSingleInput(wallet) {
switch (wallet.credentials.addressType) {
case 'P2PKH':
return 147;
default:
case 'P2SH':
return wallet.m * 72 + wallet.n * 36 + 44;
}
};
function getEstimatedSize(wallet) {
// Note: found empirically based on all multisig P2SH inputs and within m & n allowed limits.
var safetyMargin = 0.02;
var overhead = 4 + 4 + 9 + 9;
var inputSize = getEstimatedSizeForSingleInput(wallet);
var outputSize = 34;
var nbInputs = 1; //Assume 1 input
var nbOutputs = 2; // Assume 2 outputs
var size = overhead + inputSize * nbInputs + outputSize * nbOutputs;
return parseInt((size * (1 + safetyMargin)).toFixed(0));
};
// Approx utxo amount, from which the uxto is economically redeemable
root.getLowAmount = function(wallet, cb) {
root.getFeeLevels(function(err, levels) {
if (err) return cb(err);
var lowLevelRate = (lodash.find(levels[wallet.network], {
level: 'economy',
}).feePerKB / 1000).toFixed(0);
var size = getEstimatedSize(wallet);
var minFee = size * lowLevelRate;
return cb(null, parseInt(minFee / (LOW_AMOUNT_RATIO)));
});
};
return root;
});

View file

@ -90,6 +90,8 @@ angular.module('copayApp.services')
wallet.m = wallet.credentials.m;
wallet.n = wallet.credentials.n;
wallet.lowAmount =
root.updateWalletSettings(wallet);
root.wallet[walletId] = wallet;

View file

@ -93,6 +93,11 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
tx.alternativeAmountStr = root.formatAlternativeStr(tx.amount);
tx.feeStr = root.formatAmountStr(tx.fee || tx.fees);
if (tx.amountStr) {
tx.amountValueStr = tx.amountStr.split(' ')[0];
tx.amountUnitStr = tx.amountStr.split(' ')[1];
}
return tx;
};

View file

@ -413,7 +413,6 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
$log.debug('Fixing Tx Cache Unit to:' + name)
lodash.each(txs, function(tx) {
tx.amountStr = txFormatService.formatAmount(tx.amount) + name;
tx.feeStr = txFormatService.formatAmount(tx.fees) + name;
});
@ -511,6 +510,15 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
});
}
function updateLowAmount(txs) {
if (!opts.lowAmount) return;
lodash.each(txs, function(tx) {
tx.lowAmount = tx.amount < opts.lowAmount;
});
};
updateLowAmount(txs);
updateNotes(function() {
// <HACK>
@ -567,9 +575,9 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
root.getTx = function(wallet, txid, cb) {
function finish(list){
function finish(list) {
var tx = lodash.find(list, {
txid: txid
txid: txid
});
if (!tx) return cb('Could not get transaction');
@ -602,7 +610,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
});
};
root.getTxHistory = function(wallet, opts, cb) {
opts = opts || {};