What could be causing me to not receive events from my deployed contract on the Binance Smart Chain (BNB) testnet and the Aurora testnet, even after trying different nodes? How can I troubleshoot this issue and ensure that I am properly connected to a fully synced node and the event emitter function in my contract is working correctly? Also, where can I find the relevant documentation for subscribing to events on these networks and platforms?
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();
the getPastEvents works as expected for the same contract
[
{
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]
}
}
]
Ok it finally works 1 - I've added some Reconnect options
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 - call the web3.eth.subscribe with await
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);
})