interface.py (2116B)
1 # standard imports 2 import logging 3 4 # external imports 5 from chainlib.jsonrpc import JSONRPCRequest 6 from chainlib.eth.contract import ( 7 ABIContractEncoder, 8 ABIContractType, 9 abi_decode_single, 10 ) 11 from chainlib.eth.tx import TxFactory 12 from hexathon import ( 13 add_0x, 14 ) 15 from chainlib.eth.constant import ( 16 ZERO_ADDRESS, 17 ) 18 19 # local imports 20 from .encoding import ( 21 to_identifier, 22 ) 23 24 logg = logging.getLogger(__name__) 25 26 class Registry(TxFactory): 27 28 def address_of(self, contract_address, identifier_string, sender_address=ZERO_ADDRESS, id_generator=None): 29 j = JSONRPCRequest(id_generator) 30 o = j.template() 31 o['method'] = 'eth_call' 32 enc = ABIContractEncoder() 33 enc.method('addressOf') 34 enc.typ(ABIContractType.BYTES32) 35 identifier = to_identifier(identifier_string) 36 logg.debug('identifier {} {}'.format(identifier, identifier_string)) 37 enc.bytes32(identifier) 38 data = add_0x(enc.encode()) 39 tx = self.template(sender_address, contract_address) 40 tx = self.set_code(tx, data) 41 o['params'].append(self.normalize(tx)) 42 o = j.finalize(o) 43 return o 44 45 46 def identifier(self, contract_address, idx, sender_address=ZERO_ADDRESS, id_generator=None): 47 j = JSONRPCRequest(id_generator) 48 o = j.template() 49 o['method'] = 'eth_call' 50 enc = ABIContractEncoder() 51 enc.method('identifier') 52 enc.typ(ABIContractType.UINT256) 53 enc.uint256(idx) 54 data = add_0x(enc.encode()) 55 tx = self.template(sender_address, contract_address) 56 tx = self.set_code(tx, data) 57 o['params'].append(self.normalize(tx)) 58 o = j.finalize(o) 59 return o 60 61 62 def identifier_count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): 63 return self.call_noarg('identifierCount', contract_address, sender_address=sender_address, id_generator=id_generator) 64 65 66 @classmethod 67 def parse_address_of(self, v): 68 return abi_decode_single(ABIContractType.ADDRESS, v)