提问人:Marou M 提问时间:5/8/2023 最后编辑:TylerHMarou M 更新时间:5/13/2023 访问量:126
已部署的合约事件侦听不起作用
Deployed contract event listening not working
问:
是什么原因导致我无法从币安智能链 (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]
}
}
]
答:
0赞
Marou M
5/13/2023
#1
好的,它终于起作用了 1 - 我添加了一些重新连接选项
var options = {
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
},
address: contractAddress,
topics: [
web3.utils.sha3('NewPublication(address,uint256,bytes32,string,string)')
],
fromBlock: 0,
toBlock: 'latest'
};
2 - 使用 await 调用 web3.eth.subscribe
await web3.eth.subscribe('logs', options, function (error, result) {
console.log(" ************ SUBSCRIBE **********************************")
if (!error) {
console.log(result);
} else {
console.error(error);
}
}).on("changed", log => {
console.log(" ************ CHANGED **********************************")
console.log('changed');
}).on('data', log => {
console.log(" ************ DATA **********************************")
console.log(log);
}).on('error', error => {
console.log(" ************ ERROR **********************************")
console.log(error)
}).on("connected", log => {
console.log(" ************ CONNECTED **********************************")
console.log(log);
})
下一个:无法使用以太币捕获所有事件
评论