How can I check the contract balance, like Augur (REP), on an address with JSON-RPC? There doesn't seem to be alot of documentation about this.
-
3Not a duplicate of the Q&A above. This question is asking about the REP token balance for an address, not the ETH balance. – The Officious BokkyPooBah Oct 5 '16 at 11:30
-
2@BokkyPooBah Indeed I'm asking for the REP token balance. – Julian Oct 5 '16 at 12:27
To get the REP token balance via JSON-RPC, you will have to use the eth_call
method. See How to call a contract method using the eth_call JSON-RPC API for more details on using eth_call
.
And from augur-core/src/repContract.se
, the balanceOf(...)
function is defined as:
def balanceOf(address: address):
return(self.reporting[address]: uint256)
And this matches the ERC: Token standard #20, where the balanceOf(...)
function should have the signature:
function balanceOf(address _owner) constant returns (uint256 balance)
To find the function signature in geth
, I run the following command:
> web3.sha3('balanceOf(address)').substring(0,10)
"0x70a08231"
For an example, we will use the REP token balance for the address 0xab11204cfeaccffa63c2d23aef2ea9accdb0a0d5 with the following balance:
I now have to use the function signature from above with a left zero padded representation of the address to be passed to the balanceOf(...)
function:
0x70a08231000000000000000000000000ab11204cfeaccffa63c2d23aef2ea9accdb0a0d5
From etherscan.io/token/REP, the REP contract address is 0x48c80F1f4D53D5951e5D5438B54Cba84f29F32a5.
I ran the following JSON-RPC command against my Parity node (for geth
, you can leave off the --header
parameter):
curl localhost:8545 -X POST --header 'Content-type: application/json' --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"to": "0x48c80F1f4D53D5951e5D5438B54Cba84f29F32a5", "data": "0x70a08231000000000000000000000000ab11204cfeaccffa63c2d23aef2ea9accdb0a0d5"}], "id":1}'
The result from the command above is:
{"jsonrpc":"2.0","result":"0x000000000000000000000000000000000000000000010843f6b148a2ec22c604","id":1}
And to confirm the results are correct, I ran the following command in geth
:
> new BigNumber("000000000000000000000000000000000000000000010843f6b148a2ec22c604", 16)/1e18
1247958.4593995938
You will have to convert the hex result into a number in your application.
Update Oct 18 2016
As documented in RPC error "invalid or missing value for params" when calling contract constant void functions, you may have to add a block parameter to the params
list. Your command with the block parameter will be:
curl localhost:8545 -X POST --header 'Content-type: application/json' --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"to": "0x48c80F1f4D53D5951e5D5438B54Cba84f29F32a5", "data": "0x70a08231000000000000000000000000ab11204cfeaccffa63c2d23aef2ea9accdb0a0d5"}, "latest"], "id":1}'
Reference The default block parameter and eth_call
.
eth_call
without the block parameter worked previously, but now seems to be mandatory.
-
1Great reply! I'll check if this works tomorrow and when it does I"ll accept your answer. – Julian Oct 5 '16 at 13:43
-
1As usual, an excellent post from Bokky. The result returned as of today is different, but I guess that is because the owner of that account must have sold or transferred or bought more tokens. – Thomas Jay Rush Jun 16 '17 at 22:17