获得以太坊RPC的代币余额? [英] Get token balance with Ethereum RPC?

查看:933
本文介绍了获得以太坊RPC的代币余额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过以太坊RPC显示令牌余额?

how display balance of token through Ethereum RPC?

$id = 0;
$data = array();
$data['jsonrpc'] = '2.0';
$data['id'] = $id++;
$data['method'] = 'eth_call';
$data['params'] = [['from' => '0x0...', 'to' => '0x0...', 'data' => 'contract byte code here 0x0...'], 'latest'];

$ch = curl_init();
...

返回:

{"jsonrpc":"2.0","id":0,"result":"0x"}

下一步该怎么做?调用合约方法balanceOf?该怎么做?

What to do next? Call contract method balanceOf? How to do that?

推荐答案

要使用eth_call获得令牌余额,您需要todata参数. to是合同地址,这里我们需要生成data参数.正如文档 eth_call 所说,

To get token balance with eth_call you need to and data parameter. to is contract address, here we need to generate the data parameter. As the doc eth_call says,

data:DATA-(可选)方法签名的哈希值并进行编码 参数.有关详细信息,请参见以太坊-合同-ABI

data: DATA - (optional) Hash of the method signature and encoded parameters. For details see Ethereum-Contract-ABI

将此 aOS 作为 EOS令牌示例.

Take this EOS token transaction as a example.

合同地址:0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0

令牌持有人地址:0x0b88516a6d22bf8e0d3657effbd41577c5fd4cb7

您可以在此处看到合同代码.

You can see the contract code here.

contract ERC20 {
    function totalSupply() constant returns (uint supply);
    function balanceOf( address who ) constant returns (uint value);
    function allowance( address owner, address spender ) constant returns (uint _allowance);

    function transfer( address to, uint value) returns (bool ok);
    function transferFrom( address from, address to, uint value) returns (bool ok);
    function approve( address spender, uint value ) returns (bool ok);

    event Transfer( address indexed from, address indexed to, uint value);
    event Approval( address indexed owner, address indexed spender, uint value);
}

功能选择器

>>> from web3 import Web3
>>> Web3.sha3("balanceOf(address)")
HexBytes('0x70a08231b98ef4ca268c9cc3f6b4590e4bfec28280db06bb5d45e689f2a360be')

取前四个字节70a08231

参数编码

地址:等同于uint160,但假定的解释除外 和语言输入.

address: equivalent to uint160, except for the assumed interpretation and language typing.

int:enc(X)是X的大尾数补码编码, 在负数X的高阶(左侧)填充0xff, 对于正X具有零字节,因此长度是的倍数 32个字节.

int: enc(X) is the big-endian two's complement encoding of X, padded on the higher-order (left) side with 0xff for negative X and with zero bytes for positive X such that the length is a multiple of 32 bytes.

将20个字节的令牌地址填充为32个字节,并在令牌持有者地址中添加0:

Padding the 20 bytes token address to 32 bytes with 0 to token holder address:

0000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7

然后连接函数选择器和编码参数,我们得到data参数:

Then concat the function selector and encoded parameter, we get data parameter:

0x70a082310000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7

0x70a082310000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7

通过以下方式发出请求:

Make the request with:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to": "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0", "data":"0x70a082310000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7"}, "latest"],"id":67}' -H "Content-Type: application/json" http://127.0.0.1:8545/

这是卷曲结果(您可能会在这里得到不同的答案,因为在我查询请求后可能会有与此地址相关的交易)

here is the curl result (You may get different answer here, as there may be some transaction with this address done after my polling the request)

{"jsonrpc":"2.0","id":67,"result":"0x00000000000000000000000000000000000000000000014a314d9ff9b20b9800"}

您可以将十六进制格式转换为十进制

You can change convert hex format balance to decimal

>>> 0x00000000000000000000000000000000000000000000014a314d9ff9b20b9800
6090978215900000000000

检查结果,

这篇关于获得以太坊RPC的代币余额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆