def get_eventname_types(event_description): if 'name' not in event_description: raise ValueError('Not an event description, missing the name.') name = normalize_name(event_description['name']) encode_types = [element['type'] for element in event_description['inputs']] return name, encode_types
def get_eventname_types(event_description): if 'name' not in event_description: raise ValueError('Not an event description, missing the name.') name = normalize_name(event_description['name']) encode_types = [ element['type'] for element in event_description['inputs'] ] return name, encode_types
def get_event(full_abi, event_name): for description in full_abi: name = description.get('name') # skip constructors if name is None: continue normalized_name = normalize_name(name) if normalized_name == event_name: return description
def test_event(): event_abi = [{ 'name': 'Test', 'anonymous': False, 'inputs': [ { 'indexed': False, 'name': 'a', 'type': 'int256' }, { 'indexed': False, 'name': 'b', 'type': 'int256' }, ], 'type': 'event', }] contract_abi = ContractTranslator(event_abi) normalized_name = normalize_name('Test') encode_types = ['int256', 'int256'] id_ = event_id(normalized_name, encode_types) topics = [id_] data = encode_abi(encode_types, [1, 2]) result = contract_abi.decode_event(topics, data) assert result['_event_type'] == b'Test' assert result['a'] == 1 assert result['b'] == 2
def __init__(self, contract_interface): if isinstance(contract_interface, str): contract_interface = json.dumps(contract_interface) self.fallback_data = None self.constructor_data = None self.function_data = {} self.event_data = {} for description in contract_interface: entry_type = description.get('type', 'function') encode_types = [] signature = [] # If it's a function/constructor/event if entry_type != 'fallback' and 'inputs' in description: encode_types = [] signature = [] for element in description.get('inputs', []): encode_type = process_abi_type(element) encode_types.append(encode_type) signature.append((encode_type, element['name'])) if entry_type == 'function': normalized_name = normalize_name(description['name']) prefix = method_id(normalized_name, encode_types) decode_types = [] for element in description.get('outputs', []): decode_type = process_abi_type(element) decode_types.append(decode_type) # 1st is 0.6.0 way, 2nd is old is_constant = description.get('stateMutability', '') == 'view' \ or description.get('constant', False) self.function_data[normalized_name] = { 'prefix': prefix, 'encode_types': encode_types, 'decode_types': decode_types, 'is_constant': is_constant, 'signature': signature, 'payable': description.get('payable', False), } elif entry_type == 'event': normalized_name = normalize_name(description['name']) indexed = [ element['indexed'] for element in description['inputs'] ] names = [element['name'] for element in description['inputs']] # event_id == topics[0] self.event_data[event_id(normalized_name, encode_types)] = { 'types': encode_types, 'name': normalized_name, 'names': names, 'indexed': indexed, 'anonymous': description.get('anonymous', False), } elif entry_type == 'constructor': if self.constructor_data is not None: raise ValueError('Only one constructor is supported.') self.constructor_data = { 'encode_types': encode_types, 'signature': signature, } elif entry_type == 'fallback': if self.fallback_data is not None: raise ValueError( 'Only one fallback function is supported.') self.fallback_data = {'payable': description['payable']} else: raise ValueError('Unknown type {}'.format(description['type']))