其他分享
首页 > 其他分享> > 4.ERC20常用查询操作

4.ERC20常用查询操作

作者:互联网

本文写于2019

获取ERC20 Token的余额

https://blog.csdn.net/wypeng2010/article/details/81362562

curl -X POST --data-binary '{"jsonrpc":"2.0","method":"eth_call","params":[{"from": "0x954d1a58c7abd4ac8ebe05f59191Cf718eb0cB89","to": "0xee8e2882e07f89685430c27e2f77636b08df3c81","data": "0x70a08231000000000000000000000000954d1a58c7abd4ac8ebe05f59191Cf718eb0cB89"},"latest"],"id":1}' -H 'content-type: application/json;' http://127.0.0.1:18545

{"jsonrpc":"2.0","id":1,"result":"0x000000000000000000000000000000000000000143127b10e822e2cbd2000000"}

http://cw.hubwiz.com/card/c/web3.js-1.0/1/2/25/

> 
> eth.call({from:"0x954d1a58c7abd4ac8ebe05f59191Cf718eb0cB89", to:"0xee8e2882e07f89685430c27e2f77636b08df3c81", data:"0x70a08231000000000000000000000000954d1a58c7abd4ac8ebe05f59191Cf718eb0cB89"})
"0x000000000000000000000000000000000000000143127b10e822e2cbd2000000"
> 

关于ERC20 标准的解析

https://www.jianshu.com/p/a5158fbfaeb9

18160ddd -> totalSupply()
70a08231 -> balanceOf(address)
dd62ed3e -> allowance(address,address)
a9059cbb -> transfer(address,uint256)
095ea7b3 -> approve(address,uint256)
23b872dd -> transferFrom(address,address,uint256)

自己计算

>>> from ethereum.utils import *
>>> 
>>> sha3('name()').encode('hex')[:4*2]
'06fdde03'
>>> 
>>> sha3('symbol()').encode('hex')[:4*2]
'95d89b41'
>>> 
>>> 
>>> sha3('decimals()').encode('hex')[:4*2]
'313ce567'
>>> 
>>> 
>>> sha3('totalSupply()').encode('hex')[:4*2]
'18160ddd'
>>> 
>>> sha3('balanceOf(address)').encode('hex')[:4*2]
'70a08231'
>>> 
>>> 
>>> sha3('allowance(address,address)').encode('hex')[:4*2]
'dd62ed3e'
>>> 
>>> 
>>> sha3('transfer(address,uint256)').encode('hex')[:4*2]
'a9059cbb'
>>> 
>>> 
>>> sha3('approve(address,uint256)').encode('hex')[:4*2]
'095ea7b3'
>>> 
>>> 
>>> sha3('transferFrom(address,address,uint256)').encode('hex')[:4*2]
'23b872dd'
>>> 
>>> 

> 
> eth.call({from:"0x954d1a58c7abd4ac8ebe05f59191Cf718eb0cB89", to:"0xee8e2882e07f89685430c27e2f77636b08df3c81", data:"0x95d89b41"})
"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003424a430000000000000000000000000000000000000000000000000000000000"

关于字符串的解码

//以下是 python3

from eth_utils import to_bytes

encoded = to_bytes(hexstr="0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000067374617475730000000000000000000000000000000000000000000000000000")

from eth_abi import decode_abi

decoded = decode_abi(['string', 'uint256'], encoded)

assert decoded == (b'status', 12)
//使用python2实现

def my_to_bytes(hexstr='0x234234'):
	return hexstr.replace('0x', '').decode('hex')

def decode_abi_string(data):
    pass

def ceil32(x: int) -> int:
    return x if x % 32 == 0 else x + 32 - (x % 32)


def encode_string(text):
    b = text.encode('hex')
    padded_value = b.ljust(ceil32(b), '0') #右补0
    encode_size = hex(len(b))[2:].replace('L', '').rjust(64, '0')
    
0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000045553445400000000000000000000000000000000000000000000000000000000
	

标签:function,常用,uint256,hex,查询,returns,address,encode,ERC20
来源: https://blog.csdn.net/yqq1997/article/details/116784878