The ability to easily retrieve balances for NFT and ERC-20 tokens is crucial for many dApp developers. Why? Well, in Web3 applications, users can perform certain actions based on their possession of specific NFTs and their token balance. Thus, learning how to get NFT and ERC-20 token balances should definitely be high on yours and other developers priority list. Luckily, Moralis, the next-gen Web3 development platform, packs all of the most important backend tools you need to manage your dApps with ease. Additionally, With Moralis, the solution for retrieving NFT and ERC-20 token balances comes in three simple steps.
Before we take a closer look at how to get NFT and ERC-20 token balances in three steps using Moralis, we need to ensure that you understand the basics. As such, you need to ask yourself a few questions. First of all, do you know what dApps are? Moreover, do you know what ERC-20 tokens are? How about NFTs? If so, feel free to skim through this introductory section. However, if those terms and concepts are new to you, make sure to do some catching up first. Knowing the basics will make using Moralis an effortless process.
In addition, despite the fact that Moralis eliminates the need to know Solidity, you’ll find it’s far easier if you are proficient in JavaScript (JS). In case you don’t know this relatively simple and highly universal coding language, do not worry. You can learn JavaScript fast at Ivan on Tech Academy or hire an experienced JavaScript frontend developer.
How to Get NFT and ERC-20 Token Balances
As mentioned, before we jump into details of how to get NFT and ERC-20 token balances with maximum ease, let’s cover some basics. In the section below, you’ll find to-the-point and basic explanations of some essential terms. Moreover, the various terms will have links to specific articles covering the related topic. Make sure to read those articles if you want to attain a deeper understanding.
“How to Get NFT and ERC-20 Token Balances” – Prerequisites
Despite the fact that blockchain technology has made a solid name for itself, it is still relatively new. As such, the terms related to that disruptive and revolutionizing industry aren’t yet well known. As such, we’ll outline some of the basic concepts you should understand to make the most of Moralis. You’ll get familiarized with many terms as you continue your path in the Web3 world. You can find the list below.
- dApp – dApps or Web3 apps are decentralized applications. This means they function on a blockchain. While there are many types of blockchains that can host dApps, Ethereum remains the most popular one. It is the Ethereum network where smart contracts were first introduced.
- Smart Contracts – Smart contracts are pieces of code that have certain predetermined conditions specified. When those conditions are met, the smart contract executes. Smart contracts ensure that agreements are executed on the internet securely and faithfully to their terms.
- Web3 – Web3 is the next big era of the internet, which utilizes blockchain technology. Aside from containing all the features of Web2, it is also decentralized. On top of that, it has built-in payment transaction capabilities using cryptocurrencies.
- ERC-20 Tokens – ERC-20 is a standard used for creating and issuing smart contracts on the Ethereum blockchain. Tokens typically refer to cryptocurrencies native to a particular blockchain. Additionally, tokens are generally built on top of the tokens’ relevant blockchains.
- NFTs – NFTs, or non-fungible tokens, are assets created on the blockchain to be unique. This relates to the concept of non-fungibility, or uniqueness. For instance, if you have two ETH tokens, they are interchangeable (t.i. fungible). On the other hand, each NFT is unique, even though there may be multiple copies of a particular NFT. While NFT use cases started in the form of “putting” images on the blockchain, they offer an extremely wide range of use cases. In the future, NFTs may be used for all sorts of ownership proofs. Essentially, NFTs serve the role of bridging physical assets and digital ownership.
- MetaMask – MetaMask is one of the most popular cryptocurrency wallets. Besides serving as a wallet, MetaMask also acts as a gateway to access dApps, enabling web applications to communicate with blockchains more easily.
These basics will be important when it comes to helping you understand how to get NFT and ERC-20 token balances in three steps with Moralis. However, in case you’re interested in expanding your blockchain and crypto knowledge beyond these concepts, we highly recommend you to check out the Moralis blog. The Moralis blog is completely free and comes with daily in-depth updates on everything from how to create an Arbitrum token in six steps to how to authenticate users with MetaMask using Moralis!
Why Getting NFT and ERC-20 Balances is Important to dApp Developers
The key advantage of Web3 is decentralization. That way, there’s no single entity owning the servers and thus being in full control of the content and personal data. Moreover, this also eliminates the issue of a single point of failure. While these qualities may be impressive, they are not worth much if users cannot interact with dApps.
Here is where the importance of being able to get NFT and ERC-20 token balances comes into play. The latter determines whether or not users will be able to perform certain actions. Whether we consider staking, trading, purchasing, minting, borrowing, or any other “decentralized” action, knowing the users’ balance is essential. In other words, the users’ balance serves as an important variable that determines whether or not specific actions are possible.
While the majority of NFTs and all ERC-20 tokens are on the Ethereum blockchain, getting balance for tokens and NFTs on other blockchains is equally important. However, since Ethereum is the most popular chain, we will focus on the latter. Be sure to check out the full Moralis NFT API, which is already in use by high-profile projects like SuperFarm!
How Moralis Makes Getting NFT and ERC-20 Token Balances Dramatically Easier
While backend programming for dApps is rather tricky and immensely time-consuming, using Moralis makes things dramatically easier. By knowing JavaScript and the basics of blockchain, such as how to use MetaMask, you have all you need to start developing dApps.
With Moralis, all your backend coding gets covered by copying and pasting simple lines of code. Below, we cover the details of how to get NFT and ERC-20 token balances using the Moralis documentation.
To set things up properly before you can start building the functions that will enable you to get NFT and ERC-20 balances, make sure to watch the video below. We strongly recommend you create your free Moralis account right away so you can put this theory into practice as soon as possible!
-
Enable Login Using MetaMask
Before moving on to functions that enable balance retrieval, we need to set things up so that users can be authenticated. You can find the necessary code in the Moralis documentation within the “Crypto Wallet Login” category. Then simply copy and paste the code lines shown below.
Moralis.Web3.authenticate().then(function (user) {
console.log(user.get(‘ethAddress’))
})
Then, use your JavaScript knowledge to build the function around the Moralis code. The code above will enable users to sign in using their MetaMask. It basically captures users’ Ethereum addresses used in MetaMask.
Enabling users to log in is the first step required to capture balances on that particular Ethereum address. Once you’ve set the log-in option in place, you can move on to the next two steps. It is those two steps that will show you how to get NFT and ERC-20 token balances.
-
Display ERC-20 Token Balances
As promised, things are straightforward when using Moralis. Just as with the previous step, we’ll make the Moralis documentation work for us. This time we will go to the “Token Balances” section. When it comes to ERC-20 token balances, you have two options. You can go with a simple single line of code:
const balances = await Moralis.Web3.getAllERC20();
In case you want to specify a chain (if you want to capture balances from another chain than Ethereum) and a specific address, the code is just slightly longer:
const options = { chain: ‘bsc’, address: “0x…” }
const balances = await Moralis.Web3.getAllERC20(options);
As you can see, things are extremely simple with Moralis. Using the above lines of code does all the heavy lifting. Pretty amazing, right? Let’s move on to NFT balances.
-
Display NFT Balances
Guess what? As for the ERC-20 token balances, Moralis documentation offers a simple solution for NFT balances as well. For this purpose, you need to go to the “NFT Balances” section. There you will find several options (shown below).
// get NFTs for current user on Mainnet
const userEthNFTs = await Moralis.Web3.getNFTs();
// get testnet NFTs for user
const testnetNFTs = await Moralis.Web3.getNFTs({ chain: ‘ropsten’ });
// get polygon NFTs for address
const options = { chain: ‘matic’, address: ‘0x…’ };
const polygonNFTs = await Moralis.Web3.getNFTs(options);
Use the option that is suitable for your project. Aside from getting all NFTs from the current user or address, you may also get the total number of NFTs. For that purpose, you need to use the following simple line of code:
const nftCount = await Moralis.Web3.getNFTsCount();
We’ve just covered the basics of how to get NFT and ERC-20 token balances using Moralis. Essentially, all you need are three lines of code. We don’t know about you, but we believe it’s awesome how simple it is.
In case you’d like to follow more detailed step-by-step instructions for the above code implementation, make sure to watch the video below.
How to Get NFT and ERC-20 Token Balances in 3 Steps – Summary & Key Takeaways
At this point, you now know how to get NFT and ERC-20 token balances in three simple steps. This simplicity is possible thanks to the most amazing Web3 development platform called Moralis. It takes just a couple of simple lines of code!
Moreover, you are now also aware that knowing how to get NFT and ERC-20 tokens is extremely important for the functionality of dApps. It is this feature that enables users to take further action and fully interact with decentralized applications. This means you can get NFT balances and get ERC-20 balances without any hassle when you use Moralis!
We also provide some basic terms and explanations to help those of you new to the blockchain world. On top of that, we also highly recommend checking out our other blog posts. There you can find reliable and highly useful information about blockchain development. Using that as a resource, you can learn all the basics needed to take your next steps with confidence.
Next Steps in Developing dApps
As such, there are two most reasonable paths for you if you want to develop awesome dApps by focusing mainly on the frontend:
- If you already know JavaScript and know how to use MetaMask, you are basically all set. Just create your free account today and let Moralis do the heavy lifting for you. Then use Moralis documentation and Moralis YouTube channel to get totally familiar with this amazing platform.
- In case you don’t know JavaScript yet, we suggest you focus on learning it. Thanks to the Ivan on Tech Academy, you can enroll in a special course called “Javascript Programming for Blockchain Developers.” That way, you will learn this highly practical coding language and, at the same time, pace yourself to use it in the cutting-edge industry. Of course, there are numerous other amazing courses to turn yourself into a blockchain expert in no time. That way, you can go full-time crypto sooner than later. If you need some inspiration, make sure to check out some amazing success stories our students have.
Now it is up to you to take action, and start building amazing dApps with ease!