Skip to content

Commit 713f038

Browse files
committed
poll address unspent until non-empty or timeout
1 parent a2b4558 commit 713f038

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

test/integration/advanced.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var assert = require('assert')
44
var bitcoin = require('../../')
55
var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
66
var faucetWithdraw = require('./utils').faucetWithdraw
7+
var pollUnspent = require('./utils').pollUnspent
78

89
describe('bitcoinjs-lib (advanced)', function () {
910
it('can sign a Bitcoin message', function () {
@@ -33,7 +34,7 @@ describe('bitcoinjs-lib (advanced)', function () {
3334
faucetWithdraw(address, 2e4, function (err) {
3435
if (err) return done(err)
3536

36-
blockchain.addresses.unspents(address, function (err, unspents) {
37+
pollUnspent(blockchain, address, function (err, unspents) {
3738
if (err) return done(err)
3839

3940
var tx = new bitcoin.TransactionBuilder()

test/integration/multisig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var assert = require('assert')
44
var bitcoin = require('../../')
55
var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
66
var faucetWithdraw = require('./utils').faucetWithdraw
7+
var pollUnspent = require('./utils').pollUnspent
78

89
describe('bitcoinjs-lib (multisig)', function () {
910
it('can create a 2-of-3 multisig P2SH address', function () {
@@ -42,7 +43,7 @@ describe('bitcoinjs-lib (multisig)', function () {
4243
if (err) return done(err)
4344

4445
// get latest unspents from the address
45-
blockchain.addresses.unspents(address, function (err, unspents) {
46+
pollUnspent(blockchain, address, function (err, unspents) {
4647
if (err) return done(err)
4748

4849
// filter small unspents

test/integration/utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ function faucetWithdraw(address, amount, done) {
77
}).on('error', done)
88
}
99

10+
function pollUnspent(blockchain, address, done) {
11+
blockchain.addresses.unspents(address, function (err, unspents) {
12+
if (err) return done(err)
13+
14+
if(unspents == null || unspents.length === 0) {
15+
return setTimeout(function() {
16+
pollUnspent(blockchain, address, done)
17+
}, 200)
18+
}
19+
20+
done(null, unspents)
21+
})
22+
23+
}
24+
1025
module.exports = {
11-
faucetWithdraw: faucetWithdraw
26+
faucetWithdraw: faucetWithdraw,
27+
pollUnspent: pollUnspent
1228
}

0 commit comments

Comments
 (0)