Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2142a31

Browse files
authored
Create logs.py
module for get_logs
1 parent dcc3fb7 commit 2142a31

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

logs.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from etherscan.enums.actions_enum import ActionsEnum as actions
2+
from etherscan.enums.fields_enum import FieldsEnum as fields
3+
from etherscan.enums.modules_enum import ModulesEnum as modules
4+
5+
6+
class Logs:
7+
@staticmethod
8+
def get_logs(
9+
from_block: int,
10+
to_block: int,
11+
address: str,
12+
topic_0: str = "",
13+
topic_1: str = "",
14+
topic_2: str = "",
15+
topic_3: str = "",
16+
topic_0_1_opr: str = "",
17+
topic_1_2_opr: str = "",
18+
topic_2_3_opr: str = "",
19+
topic_0_2_opr: str = "",
20+
topic_0_3_opr: str = "",
21+
topic_1_3_opr: str = "",
22+
):
23+
"""This is an alternative to the native eth_getLogs. An address and/or
24+
topic_x parameters are required. When multiple topic_x parameters are
25+
used, the topic_x_y_opr ("and"/"or" operator) is also required.
26+
**NOTE: Only the first 1000 results are returned.**
27+
Args:
28+
from_block (int): Start block of the query.
29+
to_block (int): End block of the query.
30+
address (str): Address of the logs.
31+
topic_0 (str, optional): Topic 0 in the logs. Defaults to "".
32+
topic_1 (str, optional): Topic 1 in the logs. Defaults to "".
33+
topic_2 (str, optional): Topic 2 in the logs. Defaults to "".
34+
topic_3 (str, optional): Topic 3 in the logs. Defaults to "".
35+
topic_0_1_opr (str, optional): Logical operator between topic 0 and 1. Defaults to "".
36+
topic_1_2_opr (str, optional): Logical operator between topic 1 and 2. Defaults to "".
37+
topic_2_3_opr (str, optional): Logical operator between topic 2 and 3. Defaults to "".
38+
topic_0_2_opr (str, optional): Logical operator between topic 0 and 2. Defaults to "".
39+
topic_0_3_opr (str, optional): Logical operator between topic 0 and 3. Defaults to "".
40+
topic_1_3_opr (str, optional): Logical operator between topic 1 and 3. Defaults to "".
41+
Returns:
42+
dict: The event logs in a dictionary, including topics and data fields.
43+
Example::
44+
from etherscan import Etherscan
45+
async with Etherscan(YOUR_API_KEY) as client:
46+
print(
47+
await client.get_logs(
48+
from_block=4993830,
49+
to_block=4993832,
50+
address="0xe561479bebee0e606c19bb1973fc4761613e3c42",
51+
topic_0="0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
52+
topic_0_1_opr="and",
53+
topic_1="0x000000000000000000000000730e2065b9daee84c3003c05bf6d2b3a08e55667"
54+
)
55+
)
56+
Results::
57+
[
58+
{
59+
"address": "0xe561479bebee0e606c19bb1973fc4761613e3c42",
60+
"topics": [
61+
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
62+
"0x000000000000000000000000730e2065b9daee84c3003c05bf6d2b3a08e55667",
63+
"0x000000000000000000000000d7d19938eae260d7f0e0a4c36e665ff4cf4b7acc"
64+
],
65+
"data": "0x000000000000000000000000000000000000000000000000076cd96f53f24b0a",
66+
"blockNumber": "0x4c3326",
67+
"timeStamp": "0x602e9ef1",
68+
"gasPrice": "0x2540be400",
69+
"gasUsed": "0x1b0f2",
70+
"logIndex": "0xf7",
71+
"transactionHash": "0x73844fcfc6beab2e973a897c9573f4d79811b12213ce263045a203e0d3cea90e",
72+
"transactionIndex": "0xb9"
73+
}
74+
]
75+
"""
76+
return (
77+
f"{fields.MODULE}"
78+
f"{modules.LOGS}"
79+
f"{fields.ACTION}"
80+
f"{actions.GET_LOGS}"
81+
f"{fields.FROM_BLOCK}"
82+
f"{from_block}"
83+
f"{fields.TO_BLOCK}"
84+
f"{to_block}"
85+
f"{fields.ADDRESS}"
86+
f"{address}"
87+
# topic 0
88+
f"{fields.TOPIC_0}"
89+
f"{topic_0}"
90+
#
91+
# Everything below is optional. If not provided by user, then
92+
# they remain empty and do not affect the tail of the url.
93+
#
94+
# topic 0_x operators
95+
f"{fields.TOPIC_0_1_OPR*bool(topic_0_1_opr)}"
96+
f"{topic_0_1_opr}"
97+
f"{fields.TOPIC_0_2_OPR*bool(topic_0_2_opr)}"
98+
f"{topic_0_2_opr}"
99+
f"{fields.TOPIC_0_3_OPR*bool(topic_0_3_opr)}"
100+
f"{topic_0_3_opr}"
101+
# topic 1
102+
f"{fields.TOPIC_1*bool(topic_1)}"
103+
f"{topic_1}"
104+
# topic 1_x operators
105+
f"{fields.TOPIC_1_2_OPR*bool(topic_1_2_opr)}"
106+
f"{topic_1_2_opr}"
107+
f"{fields.TOPIC_1_3_OPR*bool(topic_1_3_opr)}"
108+
f"{topic_1_3_opr}"
109+
# topic 2
110+
f"{fields.TOPIC_2*bool(topic_2)}"
111+
f"{topic_2}"
112+
# topic 2_x operators
113+
f"{fields.TOPIC_2_3_OPR*bool(topic_2_3_opr)}"
114+
f"{topic_2_3_opr}"
115+
# topic 3
116+
f"{fields.TOPIC_3*bool(topic_3)}"
117+
f"{topic_3}"
118+
)

0 commit comments

Comments
 (0)