Browse Source

fix UTXOs selection & balance calculation

activeAddress
Ivan Socolsky 10 years ago
parent
commit
d4865de91d
  1. 15
      lib/server.js
  2. 4
      test/integration.js

15
lib/server.js

@ -270,11 +270,13 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
.flatten() .flatten()
.map(function(utxo) { .map(function(utxo) {
return utxo.txid + '|' + utxo.vout return utxo.txid + '|' + utxo.vout
}); })
.value();
var dictionary = _.groupBy(utxos, function(utxo) { var dictionary = _.reduce(utxos, function (memo, utxo) {
return utxo.txid + '|' + utxo.vout; memo[utxo.txid + '|' + utxo.vout] = utxo;
}); return memo;
}, {});
_.each(inputs, function(input) { _.each(inputs, function(input) {
if (dictionary[input]) { if (dictionary[input]) {
@ -308,7 +310,7 @@ CopayServer.prototype.getBalance = function(opts, cb) {
balance.totalAmount = _.reduce(utxos, function(sum, utxo) { balance.totalAmount = _.reduce(utxos, function(sum, utxo) {
return sum + utxo.amount; return sum + utxo.amount;
}, 0); }, 0);
balance.lockedAmount = _.reduce(_.without(utxos, { balance.lockedAmount = _.reduce(_.filter(utxos, {
locked: true locked: true
}), function(sum, utxo) { }), function(sum, utxo) {
return sum + utxo.amount; return sum + utxo.amount;
@ -333,6 +335,7 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
var total = 0; var total = 0;
var selected = []; var selected = [];
var inputs = _.sortBy(utxos, 'amount'); var inputs = _.sortBy(utxos, 'amount');
while (i < inputs.length) { while (i < inputs.length) {
selected.push(inputs[i]); selected.push(inputs[i]);
total += inputs[i].amount; total += inputs[i].amount;
@ -378,11 +381,11 @@ CopayServer.prototype.createTx = function(opts, cb) {
creatorId: opts.copayerId, creatorId: opts.copayerId,
toAddress: opts.toAddress, toAddress: opts.toAddress,
amount: opts.amount, amount: opts.amount,
inputs: self._selectUtxos(opts.amount, utxos),
changeAddress: opts.changeAddress, changeAddress: opts.changeAddress,
requiredSignatures: wallet.m, requiredSignatures: wallet.m,
maxRejections: wallet.n - wallet.m, maxRejections: wallet.n - wallet.m,
}); });
txp.inputs = self._selectUtxos(txp, utxos);
txp.rawTx = self._createRawTx(txp); txp.rawTx = self._createRawTx(txp);

4
test/integration.js

@ -594,7 +594,7 @@ describe('Copay server', function() {
}); });
}); });
it.only('should create tx', function(done) { it('should create tx', function(done) {
var bc = sinon.stub(); var bc = sinon.stub();
bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, helpers.createUtxos([100, 200])); bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, helpers.createUtxos([100, 200]));
server._getBlockExplorer = sinon.stub().returns(bc); server._getBlockExplorer = sinon.stub().returns(bc);
@ -626,7 +626,7 @@ describe('Copay server', function() {
}, function(err, balance) { }, function(err, balance) {
should.not.exist(err); should.not.exist(err);
balance.totalAmount.should.equal(300); balance.totalAmount.should.equal(300);
balance.lockedAmount.should.equal(200); balance.lockedAmount.should.equal(100);
done(); done();
}); });
}); });

Loading…
Cancel
Save