How to Send ERC20 Token with Web3.js?
Last Updated :
28 Apr, 2025
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:
- Set up the web3.js environment and load the ERC20 contract ABI.
- Retrieve the ERC20 contract instance using the contract address.
- Estimate the gas limit and gas price required for the transaction.
- Sign the transaction using the private key of the sender's account.
- 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:
- This code imports the Web3 library, the ERC20 library, and creates an instance of Web3 that connects to the Ethereum mainnet via Infura.
- 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.
- 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.
- 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.
Similar Reads
Computer Network Tutorial A Computer Network is a system where two or more devices are linked together to share data, resources and information. These networks can range from simple setups, like connecting two devices in your home, to massive global systems, like the Internet. Below are some uses of computer networksSharing
6 min read
Computer Network Basics
Basics of Computer NetworkingA computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
10 min read
Types of Computer NetworksA computer network is a system that connects many independent computers to share information (data) and resources. The integration of computers and other different devices allows users to communicate more easily. It is a collection of two or more computer systems that are linked together. A network
7 min read
Introduction to InternetComputers and their structures are tough to approach, and it is made even extra tough when you want to recognize phrases associated with the difficulty this is already utilized in regular English, Network, and the net will appear to be absolutely wonderful from one some other, however, they may seem
10 min read
Types of Network TopologyNetwork topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
11 min read
Network DevicesNetwork devices are physical devices that allow hardware on a computer network to communicate and interact with each other. Network devices like hubs, repeaters, bridges, switches, routers, gateways, and brouter help manage and direct data flow in a network. They ensure efficient communication betwe
9 min read
What is OSI Model? - Layers of OSI ModelThe OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re
13 min read
TCP/IP ModelThe TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Difference Between OSI Model and TCP/IP ModelData communication is a process or act in which we can send or receive data. Understanding the fundamental structures of networking is crucial for anyone working with computer systems and communication. For data communication two models are available, the OSI (Open Systems Interconnection) Model, an
4 min read
Physical Layer
Physical Layer in OSI ModelThe physical Layer is the bottom-most layer in the Open System Interconnection (OSI) Model which is a physical and electrical representation of the system. It consists of various network components such as power plugs, connectors, receivers, cable types, etc. The physical layer sends data bits from
4 min read
Types of Network TopologyNetwork topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
11 min read
Transmission Modes in Computer Networks (Simplex, Half-Duplex and Full-Duplex)Transmission modes also known as communication modes, are methods of transferring data between devices on buses and networks designed to facilitate communication. They are classified into three types: Simplex Mode, Half-Duplex Mode, and Full-Duplex Mode. In this article, we will discuss Transmission
6 min read
Types of Transmission MediaTransmission media is the physical medium through which data is transmitted from one device to another within a network. These media can be wired or wireless. The choice of medium depends on factors like distance, speed, and interference. In this article, we will discuss the transmission media. In t
9 min read
Data Link Layer
Data Link Layer in OSI ModelThe data link layer is the second layer from the bottom in the OSI (Open System Interconnection) network architecture model. Responsible for the node-to-node delivery of data within the same local network. Major role is to ensure error-free transmission of information. Also responsible for encoding,
4 min read
What is Switching?Switching is the process of transferring data packets from one device to another in a network, or from one network to another, using specific devices called switches. A computer user experiences switching all the time for example, accessing the Internet from your computer device, whenever a user req
5 min read
Virtual LAN (VLAN)Virtual LAN (VLAN) is a concept in which we can divide the devices logically on layer 2 (data link layer). Generally, layer 3 devices divide the broadcast domain but the broadcast domain can be divided by switches using the concept of VLAN. A broadcast domain is a network segment in which if a devic
7 min read
Framing in Data Link LayerFrames are the units of digital transmission, particularly in computer networks and telecommunications. Frames are comparable to the packets of energy called photons in the case of light energy. Frame is continuously used in Time Division Multiplexing process. Framing is a point-to-point connection
6 min read
Error Control in Data Link LayerData-link layer uses the techniques of error control simply to ensure and confirm that all the data frames or packets, i.e. bit streams of data, are transmitted or transferred from sender to receiver with certain accuracy. Using or providing error control at this data link layer is an optimization,
4 min read
Flow Control in Data Link LayerFlow control is design issue at Data Link Layer. It is a technique that generally observes the proper flow of data from sender to receiver. It is very essential because it is possible for sender to transmit data or information at very fast rate and hence receiver can receive this information and pro
4 min read
Piggybacking in Computer NetworksPiggybacking is the technique of delaying outgoing acknowledgment temporarily and attaching it to the next data packet. When a data frame arrives, the receiver waits and does not send the control frame (acknowledgment) back immediately. The receiver waits until its network layer moves to the next da
5 min read
Network Layer
Network Layer in OSI ModelThe Network Layer is the 5th Layer from the top and the 3rd layer from the Bottom of the OSI Model. It is one of the most important layers which plays a key role in data transmission. The main job of this layer is to maintain the quality of the data and pass and transmit it from its source to its de
5 min read
Introduction of Classful IP AddressingClassful IP addressing is an obsolete method for allocating IP addresses and dividing the available IP address space across networks. It was used from 1981 to 1993 until the introduction of CIDR (Based on Prefixes rather than classes). Classful method categorizes IP addresses into five classes (A, B
10 min read
Classless Addressing in IP AddressingThe Network address identifies a network on the internet. Using this, we can find a range of addresses in the network and total possible number of hosts in the network. Mask is a 32-bit binary number that gives the network address in the address block when AND operation is bitwise applied on the mas
7 min read
What is an IP Address?Imagine every device on the internet as a house. For you to send a letter to a friend living in one of these houses, you need their home address. In the digital world, this home address is what we call an IP (Internet Protocol) Address. It's a unique string of numbers separated by periods (IPv4) or
14 min read
IPv4 Datagram HeaderIP stands for Internet Protocol and v4 stands for Version Four (IPv4). IPv4 was the primary version brought into action for production within the ARPANET in 1983. IP version four addresses are 32-bit integers which will be expressed in decimal notation. In this article, we will discuss about IPv4 da
4 min read
Difference Between IPv4 and IPv6IPv4 and IPv6 are two versions of the system that gives devices a unique address on the internet, known as the Internet Protocol (IP). IP is like a set of rules that helps devices send and receive data online. Since the internet is made up of billions of connected devices, each one needs its own spe
7 min read
Difference between Private and Public IP addressesIP Address or Internet Protocol Address is a type of address that is required to communicate one computer with another computer for exchanging information, file, webpage, etc. Public and Private IP address are two important parts of device identity. In this article, we will see the differences betwe
6 min read
Introduction To SubnettingSubnetting is the process of dividing a large network into smaller networks called "subnets." Subnets provide each group of devices with their own space to communicate, which ultimately helps the network to work easily. This also boosts security and makes it easier to manage the network, as each sub
8 min read
What is Routing?The process of choosing a path across one or more networks is known as Network Routing. Nowadays, individuals are more connected on the internet and hence, the need to use Routing Communication is essential.Routing chooses the routes along which Internet Protocol (IP) packets get from their source t
10 min read
Network Layer ProtocolsNetwork Layer is responsible for the transmission of data or communication from one host to another host connected in a network. Rather than describing how data is transferred, it implements the technique for efficient transmission. In order to provide efficient communication protocols are used at t
9 min read
Transport Layer
Session Layer & Presentation Layer
Session Layer in OSI modelThe Session Layer is the 5th layer in the Open System Interconnection (OSI) model which plays an important role in controlling the dialogues (connections) between computers. This layer is responsible for setting up, coordinating, and terminating conversations, exchanges, and dialogues between the ap
6 min read
Presentation Layer in OSI modelPresentation Layer is the 6th layer in the Open System Interconnection (OSI) model. This layer is also known as Translation layer, as this layer serves as a data translator for the network. The data which this layer receives from the Application Layer is extracted and manipulated here as per the req
4 min read
Secure Socket Layer (SSL)SSL or Secure Sockets Layer, is an Internet security protocol that encrypts data to keep it safe. It was created by Netscape in 1995 to ensure privacy, authentication, and data integrity in online communications. SSL is the older version of what we now call TLS (Transport Layer Security).Websites us
10 min read
PPTP Full Form - Point-to-Point Tunneling ProtocolPPTP Stands for Point-to-Point Tunneling Protocol is a widely used networking protocol designed to create a secure private connection over a public network like the internet. It is Developed by Microsoft and other tech companies in the 1990s It is one of the first protocols used for Virtual Private
5 min read
Multipurpose Internet Mail Extension (MIME) ProtocolMIME (Multipurpose Internet Mail Extensions) is a standard used to extend the format of email messages, allowing them to include more than just text. It enables the transmission of multimedia content such as images, audio, video, and attachments, within email messages, as well as other types of cont
4 min read
Application Layer
Application Layer in OSI ModelThe Application Layer of OSI (Open System Interconnection) model, is the top layer in this model and takes care of network communication. The application layer provides the functionality to send and receive data from users. It acts as the interface between the user and the application. The applicati
5 min read
Client-Server ModelThe Client-Server Model is a distributed architecture where clients request services and servers provide them. Clients send requests to servers, which process them and return the results. Clients donât share resources among themselves but depend on the server. Common examples include email systems a
5 min read
World Wide Web (WWW)The World Wide Web (WWW), often called the Web, is a system of interconnected webpages and information that you can access using the Internet. It was created to help people share and find information easily, using links that connect different pages together. The Web allows us to browse websites, wat
6 min read
Introduction to Electronic MailIntroduction:Electronic mail, commonly known as email, is a method of exchanging messages over the internet. Here are the basics of email:An email address: This is a unique identifier for each user, typically in the format of [email protected] email client: This is a software program used to send,
4 min read
What is a Content Distribution Network and how does it work?Over the last few years, there has been a huge increase in the number of Internet users. YouTube alone has 2 Billion users worldwide, while Netflix has over 160 million users. Streaming content to such a wide demographic of users is no easy task. One can think that a straightforward approach to this
4 min read
Protocols in Application LayerThe Application Layer is the topmost layer in the Open System Interconnection (OSI) model. This layer provides several ways for manipulating the data which enables any type of user to access the network with ease. The Application Layer interface directly interacts with the application and provides c
7 min read
Advanced Topics
What is Network Security?Network security is defined as the activity created to protect the integrity of your network and data. Network security is the practice of protecting a computer network from unauthorized access, misuse, or attacks. It involves using tools, technologies, policies and procedures to ensure the confiden
9 min read
Computer Network | Quality of Service and MultimediaQuality of Service (QoS) is an important concept, particularly when working with multimedia applications. Multimedia applications, such as video conferencing, streaming services, and VoIP (Voice over IP), require certain bandwidth, latency, jitter, and packet loss parameters. QoS methods help ensure
7 min read
Authentication in Computer NetworkPrerequisite - Authentication and Authorization Authentication is the process of verifying the identity of a user or information. User authentication is the process of verifying the identity of a user when that user logs in to a computer system. There are different types of authentication systems wh
4 min read
Encryption, Its Algorithms And Its FutureEncryption plays a vital role in todayâs digital world, serving a major role in modern cyber security. It involves converting plain text into cipher text, ensuring that sensitive information remains secure from unauthorized access. By making data unreadable to unauthorized parties, encryption helps
10 min read
Introduction of Firewall in Computer NetworkA firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read
MAC Filtering in Computer NetworkThere are two kinds of network Adapters. A wired adapter allows us to set up a connection to a modem or router via Ethernet in a computer whereas a wireless adapter identifies and connects to remote hot spots. Each adapter has a distinct label known as a MAC address which recognizes and authenticate
10 min read
Wi-Fi Standards ExplainedWi-Fi stands for Wireless Fidelity, and it is developed by an organization called IEEE (Institute of Electrical and Electronics Engineers) they set standards for the Wi-Fi system. Each Wi-Fi network standard has two parameters : Speed - This is the data transfer rate of the network measured in Mbps
4 min read
What is Bluetooth?Bluetooth is used for short-range wireless voice and data communication. It is a Wireless Personal Area Network (WPAN) technology and is used for data communications over smaller distances. This generation changed into being invented via Ericson in 1994. It operates within the unlicensed, business,
6 min read
Generations of wireless communicationWe have made very huge improvements in wireless communication and have expanded the capabilities of our wireless communication system. We all have seen various generations in our life. Let's discuss them one by one. 0th Generation: Pre-cell phone mobile telephony technology, such as radio telephones
2 min read
Cloud NetworkingCloud Networking is a service or science in which a companyâs networking procedure is hosted on a public or private cloud. Cloud Computing is source management in which more than one computing resources share an identical platform and customers are additionally enabled to get entry to these resource
11 min read
Practice
Top 50 Plus Networking Interview Questions and Answers for 2024Networking is defined as connected devices that may exchange data or information and share resources. A computer network connects computers to exchange data via a communication media. Computer networking is the most often asked question at leading organizations such Cisco, Accenture, Uber, Airbnb, G
15+ min read
Top 50 TCP/IP Interview Questions and Answers 2025Understanding TCP/IP is essential for anyone working in IT or networking. It's a fundamental part of how the internet and most networks operate. Whether you're just starting or you're looking to move up in your career, knowing TCP/IP inside and out can really give you an edge.In this interview prepa
15+ min read
Top 50 IP Addressing Interview Questions and AnswersIn todayâs digital age, every device connected to the internet relies on a unique identifier called an IP Address. If youâre aiming for a career in IT or networking, mastering the concept of IP addresses is crucial. In this engaging blog post, weâll explore the most commonly asked IP address intervi
15+ min read
Last Minute Notes for Computer NetworksComputer Networks is an important subject in the GATE Computer Science syllabus. It encompasses fundamental concepts like Network Models, Routing Algorithms, Congestion Control, TCP/IP Protocol Suite, and Network Security. These topics are essential for understanding how data is transmitted, managed
14 min read
Computer Network - Cheat SheetA computer network is an interconnected computing device that can exchange data and share resources. These connected devices use a set of rules called communication protocols to transfer information over physical or wireless technology. Modern networks offer more than just connectivity. Enterprises
15+ min read