Moralis- Token API || Balance API || Events API
The Moralis Token API enables Web3 developers to build and scale dapps quickly and efficiently. Access all the information you need for any ERC20 tokens from multiple blockchains.
Now lets run through some cases to understand how to use them
Base structure
lets set up the base structure before we move forward
npm install moralis @moralisweb3/common-evm-utils
Setting up js
const Moralis = require("moralis").default; const { EvmChain } = require("@moralisweb3/common-evm-utils"); const runApp = async () => { await Moralis.start({ apiKey: "YOUR_API_KEY", // ...and any other configuration }); const address = "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0";//address of the ERC20 contract that deals with the Token const chain = EvmChain.ETHEREUM; //run function here //... } runApp();
Dealing with Prices
dealing with historic prices
const historicalPrice = []; for (let toBlock = 16323500; toBlock < 16323550; toBlock += 10) { const response = await Moralis.EvmApi.token.getTokenPrice({ address,// Ethereum address of the ERC-20 token chain, toBlock, }); historicalPrice.push(response?.toJSON()); } console.log(response.toJSON());
If you want the current price of the ERC20 token
const response = await Moralis.EvmApi.token.getTokenPrice({ address, chain, });
Keep in mind you'll get the value in the 1e18 format.
Get all tokens owned by an address
Moralis.EvmApi.token.getWalletTokenBalances({ address, chain, });
getTokenAllowance()
is a function provided by the Moralis SDK that allows you to retrieve the amount of an ERC-20 token that a particular Ethereum address has authorized another address to spend on their behalf.const response = await Moralis.EvmApi.token.getTokenAllowance({ address, chain, ownerAddress, spenderAddress, });
getTokenTransfers()
is a function provided by the Moralis SDK that allows you to retrieve a list of all the token transfer events for a specific ERC-20 token contract on the Ethereum blockchain.Moralis.EvmApi.token.getTokenTransfers({ address, chain, });
If you want a list of transfers that happened via a given wallet address, the following is the syntax, keep in mind the address is the address of the wallet, and not you're ERC20 contract address
Moralis.EvmApi.token.getWalletTokenTransfers({ address, chain, });
If you want the token's metadata for a given ERC20 token you can use the
getTokenMetaData
function.Moralis.EvmApi.token.getTokenMetadata({ addresses,//address of the contract dealing with the ERC20 token chain, });
The metadata will look something like this
Say you know only the symbols of the ERC20 token, but not the address, in that case you can use the following.
Moralis.EvmApi.token.getTokenMetadataBySymbol({ symbols, chain, });
Balance API
The Balance API helps Web3 developers get the native balance for a specific wallet address. for example for a given wallet address whats the balance of MATIC tokens?
In order to get the native balance of a wallet address, Moralis provides you with the getNativeBalance API endpoint. You can use the same to get the balance of a multi-sig wallet as well.
const response = await Moralis.EvmApi.balance.getNativeBalance({ address, chain, });
Events API
The Events API enables Web3 developers to quickly get all the logs and events for contracts across multiple blockchains, to help them build and scale their Dpps efficiently.
Before we move forward let us understand the difference between Events and Logs.
Logs are a lower-level mechanism for emitting information in Ethereum contracts, while events provide a higher-level abstraction that allows contracts to emit structured data about specific actions or conditions.
Logs are often used to emit information about events that do not affect the contract state, while events are used to notify interested parties about specific state changes in the contract.
Getting Logs by contract
To get the logs of the contract, Moralis enables us to use `getContractLogs endpoint`const response = await Moralis.EvmApi.events.getContractLogs({ address,//address of the contract chain,//chainId });
Getting events by contract
const abi = { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "from", type: "address", }, { indexed: true, internalType: "address", name: "to", type: "address" }, { indexed: false, internalType: "uint256", name: "amount", type: "uint256", }, ], name: "Transfer", type: "event", }; const response = await Moralis.EvmApi.events.getContractEvents({ address, chain, topic, abi, });
Thats pretty much it for my second artice. I would recommend you to read the docmunetation to understand it better, coz thats where I learnt it form, with the help of chatGPT as well ofcourse :)
Subscribe to my newsletter
Read articles from Aaron Rebelo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aaron Rebelo
Aaron Rebelo
Hi, I'm Aaron Rebelo, a MERN stack developer who's passionate about creating robust and engaging web applications. My expertise lies in using MongoDB, Express, React, and Node.js to build applications that are not only functional but also visually appealing. In addition to my work as a developer, I'm currently learning Solidity, a programming language used for developing smart contracts on the Ethereum blockchain. I believe that blockchain technology is the future, and I'm excited to be a part of this emerging industry. I'm also interested in digital marketing and believe that effective marketing strategies are key to the success of any project. That's why I'm committed to learning the latest marketing techniques and strategies to help my clients achieve their goals. In my spare time, I've started a video series on my YouTube channel, @thedecadehypothesis, where I document my progress on building new habits every 10 days. I believe that building new habits is essential for personal growth, and I'm excited to share my journey with others. Thank you for taking the time to learn more about me. I'm passionate about my work and excited about the opportunities that lie ahead. Please feel free to connect with me if you have any questions or if you're interested in working together.