How to Send ERC-20 Tokens

Transferring or sending ERC-20 tokens between accounts is relatively straightforward when using wallets and platforms like MetaMask or Coinbase. However, behind the scenes of these platforms, there is a need for complex underlying infrastructures that take time to develop. As such, when it comes to dApp (decentralized application) development, it is relatively hard to transfer ERC-20 Ethereum tokens without the proper tools. This is where Moralis comes into the picture, allowing us to add this functionality with just a single line of code. For this reason, we’re going to take a closer look in this article at the process of how to send ERC-20 tokens using Moralis.

Implementing the ability to send ERC-20 tokens into dApps has been a cumbersome task from a traditional perspective. The main reasons for this are the complex underlying data structure requirements and the need for working APIs. However, this is no longer the case with Moralis. As a platform user, it’s possible to utilize the already developed infrastructure provided by Moralis. This means that the platform takes care of all the heavy lifting. Meanwhile, you can direct your attention to develop the frontend and smart contracts of your dApps.

Moreover, the combination of the pre-built backend infrastructure and additional features such as Moralis Speedy Nodes, the NFT API, Price API, etc., makes Moralis the ultimate middleware for Web3 development. This means that the platform provides you with everything you need to develop and deploy your dApps.

So, if you want to get into dApp development, then sign up with Moralis right away! By signing up, you’ll get immediate access to all the platforms’ tools, which will benefit all your future blockchain projects. 

What are Ethereum ERC-20 Tokens?

Before we dive deeper into the process of how to send ERC-20 Ethereum tokens from one address to another, it’s a good idea to get a better understanding of what ERC-20 tokens are. As such, we’ll take a closer look throughout this section at Ethereum tokens and what ”ERC-20” actually means.

First of all, ERC-20 tokens are so-called fungible tokens, and they are one of the most powerful features of Ethereum’s ecosystem along with NFTs. The tokens can virtually represent anything from fiat currency to skill points in a blockchain-based game. As the use cases for these tokens are next to limitless, it’s highly beneficial to have a regulating standard. 

This is exactly where the ERC-20 standard comes into play. This standard regulates fungible tokens to ensure that they have the same properties. Tokens applying this standard, therefore, share the same value and type, making them interchangeable. As such, we can, for example, swap one Ether for another as they share the same properties.

ERC-20 tokens are often associated with Ethereum, and rightfully so, as this is the chain from which the standard originated. However, it isn’t limited to Ethereum as other chains utilizing the EVM (Ethereum Virtual Machine) generally also apply the standard. This means that ERC-20 tokens also exist within the ecosystems of other chains such as Polygon and BSC.

The standard implements an API for tokens that use smart contracts. It provides ERC-20 tokens with functions, such as transferring tokens between accounts and setting a total supply. 

Furthermore, the following events and methods are necessary to be considered an ERC-20 token:

Transferring ERC-20 Tokens with Moralis 

Transferring ERC-20 tokens from one address to another with just one line of code might sound like magic. However, it’s actually just Moralis. To provide you with a better understanding of how this is possible, we’re going to explain Moralis more in-depth in this section. 

There are currently more than 5,000 blockchain projects utilizing the Moralis platform. This allows them to develop dApps without unnecessary costs and complexity. Furthermore, it’s not only possible to develop dApps with Moralis. You can also develop tokens, create smart contracts, etc. Also, using Moralis’ APIs and software provides excellent scalability opportunities for your projects.

One of the most complex issues with Web3 development is developing a backend infrastructure. This takes both significant time and resources, but it can all be avoided by using Moralis. The platform provides an underlying infrastructure enabling you to shorten the time to market for all your projects removing the most bothersome process of Web3 development. 

Furthermore, along with some of the tools mentioned previously, Moralis also has native support for IPFS, cross-chain compatibility, and real-time alerts, etc. The platform also offers a blog where you’ll find guides and tutorials, enabling you to become a better developer. For example, just recently, an article was published explaining Ethereum development for beginners.

As Moralis provides both the infrastructure and development tools, the right path to becoming a dApp developer is to join Moralis. Signing up with the platform is totally free, and you’ll receive immediate access to all the platform’s features!  

How to Send ERC-20 Tokens with One Line of Code

Adding the ability to transfer ERC-20 tokens to a dApp has always been a pressing issue. The main reason for this is that the process requires us to set up a complex data structure that takes both unnecessary time and resources. However, we can avoid this detour by utilizing the already managed backend infrastructure of Moralis. Utilizing the platform allows us to add this functionality with just one single line of code! 

In the following sections, we’re going to demonstrate the power of Moralis and walk you through the complete process of how to transfer ERC-20 tokens by creating a simple web application. We’ll create a project from scratch with a single HTML file that will contain all the necessary code. The file will include some imports, a single button, and a function that takes care of the logic of sending the tokens. 

However, before developing the app, we must first create a MetaMask account. Having a MetaMask account is great in general, but we are, in this case, going to use the account as the sending address when transferring the ERC-20 tokens. So, let’s kick off this tutorial by looking closer at what MetaMask is and how you can get going with this crypto wallet.

MetaMask Explained

MetaMask is the largest and most well-established crypto wallet on the market, with over ten million active users. It’s possible to get MetaMask in two different versions, either a browser extension or a mobile application. However, in this case, we recommend getting the extension as it is more suitable for this tutorial. 

The wallet provides users with all the essential functions to fully manage all their crypto assets. As such, MetaMask users can swap, purchase, and sell assets through the platform. Furthermore, along with being a wallet, MetaMask also acts as a gateway to the extensive Ethereum ecosystem of dApps. 

Setting up MetaMask is quite simple; all we need to do is download the extension from MetaMask’s official website. Once we have the extension downloaded and added to our browser, we can start the process by clicking the ”Get Started” button. Once clicked, we’ll receive two different options: import a wallet or create a new one from scratch.

If we select the import option, we need to input the seed phrase of our already existing wallet. This is a 12-word seed phrase that you would need to input in a specific order. If the other option is selected, we create a new wallet from scratch.

However, if you run into trouble creating your account, check out the following article from the Moralis blog, that will give you a complete breakdown of MetaMask and how to set up an account. 

How to Send ERC-20 Tokens — Create a New HTML File

With a MetaMask account all set up, we can move on to the main topic of this article: “How to Transfer ERC-20 Tokens”. So, to send ERC-20 Ethereum tokens from one crypto address to another, we’re going to create our own application. As such, the first step is to set up a new project and create a new HTML file. It doesn’t really matter what we call it, but in this example, we’re calling the file ”index.html”. 

The first thing we’re going to add to the file is a few imports. We’re going to import both Web3 and Moralis. As such, this is what the imports will look like in the code itself: 

With the imports implemented, we can add a button that we’ll call ”Run code”. This button will have an ”onclick” event that will trigger our application’s function whenever it’s clicked. In this example, we’re going to run the ”runcode()” function. In the image below, the function only prints ”hello”; however, we will change this in the next step so that this function takes care of all the logic for sending ERC-20 tokens.

How to Send ERC-20 Tokens — ”runcode()” Logic

Now that we’ve finalized the fundamental structure for our application, we can continue by adding our logic to the ”runcode()” function. This process is easy, and all we need is one line of code that we can fetch from the Moralis documentation. From the documentation, we need to get the code for ERC-20 tokens, and it looks like this:

As you can see, this is actually five lines of code; however, we are structuring the code in this way to make it easier to understand. Furthermore, if we’d like, we can shorten it down to only include the last line where the Moralis ”transfer()” function is called.

However, let’s analyze the code to get a better understanding of what’s actually happening. We’re creating a new object with a few properties. First, we specify the type, which in this case is ”erc20”. 

With the type added, we can continue by setting the amount. In this case, the amount is set to 0.5 ETH, followed by the decimals, which are set to 18. Also, take notice that we are using a helper function from Moralis here to make the process of determining the amount a bit easier. Once we’ve set the proper amount, we can add the receiver address followed by the token address. 

With the new object complete, we can pass this as an argument when we call the Moralis ”transfer()” function, which executes the transaction.

How to Send ERC-20 Tokens — Testing the Application

With the code completed, we can go ahead and test our application. Once the application is launched and the ”Run code” button is clicked, it will trigger the ”runcode()” function. Once we run this function, a MetaMask window will pop up asking you to confirm the transaction.

As such, the current account that you are signed into will be the sending address for the ERC-20 tokens. Furthermore, this window will also display the transaction fee for sending the tokens. It’s important to note that every transaction comes with a small fee, and this fee may vary depending on current gas prices. 

That’s it; this is all there is to create our own app that can be used to transfer ERC-20 Ethereum tokens! All that we’ve done is made an object that we pass as an argument when we call the Moralis function, allowing us to transfer ERC-20 tokens with just one line of code.

However, if you still have questions regarding this process, check out the following video from Moralis’ YouTube channel. There you’ll see an even more clear walkthrough where Ivan takes you through the complete tutorial:

How to Send ERC-20 Tokens — Summary

Traditionally speaking, it has been complicated to enable the ability for dApps to transfer ERC-20 tokens from one address to another. The most pressing issue has been that the process requires a vast and complicated data structure. Such a structure takes both time and resources to develop, making it unattractive for business. However, we can now avoid this by simply utilizing Moralis’ platform and the Moralis SDK

The already developed infrastructure of Moralis allows us to send ERC-20 tokens with just one line of code. As such, we can significantly shorten this process and accomplish the same functionality in just a matter of minutes. All we need to do is create a new HTML file, make some imports, add a button, and finally create a simple function.

Furthermore, the possibilities when using Moralis are endless, meaning that we can do much more than sending ERC-20 tokens. Moralis makes Web3 development both quick and easy. To showcase this, check out the following articles on “How to Create a BSC Token” and “How to Verify Smart Contracts”.

Using the tools and infrastructure of Moralis will allow you and your business to save vast amounts of resources as well as shorten the time to market for all your future blockchain projects. So, if you’d like to get started in Web3 development, sign up with Moralis today!

Share on facebook
Share on twitter
Share on linkedin
Moralis Blog
Moralis Blog

Related Articles

The latest in Blockchain
Development

Subscribe to the Moralis Magazine, and be the first to find out about the latest Web3 tech!