3

I'm trying to get the balance of a token on an Ethereum address. So let's say my Ethereum address has 1 SNT. I'd like to make a call to get that 1 SNT as a response. I've written many other calls and can't seem to find or figure out this one. I've tried using the method "eth_call" and using the balanceOf method. I was thinking I could use this method in the contract of the ERC20 token:

function balanceOf(address _owner) constant returns (uint256 balance);

I tried setting the to as the token address, the data as the code for balanceOf, and my wallet address also in the data, but that always returns me 0.

payload = {
        "id": 123,
        "jsonrpc": "2.0",
        "method": "eth_call",
        "params": [
            {
                "data": "0xf7888aec000000000000000000000000" + walletAddress_With0xRemoved,
                "to": tokenAddress
            },
            "latest"
        ]
    }

I found this site which has an API, but i'd really like to do it the right way rather than using a wrapper API.

https://tokenbalance.com/

CONTRACT=0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7
ETH_ADDRESS=0x198ef1ec325a96cc354c7266a038be8b5c558f67

curl https://api.tokenbalance.com/balance/$CONTRACT/$ETH_ADDRESS

{
    "name": "Golem Network Token",
    "wallet": "0xda0aed568d9a2dbdcbafc1576fedc633d28eee9a",
    "symbol": "GNT",
    "balance": 7.998173814269688,
    "eth_balance": 0.9912952230000018,
    "decimals": 18,
    "block": 3461147
  }

Edit: I just realized this website has source code available, haha. Though this isn't the type of ethereum programming i'm used to (Go).

3
  • Also this question is similar ethereum.stackexchange.com/questions/24990/…
    – Ismael
    Commented Aug 28, 2017 at 4:30
  • 2
    In my opinion, this shouldn't be marked a duplicate unless we can edit that post to be a general answer for all ERC-20 tokens.
    – carver
    Commented Aug 29, 2017 at 18:59
  • Thanks for your help guys. That was exactly my problem, I had the incorrect hash of the function signature.
    – LampShade
    Commented Aug 30, 2017 at 18:15

1 Answer 1

9

You are nearly there with your payload, only the function ID for balanceOf is wrong.

Calculating the Function Selector

From the Solidity docs:

The first four bytes of the call data for a function call specifies the function to be called. It is the first (left, high-order in big-endian) four bytes of the Keccak (SHA-3) hash of the signature of the function. The signature is defined as the canonical expression of the basic prototype, i.e. the function name with the parenthesised list of parameter types. Parameter types are split by a single comma - no spaces are used.

The signature for the function that retrieves the ERC20 balance is balanceOf(address).

With web3.js:

> funcSelector = web3.sha3('balanceOf(address)').slice(0,10)
"0x70a08231"

This alters your payload to:

payload = {
    "id": 123,
    "jsonrpc": "2.0",
    "method": "eth_call",
    "params": [
        {
            "data": funcSelector + "000000000000000000000000" + walletAddress,
            "to": tokenAddress
        },
        "latest"
    ]
}

Currently, a call with this payload returns a non-zero balance using the contract and wallet you specified in your API example.

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