How to get balance of an erc20 token from an eth address

I need to check if one user has enough tokens to send to someone before I do the send.
Thanks in advance , you guys are super great at helping. I can;t even believe how helpful you are.

One way is to use getTokenBalances: https://docs.moralis.io/moralis-server/web3-sdk/account#gettokenbalances

const options = { chain: 'bsc', address: "0x...", to_block: "10253391" }
const balances = await Moralis.Web3API.account.getTokenBalances(options);

another way is to call a smart contract function directly if you know that smart contract address:
https://docs.moralis.io/moralis-server/web3-sdk/native#runcontractfunction

hmm when I try this code I get a weird error
code:

async function checkTheTokenTotal() {
          
          let options = { chain: 'testnet', address: '0x294e1BFd460053C77B991A3ed22482060b54278A', to_block: "99999999999" };
          let balances = await Moralis.Web3API.account.getTokenBalances(options);
        }

the error - argument of type ‘{ chain: string; address: string; to_block: string; }’ is not assignable to parameter of type ‘{ chain?: “eth” | “0x1” | “ropsten” | “0x3” | “rinkeby” | “0x4” | “goerli” | “0x5” | “kovan” | “0x2a” | “polygon” | “0x89” | “mumbai” | “0x13881” | “bsc” | “0x38” | “bsc testnet” | … 5 more … | undefined; subdomain?: string | undefined; to_block?: number | undefined; } & { …; }’.
Type ‘{ chain: string; address: string; to_block: string; }’ is not assignable to type ‘{ chain?: “eth” | “0x1” | “ropsten” | “0x3” | “rinkeby” | “0x4” | “goerli” | “0x5” | “kovan” | “0x2a” | “polygon” | “0x89” | “mumbai” | “0x13881” | “bsc” | “0x38” | “bsc testnet” | “0x61” | … 4 more … | undefined; subdomain?: string | undefined; to_block?: number | undefined; }’.
Types of property ‘chain’ are incompatible.
Type ‘string’ is not assignable to type ‘“eth” | “0x1” | “ropsten” | “0x3” | “rinkeby” | “0x4” | “goerli” | “0x5” | “kovan” | “0x2a” | “polygon” | “0x89” | “mumbai” | “0x13881” | “bsc” | “0x38” | “bsc testnet” | “0x61” | … 4 more … | undefined’.ts(2345)
let options: {
chain: string;
address: string;
to_block: string;
}`

I tried putting in ‘0x3’ … 0x3… ropsten… testnet…

I don’t expect it to work if it has random parameters

An example that works:

options = { chain: 'kovan', address: '0x294e1BFd460053C77B991A3ed22482060b54278A' };
let balances = await Moralis.Web3API.account.getTokenBalances(options);
const options = { chain: 'kovan', address: '0x294e1BFd460053C77B991A3ed22482060b54278A' };
    let balances = await Moralis.Web3API.account.getTokenBalances(options);

I getting this error
Argument of type ‘{ chain: string; address: string; }’ is not assignable to parameter of type ‘{ chain?: “kovan” | “eth” | “0x1” | “ropsten” | “0x3” | “rinkeby” | “0x4” “goerli” | “0x5” | “0x2a” | “polygon” | “0x89” | “mumbai” | “0x13881” | “bsc” | “0x38” | “bsc testnet” | … 5 more … | undefined; subdomain?: string | undefined; to_block?: number | undefined; } & { …; }’.
Type ‘{ chain: string; address: string; }’ is not assignable to type '{ chain?: “kovan” | “eth” | “0x1” | “ropsten” | "0

------------ btw how is ‘testnet’ random ? I tried ropsten as well

1 Like

To_block seemed random there.

Maybe it will work with a newer version of Moralis sdk, or maybe you can ignore those types.