Created
June 3, 2016 12:43
-
-
Save you21979/6edcf110dc5758d0298ae962ca2ad0d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var InsightRestClient = require('insight-cli').RestClient; | |
var task = require('promise-util-task'); | |
var checkTXConfirmState = function(insight, confirmation_count, txid){ | |
var reqtime = new Date() / 1000 | 0; | |
return insight.transaction(txid).then(function(tx){ | |
var state = (tx.confirmations >= confirmation_count) ? 'CONFIRM' : 'UNCONFIRM'; | |
return [tx.txid, tx.time, tx.confirmations, state] | |
}).catch(function(e){ | |
switch(e.statusCode){ | |
case 404: return [txid, reqtime, -1, 'ERROR'] | |
default: return [txid, reqtime, -1, 'RETRY'] | |
} | |
}) | |
} | |
var getUtxo = function(insight, addresses, dust_satoshi){ | |
var bitcoin_filter_dust = function(satoshi, dust_satoshi){ | |
return satoshi < dust_satoshi ? 1 : 0 | |
} | |
var utxoMapper = function(utxos){ | |
return utxos.map(function(utxo){ | |
return { | |
tx_id : utxo.txid, | |
tx_vout : utxo.vout, | |
address : utxo.address, | |
amount : utxo.satoshis, | |
confirmations : utxo.confirmations ? utxo.confirmations : 0, | |
is_dust : bitcoin_filter_dust(utxo.amount, dust_satoshi) ? 1 : 0, | |
} | |
}) | |
} | |
return insight.utxo(addresses).then(function(utxos){ | |
return utxoMapper(utxos) | |
}) | |
} | |
var InsightBlockExploler = function(uri, timeout){ | |
this.api = new InsightRestClient(uri, timeout); | |
} | |
InsightBlockExploler.prototype.checkTXConfirmState = function(count, txids){ | |
var self = this; | |
txids = (txids instanceof Array) ? txids : [txids]; | |
return task.seq(txids.map(function(txid){ return function() { | |
return checkTXConfirmState(self.api, count, txid).then(function(res){ | |
return { | |
tx_id : res[0], | |
time : res[1], | |
confirmations : res[2], | |
state : res[3], | |
} | |
}) | |
}})) | |
} | |
InsightBlockExploler.prototype.getUtxo = function(dust, addresses){ | |
var max = 20; | |
var self = this; | |
addresses = (addresses instanceof Array) ? addresses : [addresses]; | |
var list = [[]]; | |
var n = 0; | |
addresses.forEach(function(address){ | |
if(list[n].length >= max) list[++n] = []; | |
list[ n ].push(address) | |
}) | |
return task.seq(list.map(function(addresses){ return function() { | |
return getUtxo(self.api, addresses, dust) | |
}})).then(function(res){ | |
return res.reduce(function(r, v){ return [].concat(r, v) }, []) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment