Listening to Smart Contract Events on the Mantle Testnet Using Ethers.js

Introduction

Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They are a fundamental building block of decentralized applications (dApps) on blockchain platforms like Ethereum. One of the key features of smart contracts is the ability to emit events, which are used to notify external entities about specific occurrences within the contract.

In this article, we will focus mainly on listening to ERC-20 token transfer events for the BIT token on the Mantle Testnet using the popular JavaScript library, Ethers.js.

ERC-20 is a widely adopted token standard on the Ethereum blockchain, and transfer events are emitted whenever tokens are transferred between addresses. By monitoring these events, developers can track token movements, update user balances, and trigger additional actions within their dApps.

Why Listen to ERC20 Token Transfer Events?

Listening to ERC-20 token transfer events is essential for developers building dApps that involve token transactions, as it allows them to monitor and react to changes in token balances.

Events can be used to trigger updates in the user interface, notify users about important transactions, or even automate specific tasks within the dApp. By leveraging events, developers can create more responsive and dynamic applications that provide a seamless user experience.

Prerequisites

Before diving into the article, make sure you have the following prerequisites:

  • Basic understanding of Ethereum, smart contracts, and the Solidity programming language.

  • Familiarity with JavaScript and Node.js.

  • Node.js and npm are installed on your local machine.

  • An Ethereum wallet with some test BIT tokens on the Mantle Testnet.

  • A deployed ERC-20 token smart contract on the Mantle Testnet.

Install Ethers.js

First, create a new directory for your project and navigate to it in your terminal. This will be the workspace for your project, where you will store all the necessary files and configurations.

mkdir mantle-erc20-listener
cd mantle-erc20-listener

Next, initialize a new Node.js project using npm init.

This command will prompt you to provide some basic information about your project, such as its name, version, and description. You can accept the default values by pressing Enter, or provide your custom values.

npm init

After initializing your project, you will see a new package.json file in your project directory. This file contains the metadata and dependencies for your project.

Now, install the ethers.js library by running the following command:

npm install --save ethers

This command will download the ethers.js library and add it as a dependency in your package.json file.

Import ethers.js and Set Up the Provider

Create a new JavaScript file (e.g., index.js) in your project directory. This file will contain the main logic for listening to ERC-20 token transfer events.

Open the index.js file in your favorite text editor and import the ethers.js library:

const ethers = require('ethers');

To interact with the Mantle Testnet, you need to set up a provider.

NOTE: A provider is an object that connects your application to a specific Ethereum network, allowing you to send transactions and query the blockchain.

Replace YOUR_MANTLE_RPC_URL with the appropriate RPC URL for the Mantle Testnet:

NOTE: You can get a public RPC URL for the Mantle testnet on the Chainlist website

const provider = new ethers.providers.JsonRpcProvider('YOUR_MANTLE_RPC_URL');

Connect to Your ERC-20 Token Smart Contract

To connect to the BIT ERC-20 token smart contract, you need its ABI (Application Binary Interface) and the contract address. The ABI is a JSON representation of your smart contract's functions and events, which allows ethers.js to encode and decode the data that is sent to and received from the contract.

Currently, the BIT token ABI is not available but we can still use a generic ERC-20 ABI and the smart contract address on the Mantle testnet. This is because all ERC-20 tokens adhere to the same standards, ensuring compatibility and consistent function signatures.

Replace CONTRACT_ABI and CONTRACT_ADDRESS with the appropriate values:

const contractAbi = JSON.parse('CONTRACT_ABI');
const contractAddress = 'CONTRACT_ADDRESS';

Now, create a new instance of the ethers.Contract class, passing in the contract address, ABI, and provider as arguments:

const contract = new ethers.Contract(contractAddress, contractAbi, provider);

This contract object will allow you to interact with your ERC-20 token smart contract, including listening to its events.

Listen to Transfer Events

The ERC-20 token standard includes a Transfer event, which is emitted whenever tokens are transferred between addresses. The event signature is as follows:

event Transfer(address indexed from, address indexed to, uint256 value);

You can listen to this event using the on method of the contract object. The on method takes two arguments: the name of the event you want to listen to, and a callback function that will be executed whenever the event is emitted.

Create a callback function that takes three arguments: from, to, and value. These arguments correspond to the from, to, and value parameters of the Transfer event in the ERC-20 token standard.

Inside the callback function, log the event details to the console:

contract.on('Transfer', (from, to, value, event) => {
  console.log('Transfer event triggered:');
  console.log('  From:', from);
  console.log('  To:', to);
  console.log('  Value:', value.toString());
  console.log('  Event details:', event);
});

Run the Script

Save your index.js file and run it using Node.js:

node index.js

Your script will now listen to the Transfer event on the Mantle Testnet. Whenever the event is emitted, the provided callback function will be executed, and the event details will be logged into the console.

Conclusion

In this comprehensive guide, we have explored how to listen to ERC-20 token transfer events on the Mantle Testnet using ethers.js. With this knowledge, you can now monitor and react to changes in your ERC-20 token smart contracts on the Mantle Testnet, enabling you to build more powerful and engaging decentralized applications.