|
| 1 | +from .client import Client |
| 2 | + |
| 3 | + |
| 4 | +class LogsException(Exception): |
| 5 | + """Base class for exceptions in this module.""" |
| 6 | + pass |
| 7 | + |
| 8 | + |
| 9 | +class Logs(Client): |
| 10 | + """ |
| 11 | + The Event Log API was designed to provide an alternative to the native eth_getLogs. |
| 12 | + """ |
| 13 | + def __init__(self, api_key='YourApiKeyToken'): |
| 14 | + Client.__init__(self, address='', api_key=api_key) |
| 15 | + self.url_dict[self.MODULE] = 'logs' |
| 16 | + |
| 17 | + def get_logs(self, from_block: str, to_block='latest', |
| 18 | + topic0='', topic1='', topic0_1_opr='and',) -> list: |
| 19 | + """ |
| 20 | + Get Event Logs from block number [from_block] to block [to_block] , |
| 21 | + where log address = [address], topic[0] = [topic0] 'AND' topic[1] = [topic1] |
| 22 | +
|
| 23 | + Args: |
| 24 | + from_block (str): start block number |
| 25 | + to_block (str, optional): end block number. Defaults to 'latest'. |
| 26 | + topic0 (str, optional): Defaults to ''. |
| 27 | + topic1 (str, optional): Defaults to ''. |
| 28 | + topic0_1_opr (str, optional): and|or between topic0 & topic1. Defaults to 'and'. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + list: [description] |
| 32 | + """ |
| 33 | + # TODO: support multi topics |
| 34 | + if not topic0 and topic1: |
| 35 | + raise(LogsException('can not only set topic1 while topic0 is empty')) |
| 36 | + self.url_dict[self.ACTION] = 'getLogs' |
| 37 | + self.url_dict[self.FROM_BLOCK] = from_block if type( |
| 38 | + from_block) is str else str(from_block) |
| 39 | + self.url_dict[self.TO_BLOCK] = to_block if type( |
| 40 | + to_block) is str else str(to_block) |
| 41 | + self.url_dict[self.TOPIC0] = topic0 if type( |
| 42 | + topic0) is str else hex(topic0) |
| 43 | + self.url_dict[self.TOPIC1] = topic1 if type( |
| 44 | + topic1) is str else hex(topic1) |
| 45 | + self.url_dict[self.TOPIC0_1_OPR] = topic0_1_opr |
| 46 | + self.build_url() |
| 47 | + req = self.connect() |
| 48 | + return req['result'] |
0 commit comments