1

I am trying to calculate the price of an ERC20 token. My approach is to determine the total value of the token pool and divide that by the total number of tokens. However, the values I am getting for the balanceOf method are not even close to the values in etherscan. The values of totalSupply do match etherscan. I am using web3.js with node.

const web3 = new Web3('wss://') // my connection address
const contractInstance = new web3.eth.Contract(TOKEN_ABI, TOKEN_CONTRACT_ADDRESS)

contractInstance.methods.decimals().call().then(function (bal) {
    decimals = bal; // number of decimals
});

contractInstance.methods.totalSupply().call().then(function (bal) {
    console.log("Total Supply: "+bal/(10**Number.parseFloat(decimals).toFixed())); // total supply
});

contractInstance.methods.balanceOf(TOKEN_CONTRACT_ADDRESS).call().then(function (bal) {
    console.log("Balance of: "+new BigNumber(bal).toFixed()); // 
});

Is this the correct approach?

1
  • balanceOf(TOKEN_CONTRACT_ADDRESS) returns the amount of token still owned by the contract. It is unrelated to the token price. For most tokens the price is given by the free market.
    – Ismael
    Mar 17 '21 at 4:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.