def __init__(self, abi, web3, address, transaction=None, block_identifier='latest'): self.web3 = web3 self.address = address self.abi = abi self._functions = None if self.abi: if transaction is None: transaction = {} self._functions = filter_by_type('function', self.abi) for func in self._functions: fn = ContractFunction.factory(func['name'], web3=self.web3, contract_abi=self.abi, address=self.address, function_identifier=func['name']) block_id = parse_block_identifier(self.web3, block_identifier) caller_method = partial(self.call_function, fn, transaction=transaction, block_identifier=block_id) setattr(self, func['name'], caller_method)
def filter_events(self, filter_params: Dict, name_to_callback: Dict): """ Filter events for given event names Params: filter_params: arguments for the filter call name_to_callback: dict that maps event name to callbacks executed if the event is emmited """ for id, (topics, callback) in name_to_callback.items(): events = get_events( web3=self.web3, contract_address=self.contract_address, topics=topics, **filter_params, ) events_abi = filter_by_type( "event", self.contract_manager.get_contract_abi(self.contract_name)) topic_to_event_abi = { event_abi_to_log_topic(event_abi): event_abi for event_abi in events_abi } for raw_event in events: decoded_event = decode_event( codec=self.web3.codec, topic_to_event_abi=topic_to_event_abi, log_entry=raw_event, ) log.debug('Received confirmed event: \n%s', decoded_event) callback(decoded_event)
def __decode_event(self, log, abi): if isinstance(log["topics"][0], str): log["topics"][0] = decode_hex(log["topics"][0]) elif isinstance(log["topics"][0], int): log["topics"][0] = decode_hex(hex(log["topics"][0])) event_id = log["topics"][0] events = filter_by_type("event", abi) topic_to_event_abi = {event_abi_to_log_topic(event_abi): event_abi for event_abi in events} event_abi = topic_to_event_abi[event_id] return get_event_data(event_abi, log)
def find_functions_by_identifier(contract_abi, web3, address, callable_check): fns_abi = filter_by_type('function', contract_abi) return [ ContractFunction.factory(fn_abi['name'], web3=web3, contract_abi=contract_abi, address=address, function_identifier=fn_abi['name'], abi=fn_abi) for fn_abi in fns_abi if callable_check(fn_abi) ]
def __init__(self, abi, web3, address=None): if abi: self.abi = abi self._events = filter_by_type('event', self.abi) for event in self._events: setattr( self, event['name'], ContractEvent.factory(event['name'], web3=web3, contract_abi=self.abi, address=address, event_name=event['name']))
def __init__(self, abi, web3, address=None): if abi: self.abi = abi self._functions = filter_by_type('function', self.abi) for func in self._functions: setattr( self, func['name'], ContractFunction.factory(func['name'], web3=web3, contract_abi=self.abi, address=address, function_identifier=func['name']))
def find_functions_by_identifier(contract_abi, web3, address, callable_check): fns_abi = filter_by_type('function', contract_abi) return [ ContractFunction.factory( fn_abi['name'], web3=web3, contract_abi=contract_abi, address=address, function_identifier=fn_abi['name'], abi=fn_abi ) for fn_abi in fns_abi if callable_check(fn_abi) ]
def __init__(self, abi, web3, address=None): if abi: self.abi = abi self._events = filter_by_type('event', self.abi) for event in self._events: setattr( self, event['name'], ContractEvent.factory( event['name'], web3=web3, contract_abi=self.abi, address=address, event_name=event['name']))
def __init__(self, abi, web3, address=None): if abi: self.abi = abi self._functions = filter_by_type('function', self.abi) for func in self._functions: setattr( self, func['name'], ContractFunction.factory( func['name'], web3=web3, contract_abi=self.abi, address=address, function_identifier=func['name']))
def decode_event(abi: ABI, event_log: LogReceipt) -> EventData: """ Helper function to unpack event data using a provided ABI Args: abi: The ABI of the contract, not the ABI of the event event_log: The raw event data Returns: The decoded event """ event_id = event_log["topics"][0] events = filter_by_type("event", abi) topic_to_event_abi = { event_abi_to_log_topic(event_abi): event_abi for event_abi in events # type: ignore } event_abi = topic_to_event_abi[event_id] return get_event_data(ABI_CODEC, event_abi, event_log)
def get_event_list_by_protocol(protocol): events = {} for contract_name in os.listdir(f'../abi/{protocol}'): file_path = f'../abi/{protocol}/{contract_name}' abi = get_abi(file_path)['abi'] events_abi = filter_by_type('event', abi) for event_abi in events_abi: events.update({ to_hex(event_abi_to_log_topic(event_abi)): { 'name': event_abi['name'], 'inputs': event_abi['inputs'], 'anonymous': event_abi['anonymous'], 'signature': abi_to_signature(event_abi), 'abi': event_abi } }) return events
def create_event_topic_to_abi_dict() -> Dict[bytes, ABIEvent]: contract_names = [ CONTRACT_TOKEN_NETWORK_REGISTRY, CONTRACT_TOKEN_NETWORK, CONTRACT_MONITORING_SERVICE, ] event_abis = {} for contract_name in contract_names: events = filter_by_type( "event", CONTRACT_MANAGER.get_contract_abi(contract_name)) for event_abi in events: event_topic = event_abi_to_log_topic(event_abi) # type: ignore event_abis[event_topic] = event_abi return event_abis # type: ignore
def validate_abi(abi): """ Helper function for validating an ABI """ if not is_list_like(abi): raise ValueError("'abi' is not a list") if not all(is_dict(e) for e in abi): raise ValueError("'abi' is not a list of dictionaries") functions = filter_by_type('function', abi) selectors = groupby(compose(encode_hex, function_abi_to_4byte_selector), functions) duplicates = valfilter(lambda funcs: len(funcs) > 1, selectors) if duplicates: raise ValueError('Abi contains functions with colliding selectors. ' 'Functions {0}'.format( _prepare_selector_collision_msg(duplicates)))
def validate_abi(abi): """ Helper function for validating an ABI """ if not is_list_like(abi): raise ValueError("'abi' is not a list") if not all(is_dict(e) for e in abi): raise ValueError("'abi' is not a list of dictionaries") functions = filter_by_type('function', abi) selectors = groupby( compose(encode_hex, function_abi_to_4byte_selector), functions ) duplicates = valfilter(lambda funcs: len(funcs) > 1, selectors) if duplicates: raise ValueError( 'Abi contains functions with colliding selectors. ' 'Functions {0}'.format(_prepare_selector_collision_msg(duplicates)) )
def decode_event(abi_codec: ABICodec, abi: ABI, log_: LogReceipt) -> Dict: """Helper function to unpack event data using a provided ABI Args: abi_codec: The ABI codec abi: The ABI of the contract, not the ABI of the event log_: The raw event data Returns: The decoded event """ event_id = log_["topics"][0] events = filter_by_type("event", abi) topic_to_event_abi = { event_abi_to_log_topic(event_abi): event_abi for event_abi in events # type: ignore } event_abi = topic_to_event_abi[event_id] event_data = get_event_data(abi_codec=abi_codec, event_abi=event_abi, log_entry=log_) return cast(Dict[Any, Any], event_data)
def get_topics_of_events(abi: ABI) -> Dict[str, HexStr]: event_abis = filter_by_type("event", abi) return { ev["name"]: "0x" + event_abi_to_log_topic(ev).hex() for ev in event_abis # type: ignore }