From ee9b9cf0e277d8c3874f6564d0ef0bd5d049a8a1 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sun, 11 May 2014 11:47:00 +0400 Subject: [PATCH] wallet: asyncify .fill() --- lib/bcoin/wallet.js | 8 +++++--- test/wallet-test.js | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 5d37011d..927d0029 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -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() { diff --git a/test/wallet-test.js b/test/wallet-test.js index b34871db..e058a961 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -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(); + }); }); }); });