Spaces:
Runtime error
Runtime error
File size: 11,113 Bytes
e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 0519476 e2243e5 99383ec 460a250 99383ec 460a250 99383ec e2243e5 |
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import requests
import pandas as pd
import gradio as gr
import plotly.express as px
from datetime import datetime, timedelta
import json
from web3 import Web3
import time
import os
# RPC URLs
OPTIMISM_RPC_URL = 'https://opt-mainnet.g.alchemy.com/v2/U5gnXPYxeyH43MJ9tP8ONBQHEDRav7H0'
BASE_RPC_URL = 'https://base-mainnet.g.alchemy.com/v2/U5gnXPYxeyH43MJ9tP8ONBQHEDRav7H0'
ETH_RPC_URL = 'https://eth-mainnet.g.alchemy.com/v2/U5gnXPYxeyH43MJ9tP8ONBQHEDRav7H0'
# Initialize Web3 instances
print("Initializing Web3 instances...")
web3_optimism = Web3(Web3.HTTPProvider(OPTIMISM_RPC_URL))
web3_base = Web3(Web3.HTTPProvider(BASE_RPC_URL))
web3_eth = Web3(Web3.HTTPProvider(ETH_RPC_URL))
# Contract addresses for service registries
contract_address_optimism = '0x3d77596beb0f130a4415df3D2D8232B3d3D31e44'
contract_address_base = '0x3C1fF68f5aa342D296d4DEe4Bb1cACCA912D95fE'
contract_address_eth = '0x48b6af7B12C71f09e2fC8aF4855De4Ff54e775cA'
# Load the ABI from a local JSON file
with open('./contracts/service_registry_abi.json', 'r') as abi_file:
contract_abi = json.load(abi_file)
# Create the contract instances
service_registry_optimism = web3_optimism.eth.contract(address=contract_address_optimism, abi=contract_abi)
service_registry_base = web3_base.eth.contract(address=contract_address_base, abi=contract_abi)
service_registry_eth = web3_eth.eth.contract(address=contract_address_eth, abi=contract_abi)
print("Service registry contracts loaded.")
# Check if connection is successful
if not web3_optimism.is_connected():
raise Exception("Failed to connect to the Optimism network.")
if not web3_base.is_connected():
raise Exception("Failed to connect to the Base network.")
if not web3_eth.is_connected():
raise Exception("Failed to connect to the ETH network.")
print("Successfully connected to Ethereum, Optimism, and Base networks.")
def fetch_service_safes(web3, registry_contract):
print("\nFetching service safes...")
total_services = registry_contract.functions.totalSupply().call()
print(f"Total services: {total_services}")
service_safes = set()
for service_id in range(1, total_services + 1):
print(f"Processing service ID: {service_id}")
service = registry_contract.functions.getService(service_id).call()
agent_ids = service[-1] # Assuming the last element is the list of agent IDs
print(f"Agent IDs: {agent_ids}")
if 25 in agent_ids:
agent_address = registry_contract.functions.getAgentInstances(service_id).call()
service_safe = service[1]
print(f"Found agent_address: {agent_address}")
print(f"Found service safe: {service_safe}")
service_safes.add(service_safe)
print(f"Total service safes found: {len(service_safes)}")
return service_safes
# Fetch service safes for each network
service_safes_optimism = fetch_service_safes(web3_optimism, service_registry_optimism)
service_safes_base = fetch_service_safes(web3_base, service_registry_base)
service_safes_eth = fetch_service_safes(web3_eth, service_registry_eth)
service_safes_eth = {safe for safe in service_safes_eth if safe.lower() != '0x0000000000000000000000000000000000000000'}
def get_block_range_for_date(chain_id, date_str, api_key, base_url):
"""Get the block range for a specific date."""
target_date = datetime.strptime(date_str, "%Y-%m-%d").date()
start_of_day = datetime.combine(target_date, datetime.min.time())
if target_date == datetime.now().date():
end_of_day = datetime.now() # Use the current time if the target date is today
else:
end_of_day = datetime.combine(target_date, datetime.max.time())
start_timestamp = int(start_of_day.timestamp())
end_timestamp = int(end_of_day.timestamp())
# Get start block
start_response = requests.get(
f"{base_url}?module=block&action=getblocknobytime×tamp={start_timestamp}&closest=before&apikey={api_key}"
)
if start_response.status_code == 200:
start_data = start_response.json()
start_block = start_data.get('result')
else:
print(f"Error fetching start block for {date_str} on chain {chain_id}")
return None, None
if start_block is None:
print(f"No start block found for chain {chain_id} on {date_str}")
return None, None
print(f"Start block for chain {chain_id} on {date_str}: {start_block}")
# Get end block
time.sleep(1)
end_response = requests.get(
f"{base_url}?module=block&action=getblocknobytime×tamp={end_timestamp}&closest=before&apikey={api_key}"
)
if end_response.status_code == 200:
end_data = end_response.json()
end_block = end_data.get('result')
else:
print(f"Error fetching end block for {date_str} on chain {chain_id}")
return None, None
if end_block is None:
print(f"No end block found for chain {chain_id} on {date_str}")
return None, None
print(f"End block for chain {chain_id} on {date_str}: {end_block}")
return start_block, end_block
def get_transactions(api_keys, wallet_address, chain_name, start_block, end_block):
"""Retrieve transactions for the given wallet address, chain, and block range using the Etherscan or similar API."""
base_url = {
'optimism': "https://api-optimistic.etherscan.io/api",
'base': "https://api.basescan.org/api",
'ethereum': "https://api.etherscan.io/api"
}.get(chain_name)
if not base_url:
print(f"Invalid chain name: {chain_name}")
return []
params = {
'module': 'account',
'action': 'txlist',
'address': wallet_address,
'startblock': start_block,
'endblock': end_block,
'sort': 'asc',
'apikey': api_keys.get(chain_name)
}
response = requests.get(base_url, params=params)
data = response.json()
time.sleep(1)
if data['status'] != '1':
print(f"Error: {data['message']}")
return []
valid_transactions = [tx for tx in data['result'] if tx['isError'] == '0']
return valid_transactions
def date_range(start_date, end_date):
"""Generates a range of dates from start_date to end_date inclusive."""
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
delta = timedelta(days=1)
current_dt = start_dt
while current_dt <= end_dt:
yield current_dt.strftime("%Y-%m-%d")
current_dt += delta
def fetch_transactions():
api_keys = {
'optimism': 'XQ72JA5XZ51QC7TG1W295AAIF4KTV92K1K',
'base': '4BFQMVW1QUKEPVDA4VW711CF4462682CY8',
'ethereum': '3GRYJGX55W3QWCEKGREF4H53AFHCAIVVR7'
}
base_urls = {
10: "https://api-optimistic.etherscan.io/api",
8453: "https://api.basescan.org/api",
1: "https://api.etherscan.io/api"
}
current_date = datetime.today().strftime("%Y-%m-%d")
csv_filename = 'daily_transactions_new.csv'
if os.path.exists(csv_filename):
df_existing = pd.read_csv(csv_filename)
if 'date' in df_existing:
last_date_in_csv = df_existing['date'].max()
else:
df_existing['date'] = pd.to_datetime(df_existing['timestamp']).dt.date
last_date_in_csv = df_existing['date'].max()
else:
df_existing = pd.DataFrame()
last_date_in_csv = '2024-09-19'
start_date = (datetime.strptime(last_date_in_csv, "%Y-%m-%d") + timedelta(days=1)).strftime("%Y-%m-%d")
chains = {
10: ('optimism', service_safes_optimism),
8453: ('base', service_safes_base),
1: ('ethereum', service_safes_eth)
}
all_transactions = df_existing.to_dict('records') if not df_existing.empty else []
for chain_id, (chain_name, service_safes) in chains.items():
base_url = base_urls[chain_id]
api_key = api_keys[chain_name]
for safe_address in service_safes:
print(f"\nProcessing {chain_name.capitalize()} for safe address {safe_address}...")
for single_date in date_range(start_date, current_date):
start_block, end_block = get_block_range_for_date(chain_id, single_date, api_key, base_url)
if start_block is None or end_block is None:
print(f"Skipping date {single_date} for chain {chain_name} due to missing block data.")
continue
print(f"Start Block: {start_block}, End Block: {end_block} for date {single_date}")
transactions = get_transactions(api_keys, safe_address, chain_name, start_block, end_block)
if transactions:
print(f"Found {len(transactions)} transactions on {single_date} for {chain_name.capitalize()} safe address {safe_address}:")
for tx in transactions:
tx_time = datetime.fromtimestamp(int(tx['timeStamp']))
all_transactions.append({
'chain': chain_name,
'safe_address': safe_address,
'date': single_date,
'transaction_hash': tx['hash'],
'timestamp': tx_time,
'from': tx['from'],
'to': tx['to'],
'value_eth': int(tx['value']) / 1e18 # Convert value to ETH
})
else:
print(f"No transactions found for safe address {safe_address} on {single_date} on {chain_name.capitalize()}.")
df_transactions_new = pd.DataFrame(all_transactions)
df_transactions_new.to_csv(csv_filename, index=False)
return df_transactions_new
def create_transcation_visualizations():
df_transactions_new = fetch_transactions()
df_transactions_new['timestamp'] = pd.to_datetime(df_transactions_new['timestamp'])
# Group by date and chain, count transactions
daily_counts = df_transactions_new.groupby([df_transactions_new['timestamp'].dt.date, 'chain']).size().unstack(fill_value=0)
# Set up the plot
fig_tx_chain = px.bar(
daily_counts,
barmode="stack",
title="Chain Daily Activity: Transactions",
color_discrete_map={
"optimism": "blue",
"ethereum": "darkgreen",
"base": "purple"
}
)
fig_tx_chain.update_layout(
xaxis_title="Date",
yaxis_title="Daily Transaction Nr",
legend_title="Transaction Chain",
xaxis_tickformat="%Y-%m-%d",
height=700,
)
return fig_tx_chain
# Gradio interface
def dashboard():
with gr.Blocks() as demo:
gr.Markdown("# Valory Transactions Dashboard")
# Fetch and display visualizations
with gr.Tab("Transactions"):
fig_tx_chain = create_transcation_visualizations()
gr.Plot(fig_tx_chain)
# Add more tabs as needed...
return demo
# Launch the dashboard
if __name__ == "__main__":
dashboard().launch() |