Created
March 8, 2016 18:34
-
-
Save aakilfernandes/b2c930a8e4ec4c544dee to your computer and use it in GitHub Desktop.
Mine when needed
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 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