diff --git a/trc20wrapper/__init__.py b/trc20wrapper/__init__.py index 6d3cd5d..6bdc42f 100644 --- a/trc20wrapper/__init__.py +++ b/trc20wrapper/__init__.py @@ -1,4 +1,4 @@ from trc20wrapper.models import TransactionInfo -from trc20wrapper.transactions import get_transaction_by_hash +from trc20wrapper.transactions import get_transaction_by_hash, get_transactions -__all__ = ["get_transaction_by_hash", "TransactionInfo"] +__all__ = ["get_transaction_by_hash", "TransactionInfo", "get_transactions"] diff --git a/trc20wrapper/transactions.py b/trc20wrapper/transactions.py index 126ef15..a4ca036 100644 --- a/trc20wrapper/transactions.py +++ b/trc20wrapper/transactions.py @@ -43,3 +43,45 @@ async def get_transaction_by_hash( from_=from_, to=to, ) + + +async def get_transactions( + addr: str, + only_confirmed: bool = True, + limit: int = 200, +) -> list[TransactionInfo]: + url = f"https://api.trongrid.io/v1/accounts/{addr}/transactions/trc20" + params: dict = {"limit": limit} + if only_confirmed: + params["only_confirmed"] = "true" + timeout = aiohttp.ClientTimeout(total=30) + async with aiohttp.ClientSession(timeout=timeout) as s: + async with s.get(url, params=params, timeout=timeout) as r: + if r.status != 200: + return [] + json_data = await r.json() + if not isinstance(json_data, dict): + return [] + transactions: list[TransactionInfo] = [] + for tx in json_data["data"]: + hash_ = tx.get("transaction_id", "") + symbol = tx.get("token_info", {}).get("symbol", "").lower() + v = tx.get("value", "") + dec = -1 * int(tx.get("token_info", {}).get("decimals", "6")) + f = float(v[:dec] + "." + v[dec:]) + time_ = datetime.fromtimestamp( + float(tx.get("block_timestamp", "")) / 1000, timezone.utc + ) + from_ = tx.get("from", "") + to = tx.get("to", "") + transactions.append( + TransactionInfo( + symbol=symbol, + amount=f, + hash=hash_, + time=time_, + from_=from_, + to=to, + ) + ) + return transactions