Skip to content

Commit 9de6b13

Browse files
authored
Merge pull request #10 from Badiboy/main
Timeout and get_erc20_token_transfer_events_by_address_paginated
2 parents 5e36de8 + 867e433 commit 9de6b13

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

polygonscan/configs/stable.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@
226226
"sort": "asc"
227227
}
228228
},
229+
"get_erc20_token_transfer_events_by_address_paginated": {
230+
"module": "accounts",
231+
"kwargs": {
232+
"address": "0x4e83362442b8d1bec281594cea3050c8eb01311c",
233+
"startblock": 0,
234+
"endblock": 999999999,
235+
"page": 1,
236+
"offset": 10,
237+
"sort": "asc"
238+
}
239+
},
229240
"get_erc20_token_transfer_events_by_contract_address_paginated": {
230241
"module": "accounts",
231242
"kwargs": {

polygonscan/core/async_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import polygonscan
2-
from aiohttp import ClientSession
2+
from aiohttp import ClientSession, ClientTimeout
33
from polygonscan.core.base import BaseClient
44
from polygonscan.enums.fields_enum import FieldsEnum as fields
55
from polygonscan.utils.parsing import ResponseParser as parser
@@ -29,7 +29,11 @@ async def wrapper(*args, **kwargs):
2929
return wrapper
3030

3131
async def __aenter__(self):
32-
self._session = ClientSession()
32+
if self._timeout:
33+
timeout = ClientTimeout(total=self._timeout)
34+
else:
35+
timeout = None
36+
self._session = ClientSession(timeout=timeout)
3337
return await self._build()
3438

3539
async def __aexit__(self, exc_type, exc_val, exc_tb):

polygonscan/core/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ def __init__(
1111
self,
1212
api_key: str,
1313
debug: bool = False, # display generated URLs for debugging purposes
14+
timeout: int = None, # timeout for requests (None - no timeout, for backward compatibility)
1415
):
1516
self._config = self._load_config()
1617
self._api_key = api_key
1718
self._debug = debug
19+
self._timeout = timeout
1820

1921
@staticmethod
2022
def _load_config(config_file: str = CONFIG_FILE) -> dict:

polygonscan/core/sync_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def wrapper(*args, **kwargs):
2323
)
2424
if self._debug:
2525
print(f"\n{url}\n")
26-
with self._session.get(url) as response:
26+
with self._session.get(url, timeout=self._timeout) as response:
2727
return parser.parse(response.json())
2828

2929
return wrapper

polygonscan/modules/accounts.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,31 @@ def get_erc20_token_transfer_events_by_address(
187187
)
188188
return url
189189

190+
@staticmethod
191+
def get_erc20_token_transfer_events_by_address_paginated(
192+
address: str, page: int, offset: int, startblock: int, endblock: int, sort: str,
193+
) -> str:
194+
# NOTE: Returns the last 10k events
195+
url = (
196+
f"{fields.MODULE}"
197+
f"{modules.ACCOUNT}"
198+
f"{fields.ACTION}"
199+
f"{actions.TOKENTX}"
200+
f"{fields.ADDRESS}"
201+
f"{address}"
202+
f"{fields.START_BLOCK}"
203+
f"{str(startblock)}"
204+
f"{fields.END_BLOCK}"
205+
f"{str(endblock)}"
206+
f"{fields.SORT}"
207+
f"{sort}"
208+
f"{fields.PAGE}"
209+
f"{str(page)}"
210+
f"{fields.OFFSET}"
211+
f"{str(offset)}"
212+
)
213+
return url
214+
190215
@staticmethod
191216
def get_erc20_token_transfer_events_by_contract_address_paginated(
192217
contract_address: str, page: int, offset: int, sort: str

polygonscan/polygonscan.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
class PolygonScan:
1010
"""Client factory."""
1111

12-
def __new__(cls, api_key: str, asynchronous=True, debug=False) -> BaseClient:
12+
def __new__(cls, api_key: str, asynchronous=True, debug=False, timeout=None) -> BaseClient:
1313
"""Create a new client.
1414
Args:
1515
api_key (str): Your polygonscan.com API key.
1616
asynchronous (bool, optional): Whether client is async or not. Defaults to True.
1717
debug (bool, optional): Display generated URLs for debugging. Defaults to False.
18+
timeout (int, optional): Timeout for requests. Defaults to None.
1819
Returns:
1920
BaseClient: polygonscan client.
2021
"""
2122
if asynchronous:
22-
return AsyncClient(api_key=api_key, debug=debug)
23-
return SyncClient(api_key=api_key, debug=debug)
23+
return AsyncClient(api_key=api_key, debug=debug, timeout=timeout)
24+
return SyncClient(api_key=api_key, debug=debug, timeout=timeout)

0 commit comments

Comments
 (0)