import httpx
import logging
import sys
import os
import json
from datetime import datetime
from mcp_app import mcp
logging.basicConfig(
stream=sys.stderr, level=logging.INFO, format="[LOG] %(message)s", force=True
)
@mcp.tool()
async def get_transactions(address: str) -> str:
"""
Fetch the 5 latest transactions for a specific address on the FRAX Network
Args:
address: the wallet address to check
"""
if (
not isinstance(address, str)
or not address.startswith("0x")
or len(address) != 42
):
return "Error: Invalid address"
api_key = os.getenv("FRAXSCAN_API_KEY")
if not api_key:
return "Error: FRAXSCAN_API_KEY not found"
logging.info(f"Fetching transactions for {address}")
url = "https://api.etherscan.io/v2/api"
params = {
"apikey": api_key,
"module": "account",
"action": "txlist",
"address": address,
"chainid": "252",
"offset": 5,
"page": 1,
"sort": "desc",
"startblock": 0,
"endblock": 99999999,
}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, params=params)
if response.status_code != 200:
return f"Http Error: {response.status_code}"
data = response.json()
if data["status"] != "1":
if data["message"] == "No transaction found":
return "No transactions found."
return f"Api Error: {data['message']}"
# Convert to clean format
raw_txs = data["result"]
clean_txs = []
for tx in raw_txs:
dt_object = datetime.fromtimestamp(int(tx["timeStamp"]))
readable_time = dt_object.strftime("%Y-%m-%d %H:%M:%S")
value_wei = float(tx["value"])
value_ether = value_wei / 10**18
clean_txs.append(
{
"hash": tx["hash"],
"from": tx["from"],
"to": tx["to"],
"amount": f"{value_ether:.4f} frxETH",
"time": readable_time,
"block": tx["blockNumber"],
}
)
return json.dumps(clean_txs, indent=2)
except Exception as e:
return f"Error fetching transactions: {str(e)}"