Created
January 8, 2022 14:29
-
-
Save mohammadsadeghforoughi/6f9f24917edf95e9c55f2c1d3873c163 to your computer and use it in GitHub Desktop.
How to get token or contract balance with wallet public key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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