已部署的合同事件监听不起作用

问题描述 投票:0回答:0

什么可能导致我在币安智能链 (BNB) 测试网和 Aurora 测试网上没有收到我部署的合约的事件,即使在尝试了不同的节点之后也是如此?我如何解决这个问题并确保我正确连接到一个完全同步的节点并且我的合约中的事件发射器功能正常工作?另外,我在哪里可以找到订阅这些网络和平台上的事件的相关文档?

const Web3 = require('web3');

const url = 'wss://testnet.aurora.dev';
const web3 = new Web3(url);
const jsonContract = require('./Social.json');

const contractAddress = '0xf439CC546784AB129e3Bf7205F2763b4D042d02F';
const abi = jsonContract.abi;

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



console.log(web3.utils.sha3('NewPublication(address,uint256,bytes32,string,string)'))


async function listenForEvent() {
    const subscription = web3.eth.subscribe('logs', {
        address: contractAddress,
        topics: [web3.utils.sha3('NewPublication(address,uint256,bytes32,string,string)')]
    }, function (error, result) {
        if (!error) {
            console.log(result);
        } else {
            console.error(error);
        }
    }).on("changed", log =>{
        console.log('changed');
    })

    await subscription.on('data', log => {
        console.log(log);
    });

    subscription.on('error', error => console.log(error));
}

listenForEvent();


async function getPastEvents() {
    const events = await contract.getPastEvents('NewPublication', {
        fromBlock: 0,
        toBlock: 'latest'
    });

    console.log(events);
}

getPastEvents();

getPastEvents 对同一个合约按预期工作

[
  {
    removed: false,
    logIndex: 0,
    transactionIndex: 0,
    transactionHash: '0x762b686ce78f968860ca7e3d2aa4254b5dfe85245d559dc96dc7640087b76a9e',
    blockHash: '0x044ae30b0cd99173fc5e2858151b5ef00a02f6e8c36675064f32688eae6839f1',
    blockNumber: 125941567,
    address: '0xf439CC546784AB129e3Bf7205F2763b4D042d02F',
    id: 'log_21ab5939',
    returnValues: Result {
      '0': '0x57a77F5F7bB971e7207ca4AeF93Dc3B6d2dEf89d',
      '1': '10000000000',
      '2': '0x4eca8631a30e9265a9d321b42d471f6e7f1768dbc11c4d2a97af53a3c5b8b584',
      '3': 'Hello',
      '4': 'this is my first test on aurora test net',
      sender: '0x57a77F5F7bB971e7207ca4AeF93Dc3B6d2dEf89d',
      amount: '10000000000',
      id: '0x4eca8631a30e9265a9d321b42d471f6e7f1768dbc11c4d2a97af53a3c5b8b584',
      title: 'Hello',
      body: 'this is my first test on aurora test net'
    },
    event: 'NewPublication',
    signature: '0x2abd08dfb4ff7a2a5d8e630ea1515e3a0b843830a6fc26e209683f66acb70537',
    raw: {
      data: '0x00000000000000000000000000000000000000000000000000000002540be4004eca8631a30e9265a9d321b42d471f6e7f1768dbc11c4d2a97af53a3c5b8b584000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002874686973206973206d792066697273742074657374206f6e206175726f72612074657374206e6574000000000000000000000000000000000000000000000000',
      topics: [Array]
    }
  }
]
node.js events blockchain smartcontracts web3
© www.soinside.com 2019 - 2024. All rights reserved.