Here is the article:
Update to Ethereum Web3 V4.7.0: Entry events solved with ease
As a developer who creates complex applications at the top of the Ethereum blockchain, one of the most demanding tasks with which we are confronted is the management of the events published by the network. In our case, we have updated from version 1.x.x (the outdated version) to version 4.7.0 (our new favorite), and now we strive to manage these events with ease.
The first step to use Ethereum Web3 V4.7.0 is to understand the process of issuing the event. If an event is issued by a contract or calling its function, it generally triggers a recall that is transferred to all nodes connected in the network. In this way, other applications and contracts can react to the event.
In order to record these events with our web3.js library (V4.7.0), we can use Web3.js. Use specified method that takes place “on”. Here is an example of how you can do it:
`JavaScript
// is important the required modules
Const Web3 = requirements (‘Web3’);
Const Web3 = New Web3 ();
// you get the application of the Ethereum supplier
Const provider = new web3.priders.httpprovider (‘
// Define a function for managing the events output
Onesvent function (event) {
// Check whether this event is of interest (e.g. “Newblock”)
If (event.type === ‘newblock’) {
Console.log (`get a new block: $ {event.data}
);
}
}
// Subscribe to the events output
Web3.eth.on (‘Block’, Onevent);
// It is also possible to specify a kind of specific event and a return function
Web3.eth.on (‘transactionhash’, invent);
`
In this example, we import the Web3.js library and create an application for the Ethereum supplier. We therefore define a method that listens to events (in our case "Newblock"). The return function is called every time an event is transferred to the network.
If you are ready to separate the network from the network, the "offer for application" method is simply called up:
JavaScript
Web3.eth.off (‘block’, One event);
`
If we follow this example and use web3.js V4.7.0, we can easily record events published in our applications and react accordingly.
Tips and variants
- Make sure you
https: // Mainnet.infura.io/v3/Your_Project_ID ’
With your actual project.
- You can also specify a return function or an event manager as the topic of the “On” method as follows: “Web3.eth.on (” Newblock “, Invent)”.
- Remember that events are only issued if a contract is called up. For example, if you call a function of the contract from another contract, no event will be issued.
- If you need greater control over the process management process (e.g. for complex applications), take into account the use of Web3.js V5.X.X.
We hope that helps! Let us know in the comments if you have any questions or need further help.