-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_info.py
178 lines (154 loc) · 6.04 KB
/
wallet_info.py
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
# wallet_info.py
import json
from pathlib import Path
from web3 import Web3
from web3.middleware import geth_poa_middleware
from decimal import Decimal, getcontext
import logging
from run_service import (
get_local_config,
get_service_template,
CHAIN_ID_TO_METADATA,
USDC_ADDRESS,
OPERATE_HOME,
)
from utils import (
get_chain_name,
load_operator_address,
validate_config,
wei_to_unit,
wei_to_eth,
ColorCode,
)
# Set decimal precision
getcontext().prec = 18
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(message)s')
# Ensure USDC_ADDRESS is set (if not, define it here)
USDC_ADDRESS = USDC_ADDRESS or "0xA0b86991c6218b36c1d19d4a2e9Eb0cE3606eB48" # Ethereum Mainnet USDC address
USDC_ABI = [{
"constant": True,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"type": "function"
}]
def load_config():
try:
optimus_config = get_local_config()
service_template = get_service_template(optimus_config)
service_hash = service_template.get("hash")
if not service_hash:
print("Error: Service hash not found in service template.")
return {}
config_path = OPERATE_HOME / f"services/{service_hash}/config.json"
try:
with open(config_path, "r") as f:
return json.load(f)
except FileNotFoundError:
print(f"Error: Config file not found at {config_path}")
return {}
except json.JSONDecodeError:
print("Error: Config file contains invalid JSON.")
return {}
except Exception as e:
print(f"An error occurred while loading the config: {e}")
return {}
def get_balance(web3, address):
try:
balance = web3.eth.get_balance(address)
return Decimal(Web3.from_wei(balance, 'ether'))
except Exception as e:
print(f"Error getting balance for address {address}: {e}")
return Decimal(0)
def get_usdc_balance(web3, address):
try:
usdc_contract = web3.eth.contract(address=USDC_ADDRESS, abi=USDC_ABI)
balance = usdc_contract.functions.balanceOf(address).call()
return Decimal(balance) / Decimal(1e6) # USDC has 6 decimal places
except Exception as e:
print(f"Error getting USDC balance for address {address}: {e}")
return Decimal(0)
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Decimal):
return float(o)
return super(DecimalEncoder, self).default(o)
def save_wallet_info():
config = load_config()
if not config:
print("Error: Configuration could not be loaded.")
return
main_wallet_address = config.get('keys', [{}])[0].get('address')
if not main_wallet_address:
print("Error: Main wallet address not found in configuration.")
return
main_balances = {}
safe_balances = {}
for chain_id, chain_config in config.get('chain_configs', {}).items():
rpc_url = chain_config.get('ledger_config', {}).get('rpc')
if not rpc_url:
print(f"Error: RPC URL not found for chain ID {chain_id}.")
continue
try:
web3 = Web3(Web3.HTTPProvider(rpc_url))
if chain_id != "1": # Ethereum Mainnet
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
chain_name = get_chain_name(chain_id, CHAIN_ID_TO_METADATA)
# Get main wallet balance
main_balance = get_balance(web3, main_wallet_address)
main_balances[chain_name] = {
"token": "ETH",
"balance": main_balance,
"balance_formatted": f"{main_balance:.6f} ETH"
}
# Get Safe balance
safe_address = chain_config.get('chain_data', {}).get('multisig')
if not safe_address:
print(f"Error: Safe address not found for chain ID {chain_id}.")
continue
safe_balance = get_balance(web3, safe_address)
safe_balances[chain_name] = {
"address": safe_address,
"token": "ETH",
"balance": safe_balance,
"balance_formatted": f"{safe_balance:.6f} ETH"
}
# Get USDC balance for Ethereum Mainnet (chain ID 1)
if chain_id == "1":
usdc_balance = get_usdc_balance(web3, safe_address)
safe_balances[chain_name]["usdc_balance"] = usdc_balance
safe_balances[chain_name]["usdc_balance_formatted"] = f"{usdc_balance:.2f} USDC"
except Exception as e:
print(f"An error occurred while processing chain ID {chain_id}: {e}")
continue
wallet_info = {
"main_wallet_address": main_wallet_address,
"main_wallet_balances": main_balances,
"safe_addresses": {
chain_id: chain_config.get('chain_data', {}).get('multisig', 'N/A')
for chain_id, chain_config in config.get('chain_configs', {}).items()
},
"safe_balances": safe_balances,
"chain_configs": {
chain_id: {
"rpc": chain_config.get('ledger_config', {}).get('rpc'),
"multisig": chain_config.get('chain_data', {}).get('multisig'),
"token": chain_config.get('chain_data', {}).get('token'),
"fund_requirements": chain_config.get('chain_data', {}).get('user_params', {}).get('fund_requirements')
}
for chain_id, chain_config in config.get('chain_configs', {}).items()
}
}
file_path = OPERATE_HOME / "wallets" / "wallet_info.json"
try:
with open(file_path, "w") as f:
json.dump(wallet_info, f, indent=2, cls=DecimalEncoder)
print(f"Wallet info saved to {file_path}")
except Exception as e:
print(f"Error saving wallet info to {file_path}: {e}")
if __name__ == "__main__":
try:
save_wallet_info()
except Exception as e:
print(f"An unexpected error occurred: {e}")