8

Via Geth commandline I can get my smartcontract token balance with:

var mytest = eth.contract([{interface}]).at(contract address);
mytest.balanceOf(eth.accounts[0])
>998

I want to present the token balance on my website. How can I do that?

I tried JSON-RPC and several methods like eth_getTransactionByHash, but can't find out how to get the balance.

2

Use web3.js to present the balance on your website.

Geth's console, where you were able to obtain the token balance, is Javascript and uses web3.js under the hood, so your website code will be almost identical.

Otherwise, with JSON-RPC you have to use eth_call and you have to ABI encode the function you are calling and its parameters. It is much more complicated, for an example see: How to call a contract method using the eth_call JSON-RPC API


If server-side rendering is desired, that design decision could be examined because a user should be able to run a decentralized application without a server: Swarm and IPFS are technologies to keep in mind as the direction DApps will likely take.

  • Now I get an error: Uncaught TypeError: MyContract.at is not a function. With these details: var MyContract = eth.contract([{interface}]). And: var tokens = MyContract.at("MyAddress").balanceOf(eth.accounts[0]); console.log(tokens); – Jeroen May 16 '16 at 10:38
  • 1
    Can't spot what's wrong, posting more details in a separate question can help people spot it. – eth May 16 '16 at 19:47
  • Use web.eth.contract(interface) and also probably you need to use JSON.parse(abi). – Aniket Sep 16 '16 at 5:56
1

Adding to answer of @eth♦ and comment from @Jeroen

Get Smartcontract Token balance with web3 1.0.0-beta.31

In 1.0.0-beta... most is handled via Promises now, hence you could do:

var mytest = new web3.eth.Contract({interface}, '0x123....', {
    from: '0x456...', // default from address
    gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
});

mytest.methods.balanceOf('0x456...').call()
    .then(function(result){
    //the result holds your Token Balance that you can assign to a var
    var myTokenBalance = result;
    return result;
});
  • Where {interface} is the ABI of your Contract 0x123.....
  • Where 0x123.... is the address of your Contract.
  • Where 0x456.... is the address of your Ethereum Account that holds the Tokens (i.e. the ETH account you wish to retrieve its Token Balance from)

Don't forget to add the new before web3.eth.Contract(....)

@Jeroen That way you won't get the error

MyContract.at is not a function

anymore.

Note: I am using the IPC Provider geth.ipc to interact with my node (as I am on the same PC, localhost, hence more save instead of using HTTP request).

  if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
  } else {
    var net = require('net');
    var web3 = new Web3('/home/yourHomeFolder/.ethereum/geth.ipc', net);
  };

Edit: For reference following the ABI JSON ( web3.eth.Contract({interface} ) I am using for my smart contract, being myContractABI my {interface}:

    myContractABI = [
    {
        "constant": false,
        "inputs": [
            {
                "name": "newSellPrice",
                "type": "uint256"
            },
            {
                "name": "newBuyPrice",
                "type": "uint256"
            }
        ],
        "name": "setPrices",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "name",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_spender",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "approve",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "totalSupply",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_from",
                "type": "address"
            },
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transferFrom",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "decimals",
        "outputs": [
            {
                "name": "",
                "type": "uint8"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "burn",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "sellPrice",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "balanceOf",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "target",
                "type": "address"
            },
            {
                "name": "mintedAmount",
                "type": "uint256"
            }
        ],
        "name": "mintToken",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_from",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "burnFrom",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "buyPrice",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "owner",
        "outputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "symbol",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [],
        "name": "buy",
        "outputs": [],
        "payable": true,
        "stateMutability": "payable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transfer",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "frozenAccount",
        "outputs": [
            {
                "name": "",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_spender",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            },
            {
                "name": "_extraData",
                "type": "bytes"
            }
        ],
        "name": "approveAndCall",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            },
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "allowance",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "amount",
                "type": "uint256"
            }
        ],
        "name": "sell",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "target",
                "type": "address"
            },
            {
                "name": "freeze",
                "type": "bool"
            }
        ],
        "name": "freezeAccount",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "transferOwnership",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "initialSupply",
                "type": "uint256"
            },
            {
                "name": "tokenName",
                "type": "string"
            },
            {
                "name": "tokenSymbol",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": false,
                "name": "target",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "frozen",
                "type": "bool"
            }
        ],
        "name": "FrozenFunds",
        "type": "event"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "name": "from",
                "type": "address"
            },
            {
                "indexed": true,
                "name": "to",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "value",
                "type": "uint256"
            }
        ],
        "name": "Transfer",
        "type": "event"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "name": "from",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "value",
                "type": "uint256"
            }
        ],
        "name": "Burn",
        "type": "event"
    }
];
  • I was looking for this answer 2 days ago) sad, that it appeared just 14 hours ago, but it is really really helpful answer. Actually, my solution looks 99% exactly like yours. – maxkoryukov Mar 27 '18 at 11:21
  • how can I get the ABI for {interface}-parameter? I deployed my contract with truffle. – delete Apr 29 '18 at 11:43
  • I'm trying to achieve this in C#. Can I see the Json for this? I can't figure it out without seeing what I'm supposed to put in the POST body. – Melbourne Developer Jun 13 '18 at 9:46
  • @MelbourneDeveloper I have added JSON of my smart contract - hope that was what you were asking for. – Juergen Fink Jun 13 '18 at 22:52
  • I need some documentation about how to check which tokens are at an address, and what the balance is. Web3 is not going to help me because I use C#. – Melbourne Developer Jun 15 '18 at 9:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.