接: heco private chain 通过智能合约添加验证人

1. 代码:

var Web3 = require("web3");

var web3;

if (typeof web3 !== "undefined") {
  web3 = new Web3(web3.currentProvider);
} else {
  web3 = new Web3(
    new Web3.providers.WebsocketProvider("ws://127.0.0.1:8546")
  );
}
console.log("web3=====",web3);

var contractAbi = [{ "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }, { "indexed": true, "internalType": "address", "name": "proposer", "type": "address" }, { "indexed": true, "internalType": "address", "name": "dst", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "time", "type": "uint256" }], "name": "LogCreateProposal", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }, { "indexed": true, "internalType": "address", "name": "dst", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "time", "type": "uint256" }], "name": "LogPassProposal", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }, { "indexed": true, "internalType": "address", "name": "dst", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "time", "type": "uint256" }], "name": "LogRejectProposal", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "val", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "time", "type": "uint256" }], "name": "LogSetUnpassed", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "id", "type": "bytes32" }, { "indexed": true, "internalType": "address", "name": "voter", "type": "address" }, { "indexed": false, "internalType": "bool", "name": "auth", "type": "bool" }, { "indexed": false, "internalType": "uint256", "name": "time", "type": "uint256" }], "name": "LogVote", "type": "event" }, { "inputs": [], "name": "MaxValidators", "outputs": [{ "internalType": "uint16", "name": "", "type": "uint16" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "MinimalStakingCoin", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "PROPOSAL_ADDR", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "ProposalAddr", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "PunishContractAddr", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "StakingLockPeriod", "outputs": [{ "internalType": "uint64", "name": "", "type": "uint64" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "ValidatorContractAddr", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "WithdrawProfitPeriod", "outputs": [{ "internalType": "uint64", "name": "", "type": "uint64" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "initialized", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "miner", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "", "type": "address" }], "name": "pass", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "proposalLastingPeriod", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "proposals", "outputs": [{ "internalType": "address", "name": "proposer", "type": "address" }, { "internalType": "address", "name": "dst", "type": "address" }, { "internalType": "string", "name": "details", "type": "string" }, { "internalType": "uint256", "name": "createTime", "type": "uint256" }, { "internalType": "uint16", "name": "agree", "type": "uint16" }, { "internalType": "uint16", "name": "reject", "type": "uint16" }, { "internalType": "bool", "name": "resultExist", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "val", "type": "address" }, { "internalType": "address", "name": "punish", "type": "address" }, { "internalType": "address", "name": "proposal", "type": "address" }], "name": "setContracts", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "miner_", "type": "address" }], "name": "setMiner", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "", "type": "address" }, { "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "votes", "outputs": [{ "internalType": "address", "name": "voter", "type": "address" }, { "internalType": "uint256", "name": "voteTime", "type": "uint256" }, { "internalType": "bool", "name": "auth", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address[]", "name": "vals", "type": "address[]" }], "name": "initialize", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "dst", "type": "address" }, { "internalType": "string", "name": "details", "type": "string" }], "name": "createProposal", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "id", "type": "bytes32" }, { "internalType": "bool", "name": "auth", "type": "bool" }], "name": "voteProposal", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "val", "type": "address" }], "name": "setUnpassed", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "nonpayable", "type": "function" }];

var contractaAddress = "0x000000000000000000000000000000000000F002";

MyContract = new web3.eth.Contract(contractAbi, contractaAddress);
//console.log(MyContract.events.orderlog);

var myEvent = MyContract.events.LogCreateProposal({
    filter: {},
    fromBlock: 0
}, function (error, event) { })
    .on('data', function (event) {
        console.log(event); // same results as the optional callback above
    })
    .on('changed', function (event) {
        // remove event from local database
    })
    .on('error', console.error);

2. 运行

npm init -y
npm install web3 --save
node map_event.js

3. 执行结果

{
  address: '0x000000000000000000000000000000000000F002',
  blockNumber: 64274,
  transactionHash: '0x950203bc5d391a6b8180c5be6d290403f39fe81a8666e40b74cc3b9b094ce1a6',
  transactionIndex: 0,
  blockHash: '0x9abb16d015c49268c04909bcea414994d9bc0867dc8648b5e3dd33071d86daf5',
  logIndex: 0,
  removed: false,
  id: 'log_14a4176e',
  returnValues: Result {
    '0': '0x88437e94b8ffc61e30087ba66182c8f291e4af36d289ae299f676f014fdba411',
    '1': '0xc65dD4299C682f335d6e15e2B5774D015E01E479',
    '2': '0xc65dD4299C682f335d6e15e2B5774D015E01E479',
    '3': '1614340623',
    id: '0x88437e94b8ffc61e30087ba66182c8f291e4af36d289ae299f676f014fdba411',
    proposer: '0xc65dD4299C682f335d6e15e2B5774D015E01E479',
    dst: '0xc65dD4299C682f335d6e15e2B5774D015E01E479',
    time: '1614340623'
  },
  event: 'LogCreateProposal',
  signature: '0xc10f2f4d53a0e342536c6af3cce9c6ee25c32dbb323521ce0e1d4494a3e362e8',
  raw: {
    data: '0x000000000000000000000000000000000000000000000000000000006038e20f',
    topics: [
      '0xc10f2f4d53a0e342536c6af3cce9c6ee25c32dbb323521ce0e1d4494a3e362e8',
      '0x88437e94b8ffc61e30087ba66182c8f291e4af36d289ae299f676f014fdba411',
      '0x000000000000000000000000c65dd4299c682f335d6e15e2b5774d015e01e479',
      '0x000000000000000000000000c65dd4299c682f335d6e15e2b5774d015e01e479'
    ]
  }

参考:

  1. 使用web3.js监听以太坊智能合约event: https://www.bcskill.com/index.php/archives/1022.html

标签: none

添加新评论