Get token balance with Ethereum RPC?

前端 未结 4 2016
遥遥无期
遥遥无期 2021-02-06 13:38

how display balance of token through Ethereum RPC?

$id = 0;
$data = array();
$data[\'jsonrpc\'] = \'2.0\';
$data[\'id\'] = $id++;
$data[\'method\'] = \'eth_call\         


        
4条回答
  •  北海茫月
    2021-02-06 13:56

    When calling a Solidity contract function, in general, data should be the following, encoded as a hex string:

    1. The "function selector," which is the first four bytes of the keccak-256 hash of the signature of the function you're calling.
    2. The ABI-encoded arguments to the function you're calling.

    The function signature for an ERC20 token's balanceOf is balanceOf(address). The keccak-256 hash is 70a08231b98ef4ca268c9cc3f6b4590e4bfec28280db06bb5d45e689f2a360be, so the first four bytes are 70a08231.

    The function only takes a single parameter: the address of the account whose balance you're trying to look up. To ABI-encode it, simply left-pad it with zeros until it's 32 bytes long. Since addresses are 20 bytes, this means adding 12 bytes of zeros (or 24 characters in hex).

    So the full data field should be "0x70a08231" + "000000000000000000000000" + address.

提交回复
热议问题