启动geth
./geth --datadir walker/ --miner.etherbase 0x15208e586d12e5c40c5865cbfdc0057827238e84 --mine --miner.threads 1 --http --http.vhosts "*" --http.addr 0.0.0.0 --http.port 8545 --rpccorsdomain "*" --http.api "db,eth,net,web3,personal,debug"
--ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.api "db,eth,net,web3,personal,debug"
启动console
./geth attach walker/geth.ipc
部署合约
var _myStr = "geely";
var greetingContract = web3.eth.contract([{"inputs":[{"internalType":"string","name":"_myStr","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"_str","type":"string"}],"name":"EventNewStr","type":"event"},{"constant":true,"inputs":[],"name":"getMystring","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"string","name":"_myStr","type":"string"}],"name":"setMyString","outputs":[],"payable":true,"stateMutability":"payable","type":"function"}]);
var greeting = greetingContract.new(
_myStr,
{
from: web3.eth.accounts[0],
data: '0x608060405234801561001057600080fd5b506040516105883803806105888339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b8382019150602082018581111561006957600080fd5b825186600182028301116401000000008211171561008657600080fd5b8083526020830192505050908051906020019080838360005b838110156100ba57808201518184015260208101905061009f565b50505050905090810190601f1680156100e75780820380516001836020036101000a031916815260200191505b50604052505050806000908051906020019061010492919061010b565b50506101b0565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061014c57805160ff191683800117855561017a565b8280016001018555821561017a579182015b8281111561017957825182559160200191906001019061015e565b5b509050610187919061018b565b5090565b6101ad91905b808211156101a9576000816000905550600101610191565b5090565b90565b6103c9806101bf6000396000f3fe6080604052600436106100295760003560e01c80637307a3491461002e57806382180568146100be575b600080fd5b34801561003a57600080fd5b50610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b80600090805190602001906102319291906102ef565b507f72d7616f3edfa86157d7e5db9c93891f92331afc736663bf4da5e83712fa27ba600060405180806020018281038252838181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156102de5780601f106102b3576101008083540402835291602001916102de565b820191906000526020600020905b8154815290600101906020018083116102c157829003601f168201915b50509250505060405180910390a150565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061033057805160ff191683800117855561035e565b8280016001018555821561035e579182015b8281111561035d578251825591602001919060010190610342565b5b50905061036b919061036f565b5090565b61039191905b8082111561038d576000816000905550600101610375565b5090565b9056fea265627a7a7231582020fc0760b3581a0f1960447fe09912384afeaf8c8ed98a16483b35a0cba725f964736f6c634300050d0032',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
启动js
使用schedule持续的监听event
node event.js
event.js源码如下
const schedule =require("node-schedule");
const Web3 =require("web3");
const web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8546"));
web3.eth.defaultAccount =web3.eth.accounts[0];
var abi = [{"inputs":[{"internalType":"string","name":"_myStr","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"_str","type":"string"}],"name":"EventNewStr","type":"event"},{"constant":true,"inputs":[],"name":"getMystring","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"string","name":"_myStr","type":"string"}],"name":"setMyString","outputs":[],"payable":true,"stateMutability":"payable","type":"function"}];
var contractId ="0xafe5d688fe670cbe5fcda00c1c9faeff497f1eb2";
var greet =new web3.eth.Contract(abi,contractId);
// var myEvent = greet.events.EventNewStr({
// filter:{},
// fromBlock:0,
// },function(error,event){})
// .on('data',function(event){
// console.log(event);
// })
// .on("changed",function(event){
// //
// })
// .on('error',console.error);
// var myEvent = greet.getPastEvents('allEvents',{fromBlock:0,toBlock: 4180} ,async (error, events) => {
// for (const event of events){
// console.log( event)
// }
// })
let PrintEvents = async (fromBlock, toBlock) =>{
console.log('fromBlock:',fromBlock,'toBlock:',toBlock);
var myEvent = greet.getPastEvents('allEvents',{
fromBlock:fromBlock,
toBlock: toBlock,
},
async (error, events) => {
for (const event of events){
console.log(event)
}
})
}
schedule.scheduleJob("5 * * * * *",async() => {
var blockHeight = await web3.eth.getBlockNumber();
console.log("schedule","blockHeight:",blockHeight);
await PrintEvents(blockHeight-10,blockHeight);
})
合约源码为
pragma solidity ^0.5.12;
contract greeting {
string myStr;
event EventNewStr(string _str);
constructor(string memory _myStr) public{
myStr = _myStr;
}
function getMystring() view public returns (string memory){
return myStr;
}
function setMyString(string memory _myStr)public payable{
myStr =_myStr;
emit EventNewStr(myStr);
}
}
发送交易
> greeting.setMyString.sendTransaction("walker from asa",{from:eth.accounts[0]})
"0xdb4603462bcf713218d1a4c3dd2e7ca74f730d1a18a53ace8eec5587c789f7bc"
获得event
{
address: '0xaFE5d688Fe670cBE5Fcda00C1C9FAeff497f1eb2',
blockNumber: 4343,
transactionHash: '0xdb4603462bcf713218d1a4c3dd2e7ca74f730d1a18a53ace8eec5587c789f7bc',
transactionIndex: 1,
blockHash: '0x43c011eeddddd9d0258749e8503d226a0691a133c63145aa5062725af423c855',
logIndex: 1,
removed: false,
id: 'log_38c57665',
returnValues: Result { '0': 'walker from asa', _str: 'walker from asa' },
event: 'EventNewStr',
signature: '0x72d7616f3edfa86157d7e5db9c93891f92331afc736663bf4da5e83712fa27ba',
raw: {
data: '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f77616c6b65722066726f6d206173610000000000000000000000000000000000',
topics: [
'0x72d7616f3edfa86157d7e5db9c93891f92331afc736663bf4da5e83712fa27ba'
]
}
}