Skip to content

Commit

Permalink
Merge pull request #10 from Badiboy/main
Browse files Browse the repository at this point in the history
Timeout and get_erc20_token_transfer_events_by_address_paginated
  • Loading branch information
tarsil authored Jun 23, 2024
2 parents 5e36de8 + 867e433 commit 9de6b13
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions polygonscan/configs/stable.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@
"sort": "asc"
}
},
"get_erc20_token_transfer_events_by_address_paginated": {
"module": "accounts",
"kwargs": {
"address": "0x4e83362442b8d1bec281594cea3050c8eb01311c",
"startblock": 0,
"endblock": 999999999,
"page": 1,
"offset": 10,
"sort": "asc"
}
},
"get_erc20_token_transfer_events_by_contract_address_paginated": {
"module": "accounts",
"kwargs": {
Expand Down
8 changes: 6 additions & 2 deletions polygonscan/core/async_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import polygonscan
from aiohttp import ClientSession
from aiohttp import ClientSession, ClientTimeout
from polygonscan.core.base import BaseClient
from polygonscan.enums.fields_enum import FieldsEnum as fields
from polygonscan.utils.parsing import ResponseParser as parser
Expand Down Expand Up @@ -29,7 +29,11 @@ async def wrapper(*args, **kwargs):
return wrapper

async def __aenter__(self):
self._session = ClientSession()
if self._timeout:
timeout = ClientTimeout(total=self._timeout)
else:
timeout = None
self._session = ClientSession(timeout=timeout)
return await self._build()

async def __aexit__(self, exc_type, exc_val, exc_tb):
Expand Down
2 changes: 2 additions & 0 deletions polygonscan/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def __init__(
self,
api_key: str,
debug: bool = False, # display generated URLs for debugging purposes
timeout: int = None, # timeout for requests (None - no timeout, for backward compatibility)
):
self._config = self._load_config()
self._api_key = api_key
self._debug = debug
self._timeout = timeout

@staticmethod
def _load_config(config_file: str = CONFIG_FILE) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion polygonscan/core/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def wrapper(*args, **kwargs):
)
if self._debug:
print(f"\n{url}\n")
with self._session.get(url) as response:
with self._session.get(url, timeout=self._timeout) as response:
return parser.parse(response.json())

return wrapper
Expand Down
25 changes: 25 additions & 0 deletions polygonscan/modules/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ def get_erc20_token_transfer_events_by_address(
)
return url

@staticmethod
def get_erc20_token_transfer_events_by_address_paginated(
address: str, page: int, offset: int, startblock: int, endblock: int, sort: str,
) -> str:
# NOTE: Returns the last 10k events
url = (
f"{fields.MODULE}"
f"{modules.ACCOUNT}"
f"{fields.ACTION}"
f"{actions.TOKENTX}"
f"{fields.ADDRESS}"
f"{address}"
f"{fields.START_BLOCK}"
f"{str(startblock)}"
f"{fields.END_BLOCK}"
f"{str(endblock)}"
f"{fields.SORT}"
f"{sort}"
f"{fields.PAGE}"
f"{str(page)}"
f"{fields.OFFSET}"
f"{str(offset)}"
)
return url

@staticmethod
def get_erc20_token_transfer_events_by_contract_address_paginated(
contract_address: str, page: int, offset: int, sort: str
Expand Down
7 changes: 4 additions & 3 deletions polygonscan/polygonscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
class PolygonScan:
"""Client factory."""

def __new__(cls, api_key: str, asynchronous=True, debug=False) -> BaseClient:
def __new__(cls, api_key: str, asynchronous=True, debug=False, timeout=None) -> BaseClient:
"""Create a new client.
Args:
api_key (str): Your polygonscan.com API key.
asynchronous (bool, optional): Whether client is async or not. Defaults to True.
debug (bool, optional): Display generated URLs for debugging. Defaults to False.
timeout (int, optional): Timeout for requests. Defaults to None.
Returns:
BaseClient: polygonscan client.
"""
if asynchronous:
return AsyncClient(api_key=api_key, debug=debug)
return SyncClient(api_key=api_key, debug=debug)
return AsyncClient(api_key=api_key, debug=debug, timeout=timeout)
return SyncClient(api_key=api_key, debug=debug, timeout=timeout)

0 comments on commit 9de6b13

Please sign in to comment.