wallet: asyncify .fill()

This commit is contained in:
Fedor Indutny 2014-05-11 11:47:00 +04:00
parent 6d99072caf
commit ee9b9cf0e2
2 changed files with 19 additions and 12 deletions

View File

@ -247,7 +247,9 @@ Wallet.prototype.balance = function balance() {
return this.tx.balance();
};
Wallet.prototype.fill = function fill(tx) {
Wallet.prototype.fill = function fill(tx, cb) {
cb = utils.asyncify(cb);
// NOTE: tx should be prefilled with all outputs
var cost = tx.funds('out');
@ -292,7 +294,7 @@ Wallet.prototype.fill = function fill(tx) {
if (tx.funds('in').cmp(total) < 0) {
var err = new Error('Not enough funds');
err.minBalance = total;
throw err;
return cb(err);
}
// How much money is left after sending outputs
@ -314,7 +316,7 @@ Wallet.prototype.fill = function fill(tx) {
// Sign transaction
this.sign(tx);
return tx;
cb(null, tx);
};
Wallet.prototype.toJSON = function toJSON() {

View File

@ -131,7 +131,7 @@ describe('Wallet', function() {
}));
});
it('should fill tx with inputs', function() {
it('should fill tx with inputs', function(cb) {
var w1 = bcoin.wallet();
var w2 = bcoin.wallet();
@ -143,16 +143,21 @@ describe('Wallet', function() {
// Create new transaction
var t2 = bcoin.tx().out(w2, 5460);
w1.fill(t2);
assert(t2.verify());
w1.fill(t2, function(err) {
assert(!err);
assert(t2.verify());
assert.equal(t2.funds('in').toString(10), 16380);
assert.equal(t2.funds('out').toString(10), 6380);
assert.equal(t2.funds('in').toString(10), 16380);
assert.equal(t2.funds('out').toString(10), 6380);
// Create new transaction
var t2 = bcoin.tx().out(w2, 15000);
assert.throws(function() {
w1.fill(t2);
// Create new transaction
var t3 = bcoin.tx().out(w2, 15000);
w1.fill(t3, function(err) {
assert(err);
assert.equal(err.minBalance.toString(10), 25000);
cb();
});
});
});
});