Spaces:
Runtime error
Runtime error
gauravlochab
commited on
Commit
·
07ba1e6
1
Parent(s):
9150947
Add transaction metrics and visualizations
Browse files
app.py
CHANGED
|
@@ -18,52 +18,116 @@ def process_transactions(data):
|
|
| 18 |
# Convert the data into a pandas DataFrame for easy manipulation
|
| 19 |
rows = []
|
| 20 |
for tx in transactions:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
rows.append({
|
| 22 |
"transactionId": tx["transactionId"],
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
})
|
| 32 |
|
| 33 |
df = pd.DataFrame(rows)
|
| 34 |
return df
|
| 35 |
|
| 36 |
-
# Function to create
|
| 37 |
-
def
|
| 38 |
transactions_data = fetch_transactions()
|
| 39 |
df = process_transactions(transactions_data)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Gradio interface
|
| 56 |
def dashboard():
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
-
gr.Markdown("# Valory Transactions Dashboard
|
| 59 |
|
| 60 |
# Fetch and display visualizations
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
return demo
|
| 66 |
|
| 67 |
# Launch the dashboard
|
| 68 |
if __name__ == "__main__":
|
| 69 |
dashboard().launch()
|
|
|
|
|
|
| 18 |
# Convert the data into a pandas DataFrame for easy manipulation
|
| 19 |
rows = []
|
| 20 |
for tx in transactions:
|
| 21 |
+
# Normalize amounts
|
| 22 |
+
sending_amount = float(tx["sending"]["amount"]) / (10 ** tx["sending"]["token"]["decimals"])
|
| 23 |
+
receiving_amount = float(tx["receiving"]["amount"]) / (10 ** tx["receiving"]["token"]["decimals"])
|
| 24 |
+
|
| 25 |
+
# Convert timestamps to datetime objects
|
| 26 |
+
sending_timestamp = datetime.utcfromtimestamp(tx["sending"]["timestamp"])
|
| 27 |
+
receiving_timestamp = datetime.utcfromtimestamp(tx["receiving"]["timestamp"])
|
| 28 |
+
|
| 29 |
+
# Prepare row data
|
| 30 |
rows.append({
|
| 31 |
"transactionId": tx["transactionId"],
|
| 32 |
+
"from_address": tx["fromAddress"],
|
| 33 |
+
"to_address": tx["toAddress"],
|
| 34 |
+
"sending_chain": tx["sending"]["chainId"],
|
| 35 |
+
"receiving_chain": tx["receiving"]["chainId"],
|
| 36 |
+
"sending_token_symbol": tx["sending"]["token"]["symbol"],
|
| 37 |
+
"receiving_token_symbol": tx["receiving"]["token"]["symbol"],
|
| 38 |
+
"sending_amount": sending_amount,
|
| 39 |
+
"receiving_amount": receiving_amount,
|
| 40 |
+
"sending_amount_usd": float(tx["sending"]["amountUSD"]),
|
| 41 |
+
"receiving_amount_usd": float(tx["receiving"]["amountUSD"]),
|
| 42 |
+
"sending_gas_used": int(tx["sending"]["gasUsed"]),
|
| 43 |
+
"receiving_gas_used": int(tx["receiving"]["gasUsed"]),
|
| 44 |
+
"sending_timestamp": sending_timestamp,
|
| 45 |
+
"receiving_timestamp": receiving_timestamp,
|
| 46 |
+
"date": sending_timestamp.date(), # Group by day
|
| 47 |
+
"week": sending_timestamp.strftime('%Y-%W') # Group by week
|
| 48 |
})
|
| 49 |
|
| 50 |
df = pd.DataFrame(rows)
|
| 51 |
return df
|
| 52 |
|
| 53 |
+
# Function to create visualizations based on the metrics
|
| 54 |
+
def create_visualizations():
|
| 55 |
transactions_data = fetch_transactions()
|
| 56 |
df = process_transactions(transactions_data)
|
| 57 |
|
| 58 |
+
# Ensure that chain IDs are strings for consistent grouping
|
| 59 |
+
df["sending_chain"] = df["sending_chain"].astype(str)
|
| 60 |
+
df["receiving_chain"] = df["receiving_chain"].astype(str)
|
| 61 |
+
|
| 62 |
+
# 1. Number of Transactions per Chain per Day per Agent
|
| 63 |
+
tx_per_chain_agent = df.groupby(["date", "from_address", "sending_chain"]).size().reset_index(name="transaction_count")
|
| 64 |
+
fig_tx_chain_agent = px.bar(tx_per_chain_agent, x="date", y="transaction_count", color="sending_chain", barmode="group",
|
| 65 |
+
facet_col="from_address", title="Number of Transactions per Chain per Agent per Day")
|
| 66 |
+
|
| 67 |
+
# 2. Number of Opportunities Taken per Agent per Day
|
| 68 |
+
opportunities_per_agent = df.groupby(["date", "from_address"]).size().reset_index(name="opportunities_taken")
|
| 69 |
+
fig_opportunities_agent = px.bar(opportunities_per_agent, x="date", y="opportunities_taken", color="from_address",
|
| 70 |
+
title="Number of Opportunities Taken per Agent per Day")
|
| 71 |
|
| 72 |
+
# 3. Amount of Investment in Pools Daily per Agent (Note: Assuming sending_amount_usd as investment)
|
| 73 |
+
# Since we might not have explicit data about pool investments, we'll use sending_amount_usd
|
| 74 |
+
investment_per_agent = df.groupby(["date", "from_address"])["sending_amount_usd"].sum().reset_index()
|
| 75 |
+
fig_investment_agent = px.bar(investment_per_agent, x="date", y="sending_amount_usd", color="from_address",
|
| 76 |
+
title="Amount of Investment (USD) per Agent per Day")
|
| 77 |
|
| 78 |
+
# 4. Number of Swaps per Day
|
| 79 |
+
# Assuming each transaction is a swap if sending and receiving tokens are different
|
| 80 |
+
df["is_swap"] = df.apply(lambda x: x["sending_token_symbol"] != x["receiving_token_symbol"], axis=1)
|
| 81 |
+
swaps_per_day = df[df["is_swap"]].groupby("date").size().reset_index(name="swap_count")
|
| 82 |
+
fig_swaps_per_day = px.bar(swaps_per_day, x="date", y="swap_count", title="Number of Swaps per Day")
|
| 83 |
|
| 84 |
+
# 5. Aggregated Metrics over All Traders
|
| 85 |
+
amount_usd = df["sending_amount_usd"]
|
| 86 |
+
stats = {
|
| 87 |
+
"Total": amount_usd.sum(),
|
| 88 |
+
"Average": amount_usd.mean(),
|
| 89 |
+
"Min": amount_usd.min(),
|
| 90 |
+
"Max": amount_usd.max(),
|
| 91 |
+
"25th Percentile": amount_usd.quantile(0.25),
|
| 92 |
+
"50th Percentile (Median)": amount_usd.median(),
|
| 93 |
+
"75th Percentile": amount_usd.quantile(0.75),
|
| 94 |
+
}
|
| 95 |
+
stats_df = pd.DataFrame(list(stats.items()), columns=["Metric", "Value"])
|
| 96 |
+
|
| 97 |
+
# Visualization for Aggregated Metrics
|
| 98 |
+
fig_stats = px.bar(stats_df, x="Metric", y="Value", title="Aggregated Transaction Amount Metrics (USD)")
|
| 99 |
+
|
| 100 |
+
return fig_tx_chain_agent, fig_opportunities_agent, fig_investment_agent, fig_swaps_per_day, fig_stats
|
| 101 |
|
| 102 |
# Gradio interface
|
| 103 |
def dashboard():
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
+
gr.Markdown("# Valory Transactions Dashboard")
|
| 106 |
|
| 107 |
# Fetch and display visualizations
|
| 108 |
+
with gr.Tab("Transactions per Chain per Agent"):
|
| 109 |
+
fig_tx_chain_agent, _, _, _, _ = create_visualizations()
|
| 110 |
+
gr.Plot(fig_tx_chain_agent)
|
| 111 |
+
|
| 112 |
+
with gr.Tab("Opportunities per Agent"):
|
| 113 |
+
_, fig_opportunities_agent, _, _, _ = create_visualizations()
|
| 114 |
+
gr.Plot(fig_opportunities_agent)
|
| 115 |
+
|
| 116 |
+
with gr.Tab("Investment per Agent"):
|
| 117 |
+
_, _, fig_investment_agent, _, _ = create_visualizations()
|
| 118 |
+
gr.Plot(fig_investment_agent)
|
| 119 |
+
|
| 120 |
+
with gr.Tab("Swaps per Day"):
|
| 121 |
+
_, _, _, fig_swaps_per_day, _ = create_visualizations()
|
| 122 |
+
gr.Plot(fig_swaps_per_day)
|
| 123 |
+
|
| 124 |
+
with gr.Tab("Aggregated Metrics"):
|
| 125 |
+
_, _, _, _, fig_stats = create_visualizations()
|
| 126 |
+
gr.Plot(fig_stats)
|
| 127 |
|
| 128 |
return demo
|
| 129 |
|
| 130 |
# Launch the dashboard
|
| 131 |
if __name__ == "__main__":
|
| 132 |
dashboard().launch()
|
| 133 |
+
|