0

I use code like this to get my Ethereum balance with web3

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="./web3.min.js"></script>
    <script type="text/javascript">
        window.addEventListener('load', function () {
            if (typeof web3 !== 'undefined') {
                console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
                window.web3 = new Web3(web3.currentProvider);
            } else {
                console.log('No Web3 Detected... using HTTP Provider')
                window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
            }
        })
        function getBalance() {
            var address, wei, balance
            address = document.getElementById("address").value
            try {
                web3.eth.getBalance(address, function (error, wei) {
                    if (!error) {
                        var balance = web3.fromWei(wei, 'ether');
                        document.getElementById("output").innerHTML = balance + " ETH";
                    }
                });
            } catch (err) {
                document.getElementById("output").innerHTML = err;
            }
        }
    </script>
</head>
<body>
    <h1>ETH Balance Fetcher</h1>
    <p>Enter your Ethereum Address:</p>
    <input type="text" size="50" id="address" />
    <button type="button" onClick="getBalance();">Get Balance</button>
    <br />
    <br />
    <div id="output"></div>
</body>
</html>

But it does not work for tokens.

For example i create ERC20 and contract adress is 0xa318948fd693281fa6a95a409b3efb79e228edec (I add it in metamask and it shows my token balance)

How can I read it with web3? tnx for helping

5
  • what do you mean by "it not work for tokens"
    – Jaime
    Apr 22 '18 at 17:56
  • 0xa318948fd693281fa6a95a409b3efb79e228edec is my token address i have 100 tokens it show 0
    – Danial Qsk
    Apr 22 '18 at 18:47
  • If anyone uses the code above make sure you include the human_standard_token_abi.js
    – drew
    Oct 31 '18 at 11:53
  • This is a separate question, please make it like that :P
    – GrandFleet
    Oct 31 '18 at 16:30
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Nov 1 '18 at 10:58
0

You are using my ETH-Balance sample, which will not work if you are looking at an ERC20 token. This sample is meant to get the Ether balance at a particular address.

Instead you should use my other sample, ERC-20-Token-Balance where you can input both the contract address, and the address where you want to check the balance of the tokens.

You can try it out live here: http://shawntabrizi.com/ethbalance/erc20/

async function getERC20Balance() {
    var address, contractAddress, contractABI, tokenContract, decimals, balance, name, symbol, adjustedBalance
    address = document.getElementById("address").value
    contractAddress = document.getElementById("contractAddress").value
    contractABI = human_standard_token_abi

    tokenContract = web3.eth.contract(contractABI).at(contractAddress)

    decimals = promisify(cb => tokenContract.decimals(cb))
    balance = promisify(cb => tokenContract.balanceOf(address, cb))
    name = promisify(cb => tokenContract.name(cb))
    symbol = promisify(cb => tokenContract.symbol(cb))

    try {
        adjustedBalance = await balance / Math.pow(10, await decimals)
        document.getElementById("output2").innerHTML = adjustedBalance;
        document.getElementById("output2").innerHTML += " " + await symbol + " (" + await name + ")";
    } catch (error) {
        document.getElementById("output2").innerHTML = error;
    }
}
1
  • wow its work! tnx a lot
    – Danial Qsk
    Apr 22 '18 at 20:30

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .