Spaces:
Build error
Build error
import os | |
import sys | |
import json | |
import urllib.request | |
import urllib.parse | |
import urllib.error | |
from urllib.error import URLError, HTTPError | |
# Simple dotenv implementation since the module may not be available | |
def load_dotenv(): | |
try: | |
with open('.env', 'r') as file: | |
for line in file: | |
line = line.strip() | |
if not line or line.startswith('#') or '=' not in line: | |
continue | |
key, value = line.split('=', 1) | |
os.environ[key] = value | |
except Exception as e: | |
print(f"Error loading .env file: {e}") | |
return False | |
return True | |
# Load environment variables | |
load_dotenv() | |
# Get API key from .env | |
ARBISCAN_API_KEY = os.getenv("ARBISCAN_API_KEY") | |
if not ARBISCAN_API_KEY: | |
print("ERROR: ARBISCAN_API_KEY not found in .env file") | |
sys.exit(1) | |
print(f"Using Arbiscan API Key: {ARBISCAN_API_KEY[:5]}...") | |
# Test addresses (known active ones) | |
TEST_ADDRESSES = [ | |
"0x5d8908afee1df9f7f0830105f8be828f97ce9e68", # Arbitrum Treasury | |
"0x2b1ad6184a6b0fac06bd225ed37c2abc04415ff4", # Large holder | |
"0xc47ff7f9efb3ef39c33a2c492a1372418d399ec2", # Active trader | |
] | |
# User-provided addresses (from command line arguments) | |
if len(sys.argv) > 1: | |
USER_ADDRESSES = sys.argv[1:] | |
TEST_ADDRESSES.extend(USER_ADDRESSES) | |
print(f"Added user-provided addresses: {USER_ADDRESSES}") | |
def test_api_key(): | |
"""Test if the API key is valid""" | |
base_url = "https://api.arbiscan.io/api" | |
params = { | |
"module": "stats", | |
"action": "ethsupply", | |
"apikey": ARBISCAN_API_KEY | |
} | |
try: | |
print("\n===== TESTING API KEY =====") | |
# Construct URL with parameters | |
query_string = urllib.parse.urlencode(params) | |
url = f"{base_url}?{query_string}" | |
print(f"Making request to: {url}") | |
# Make the request | |
with urllib.request.urlopen(url) as response: | |
response_data = response.read().decode('utf-8') | |
data = json.loads(response_data) | |
print(f"Response status code: {response.status}") | |
print(f"Response JSON status: {data.get('status')}") | |
print(f"Response message: {data.get('message', 'No message')}") | |
if data.get("status") == "1": | |
print("β API KEY IS VALID") | |
return True | |
else: | |
print("β API KEY IS INVALID OR HAS ISSUES") | |
if "API Key" in data.get("message", ""): | |
print(f"Error message: {data.get('message')}") | |
print("β You need to register for an API key at https://arbiscan.io/myapikey") | |
return False | |
except HTTPError as e: | |
print(f"β HTTP Error: {e.code} - {e.reason}") | |
return False | |
except URLError as e: | |
print(f"β URL Error: {e.reason}") | |
return False | |
except Exception as e: | |
print(f"β Error testing API key: {str(e)}") | |
return False | |
def test_address(address): | |
"""Test if an address has transactions on Arbitrum""" | |
base_url = "https://api.arbiscan.io/api" | |
# Test for token transfers | |
params_token = { | |
"module": "account", | |
"action": "tokentx", | |
"address": address, | |
"startblock": "0", | |
"endblock": "99999999", | |
"page": "1", | |
"offset": "10", # Just get 10 for testing | |
"sort": "desc", | |
"apikey": ARBISCAN_API_KEY | |
} | |
# Test for normal transactions | |
params_normal = { | |
"module": "account", | |
"action": "txlist", | |
"address": address, | |
"startblock": "0", | |
"endblock": "99999999", | |
"page": "1", | |
"offset": "10", # Just get 10 for testing | |
"sort": "desc", | |
"apikey": ARBISCAN_API_KEY | |
} | |
print(f"\n===== TESTING ADDRESS: {address} =====") | |
# Check token transfers | |
try: | |
print("Testing token transfers...") | |
# Construct URL with parameters | |
query_string = urllib.parse.urlencode(params_token) | |
url = f"{base_url}?{query_string}" | |
# Make the request | |
with urllib.request.urlopen(url) as response: | |
response_data = response.read().decode('utf-8') | |
data = json.loads(response_data) | |
if data.get("status") == "1": | |
transfers = data.get("result", []) | |
print(f"β Found {len(transfers)} token transfers") | |
if transfers: | |
print(f"First transfer: {json.dumps(transfers[0], indent=2)[:200]}...") | |
else: | |
print(f"β No token transfers found: {data.get('message', 'Unknown error')}") | |
except HTTPError as e: | |
print(f"β HTTP Error: {e.code} - {e.reason}") | |
except URLError as e: | |
print(f"β URL Error: {e.reason}") | |
except Exception as e: | |
print(f"β Error testing token transfers: {str(e)}") | |
# Check normal transactions | |
try: | |
print("\nTesting normal transactions...") | |
# Construct URL with parameters | |
query_string = urllib.parse.urlencode(params_normal) | |
url = f"{base_url}?{query_string}" | |
# Make the request | |
with urllib.request.urlopen(url) as response: | |
response_data = response.read().decode('utf-8') | |
data = json.loads(response_data) | |
if data.get("status") == "1": | |
transactions = data.get("result", []) | |
print(f"β Found {len(transactions)} normal transactions") | |
if transactions: | |
print(f"First transaction: {json.dumps(transactions[0], indent=2)[:200]}...") | |
else: | |
print(f"β No normal transactions found: {data.get('message', 'Unknown error')}") | |
except HTTPError as e: | |
print(f"β HTTP Error: {e.code} - {e.reason}") | |
except URLError as e: | |
print(f"β URL Error: {e.reason}") | |
except Exception as e: | |
print(f"β Error testing normal transactions: {str(e)}") | |
def main(): | |
"""Main function to run tests""" | |
print("=================================================") | |
print("Arbitrum API Diagnostic Tool") | |
print("=================================================") | |
# Test the API key first | |
api_valid = test_api_key() | |
if not api_valid: | |
print("\nβ οΈ Please update your API key in the .env file") | |
print("Register for an API key at https://arbiscan.io/myapikey") | |
return | |
# Test each address | |
for address in TEST_ADDRESSES: | |
test_address(address) | |
print("\n=================================================") | |
print("RECOMMENDATIONS:") | |
print("1. If your API key is invalid, update it in the .env file") | |
print("2. If test addresses work but yours don't, your addresses might not have activity on Arbitrum") | |
print("3. Use one of the working test addresses in your app for testing") | |
print("=================================================") | |
if __name__ == "__main__": | |
main() | |