Here’s an article:
Update to Ethereum Web3 V4.7.0: Easily capture events
As Ethereum Blockchain, as the complex programs of the creator’s buildings, one of the most challenges we face is the management of the network’s events. In our case, we have updated from version 1.x.x (no longer used version) to version 4.7.0 (our new favorite), and now we want to learn how to manage these exhaust events easily.
The first step of using Ethereum Web3 V4.7.0 is to understand the process of exhausting the event. In general, when the event is transformed by contracts or a call to its function, it causes a feedback that is broadcast into all connected network nodes. This allows other applications and contracts to respond to the event.
To capture the dissertation events with our Web3.Js Library (V4.7.0), we can use the ON method confirmed by Web3.js. Here’s an example of how you can do this:
`Javascript
// Import the necessary modules
Const Web3 = Require (‘Web3’);
Const Web3 = New Web3 ();
// Get a copy of Ethereum provider
CONST Provider = New Web3.Providers.Httpprovider (‘
// Define the function of the processing of
Function is carried (event) {
// Check that this event is interest (eg Newblock)
if (event.type == ‘newblock’) {
Console.log (received new block: $ {Event.data}
);
}
}
// Subscribe to discarded events
Web3.eth.on ('block', odd);
// You can also a specific specific type of event type and backlit connection function
Web3.eth.on ('transactionhash', oddigent);
In this example, we import the Web3.js library and create a copy of the Ethereum provider. Then we define the “activation” method that listens to the events that are of interest (Newblock in our case). The cancellation feature of Onevent is summoned when it receives an event broadcast to the network.
When you are ready to distance yourself from the network, just call off the Off method of your copy:
`Javascript
Web3.eth.off (‘block’, odgigent);
`
By observing this example and using the Web3.js V4.7.0, we can easily capture the events in our programs and respond accordingly.
Tips and options
- Make sure to change
'https: // mainnet.infura.io/v3/yopraject_id' with your actual infura project ID.
- You can also use a specific backlift function or event driver as an argument to use the On method like this:Web3.eth.on (‘Newblock’, ODD) ‘.
- Remember that events are only thrown when called contract. For example, if you call a contract function from another contract, it won the exhaust at the event.
- If you need more control of the event management process (eg complex use), consider using Web3.js v5.x.x.
Hope it will help! Message us in the comments if you have any questions or you need additional help.