php - 使用以太坊 RPC 获取代币余额?

标签 php token blockchain 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?如何做到这一点?

最佳答案

要通过 eth_call 获得代币余额,您需要 todata 参数。 to为合约地址,这里需要生成data参数。作为文档 eth_call说,

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

拿这个EOS token transaction举个例子。

合约地址:0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0

代币持有者地址:0x0b88516a6d22bf8e0d3657effbd41577c5fd4cb7

可以看到合约代码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);
}

Function Selector

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

取前四个字节70a08231

Argument Encoding

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

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.

0 将 20 字节的 token 地址填充到 32 字节到 token 持有者地址:

0000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7

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

0x70a082310000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7

发出请求:

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/

这里是 curl 结果(你可能在这里得到不同的答案,因为在我轮询请求后可能有一些交易与这个地址完成)

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

您可以更改将十六进制格式余额转换为十进制

>>> 0x00000000000000000000000000000000000000000000014a314d9ff9b20b9800
6090978215900000000000

检查结果,

enter image description here

关于php - 使用以太坊 RPC 获取代币余额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48228662/

相关文章:

github - 注册并临近智能合约

php - AES 加密与 Node 和 PHP 不同

php - 我的 php 登录表单有什么问题?

javascript - 由于脚本的原因,我的查询无法工作(它以错误的方式工作)!?但为什么? session 和 PHP

hyperledger-fabric - 如何修复 Hyperledger Fabric 中的 "rpc error: code = Unknown desc = access denied: channel [mychannel] creator org [Org1MSP]"错误

python - Cartesi Rollups 中的门户是如何工作的?

php - 使用 MySQL 处理客户端对 PHP 页面的多个请求

php - 使用 PHP 从数据库中预填充 token 输入文本框中的值

java - 使用 Joss 使用 Swift 进行身份验证

java - 带有 Jackson token 流的 JSON 中的非常长的字符串(> 1 gig)