88
99import argparse
1010import json
11- import os
1211import sys
1312from pathlib import Path
1413
15- from web3 import Web3
16-
17- CHAIN_ID = 143
18- RPC_URL = os .environ .get ("MONAD_RPC_URL" , "https://rpc.monad.xyz" )
19- ERC20_ABI = [
20- {
21- "constant" : True ,
22- "inputs" : [],
23- "name" : "name" ,
24- "outputs" : [{"name" : "" , "type" : "string" }],
25- "type" : "function" ,
26- },
27- {
28- "constant" : True ,
29- "inputs" : [],
30- "name" : "symbol" ,
31- "outputs" : [{"name" : "" , "type" : "string" }],
32- "type" : "function" ,
33- },
34- {
35- "constant" : True ,
36- "inputs" : [],
37- "name" : "decimals" ,
38- "outputs" : [{"name" : "" , "type" : "uint8" }],
39- "type" : "function" ,
40- },
41- ]
14+ from utils .web3 import (
15+ fetch_token_data_with_retry ,
16+ get_web3_connection ,
17+ validate_address ,
18+ )
4219
4320
4421def get_mainnet_directory () -> Path :
@@ -59,55 +36,6 @@ def get_mainnet_directory() -> Path:
5936 return mainnet_dir
6037
6138
62- def validate_address (address : str ) -> str :
63- """Validate and normalize an Ethereum address.
64-
65- Args:
66- address: The address string to validate.
67-
68- Returns:
69- str: Checksummed address.
70-
71- Raises:
72- ValueError: If the address is invalid.
73- """
74- if not Web3 .is_address (address ):
75- raise ValueError (f"Invalid Ethereum address: { address } " )
76-
77- return Web3 .to_checksum_address (address )
78-
79-
80- def fetch_token_data (web3 : Web3 , address : str ) -> dict :
81- """Fetch token data from the blockchain.
82-
83- Args:
84- web3: Web3 instance connected to the chain.
85- address: Token contract address.
86-
87- Returns:
88- dict: Token data containing name, symbol, and decimals.
89-
90- Raises:
91- Exception: If fetching token data fails.
92- """
93- contract = web3 .eth .contract (address = address , abi = ERC20_ABI )
94-
95- try :
96- name = contract .functions .name ().call ()
97- symbol = contract .functions .symbol ().call ()
98- decimals = contract .functions .decimals ().call ()
99- except Exception as e :
100- raise Exception (f"Failed to fetch token data: { e } " ) from e
101-
102- return {
103- "chainId" : CHAIN_ID ,
104- "address" : address ,
105- "name" : name ,
106- "symbol" : symbol ,
107- "decimals" : decimals ,
108- }
109-
110-
11139def create_token_directory (mainnet_dir : Path , token_data : dict ) -> Path :
11240 """Create token directory and data.json file.
11341
@@ -167,15 +95,11 @@ def main() -> int:
16795 address = validate_address (address )
16896 print (f"Checksummed address: { address } " )
16997
170- web3 = Web3 (Web3 .HTTPProvider (RPC_URL ))
171- if not web3 .is_connected ():
172- print ("Error: Failed to connect to the RPC" )
173- return 1
174-
98+ web3 = get_web3_connection ()
17599 print ("Connected successfully" )
176100
177101 print (f"\n Fetching token data from { address } ..." )
178- token_data = fetch_token_data (web3 , address )
102+ token_data = fetch_token_data_with_retry (web3 , address )
179103
180104 print ("\n Token found:" )
181105 print (f" Name: { token_data ['name' ]} " )
@@ -196,13 +120,7 @@ def main() -> int:
196120 )
197121
198122 return 0
199- except ValueError as e :
200- print (f"Error: { e } " )
201- return 1
202- except FileNotFoundError as e :
203- print (f"Error: { e } " )
204- return 1
205- except FileExistsError as e :
123+ except (ValueError , ConnectionError , FileNotFoundError , FileExistsError ) as e :
206124 print (f"Error: { e } " )
207125 return 1
208126 except Exception as e :
0 commit comments