rmi-backend/app/core/tron_provider.py
opencode c762564d40 style(rmi-backend): complete lint cleanup — 1175→0 ruff errors
- Fix 71 invalid-syntax files (class-body newline-broken assignments)
- Add from/None chain to 307 B904 raise-without-from sites
- Add B008 ignore to ruff.toml (already in pyproject.toml)
- Noqa F401 on __init__.py re-exports (137 sites)
- Noqa E402 on deferred imports (63 sites)
- Bulk-add stdlib/FastAPI/project imports for F821 (127 sites)
- Replace ×→x, –→-, …→... in docstrings (4093 chars)
- Manual refactor of 5 SIM103/SIM116 patterns

Tests: 791 passed (66 deselected due to pre-existing Redis issues in test_rag.py)
Co-authored-by: opencode <opencode@rugmunch.io>
2026-07-06 15:43:20 +02:00

68 lines
2.5 KiB
Python

"""Tron blockchain provider - free TronGrid API, no key needed."""
import logging
import httpx
logger = logging.getLogger(__name__)
TRONGRID = "https://api.trongrid.io"
async def tron_balance(address: str) -> dict | None:
"""Get TRX balance + TRC20 tokens for an address."""
try:
async with httpx.AsyncClient(timeout=10) as c:
# TRX balance
r = await c.get(f"{TRONGRID}/v1/accounts/{address}")
if r.status_code == 200:
data = r.json().get("data", [{}])[0]
balance_trx = data.get("balance", 0) / 1_000_000
# TRC20 tokens
r2 = await c.get(f"{TRONGRID}/v1/accounts/{address}/transactions/trc20", params={"limit": 1})
tokens = []
if r2.status_code == 200:
for t in r2.json().get("data", [])[:1]:
tokens.append(
{
"token": t.get("token_info", {}).get("symbol", "?"),
"address": t.get("token_info", {}).get("address", ""),
}
)
return {
"address": address,
"balance_trx": round(balance_trx, 2),
"bandwidth": data.get("free_net_usage", 0),
"energy": data.get("account_resource", {}).get("energy_usage", 0),
"trc20_tokens": tokens,
"provider": "trongrid",
}
except Exception as e:
logger.warning(f"Tron balance failed: {e}")
return None
async def tron_transactions(address: str, limit: int = 10) -> dict | None:
"""Get recent transactions for an address."""
try:
async with httpx.AsyncClient(timeout=10) as c:
r = await c.get(f"{TRONGRID}/v1/accounts/{address}/transactions", params={"limit": limit})
if r.status_code == 200:
txs = r.json().get("data", [])
return {
"address": address,
"transactions": [
{
"tx_id": tx.get("txID", "")[:16],
"block": tx.get("blockNumber", 0),
"timestamp": tx.get("block_timestamp", 0),
}
for tx in txs[:limit]
],
"count": len(txs),
"provider": "trongrid",
}
except Exception:
pass
return None