Crypto Common Functions

Handy helper functions

Moralis provides you with some handy helper functions to easily get token and NFT balances.

  • getAllERC20

  • getERC20

  • getNFTs

  • getTransactions

These are accessed like Moralis.Web3.getAllERC20() and will get data for the current user or a specified address. Other options include the chain and sort order.

Installing the Plugin

These functions are available by default as of Moralis server version 0.0.223. New instances should have it already, but if your instance does not yet have the "Convenience Functions" plugin it will need to be installed

  • Click the "..." button on your server instance to expand it

  • Click the "Plugins" button

  • Go to the "Browse" tab

  • Scroll down to the "Convenience Functions" plugin (or search for it in the search bar)

  • It should say "Installed". If it does not it will say "Install" instead. Click the "Install" button.

  • Also make sure you're using the latest version of the SDK.

Supported Chains

All helper functions have a chain option to specify which blockchain to get data from. The following are the currently supported values for the chain option. Any of the "Lookup Values" will match the corresponding chain. If not specified the chain will default to Ethereum Mainnet.

Chain

Lookup Values

Ethereum Mainnet

eth, mainnet, 0x1

Ropsten (Ethereum Testnet)

testnet, ropsten, 0x3

Rinkeby (Ethereum Testnet)

rinkeby, 0x4

Goerli (Ethereum Testnet)

goerli, 0x5

Kovan (Ethereum Testnet)

kovan, 0x2a

Binance Smart Chain Mainnet

bsc, binance, binance smart chain, 0x38

Binance Smart Chain Testnet

bsc testnet, binance testnet, binance smart chain testnet, 0x61

Polygon (Matic) Mainnet

matic, polygon, 0x89

Mumbai (Matic Testnet)

mumbai, matic testnet, polygon testnet, 0x13881

Local Dev Chain (Ganache, Hardhat)

ganache, hardhat, localdevchain, local devchain ,dev, 0x539

getAllERC20

Retrieve all token balances of the current user or specified address. Returns an array of token objects (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values listed above. Default value Eth.

  • address (optional): A user address (i.e. 0x1a2b3x...). If specified the user attached to the query is ignored and the address is used instead.

const balances = await Moralis.Web3.getAllERC20();

Without any parameters specified, it defaults to 'Eth' as chain and the current User, but you can also specify the chain and addressin an options object:

const options = { chain: 'bsc', address: "0x..." }
const balances = await Moralis.Web3.getAllERC20(options);

getERC20

Get a specific token balance. Returns a token object (asynchronous).

Options

  • chain(optional): The blockchain to get data from. Valid values listed above. Default value Eth.

  • address (optional): A user address (i.e. 0x1a2b3x...). If specified the user attached to the query is ignored and the address is used instead.

  • symbol (optional): get the balance for the specified symbol only (i.e. LINK)

const linkBalance = await Moralis.Web3.getERC20({chain: 'ropsten', symbol: 'LINK'});

Leave out the symbol to get the native token balance for the given chain.

const ethBalance = await Moralis.Web3.getERC20(); // defaults to ETH
const bnbBalance = await Moralis.Web3.getERC20({ chain: 'bsc' });

getNFTs

Get all NFTs of the current user or address. Supports both ERC721 and ERC1155. Returns an array of NFT objects (asynchronous).

Options

  • chain(optional): The blockchain to get data from. Valid values listed above. Default value Eth.

  • address (optional): A user address (i.e. 0x1a2b3x...). If specified the user attached to the query is ignored and the address is used instead.

// 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);

getNFTsCount

Get the total number NFTs owned by the current user or address. Returns a number (asynchronous).

Options

  • chain(optional): The blockchain to get data from. Valid values listed above. Default value Eth.

  • address (optional): A user address (i.e. 0x1a2b3x...). If specified the user attached to the query is ignored and the address is used instead.

const nftCount = await Moralis.Web3.getNFTsCount();

Can use the options in the same way as above for getNFTs.

getTransactions

Get the transactions of the current user or address. Returns an array of transaction objects (asynchronous).

Options

  • chain(optional): The blockchain to get data from. Valid values listed above. Default value Eth.

  • address (optional): A user address (i.e. 0x1a2b3x...). If specified the user attached to the query is ignored and the address is used instead.

  • order (optional): sorts by block number. Use desc for descending (default) and asc for ascending

// get mainnet transactions for the current user
const userTrans = await Moralis.Web3.getTransactions();
// get BSC transactions for a given address
// with most recent transactions appearing first
const options = { chain: "bsc", address: "0x...", order: "desc" };
const transactions = await Moralis.Web3.getTransactions(options);

getTransactionsCount

Get the total transaction count for the current user or address. Returns a number (asynchronous).

Options

  • chain(optional): The blockchain to get data from. Valid values listed above. Default value Eth.

  • address (optional): A user address (i.e. 0x1a2b3x...). If specified the user attached to the query is ignored and the address is used instead.

const numTx = await Moralis.Web3.getTransactionsCount();

Can use the options in the same way as above for getTransactions.