2015-05-30 12:58:07 +02:00
|
|
|
var https = require('https')
|
|
|
|
|
2015-05-30 15:30:37 +02:00
|
|
|
function faucetWithdraw (address, amount, done) {
|
|
|
|
var url = 'https://coconut-macaroon.herokuapp.com/withdrawal?address=' + address + '&amount=' + amount
|
|
|
|
https.get(url, function (res) {
|
|
|
|
res.statusCode === 200 ? done(null) : done(new Error('non-200 status: ' + res.statusCode))
|
2015-05-30 12:58:07 +02:00
|
|
|
}).on('error', done)
|
|
|
|
}
|
|
|
|
|
2015-05-30 15:30:37 +02:00
|
|
|
function pollUnspent (blockchain, address, done) {
|
2015-05-30 13:17:42 +02:00
|
|
|
blockchain.addresses.unspents(address, function (err, unspents) {
|
|
|
|
if (err) return done(err)
|
|
|
|
|
2015-05-30 15:30:37 +02:00
|
|
|
if (!unspents || unspents.length === 0) {
|
|
|
|
return setTimeout(function () {
|
2015-05-30 13:17:42 +02:00
|
|
|
pollUnspent(blockchain, address, done)
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
|
|
|
|
done(null, unspents)
|
|
|
|
})
|
2015-05-30 15:19:57 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 15:30:37 +02:00
|
|
|
function pollSummary (blockchain, address, done) {
|
2015-05-30 15:19:57 +02:00
|
|
|
blockchain.addresses.summary(address, function (err, result) {
|
|
|
|
if (err) return done(err)
|
|
|
|
|
2015-05-30 15:30:37 +02:00
|
|
|
if (result.balance === 0) {
|
|
|
|
return setTimeout(function () {
|
2015-05-30 15:19:57 +02:00
|
|
|
pollSummary(blockchain, address, done)
|
|
|
|
}, 200)
|
|
|
|
}
|
2015-05-30 13:17:42 +02:00
|
|
|
|
2015-05-30 15:19:57 +02:00
|
|
|
done(null, result)
|
|
|
|
})
|
2015-05-30 13:17:42 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 12:58:07 +02:00
|
|
|
module.exports = {
|
2015-05-30 13:17:42 +02:00
|
|
|
faucetWithdraw: faucetWithdraw,
|
2015-05-30 15:19:57 +02:00
|
|
|
pollUnspent: pollUnspent,
|
|
|
|
pollSummary: pollSummary
|
2015-05-30 12:58:07 +02:00
|
|
|
}
|