Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mohammadsadeghforoughi/6f9f24917edf95e9c55f2c1d3873c163 to your computer and use it in GitHub Desktop.
Save mohammadsadeghforoughi/6f9f24917edf95e9c55f2c1d3873c163 to your computer and use it in GitHub Desktop.
How to get token or contract balance with wallet public key
//intitate web3 here
export const getBalanceWithContract = async (address: string) => {
let tokenContract = await new web3.eth.Contract(tokenAbi, address);
let tokenBalance = await tokenContract.methods.balanceOf(WALLET_PUB).call();
let decimals = await getTokenDecimals(address);
tokenBalance = tokenBalance * (1 / Math.pow(10, decimals));
tokenBalance = tokenBalance.toString();
return tokenBalance;
};
const getTokenDecimals = async (address: string) => {
const tokenContract = await new web3.eth.Contract(tokenAbi, address);
let decimals = await tokenContract.methods.decimals().call();
return decimals;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment