I’m using event subscriptions (via web3 websocket provider). When a new event is logged, i need to augment the smart contract event data with some block information. It looks like even though the block is mined (since the event is logged) infura does not have the block information (it returns null).
I’m suspecting some difference in how the events and blocks are cached by infura, since calling getBlock
a short amount of time after returns the information:
Below is a small snippet that reproduces the problem (after plugging in a smart contract and its address + triggering a new event):
(async () => {
try {
const assert = require('assert');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/ws/v3/<id>'));
setInterval (() => web3.eth.net.isListening().then( (status) => {}), 1000);
const MyContract = require('./MyContract.json')
const myContract = new web3.eth.Contract(MyContract.abi, "0x00");
let lastBlockNumber = await web3.eth.getBlockNumber();
myContract.events.MyContractEvent({
fromBlock: lastBlockNumber
}, async (error, event) => {
let block = await web3.eth.getBlock (event.blockNumber);
assert.notEqual(block, null);
});
} catch (e) {
console.error(e);
}
})();