Fork me on GitHub
pikachu's Blog

nodejs 监听私有链pending交易并自动miner

前言
nodejs 监听私有链 pending 交易并自动 miner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const Web3 = require('web3')
var web3 = new Web3()
//web3 = new Web3(new Web3.providers.IpcProvider('', ipcProviderWrapper))
const web3Admin = require('./web3admin.js')
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8548'))
web3Admin.extend(web3)

function sleep(millis) {
return new Promise(function (resolve, reject) {
setTimeout(function () { resolve(); }, millis);
});
}

async function main(){
while(1){
if(web3.txpool.status['pending']==1){
console.log("turning off mining", web3.miner.start());
console.log("isMining?", web3.eth.mining);
}else{
console.log("turning off mining", web3.miner.stop());
console.log("isMining?", web3.eth.mining);
await sleep(10000);
}
}
}

main()
---------------- The End ----------------
谢谢大爷~

Author:pikachu
Link:https://hitcxy.com/2020/auto-pending-miner/
Contact:hitcxy.cn@gmail.com
本文基于 知识共享署名-相同方式共享 4.0 国际许可协议发布
转载请注明出处,谢谢!