web3.js – 执行事件查询

完整代码如下:

app.js

const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY') // YOUR_INFURA_API_KEY替换为你自己的key

// OMG Token Contract
const abi = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]
const address = '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07'

const contract = new web3.eth.Contract(abi, address)

// 获取事件
contract.getPastEvents(
  'AllEvents',
  {
    fromBlock: 8717848,
    toBlock: 'latest'
  },
).then(function(events) {
    console.log(events)
});

运行app.js:

$ node app

输出

[ { address: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07',
    blockHash:
     '0x1a1dab6709a8b634c9a5e7e24f6d232b48f82b445154f3cf6d8052bdb0550d53',
    blockNumber: 8717848,
    logIndex: 92,
    removed: false,
    transactionHash:
     '0x2ae9c47967eceee1eee042d3d49d89fb8994b2362969a6a7e18c92204bdf5757',
    transactionIndex: 106,
    id: 'log_b0adb365',
    returnValues:
     Result {
       '0': '0xB8243475CFFA57E65e739793966fBC03DA35d3aD',
       '1': '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE',
       '2': '27768634590000000000',
       from: '0xB8243475CFFA57E65e739793966fBC03DA35d3aD',
       to: '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE',
       value: '27768634590000000000' },
    event: 'Transfer',
    signature:
     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    raw:
     { data:
        '0x000000000000000000000000000000000000000000000001815e0208fa932c00',
       topics: [Array] } },

  ...


  { address: '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07',
    blockHash:
     '0x86b830ce21ac1d2fe88b3210bd9ca6ce8a6dc807a470d3f4cf57d17b3fe0e2bc',
    blockNumber: 8718031,
    logIndex: 178,
    removed: false,
    transactionHash:
     '0x76d8fdef6a7dd0099357ddf00c9c812196a98bc8bb3deab31f7f5b8dd823fc4e',
    transactionIndex: 187,
    id: 'log_e6809a37',
    returnValues:
     Result {
       '0': '0x0e936B11c2e7b601055e58c7E32417187aF4de4a',
       '1': '0x11614c5F1Eb215Ecffe657Da56d3Dd12df395dc8',
       '2': '115164104778191566836',
       from: '0x0e936B11c2e7b601055e58c7E32417187aF4de4a',
       to: '0x11614c5F1Eb215Ecffe657Da56d3Dd12df395dc8',
       value: '115164104778191566836' },
    event: 'Transfer',
    signature:
     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    raw:
     { data:
        '0x0000000000000000000000000000000000000000000000063e39172153f977f4',
       topics: [Array] } } ]


浙ICP备17015664号 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载