[Ethereum] Get smartcontract token balance via JSON RPC

dapp-developmentgo-ethereumjson-rpctokensweb3js

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.

Best Answer

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.