Spaces:
Runtime error
Runtime error
gauravlochab
commited on
Commit
·
011e380
1
Parent(s):
ece75b4
adding the graph for number of agents
Browse files
app.py
CHANGED
@@ -42,24 +42,45 @@ def get_vanity_transactions(date):
|
|
42 |
def fetch_and_aggregate_transactions():
|
43 |
total_services = service_registry.functions.totalSupply().call()
|
44 |
aggregated_transactions = []
|
|
|
45 |
|
46 |
for service_id in range(1, total_services + 1):
|
47 |
service = service_registry.functions.getService(service_id).call()
|
48 |
|
49 |
# Extract the list of agent IDs from the service data
|
50 |
agent_ids = service[-1] # Assuming the last element is the list of agent IDs
|
51 |
-
|
52 |
# Check if 25 is in the list of agent IDs
|
53 |
if 25 in agent_ids:
|
54 |
agent_address = service_registry.functions.getAgentInstances(service_id).call()[1][0]
|
55 |
response_transfers = get_transfers("valory", agent_address)
|
56 |
aggregated_transactions.extend(response_transfers["transfers"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
return aggregated_transactions
|
59 |
|
60 |
# Function to parse the transaction data and prepare it for visualization
|
61 |
-
def
|
62 |
-
transactions = data
|
63 |
|
64 |
# Convert the data into a pandas DataFrame for easy manipulation
|
65 |
rows = []
|
@@ -93,13 +114,14 @@ def process_transactions(data):
|
|
93 |
"week": sending_timestamp.strftime('%Y-%W') # Group by week
|
94 |
})
|
95 |
|
96 |
-
|
97 |
-
|
|
|
98 |
|
99 |
# Function to create visualizations based on the metrics
|
100 |
def create_visualizations():
|
101 |
transactions_data = fetch_and_aggregate_transactions()
|
102 |
-
|
103 |
|
104 |
# Map chain IDs to chain names
|
105 |
chain_name_map = {
|
@@ -107,25 +129,24 @@ def create_visualizations():
|
|
107 |
8453: "Base",
|
108 |
1: "Ethereum"
|
109 |
}
|
110 |
-
|
111 |
-
|
112 |
|
113 |
# Ensure that chain IDs are strings for consistent grouping
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
|
118 |
# Total transactions per chain per day
|
119 |
-
tx_per_chain =
|
120 |
|
121 |
# Add vanity transactions for each day
|
122 |
-
date_range = pd.date_range(start=
|
123 |
vanity_transactions = pd.concat([get_vanity_transactions(date) for date in date_range])
|
124 |
|
125 |
# Combine actual and vanity transactions
|
126 |
tx_per_chain = pd.concat([tx_per_chain, vanity_transactions]).groupby(["date", "sending_chain"]).sum().reset_index()
|
127 |
-
|
128 |
-
#tx_per_chain = df.groupby(["date", "sending_chain"]).size().reset_index(name="transaction_count")
|
129 |
fig_tx_chain = px.bar(
|
130 |
tx_per_chain,
|
131 |
x="date",
|
@@ -149,11 +170,12 @@ def create_visualizations():
|
|
149 |
height=700,
|
150 |
)
|
151 |
fig_tx_chain.update_xaxes(tickformat="%Y-%m-%d")
|
|
|
152 |
# Identify swap transactions
|
153 |
-
|
154 |
|
155 |
# Total swaps per chain per day
|
156 |
-
swaps_per_chain =
|
157 |
fig_swaps_chain = px.bar(
|
158 |
swaps_per_chain,
|
159 |
x="date",
|
@@ -179,10 +201,10 @@ def create_visualizations():
|
|
179 |
fig_swaps_chain.update_xaxes(tickformat="%Y-%m-%d")
|
180 |
|
181 |
# Identify bridge transactions
|
182 |
-
|
183 |
|
184 |
# Total bridges per chain per day
|
185 |
-
bridges_per_chain =
|
186 |
fig_bridges_chain = px.bar(
|
187 |
bridges_per_chain,
|
188 |
x="date",
|
@@ -206,8 +228,9 @@ def create_visualizations():
|
|
206 |
height=700,
|
207 |
)
|
208 |
fig_bridges_chain.update_xaxes(tickformat="%Y-%m-%d")
|
|
|
209 |
# Investment per agent per day
|
210 |
-
investment_per_agent =
|
211 |
fig_investment_agent = px.bar(
|
212 |
investment_per_agent,
|
213 |
x="date",
|
@@ -236,7 +259,25 @@ def create_visualizations():
|
|
236 |
height=700,
|
237 |
)
|
238 |
fig_investment_agent.update_xaxes(tickformat="%Y-%m-%d")
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
# Gradio interface
|
242 |
def dashboard():
|
@@ -245,20 +286,24 @@ def dashboard():
|
|
245 |
|
246 |
# Fetch and display visualizations
|
247 |
with gr.Tab("Transactions"):
|
248 |
-
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent = create_visualizations()
|
249 |
gr.Plot(fig_tx_chain)
|
250 |
|
251 |
with gr.Tab("Swaps"):
|
252 |
-
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent = create_visualizations()
|
253 |
gr.Plot(fig_swaps_chain)
|
254 |
|
255 |
with gr.Tab("Bridges"):
|
256 |
-
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent = create_visualizations()
|
257 |
gr.Plot(fig_bridges_chain)
|
258 |
|
259 |
with gr.Tab("Investment"):
|
260 |
-
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent = create_visualizations()
|
261 |
gr.Plot(fig_investment_agent)
|
|
|
|
|
|
|
|
|
262 |
|
263 |
return demo
|
264 |
|
|
|
42 |
def fetch_and_aggregate_transactions():
|
43 |
total_services = service_registry.functions.totalSupply().call()
|
44 |
aggregated_transactions = []
|
45 |
+
weekly_agent_counts = {}
|
46 |
|
47 |
for service_id in range(1, total_services + 1):
|
48 |
service = service_registry.functions.getService(service_id).call()
|
49 |
|
50 |
# Extract the list of agent IDs from the service data
|
51 |
agent_ids = service[-1] # Assuming the last element is the list of agent IDs
|
52 |
+
|
53 |
# Check if 25 is in the list of agent IDs
|
54 |
if 25 in agent_ids:
|
55 |
agent_address = service_registry.functions.getAgentInstances(service_id).call()[1][0]
|
56 |
response_transfers = get_transfers("valory", agent_address)
|
57 |
aggregated_transactions.extend(response_transfers["transfers"])
|
58 |
+
|
59 |
+
# Track the weekly number of agents
|
60 |
+
creation_event = service_registry.events.CreateService.create_filter(
|
61 |
+
from_block=0, argument_filters={'serviceId': service_id, 'configHash': service[2]}
|
62 |
+
).get_all_entries()
|
63 |
+
|
64 |
+
if creation_event:
|
65 |
+
block_number = creation_event[0]['blockNumber']
|
66 |
+
block = web3.eth.get_block(block_number)
|
67 |
+
creation_timestamp = datetime.fromtimestamp(block['timestamp'])
|
68 |
+
week_str = creation_timestamp.strftime('%Y-%W')
|
69 |
+
|
70 |
+
if week_str not in weekly_agent_counts:
|
71 |
+
weekly_agent_counts[week_str] = set()
|
72 |
+
|
73 |
+
for agent_id in agent_ids:
|
74 |
+
weekly_agent_counts[week_str].add(agent_id)
|
75 |
+
|
76 |
+
# Convert set to count
|
77 |
+
weekly_agent_counts = {week: len(agents) for week, agents in weekly_agent_counts.items()}
|
78 |
|
79 |
+
return aggregated_transactions, weekly_agent_counts
|
80 |
|
81 |
# Function to parse the transaction data and prepare it for visualization
|
82 |
+
def process_transactions_and_agents(data):
|
83 |
+
transactions, weekly_agent_counts = data
|
84 |
|
85 |
# Convert the data into a pandas DataFrame for easy manipulation
|
86 |
rows = []
|
|
|
114 |
"week": sending_timestamp.strftime('%Y-%W') # Group by week
|
115 |
})
|
116 |
|
117 |
+
df_transactions = pd.DataFrame(rows)
|
118 |
+
df_agents = pd.DataFrame(list(weekly_agent_counts.items()), columns=['week', 'agent_count'])
|
119 |
+
return df_transactions, df_agents
|
120 |
|
121 |
# Function to create visualizations based on the metrics
|
122 |
def create_visualizations():
|
123 |
transactions_data = fetch_and_aggregate_transactions()
|
124 |
+
df_transactions, df_agents = process_transactions_and_agents(transactions_data)
|
125 |
|
126 |
# Map chain IDs to chain names
|
127 |
chain_name_map = {
|
|
|
129 |
8453: "Base",
|
130 |
1: "Ethereum"
|
131 |
}
|
132 |
+
df_transactions["sending_chain"] = df_transactions["sending_chain"].map(chain_name_map)
|
133 |
+
df_transactions["receiving_chain"] = df_transactions["receiving_chain"].map(chain_name_map)
|
134 |
|
135 |
# Ensure that chain IDs are strings for consistent grouping
|
136 |
+
df_transactions["sending_chain"] = df_transactions["sending_chain"].astype(str)
|
137 |
+
df_transactions["receiving_chain"] = df_transactions["receiving_chain"].astype(str)
|
138 |
+
df_transactions['date'] = pd.to_datetime(df_transactions['date'])
|
139 |
|
140 |
# Total transactions per chain per day
|
141 |
+
tx_per_chain = df_transactions.groupby(["date", "sending_chain"]).size().reset_index(name="transaction_count")
|
142 |
|
143 |
# Add vanity transactions for each day
|
144 |
+
date_range = pd.date_range(start=df_transactions['date'].min(), end=df_transactions['date'].max())
|
145 |
vanity_transactions = pd.concat([get_vanity_transactions(date) for date in date_range])
|
146 |
|
147 |
# Combine actual and vanity transactions
|
148 |
tx_per_chain = pd.concat([tx_per_chain, vanity_transactions]).groupby(["date", "sending_chain"]).sum().reset_index()
|
149 |
+
|
|
|
150 |
fig_tx_chain = px.bar(
|
151 |
tx_per_chain,
|
152 |
x="date",
|
|
|
170 |
height=700,
|
171 |
)
|
172 |
fig_tx_chain.update_xaxes(tickformat="%Y-%m-%d")
|
173 |
+
|
174 |
# Identify swap transactions
|
175 |
+
df_transactions["is_swap"] = df_transactions.apply(lambda x: x["sending_token_symbol"] != x["receiving_token_symbol"], axis=1)
|
176 |
|
177 |
# Total swaps per chain per day
|
178 |
+
swaps_per_chain = df_transactions[df_transactions["is_swap"]].groupby(["date", "sending_chain"]).size().reset_index(name="swap_count")
|
179 |
fig_swaps_chain = px.bar(
|
180 |
swaps_per_chain,
|
181 |
x="date",
|
|
|
201 |
fig_swaps_chain.update_xaxes(tickformat="%Y-%m-%d")
|
202 |
|
203 |
# Identify bridge transactions
|
204 |
+
df_transactions["is_bridge"] = df_transactions.apply(lambda x: x["sending_chain"] != x["receiving_chain"], axis=1)
|
205 |
|
206 |
# Total bridges per chain per day
|
207 |
+
bridges_per_chain = df_transactions[df_transactions["is_bridge"]].groupby(["date", "sending_chain"]).size().reset_index(name="bridge_count")
|
208 |
fig_bridges_chain = px.bar(
|
209 |
bridges_per_chain,
|
210 |
x="date",
|
|
|
228 |
height=700,
|
229 |
)
|
230 |
fig_bridges_chain.update_xaxes(tickformat="%Y-%m-%d")
|
231 |
+
|
232 |
# Investment per agent per day
|
233 |
+
investment_per_agent = df_transactions.groupby(["date", "from_address", "sending_chain"])["sending_amount_usd"].sum().reset_index()
|
234 |
fig_investment_agent = px.bar(
|
235 |
investment_per_agent,
|
236 |
x="date",
|
|
|
259 |
height=700,
|
260 |
)
|
261 |
fig_investment_agent.update_xaxes(tickformat="%Y-%m-%d")
|
262 |
+
|
263 |
+
# Number of agents per week
|
264 |
+
fig_agents_weekly = px.bar(
|
265 |
+
df_agents,
|
266 |
+
x="week",
|
267 |
+
y="agent_count",
|
268 |
+
title="Number of Agents Week Over Week",
|
269 |
+
labels={"week": "Week", "agent_count": "Number of Agents"},
|
270 |
+
color_discrete_sequence=["purple"]
|
271 |
+
)
|
272 |
+
fig_agents_weekly.update_layout(
|
273 |
+
xaxis_title=None,
|
274 |
+
yaxis=dict(tickmode='linear', tick0=0, dtick=1),
|
275 |
+
xaxis=dict(tickmode='array', tickvals=df_agents['week'], tickangle=45),
|
276 |
+
bargap=0.8,
|
277 |
+
height=700,
|
278 |
+
)
|
279 |
+
|
280 |
+
return fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent, fig_agents_weekly
|
281 |
|
282 |
# Gradio interface
|
283 |
def dashboard():
|
|
|
286 |
|
287 |
# Fetch and display visualizations
|
288 |
with gr.Tab("Transactions"):
|
289 |
+
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent, fig_agents_weekly = create_visualizations()
|
290 |
gr.Plot(fig_tx_chain)
|
291 |
|
292 |
with gr.Tab("Swaps"):
|
293 |
+
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent, fig_agents_weekly = create_visualizations()
|
294 |
gr.Plot(fig_swaps_chain)
|
295 |
|
296 |
with gr.Tab("Bridges"):
|
297 |
+
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent, fig_agents_weekly = create_visualizations()
|
298 |
gr.Plot(fig_bridges_chain)
|
299 |
|
300 |
with gr.Tab("Investment"):
|
301 |
+
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent, fig_agents_weekly = create_visualizations()
|
302 |
gr.Plot(fig_investment_agent)
|
303 |
+
|
304 |
+
with gr.Tab("Agents Week Over Week"):
|
305 |
+
fig_tx_chain, fig_swaps_chain, fig_bridges_chain, fig_investment_agent, fig_agents_weekly = create_visualizations()
|
306 |
+
gr.Plot(fig_agents_weekly)
|
307 |
|
308 |
return demo
|
309 |
|