File size: 7,014 Bytes
011960a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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()