Skip to content

Instantly share code, notes, and snippets.

View mohammadsadeghforoughi's full-sized avatar
🐱
Focusing

Mohammad Sadegh Foroughi mohammadsadeghforoughi

🐱
Focusing
View GitHub Profile
@mohammadsadeghforoughi
mohammadsadeghforoughi / gist:6f9f24917edf95e9c55f2c1d3873c163
Created January 8, 2022 14:29
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;
@mohammadsadeghforoughi
mohammadsadeghforoughi / gist:d97599f1f8d85138e2e9a4043bfbac3c
Created January 8, 2022 14:27
How to get token decimals in web3
// initate the web3 here
const getTokenDecimals = async (address: string) => {
const tokenContract = await new web3.eth.Contract(tokenAbi, address);
let decimals = await tokenContract.methods.decimals().call();
return decimals;
};
let decimals = await getTokenDecimals('0xeae2bbbc0000f605bd37a02c7fe346a3b68b03eb')