Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #353 from blockchain/max-available
Browse files Browse the repository at this point in the history
fix(): max available amount
  • Loading branch information
mpfluger committed Apr 3, 2017
2 parents 1643b4a + df589d5 commit 3fecba5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,29 @@ Helpers.isEmailInvited = function (email, fraction) {
};

Helpers.blockchainFee = (amount, options) =>
amount < options.min_tx_amount
amount <= options.min_tx_amount
? 0
: Math.min(Math.floor(amount * options.percent), options.max_service_charge);

Helpers.balanceMinusFee = (balance, options) => {
if (!options || options.max_service_charge === undefined ||
options.percent === undefined ||
options.min_tx_amount === undefined) {
return balance;
}
if (balance <= options.min_tx_amount) {
return balance;
}
const point = Math.floor(options.max_service_charge * ((1 / options.percent) + 1));
if (options.min_tx_amount < balance && balance <= point) {
const maxWithFee = Math.floor(balance / (1 + options.percent));
return Math.max(maxWithFee, options.min_tx_amount);
}
return point < balance
? balance - options.max_service_charge
: balance;
};

Helpers.guidToGroup = (guid) => {
let hashed = WalletCrypo.sha256(new Buffer(guid.replace(/-/g, ''), 'hex'));
return hashed[0] & 1 ? 'b' : 'a';
Expand Down
6 changes: 4 additions & 2 deletions src/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function Payment (wallet, payment) {
confEstimation: 'unknown',
txSize: 0, // transaciton size
blockchainFee: 0,
blockchainAddress: null
blockchainAddress: null,
serviceChargeOptions: {}
};

var p = payment || initialState;
Expand Down Expand Up @@ -248,6 +249,7 @@ Payment.amount = function (amounts, absoluteFee, feeOptions) {
console.log('No amounts set.');
} // fi switch
return function (payment) {
payment.serviceChargeOptions = feeOptions || {};
payment.blockchainFee = feeOptions
? Helpers.blockchainFee(formatAmo.reduce(Helpers.add, 0), feeOptions)
: 0;
Expand Down Expand Up @@ -376,7 +378,7 @@ Payment.prebuild = function (absoluteFee) {

var usableCoins = Transaction.filterUsableCoins(payment.coins, payment.feePerKb);
var max = Transaction.maxAvailableAmount(usableCoins, payment.feePerKb);
payment.sweepAmount = max.amount - payment.blockchainFee;
payment.sweepAmount = Helpers.balanceMinusFee(max.amount, payment.serviceChargeOptions);
payment.sweepFee = max.fee;
payment.balance = Transaction.sumOfCoins(payment.coins);

Expand Down
3 changes: 1 addition & 2 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ Transaction.sumOfCoins = function (coins) {

Transaction.selectCoins = function (usableCoins, amounts, fee, isAbsoluteFee) {
var amount = amounts.reduce(Helpers.add, 0);
var nouts = amounts.length;
var sorted = usableCoins.sort(function (a, b) { return b.value - a.value; });
var len = sorted.length;
var sel = [];
Expand All @@ -186,7 +185,7 @@ Transaction.selectCoins = function (usableCoins, amounts, fee, isAbsoluteFee) {
for (var ii = 0; ii < len; ii++) {
var coin2 = sorted[ii];
accAm = accAm + coin2.value;
accFee = Transaction.guessFee(ii + 1, nouts + 1, fee);
accFee = Transaction.guessFee(ii + 1, 2, fee);
sel.push(coin2);
if (accAm >= accFee + amount) { return {'coins': sel, 'fee': accFee}; }
}
Expand Down
2 changes: 1 addition & 1 deletion tests/transaction_spend_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ describe('Transaction', () => {
let fee = 10000;
let isAbsFee = false;
let s = Transaction.selectCoins(coins, amounts, fee, isAbsFee);
expect(s).toEqual({'coins': [{value: 40000}, {value: 30000}], 'fee': 4080});
expect(s).toEqual({'coins': [{value: 40000}, {value: 30000}], 'fee': 3740});
});

it('Transaction.selectCoins with absolute fee', () => {
Expand Down

0 comments on commit 3fecba5

Please sign in to comment.