def get_logs( contract: Contract, event_name: str, from_block: Union[int, str] = 0, to_block: Union[int, str] = 'pending', argument_filters: Dict[str, Any] = None ): event_abi = [ abi_element for abi_element in contract.abi if abi_element['type'] == 'event' and abi_element['name'] == event_name ] assert len(event_abi) == 1, 'No event found matching name {}.'.format(event_name) event_abi = event_abi[0] if argument_filters is None: argument_filters = {} filter_params = input_filter_params_formatter(construct_event_filter_params( event_abi, argument_filters=argument_filters, address=contract.address, fromBlock=from_block, toBlock=to_block )[1]) response = _get_logs_raw(contract, filter_params) logs = log_array_formatter(response) logs = [dict(log) for log in logs] for log in logs: log['args'] = get_event_data(event_abi, log)['args'] return logs
def filter(self, filter_params): if is_string(filter_params): if filter_params == "latest": filter_id = self.web3._requestManager.request_blocking( "eth_newBlockFilter", [], ) return BlockFilter(self.web3, filter_id) elif filter_params == "pending": filter_id = self.web3._requestManager.request_blocking( "eth_newPendingTransactionFilter", [], ) return TransactionFilter(self.web3, filter_id) else: raise ValueError( "The filter API only accepts the values of `pending` or " "`latest` for string based filters") elif isinstance(filter_params, dict): formatted_filter_params = formatters.input_filter_params_formatter( filter_params) filter_id = self.web3._requestManager.request_blocking( "eth_newFilter", [formatted_filter_params], ) return LogFilter(self.web3, filter_id) else: raise ValueError( "Must provide either a string or a valid filter object")
def get_logs(self, event_name, from_block=0, to_block='latest', filters=None): filter_kwargs = { 'fromBlock': from_block, 'toBlock': to_block, 'address': self.contract_addr } event_abi = [ i for i in self.auction_contract.abi if i['type'] == 'event' and i['name'] == event_name ][0] assert event_abi filters = filters if filters else {} filter_ = construct_event_filter_params(event_abi, argument_filters=filters, **filter_kwargs)[1] filter_params = input_filter_params_formatter(filter_) response = self.chain.web3._requestManager.request_blocking( 'eth_getLogs', [filter_params]) logs = log_array_formatter(response) logs = [dict(log) for log in logs] for log in logs: log['args'] = get_event_data(event_abi, log)['args'] return logs
def __init__(self, web3, abi, address, event_name, from_block=0, to_block='latest', filters=None, callback=None): self.web3 = web3 filter_kwargs = { 'fromBlock': from_block, 'toBlock': to_block, 'address': address } self.event_abi = [ i for i in abi if i['type'] == 'event' and i['name'] == event_name ][0] assert self.event_abi filters = filters if filters else {} self.filter = construct_event_filter_params(self.event_abi, argument_filters=filters, **filter_kwargs)[1] filter_params = input_filter_params_formatter(self.filter) self.filter = web3.eth.filter(filter_params) for log in self.get_logs(): callback(log) self.watch_logs(callback)
def __init__(self, web3, abi, address, event_name, from_block=0, to_block='latest', filters=None, callback=None): self.web3 = web3 self.event_name = event_name # Callback for every registered log self.callback = callback filter_kwargs = { 'fromBlock': from_block, 'toBlock': to_block, 'address': address } event_abi = [i for i in abi if i['type'] == 'event' and i['name'] == event_name] if len(event_abi) == 0: return None self.event_abi = event_abi[0] assert self.event_abi filters = filters if filters else {} self.filter = construct_event_filter_params( self.event_abi, argument_filters=filters, **filter_kwargs)[1] filter_params = input_filter_params_formatter(self.filter) self.filter = web3.eth.filter(filter_params)
def filter(self, filter_params): if is_string(filter_params): if filter_params == "latest": filter_id = self.web3._requestManager.request_blocking( "eth_newBlockFilter", [], ) return BlockFilter(self.web3, filter_id) elif filter_params == "pending": filter_id = self.web3._requestManager.request_blocking( "eth_newPendingTransactionFilter", [], ) return TransactionFilter(self.web3, filter_id) else: raise ValueError( "The filter API only accepts the values of `pending` or " "`latest` for string based filters" ) elif isinstance(filter_params, dict): formatted_filter_params = formatters.input_filter_params_formatter(filter_params) filter_id = self.web3._requestManager.request_blocking( "eth_newFilter", [formatted_filter_params], ) return LogFilter(self.web3, filter_id) else: raise ValueError("Must provide either a string or a valid filter object")