get transactions

This commit is contained in:
2025-07-10 20:32:15 +03:00
parent c9a8ecad29
commit 08e614480a
2 changed files with 44 additions and 2 deletions

View File

@ -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"]

View File

@ -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