Skip to content

Commit

Permalink
Adding back leaderboard in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui committed Aug 22, 2023
1 parent d6d1d58 commit bd7b650
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
25 changes: 10 additions & 15 deletions lib/chainsync/bin/run_hyperdrive_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@

import mplfinance as mpf
import streamlit as st
from chainsync.dashboard import (
build_leaderboard,
build_ticker,
calc_ohlcv,
get_user_lookup,
plot_fixed_rate,
plot_ohlcv,
)
from chainsync.dashboard import build_leaderboard, build_ticker, get_user_lookup, plot_fixed_rate, plot_ohlcv
from chainsync.db.base import get_user_map, initialize_session
from chainsync.db.hyperdrive import get_all_traders, get_pool_analysis, get_pool_config, get_ticker
from chainsync.db.hyperdrive import get_all_traders, get_pool_analysis, get_pool_config, get_ticker, get_wallet_pnl
from ethpy import build_eth_config

# pylint: disable=invalid-name
Expand Down Expand Up @@ -56,17 +49,19 @@
# Adds user lookup to the ticker
display_ticker = build_ticker(ticker, user_lookup)

# TODO calculate ohlcv and volume
ohlcv = calc_ohlcv(combined_data, config_data, freq="5T")

# TODO get wallet pnl and calculate leaderboard
comb_rank, ind_rank = build_leaderboard(current_returns, user_lookup)
wallet_pnl = get_wallet_pnl(session, start_block=-max_live_blocks, coerce_float=False)
# Get the latest updated block
latest_wallet_pnl = wallet_pnl[wallet_pnl["blockNumber"] == wallet_pnl["blockNumber"].max()]

comb_rank, ind_rank = build_leaderboard(latest_wallet_pnl, user_lookup)

# TODO calculate ohlcv and volume
# ohlcv = calc_ohlcv(combined_data, config_data, freq="5T")

with ticker_placeholder.container():
st.header("Ticker")
st.dataframe(ticker, height=200, use_container_width=True)
st.header("PNL")
st.dataframe(current_wallet, height=500, use_container_width=True)
st.header("Total Leaderboard")
st.dataframe(comb_rank, height=500, use_container_width=True)
st.header("Wallet Leaderboard")
Expand Down
22 changes: 10 additions & 12 deletions lib/chainsync/chainsync/dashboard/build_leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@
from .usernames import address_to_username, combine_usernames


def build_leaderboard(pnl: pd.Series, lookup: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
def build_leaderboard(wallet_pnl: pd.Series, lookup: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
"""Rank users by PNL, individually and bomined across their accounts."""
pnl = pnl.reset_index() # type: ignore
usernames = address_to_username(lookup, pnl["walletAddress"])
pnl.insert(1, "username", usernames.values.tolist())
# TODO: Hard coded funding provider from migration account
migration_addr = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
# Don't show this account
pnl = pnl[pnl["walletAddress"] != migration_addr]
total_pnl = wallet_pnl.groupby("walletAddress")["pnl"].sum().reset_index()

usernames = address_to_username(lookup, total_pnl["walletAddress"])
total_pnl.insert(1, "username", usernames.values.tolist())

# Rank based on pnl
user = combine_usernames(pnl["username"])
pnl["user"] = user["user"].values
user = combine_usernames(total_pnl["username"])
total_pnl["user"] = user["user"].values

ind_leaderboard = (
pnl[["username", "walletAddress", "pnl"]]
total_pnl[["username", "walletAddress", "pnl"]]
.sort_values("pnl", ascending=False) # type: ignore
.reset_index(drop=True)
)
comb_leaderboard = (
pnl[["user", "pnl"]].groupby("user")["pnl"].sum().reset_index().sort_values("pnl", ascending=False)
total_pnl[["user", "pnl"]].groupby("user")["pnl"].sum().reset_index().sort_values("pnl", ascending=False)
).reset_index(drop=True)

return (comb_leaderboard, ind_leaderboard)
1 change: 1 addition & 0 deletions lib/chainsync/chainsync/dashboard/build_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def build_ticker(ticker_data: pd.DataFrame, lookup: pd.DataFrame) -> pd.DataFram
usernames = address_to_username(lookup, ticker_data["walletAddress"])

ticker_data = ticker_data.copy()
ticker_data = ticker_data.drop("id", axis=1)
ticker_data.insert(2, "username", usernames.values) # type: ignore
ticker_data.columns = ["blockNumber", "Timestamp", "User", "Wallet", "Method", "Token Deltas"]
# Shorten wallet address string
Expand Down
1 change: 1 addition & 0 deletions lib/chainsync/chainsync/db/hyperdrive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
get_transactions,
get_wallet_deltas,
get_wallet_info_history,
get_wallet_pnl,
)
from .schema import (
CheckpointInfo,
Expand Down

0 comments on commit bd7b650

Please sign in to comment.