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 :)