mbot/util.py

26 lines
871 B
Python
Raw Normal View History

2024-06-07 22:00:14 +00:00
import json
import requests
from loguru import logger
from typing import Any, Optional, Dict
from web3.types import ChecksumAddress
def fetch_abi(address: ChecksumAddress, headers: Optional[Dict[str, Any]] = None,
params: Optional[Dict[str, Any]] = None, key: Optional[str] = None) -> Optional[Dict[str, Any]]:
url = f'https://api.etherscan.io/api?module=contract&action=getabi&address={address}&apikey={key}'
2024-06-07 22:00:14 +00:00
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
# Check for error in the response
if data['status'] == '0':
logger.error(f"Error fetching ABI: {data.get('result')}")
return None
return json.loads(data['result'])
def load_config(path: str) -> dict:
with open(path, 'r', encoding='utf-8') as f:
return json.loads(f.read())