Start a conversation

Listening to events

Using our Blockchain gateway and web3js, we set up event listeners for our newly created network.  For this tutorial, we would be using the TetherContract. The TetherContract is the most used contract on ethereum. The use case is that we want to stream all the transfers of tether through a websocket. Using this example, it should show an event approximately every 15 seconds and by the end of this tutorial, you would have a websocket listener for your network events!


Installing dependencies

To start using web3.js, there are some dependencies required. 

First, check for node js by running:  

node -v

That should return the version of node you have e.g  

v13.1.0 . Otherwise, install node. 

 Also install npm.

Then you can install web3, by running this in your terminal:  

npm install web3


Setting up your network

To setup your network:

  • Go to our blockchain gateway: https://tbg.unchain.io/
  • Login or Sign in and in the networks tab, click on 'Create New Network'
  • Fill in the Network name, you can give it a specific name 
  • Choose the protocol: Ethereum  
  • Select a network configuration: We will be using Mainnet for this tutorial

Mainnet is the actual ethereum network where transactions are happening live, while Ropsten is the test network. Since we will be using Mainnet for this tutorial, you can learn more about mainnet here

Once you create your network you should get a TbgNodes URL like this: wss://mainnet.nodes.unchain.io/v0/ws/ed83f172-9060-434f-88dd-49568f3148f3 and a username and password

We strip this url of 'wss://' so that all we're left with is this part of the url: mainnet.nodes.unchain.io/v0/ws/ed83f172-9060-434f-88dd-49568f3148f3. This is the part we would need to create the secure URL


Assigning variables

In your js file, assign your dependencies by adding:

const Web3 = require('web3');

Copy the username and password from the network you created in the step above and assign your username and your password to the username and password variables as shown in the code snippet below. 

Concatenate with the URL gotten above (the one which we have now stripped of 'wss://') to get your full secure websocket url, which we would use to connect to the Ethereum mainnet. 

const username = 'YOUR_USERNAME_GOES_HERE';
const password = 'YOUR_PASSWORD_GOES_HERE';
const wsSecURL = 'wss://' + username + ':' + password + '@mainnet.nodes.unchain.io/v0/ws/43fe099-2c4b-48b1-b557-5e862a1f8f8b';

Also, assign the secure URL to the web3 websocket provider by adding:

const web3ws = new Web3(new Web3.providers.WebsocketProvider(wsSecURL));


Setting up your contract

To set up our contract, we need the contract abi and the contract address. As mentioned earlier, for this tutorial, we would be using the Tether Contract. You can can copy the ABI for the Tether contract here. And to get the tether contract address, you can go here to get it. If you want to use your own contract abi and address then you can also use that.

In your root directory, make a contract_abi.json file, and copy the contents of your contract abi to this file.  

When you get the contract address and have the contract_abi.json file in your root directory then you can assign them to their variables as shown below: 

const contract_abi = require('./contract_abi.json');
const contractAddress = 'YOUR_CONTRACT_ADDRESS_GOES_HERE';

Finally, create your contract by adding the line shown below. This web3js method takes the contract abi and the contract address and creates a contract from it. This the contract which we will read from. 

var Contract = new web3ws.eth.Contract(contract_abi, contractAddress);


Listening to Events

We have the TetherContract set up, whose events we would like to listen to. Add this piece of code below to be able to listen to events from the contract. This logs when there's a new event, when an event is changed, or if there's an error. 

Contract.events.allEvents()
    .on('data', (event) => {
        console.log(event)
        // store event to local database or present to client
    })
    .on('changed', (event) => {
        console.log(event)
        // i.e. remove event from local database or remove for client state
    })
    .on('error', (error) => {
        // something went wrong
        console.log(error);
    });


Finishing up

You can run this program by running the command below in your terminal. 

node name_of_file.js

When you run this command, you would see something like this in the log, which will be updated with new events approximately every 15 seconds. 


There, you can now listen to events happening in your smart contract!

To see the full code sample for this tutorial, go here.

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Support Engineer

  2. Posted
  3. Updated

Comments