Open In App

How to Send ERC20 Token with Web3.js?

Last Updated : 06 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sending ERC20 tokens is a common task in the world of Ethereum and decentralized applications. ERC20 tokens are a type of digital asset that lives on the Ethereum blockchain and follows a specific set of rules. The article focuses on discussing how to send ERC20 tokens using Web3.js, a popular JavaScript library for interacting with the Ethereum blockchain along with an example code and its explanation.

Prerequisites

  • Understanding of Ethereum and ERC20 tokens.
  • Web3.js enabled environment: This can be a local development environment or a web-based Ethereum wallet such as MetaMask.
  • ERC20 token contract address and ABI (Application Binary Interface): The ABI is a JSON file that contains information about the functions and parameters of the ERC20 contract.

Anatomy of Ethereum Transaction

In Ethereum, a transaction is a message that is sent from one account to another. It contains several pieces of information, including:

  • The address of the sender and recipient.
  • The amount of Ether or ERC20 tokens to be transferred.
  • The gas limit and gas price determine how much the transaction will cost in terms of Ether.
  • The data field can be used to encode additional information such as the function call and its parameters.

Approach

Follow the steps below to send ERC2o tokens with Web3.js:

  1. Set up the web3.js environment and load the ERC20 contract ABI.
  2. Retrieve the ERC20 contract instance using the contract address.
  3. Estimate the gas limit and gas price required for the transaction.
  4. Sign the transaction using the private key of the sender’s account.
  5. Submit the signed transaction to the Ethereum network.

Implementation

To send ERC20 tokens with Web3.js, you can follow the steps below:

1. Import the required modules from Web3.js, ‘web3’ and ‘ERC20’.

const Web3 = require(‘web3’);
const ERC20 = require(‘web3-eth-erc20’);

Here, 

  • const Web3 = require(‘web3’): This statement is used to import the Web3 library into your JavaScript code. This library provides an interface for interacting with the Ethereum blockchain, and it exports an object that you can use to access various methods and properties of the Web3 library.
  • const ERC20 = require(‘web3-eth-erc20’): This statement is used to import the ERC20 library, which is a specific module within the Web3 library that provides an interface for interacting with ERC-20 tokens on the Ethereum blockchain. This library exports an object that you can use to access various methods and properties related to ERC-20 tokens.

2. Set up the connection to the Ethereum network using the Web3.js instance. You can use the Infura API to connect to the mainnet or testnet of your choice.

const web3 = new Web3(‘https://mainnet.infura.io/v3/your-api-key’);

Here,

const web3 = new Web3(‘https://mainnet.infura.io/v3/your-api-key’): This statement is used to create a new instance of the Web3 class, which provides an interface for interacting with the Ethereum blockchain. The new Web3() constructor takes a URL as an argument, which specifies the location of an Ethereum node that the Web3 instance will connect to. In this case, the URL provided is for the Infura Ethereum node, which is a publicly accessible Ethereum node that you can use to interact with the Ethereum mainnet (i.e., the main Ethereum network).

3. Once this statement is executed, the web3 variable will contain an instance of the Web3 class that is connected to the Infura Ethereum node. You can then use this instance to perform various operations on the Ethereum blockchain, such as reading and writing data and interacting with smart contracts.

4. Instantiate the ERC20 contract object using the contract ABI and the address of the ERC20 token. You can get the ABI and address from the token’s official website or from a third-party contract explorer like Etherscan.

const contract = new ERC20(web3, ‘0x1234567890abcdefghijklmnopqrstuvwxyz’, {
 from: ‘0x1234567890abcdefghijklmnopqrstuvwxyz’,
 gas: 6721975,
});

Here,

const contract = new ERC20(web3, ‘0x1234567890abcdefghijklmnopqrstuvwxyz’, { from: ‘0x1234567890abcdefghijklmnopqrstuvwxyz’, gas: 6721975,}): This statement is used to create a new instance of the ERC20 class, which provides an interface for interacting with ERC-20 tokens on the Ethereum blockchain. The new ERC20() constructor takes two arguments:

  • The web3 instance that you created in the previous step, provides the connection to the Ethereum node.
  • The address of the ERC-20 token contract on the Ethereum blockchain.

The new ERC20() constructor also takes an optional third argument, which is an object containing various options for configuring the ERC20 instance. In this case, the options object includes the from the property, which specifies the Ethereum address that will be used as the sender of transactions, and the gas property, which specifies the maximum amount of gas (i.e., the fee paid to the Ethereum network to process transactions) that will be used for transactions.

5. Once this statement is executed, the contract variable will contain an instance of the ERC20 class that is connected to the specified ERC-20 token contract on the Ethereum blockchain. One can then use this instance to perform various operations on the ERC-20 token, such as checking the token balance of an Ethereum address or transferring tokens to another address.

6. Check the balance of the ERC20 token by calling the ‘balanceOf()’ method. This method takes the address of the wallet as input and returns the balance of the token in the wallet.

const balance = await contract.balanceOf(‘0x1234567890abcdefghijklmnopqrstuvwxyz’);
console.log(balance);

The output of this code would be the balance of the Ethereum contract associated with the address ‘0x1234567890abcdefghijklmnopqrstuvwxyz’, which is retrieved by calling the balanceOf() method on the contract object. This value would be printed to the console using the console.log() method.

7. To send the ERC20 token, call the ‘transfer()’ method and pass the recipient’s address and the amount of tokens to be transferred as input parameters.

await contract.transfer(‘0x1234567890abcdefghijklmnopqrstuvwxyz’, 10);

The output of this code would be the result of calling the transfer() method on the contract object, with the arguments ‘0x1234567890abcdefghijklmnopqrstuvwxyz’ and 10. This method would likely transfer 10 units of some token or asset associated with the contract to the address ‘0x1234567890abcdefghijklmnopqrstuvwxyz’.

This code example shows how to send ERC20 tokens using Web3.js. The code imports the required modules, set up the connection to the Ethereum network, instantiates the ERC20 contract object, checks the balance of the token, and sends the token to the recipient. You can customize the code to your specific needs, such as adding error handling or using different parameters for the contract methods.

Below is the complete code for the above approach:
 

Javascript




async function main() {
  const Web3 = require('web3');
  const ERC20 = require('web3-eth-erc20');
  const web3 = new Web3('https://mainnet.infura.io/v3/your-api-key');
  const contract = new ERC20(web3, '0x1234567890abcdefghijklmnopqrstuvwxyz', {
    from: '0x1234567890abcdefghijklmnopqrstuvwxyz',
    gas: 6721975,
  });
  const balance = await contract.balanceOf('0x1234567890abcdefghijklmnopqrstuvwxyz');
  console.log(balance);
  await contract.transfer('0x1234567890abcdefghijklmnopqrstuvwxyz', 10);
}
 
main();


Explanation: 

  1. This code imports the Web3 library, the ERC20 library, and creates an instance of Web3 that connects to the Ethereum mainnet via Infura. 
  2. It then creates an instance of an ERC20 contract by providing the contract’s address and the account address to use as the form field when calling contract methods.
  3. The code then calls the balanceOf method on the contract to get the balance of the specified Ethereum address and logs the result to the console. 
  4. It then calls the transfer method on the contract to transfer 10 units of the ERC20 token to the specified Ethereum address.

Note that this code will only work if it is run in an environment that has the necessary libraries installed and if it is able to connect to the Ethereum network.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads