Skip to content

Instantly share code, notes, and snippets.

@aakilfernandes
Created March 8, 2016 18:34
Show Gist options
  • Save aakilfernandes/b2c930a8e4ec4c544dee to your computer and use it in GitHub Desktop.
Save aakilfernandes/b2c930a8e4ec4c544dee to your computer and use it in GitHub Desktop.
Mine when needed
var miner = miner || admin.miner
web3.eth.filter("pending").watch(function () {
if (miner.hashrate > 0) return;
console.log("===== Pending transactions! Looking for next block... =====");
miner.start(1);
});
web3.eth.filter("latest").watch(function () {
if (pendingTransactions()) return
console.log("===== No transactions left. Stopping miner... =====");
miner.stop();
});
function pendingTransactions(){
if (web3.eth.pendingTransactions === undefined || web3.eth.pendingTransactions === null) {
return txpool.status.pending || txpool.status.queued;
}
else if (typeof web3.eth.pendingTransactions === "function") {
return web3.eth.pendingTransactions().length > 0;
}
else {
return web3.eth.pendingTransactions.length > 0 || web3.eth.getBlock('pending').transactions.length > 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment