poll address unspent until non-empty or timeout

This commit is contained in:
Wei Lu 2015-05-30 19:17:42 +08:00
parent a2b4558a0b
commit 713f038583
3 changed files with 22 additions and 4 deletions

View file

@ -4,6 +4,7 @@ var assert = require('assert')
var bitcoin = require('../../')
var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
var faucetWithdraw = require('./utils').faucetWithdraw
var pollUnspent = require('./utils').pollUnspent
describe('bitcoinjs-lib (advanced)', function () {
it('can sign a Bitcoin message', function () {
@ -33,7 +34,7 @@ describe('bitcoinjs-lib (advanced)', function () {
faucetWithdraw(address, 2e4, function (err) {
if (err) return done(err)
blockchain.addresses.unspents(address, function (err, unspents) {
pollUnspent(blockchain, address, function (err, unspents) {
if (err) return done(err)
var tx = new bitcoin.TransactionBuilder()

View file

@ -4,6 +4,7 @@ var assert = require('assert')
var bitcoin = require('../../')
var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
var faucetWithdraw = require('./utils').faucetWithdraw
var pollUnspent = require('./utils').pollUnspent
describe('bitcoinjs-lib (multisig)', function () {
it('can create a 2-of-3 multisig P2SH address', function () {
@ -42,7 +43,7 @@ describe('bitcoinjs-lib (multisig)', function () {
if (err) return done(err)
// get latest unspents from the address
blockchain.addresses.unspents(address, function (err, unspents) {
pollUnspent(blockchain, address, function (err, unspents) {
if (err) return done(err)
// filter small unspents

View file

@ -7,6 +7,22 @@ function faucetWithdraw(address, amount, done) {
}).on('error', done)
}
module.exports = {
faucetWithdraw: faucetWithdraw
function pollUnspent(blockchain, address, done) {
blockchain.addresses.unspents(address, function (err, unspents) {
if (err) return done(err)
if(unspents == null || unspents.length === 0) {
return setTimeout(function() {
pollUnspent(blockchain, address, done)
}, 200)
}
done(null, unspents)
})
}
module.exports = {
faucetWithdraw: faucetWithdraw,
pollUnspent: pollUnspent
}