chainlib/cic_tools/eth/method.py

62 lines
1.4 KiB
Python
Raw Normal View History

2021-02-08 10:39:42 +01:00
import sha3
import uuid
from hexathon import add_0x
from eth_abi import encode_single
2021-02-09 09:42:46 +01:00
from .hash import keccak256_string_to_hex
from .constant import ZERO_ADDRESS
2021-02-08 10:39:42 +01:00
# TODO: move to cic-contracts
2021-02-09 09:42:46 +01:00
erc20_balance_signature = keccak256_string_to_hex('balanceOf(address)')[:8]
erc20_decimals_signature = keccak256_string_to_hex('decimals()')[:8]
2021-02-08 10:39:42 +01:00
def jsonrpc_template():
return {
'jsonrpc': '2.0',
'id': str(uuid.uuid4()),
'method': None,
'params': [],
}
2021-02-09 09:42:46 +01:00
def erc20_balance(contract_address, address, sender_address=ZERO_ADDRESS):
2021-02-08 10:39:42 +01:00
o = jsonrpc_template()
o['method'] = 'eth_call'
data = erc20_balance_signature
data += encode_single('address', address).hex()
data = add_0x(data)
a = call(contract_address, data=data)
o['params'].append(a)
o['params'].append('latest')
return o
2021-02-09 09:42:46 +01:00
def erc20_decimals(contract_address, sender_address=ZERO_ADDRESS):
2021-02-08 10:39:42 +01:00
o = jsonrpc_template()
o['method'] = 'eth_call'
arg = add_0x(erc20_decimals_signature)
#o['params'].append(arg)
a = call(contract_address, arg)
o['params'].append(a)
o['params'].append('latest')
return o
2021-02-09 09:42:46 +01:00
def call(contract_address, data, sender_address=ZERO_ADDRESS):
2021-02-08 10:39:42 +01:00
return {
'from': sender_address,
'to': contract_address,
'data': data,
}
def jsonrpc_result(o, ep):
if o.get('error') != None:
raise ep.translate(o)
return o['result']