eth-contract-registry/python/eth_contract_registry/interface.py

48 lines
1.3 KiB
Python
Raw Normal View History

2021-08-24 21:28:17 +02:00
import logging
2021-04-30 13:11:22 +02:00
# external imports
2021-06-28 09:29:37 +02:00
from chainlib.jsonrpc import JSONRPCRequest
2021-04-30 13:11:22 +02:00
from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractType,
abi_decode_single,
)
from chainlib.eth.tx import TxFactory
from hexathon import (
add_0x,
)
from chainlib.eth.constant import (
ZERO_ADDRESS,
)
# local imports
from .encoding import (
to_identifier,
)
2021-08-24 21:28:17 +02:00
logg = logging.getLogger(__name__)
2021-04-30 13:11:22 +02:00
class Registry(TxFactory):
2021-06-28 09:29:37 +02:00
def address_of(self, contract_address, identifier_string, sender_address=ZERO_ADDRESS, id_generator=None):
j = JSONRPCRequest(id_generator)
o = j.template()
2021-04-30 13:11:22 +02:00
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('addressOf')
enc.typ(ABIContractType.BYTES32)
identifier = to_identifier(identifier_string)
2021-08-24 21:28:17 +02:00
logg.debug('identifier {} {}'.format(identifier, identifier_string))
2021-04-30 13:11:22 +02:00
enc.bytes32(identifier)
data = add_0x(enc.encode())
tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx))
2021-06-28 09:29:37 +02:00
o = j.finalize(o)
2021-04-30 13:11:22 +02:00
return o
@classmethod
def parse_address_of(self, v):
return abi_decode_single(ABIContractType.ADDRESS, v)