Thursday, March 30, 2023
The Dao Makers
No Result
View All Result
CRYPTO MARKETCAP
  • Home
  • Bitcoin
  • Launchpads
  • Crypto Updates
    • General
    • Blockchain
    • Ethereum
    • Altcoin
    • Mining
    • Crypto Exchanges
  • NFT
  • DeFi
  • Web3
  • Metaverse
  • Analysis
  • Regulations
  • Scam Alert
  • Videos
  • Home
  • Bitcoin
  • Launchpads
  • Crypto Updates
    • General
    • Blockchain
    • Ethereum
    • Altcoin
    • Mining
    • Crypto Exchanges
  • NFT
  • DeFi
  • Web3
  • Metaverse
  • Analysis
  • Regulations
  • Scam Alert
  • Videos
No Result
View All Result
The Dao Makers
No Result
View All Result

How to Get the Token Balance of an Address – Moralis Web3

by The Dao Makers
December 12, 2022
in Web3
Reading Time: 12 mins read
0 0
A A
0
Home Web3
Share on FacebookShare on Twitter


Be a part of us on this tutorial as we present you probably the most accessible option to get the token stability of an handle! In actual fact, with Moralis and the Token API, you may question an handle’ ERC-20 token stability in three steps: 

Create a challenge and set up the Moralis SDK with the next terminal command:npm set up moralis @moralisweb3/common-evm-utils Arrange a brand new file and name the ”getWalletTokenBalances” endpoint with the next code (add your Moralis API key, an handle, and the chain you need to question): const Moralis = require(‘moralis’).default;
const { EvmChain } = require(‘@moralisweb3/common-evm-utils’);

const runApp = async () => {
await Moralis.begin({
apiKey: “YOUR_API_KEY”,
// …and another configuration
});

const handle=”0xBf6521AF44F5D56813A93dCE548E6594Daa00794″;

const chain = EvmChain.ETHEREUM;

const response = await Moralis.EvmApi.token.getWalletTokenBalances({
handle,
chain,
});

console.log(response.toJSON());
}

runApp(); Execute this system by working the next command: node “FILE_NAME”

When you observe the steps above, you need to obtain a response just like the one proven beneath: 

{
“token_address”: “0xff20817765cb7f73d4bde2e66e067e58d11095c2”,
“identify”: “Amp”,
“image”: “AMP”,
“emblem”: “https://cdn.moralis.io/eth/0xff20817765cb7f73d4bde2e66e067e58d11095c2.png”,
“thumbnail”: “https://cdn.moralis.io/eth/0xff20817765cb7f73d4bde2e66e067e58d11095c2_thumb.png”,
“decimals”: 18,
“stability”: “24109691515670000000000”
}

For extra info relating to the ”getWalletTokenBalances” endpoint, try Moralis’ official get stability by pockets documentation! 

Overview

On-chain information related to pockets addresses is significant info on the subject of Web3 improvement. Many decentralized functions (dapps) and platforms, together with DEXs, wallets, and so forth., depend on info akin to token balances. Consequently, Web3 builders require simple and environment friendly methods to acquire such a blockchain information, which is the place Moralis enters the image! With Moralis’ Token API, you may simply get the token stability of an handle with only some traces of code. If you wish to be taught extra about this, be part of us on this tutorial as we present you the best option to question the ERC-20 stability of a pockets handle! 

To start with, the article seems to be on the fundamentals to briefly define what a pockets handle is and what the token stability of an handle refers to. When you grasp these two simple ideas, we are going to soar straight into the central a part of this tutorial. From there, we are going to present you methods to create an software for querying an handle’ token stability! 

All through this tutorial, you’ll familiarize your self with the Token API. Nevertheless, that is merely one in all Moralis’ varied Web3 APIs. One other distinguished instance you may discover fascinating is the Streams API. With this instrument, you may effortlessly stream on-chain information into the backend of your Web3 initiatives via Moralis webhooks. That mentioned, for this tutorial, you want a Moralis account. Thus, earlier than you progress ahead, enroll with Moralis instantly! 

What’s a Pockets Handle? 

For the uninitiated, the preliminary two sections of this text will briefly discover the fundamental ideas of a pockets handle and the token stability of an handle. As such, if you’re already accustomed to these two elements, be at liberty to skip straight into the ”Tips on how to Get the Token Stability of an Handle…” part. In any other case, be part of us as we begin this information by answering the query, ”what’s a pockets handle?”.

A pockets handle is a randomly generated string of letters and numbers linked to a Web3 pockets. Pockets addresses are additionally typically known as public keys and might be shared, very like an e-mail handle. Furthermore, all blockchain wallets have distinctive addresses, appearing like account numbers the place different customers can ship belongings. Accordingly, it’s via pockets addresses that cryptocurrencies and NFTs might be despatched from one account to a different on blockchains. 

You’ll be able to typically discover your pockets handle fairly simply via your pockets supplier’s person interface (UI), they usually can look one thing like this: ”0xd06Ffc9107649344e7….”. For instance, let’s take a fast have a look at MetaMask, one in all right this moment’s most vital pockets suppliers. By logging in to your MetaMask account, you may simply copy your pockets handle by clicking on the button on the high of the UI: 

metamask user interface showing token balance address

What’s the Token Stability of an Handle? 

As this tutorial teaches you methods to get the token stability of an handle, you have to additionally perceive what an handle’ token stability is. Thankfully, this isn’t that obscure, because it merely refers back to the tokens held by a pockets. Accordingly, the token stability of an handle is all of the NFTs and fungible tokens held by the pockets to which the handle refers! 

three coins falling from the sky, ethereum, solana, and bitcoin

Nonetheless, now that you’ve got familiarized your self with what the token stability of an handle refers to, it’s time to discover the central a part of this text. As such, be part of us within the subsequent part, the place we illustrate methods to get the token stability of an handle! 

Tips on how to Get the Token Stability of an Handle Utilizing Moralis’ Token API 

Within the following sections, you’ll discover ways to get the token stability of an handle utilizing Moralis’ Token API. To display the accessibility of Moralis, we are going to present methods to arrange a simple NextJS software implementing this performance. Consequently, via the app’s UI, you may enter an handle and question its ERC-20 token stability with the press of a button. What’s extra, to make the tutorial as simple as doable to observe, it has been divided into the next 4 sections: 

Software DemoPrerequisites and Moralis SetupHow to Set Up the ApplicationApplication Code Breakdown

By the top of this tutorial, you should have realized methods to get the token stability of an handle utilizing Moralis’ Token API. From there, you may apply the identical elementary ideas to future blockchain initiatives to simply implement comparable performance! 

moralis

When you would moderately watch a video tutorial of the method, you may try the next clip from the Moralis YouTube channel. Within the video, a Moralis software program engineer walks you thru the whole course of and gives an in depth breakdown of the code: 

You may as well be part of us right here as we begin with a demo to point out you the way the appliance works! 

Software Demo 

To kick issues off, this part of the tutorial gives a quick demo of the appliance. Doing so offers you a extra profound understanding of what you might be working in the direction of. Nonetheless, right here is the appliance’s touchdown web page: 

application landing page with the title get any wallets token balance

The person interface (UI) is comparatively simple. It incorporates a heading, an enter area, and a ”Submit” button. To make use of the appliance, all you might want to do is enter a legitimate pockets handle into the enter area and hit ”Submit”. Doing so will generate a response just like the one beneath (relying on the pockets’s tokens):

application page showing input field to enter in a wallet address and also showing the submit button

Consequently, on this occasion, the appliance makes use of Moralis’ Token API to get the token stability of the handle you provided and shows the tokens’ thumbnails, names, and balances in USD. Along with these parts, the API gives different kinds of information, such because the tokens’ addresses, symbols, and so forth. Nevertheless, these should not displayed on the UI. 

If you wish to discover ways to create this software, be part of us within the following sections, the place we offer a complete walkthrough of the whole course of!

Stipulations and Moralis Setup 

Earlier than diving into the appliance code, there are just a few conditions you might want to cope with. To start with, guarantee that you’ve got NodeJS and npm arrange. When you need assistance with this, go to the next web page to put in the newest model of Node.js: https://nodejs.org/en/. 

After getting arrange NodeJS and npm, you have to register for a Moralis account. So, in case you have not already, be part of Moralis instantly. Becoming a member of Moralis is totally free and solely takes a few seconds. Nevertheless, you may marvel, ”why do I want a Moralis account?”. In an effort to work together with the Token API and make calls, you want a Moralis API key. Yow will discover this by logging in to the Moralis admin panel and navigating to the ”Web3 APIs” tab: 

web3 api page on the moralis admin panel

Go forward and replica the API key, as you might want to add it to the code in a while on this tutorial: 

arrow pointing at the copy button for a user api key

That covers the conditions. From right here, it’s time to dive deeper into the appliance code!  

Tips on how to Set Up the Software 

We’ll use an already pre-made software template to make this tutorial as simple as doable. Yow will discover the entire GitHub repository for the challenge down beneath: 

Get Any Pockets Token Stability App Repo – https://github.com/MoralisWeb3/youtube-tutorials/tree/predominant/get-any-wallets-token-balance

So, to start with, open the repository above and clone the challenge to your native listing. With an area copy of the appliance, you need to now have a file construction just like the one proven beneath: 

code structure inside visual studio code

When you examine your native listing, you’ll rapidly discover that the ”.env.native” file is lacking. Thus, ensure you create this file and add the contents beneath and exchange ”YOUR_API_KEY” with the important thing you copied within the earlier part:

NEXT_PUBLIC_MORALIS_API_KEY=”YOUR_API_KEY”

Lastly, run the next command to put in the Moralis SDK: 

npm set up moralis @moralisweb3/common-evm-utils

That’s truly all you might want to do to make the appliance operational. Nevertheless, within the following part, we are going to break down the code to elucidate the logic in additional element. By instructing you the way the appliance works, you may apply comparable ideas in future improvement endeavors. 

Software Code Breakdown 

This part will break down the important components of the code, together with the logic for methods to get the token stability of an handle. Particularly, we are going to give attention to the next three information: ”index.js”, ”header.js”, and ”predominant.js”.

”index.js” – The ”index.js” file incorporates the code for the appliance’s homepage. It seems to be like this: import Head from “subsequent/head”;
import kinds from “../kinds/House.module.css”;

import Header from “../elements/header”;
import Primary from “../elements/predominant”;

export default operate House() {
return (
<part className={kinds.container}>
<Head>
<title>Get Token Worth</title>
<meta identify=”description” content material=”Generated by create subsequent app” />
<hyperlink rel=”icon” href=”https://moralis.io/favicon.ico” />
</Head>

<predominant className={kinds.predominant}>
<Header />
<Primary />
</predominant>
</part>
);
}

As you may see on the backside of the file, the code right here is accountable for rendering two elements, ”Header” and ”Primary”: 

<predominant className={kinds.predominant}>
<Header />
<Primary />
</predominant>
</part>

Now, allow us to take a quick have a look at ”header.js”. 

”header.js” – This file is comparatively simple and incorporates the code for the title and emblem on the high left of the appliance’s UI:import Picture from “subsequent/picture”;
import kinds from “../kinds/House.module.css”;

import Brand from “../public/belongings/Moralis_logo.png”;

export default operate Header() {
return (
<part className={kinds.header}>
<Picture src={Brand} alt=”Brand picture” width=”102″ peak=”82″ />
<h1 className={kinds.title}>Get Any Pockets’s Token Stability</h1>
</part>
);
}

Lastly, we now have the ”predominant.js” file. Nevertheless, this file is sort of intensive in comparison with the others, and that is the place we discover a lot of the logic wanted to get the token stability of an handle. As such, this file deserves its personal sub-section! 

The ”predominant.js” File   

In comparison with the 2 earlier information, ”predominant.js” is comparatively intensive. Due to this fact, we’ve cut up the code into smaller components to make it simpler to observe together with. As such, allow us to initially take a better have a look at the primary 4 traces of code: 

import { useState } from “react”;
const Moralis = require(“moralis”).default;
const { EvmChain } = require(“@moralisweb3/common-evm-utils”);
import kinds from “../kinds/House.module.css”;

This half is accountable for the required imports. That is additionally the place we set up Moralis and the widespread EVM utils. Furthermore, the rest of the code is the ”Header()” operate. That is the place the central logic for getting the token stability of an handle is discovered within the ”handleSubmit()” operate: 

const handleSubmit = async () => {
handle = doc.querySelector(“#walletAddress”).worth;
const chain = EvmChain.ETHEREUM;

await Moralis.begin({
apiKey: course of.env.NEXT_PUBLIC_MORALIS_API_KEY,
});

const response = await Moralis.EvmApi.token.getWalletTokenBalances({
handle,
chain,
});

console.log(response.toJSON());
setResult(response.toJSON());
setShowResult(true);
doc.querySelector(“#walletAddress”).worth = “”;
};

Within the first a part of this operate, we get the handle from the UI and retailer it within the ”handle” variable. From there, we set the ”chain” variable to Ethereum. Subsequent up, we initialize Moralis with the ”Moralis.begin()” operate utilizing the API key from the ”.env.native” file. Lastly, we name Moralis’ ”getWalletTokenBalances()” operate, passing the ”handle” and ”chain” variables as arguments. The returned info is saved within the ”response” variable, offering entry to all of the tokens held by the pockets handle inputted by the person. 

From there, the final a part of the code is accountable for rendering the weather of the UI. That is additionally the place we resolve what info to show to the person:  

return (
<part className={kinds.predominant}>
<type
className={kinds.getTokenForm}
identify=”create-profile-form”
methodology=”POST”
motion=”#”
>
<label className={kinds.label} htmlFor=”walletAddress”>
Add ERC20 Pockets Handle
</label>
<enter
className={kinds.walletAddress}
sort=”textual content”
id=”walletAddress”
identify=”walletAddress”
maxLength=”120″
required
/>
</type>
<button className={kinds.form_btn} onClick={handleSubmit}>
Submit
</button>
<part className={kinds.end result}>
{showResult &&
end result.map((token) => {
return (
<part
className={kinds.tokenContainer}
key={end result.indexOf(token)}
>
<img src={token.thumbnail} />
<p className={kinds.identify}>{token.identify}</p>
<p className={kinds.quantity}>
{(token.stability / 10 ** token.decimals).toFixed(2)}
</p>
</part>
);
})}
</part>
</part>
);

Nonetheless, that covers the whole thing of the code and this software. For a extra detailed code breakdown, please try the video from the ”Tips on how to Get the Token Stability of an Handle Utilizing Moralis’ Token API” part. Moreover, if you wish to be taught extra in regards to the Token API and the Moralis Token API endpoint used on this tutorial, please go to its official documentation web page! Additionally, be sure to take a look at the Token API documentation web page!

Abstract – Tips on how to Get the Token Stability of an Handle

On this article, you realized methods to get the token stability of an handle. We additionally confirmed you methods to create a simple software from which customers may enter an handle and obtain its token stability in return. Moreover, due to Moralis’ Token API, you had been capable of question the ERC-20 token stability of an handle with solely a few traces of code! 

Furthermore, we divided the tutorial into 4 predominant sections: 

Software DemoPrerequisites and Moralis SetupHow to Set Up the ApplicationApplication Code Breakdown

By protecting every half, you may arrange the appliance permitting you to get the token stability of an handle! 

When you discovered this tutorial fascinating, try extra content material right here on the Moralis Web3 weblog. For subjects just like this text, learn our articles on methods to get all tokens owned by a pockets and get the stability of an ERC20 token. Additionally, discover the last word ERC20 token API and the perfect ERC20 token stability API! As well as, learn the way Web3 information storage works or learn up on all you might want to learn about ethers.js! 

moralis blog landing page

What’s extra, if you’re critical about entering into Web3 improvement, enroll with Moralis straight away. With Moralis and instruments akin to Moralis’ Web3 APIs, you may leverage the facility of blockchain know-how to its fullest!



Source link

Tags: addressBalanceMoralisTokenWeb3
Previous Post

Decentralized Finance Protocols Flounder as Value Locked in Defi Shed More Than 25% Since FTX Collapsed – Defi Bitcoin News

Next Post

Thoma Bravo Acquires Business Spend Management Firm Coupa Software for $8 Billion

Related Posts

Web3

Rollups-as-a-service will be for Web3 what AWS was to Web2

March 30, 2023
Web3

OpenSea Gas Fee – Understanding Gas Fees on OpenSea

March 29, 2023
Web3

MATIC Faucet – Free Polygon Testnet Faucet 2023

March 27, 2023
Web3

Hong Kong blockchain initiatives natural evolution for finance hub, says Signum Digital CEO

March 24, 2023
Web3

Hong Kong poised for ambitious changes

March 23, 2023
Web3

How DAOs can be remade to be more successful

March 23, 2023
Next Post

Thoma Bravo Acquires Business Spend Management Firm Coupa Software for $8 Billion

SBF admits responsibility for FTX failure in Twitter space but remains vague on details

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • USD
  • EUR
  • GPB
  • AUD
  • JPY
  • bitcoinBitcoin(BTC)
    $18,933.97-0.86%
  • ethereumEthereum(ETH)
    $1,307.62-2.72%
  • tetherTether(USDT)
    $1.000.02%
  • usd-coinUSD Coin(USDC)
    $1.00-0.08%
  • binancecoinBNB(BNB)
    $275.09-1.90%
  • rippleXRP(XRP)
    $0.4958113.93%
  • binance-usdBinance USD(BUSD)
    $1.000.03%
  • cardanoCardano(ADA)
    $0.452813-2.01%
  • solanaSolana(SOL)
    $33.06-2.12%
  • dogecoinDogecoin(DOGE)
    $0.062007-5.94%
  • Trending
  • Comments
  • Latest

At December 4, 2022, USD Coin contract distributed the USDCGift token to the eligible holders of USD Coin on the Ethereum mainnet network. : ethereum

December 5, 2022

Intuit Pulls from Mint to Build New Credit Karma Net Worth Tool

March 9, 2023

Crypto News – Binance Delisting Alert ⚠️

March 18, 2023

🦄 uniswap-universal-router-decoder ✨ v0.8.0 ✨ has just been released !!

March 18, 2023

Donald Trump’s Pathetic, Embarrassing Announcement & Santa’s Lap or Booster Shot?

March 21, 2023

Gilane Tawadros on her greatest influences

December 3, 2022

The Crypto Market and Altcoins Will Skyrocket After The Bitcoin Halving

46

Mark Zuckerberg addressed laid off employees today — here's what he said

47

Trump Makes A Fool Of Himself Hawking Weird Imaginary Trading Cards

43

Earth 2 Version 1 3D Earth Showcase (watch in 4K)

50

ARE NFTS OFFICIALLY DEAD? NFT COLLAPSE EXPLAINED!

27

Influencer Crypto Scammer Tier List

24

BTC Rises to $29,000 for First Time Since Last June – Market Updates Bitcoin News

March 30, 2023

SEC Boss Gary Gensler Seeking $2.436 Billion To Protect Investors

March 30, 2023

Why Elon Musk, Over 2,600 Tech Experts Are Calling For ‘Immediate Pause’ In AI Development

March 30, 2023

Crypto Whales and ‘Smart Money’ Now Accumulating One Ethereum-Based Altcoin: On-Chain Data

March 30, 2023

Chatgpt More Useful Than Crypto, Nvidia Tech Chief Says – Mining Bitcoin News

March 30, 2023

Binance says FT is ‘dramatically mischaracterizing events’

March 30, 2023
Telegram Twitter TikTok Youtube RSS
The Dao Makers

Find the latest Bitcoin, Ethereum, blockchain, crypto, Business, Fintech News, interviews, and price analysis at The Dao Makers.

CATEGORIES

  • Altcoin
  • Analysis
  • Bitcoin
  • Blockchain
  • Crypto Exchanges
  • Crypto Updates
  • Dating Online
  • DeFi
  • Ethereum
  • Fix Driver Errors
  • Launchpads
  • Metaverse
  • Mining
  • NFT
  • Regulations
  • Scam Alert
  • Uncategorized
  • Videos
  • Web3

SITE MAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 The Dao Makers.
The Dao Makers is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • Bitcoin
  • Launchpads
  • Crypto Updates
    • General
    • Blockchain
    • Ethereum
    • Altcoin
    • Mining
    • Crypto Exchanges
  • NFT
  • DeFi
  • Web3
  • Metaverse
  • Analysis
  • Regulations
  • Scam Alert
  • Videos

Copyright © 2022 The Dao Makers.
The Dao Makers is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In