Skip to content

Commit c4ec497

Browse files
authored
Add on-chain metadata verification (#45)
1 parent 363071b commit c4ec497

File tree

6 files changed

+368
-98
lines changed

6 files changed

+368
-98
lines changed

mainnet/USD1/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"chainId": 143,
33
"address": "0x111111d2bf19e43C34263401e0CAd979eD1cdb61",
4-
"name": "USD1",
4+
"name": "World Liberty Financial USD",
55
"symbol": "USD1",
66
"decimals": 6
77
}

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ignore = [
3333
"PLR0912", # Too many branches
3434
"PLR0913", # Too many arguments to function call
3535
"PLR2004", # Magic value used in comparison
36+
"PERF203", # try-except in loop
3637
]
3738

3839
[lint.isort]

scripts/add_token.py

Lines changed: 8 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,14 @@
88

99
import argparse
1010
import json
11-
import os
1211
import sys
1312
from 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

4421
def 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-
11139
def 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"\nFetching 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("\nToken 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:

scripts/utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)