Skip to content

Commit

Permalink
Improve performance and add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAurelius committed Apr 4, 2024
1 parent 0008f84 commit 9cd7c1c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
24 changes: 21 additions & 3 deletions src/apps/treasury/index.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import dash
from dash_extensions.enrich import html
from flask_caching import Cache

from src.apps.treasury.app import app
from src.apps.treasury.util.constants import CACHE_TIMEOUT

CONTENT_STYLE = {
"position": "relative",
Expand All @@ -19,9 +22,24 @@
"background-color": "#FFF",
}

app.layout = html.Div([
dash.page_container
])
cache = Cache(
app.server,
config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': '/tmp/cache-directory',
'CACHE_DEFAULT_TIMEOUT': CACHE_TIMEOUT
}
)


@cache.memoize()
def get_layout():
return html.Div([
dash.page_container
])


app.layout = get_layout

# For Gunicorn to reference
server = app.server
Expand Down
41 changes: 28 additions & 13 deletions src/apps/treasury/pages/hud.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,33 @@

dash.register_page(__name__, title="KlimaDAO Treasury Heads Up Display")

# TODO: add caching

# TODO: combine repetitive queries to `last_metric` into a single DF call and unpack instead
# last_metric_df = sg.query_df([last_metric.marketCap, last_metric.treasuryMarketValue, ...])
def query_last_metric():
df = sg.query_df([
last_metric.marketCap,
last_metric.treasuryMarketValue,
last_metric.daoBalanceUSDC, last_metric.treasuryBalanceUSDC,
last_metric.treasuryMarketValue, last_metric.daoBalanceKLIMA,
last_metric.klimaPrice, last_metric.totalSupply
])

df = df.rename({
c: c.replace('protocolMetrics_', '') for c in df.columns
}, axis=1)

return df


last_metric_df = query_last_metric()

mcap = last_metric_df.loc[0, 'marketCap']
mval = last_metric_df.loc[0, 'treasuryMarketValue']

# Market Cap indicator
metric_fig = go.Figure(
go.Indicator(
mode="number",
value=sg.query([last_metric.marketCap]),
value=mcap,
number={"prefix": "$", "valueformat": ".2s", "font": {"family": "Poppins-ExtraBold"}},
title={
"text":
Expand All @@ -43,7 +60,7 @@
# Total Treasury Market Value ($) indicator
go.Indicator(
mode="number",
value=sg.query([last_metric.treasuryMarketValue]),
value=mval,
number={"prefix": "$", "valueformat": ".2s", "font": {"family": "Poppins-ExtraBold"}},
title={
"text":
Expand Down Expand Up @@ -81,28 +98,26 @@
total_illiquid_spot = illiquid_assets[illiquid_assets.Is_Spot]["Dollars"].sum()

# Pull sum of DAO and Treasury USDC holdings for OpEx
dao_usdc, treasury_usdc = sg.query([last_metric.daoBalanceUSDC, last_metric.treasuryBalanceUSDC])
dao_usdc = last_metric_df.loc[0, 'daoBalanceUSDC']
treasury_usdc = last_metric_df.loc[0, 'treasuryBalanceUSDC']
total_usdc = dao_usdc + treasury_usdc

# Total treasury market value
# TODO: check that treasuryMarketValue field includes all assets (raw carbon, all LPs, raw KLIMA)
# TODO: add Solid World CRISP-C LP value
# TODO: add offchain assets (added illiquid spot tonnes, may be other assets)
(treasury_value, dao_klima, klima_price) = sg.query([
last_metric.treasuryMarketValue, last_metric.daoBalanceKLIMA,
last_metric.klimaPrice
])
treasury_value = last_metric_df.loc[0, 'treasuryMarketValue']
dao_klima = last_metric_df.loc[0, 'daoBalanceKLIMA']
klima_price = last_metric_df.loc[0, 'klimaPrice']

# Add DAO KLIMA holdings since it is not considered part of OpEx bucket
total_treasury_value = (
treasury_value + (dao_klima * klima_price) + total_illiquid_spot
)

# TODO: make sure treasury KLIMA is included in treasuryMarketValue

# Separate out enough lowest price carbon tonnes from total treasury value
# to back all KLIMA supply as Carbon Backing
klima_supply = sg.query([last_metric.totalSupply])
klima_supply = last_metric_df.loc[0, 'totalSupply']

# TODO: Replace hard-coded BCT with dynamic check for lowest priced tonne
lowest_price_carbon_addr = BCT_ERC20_CONTRACT
Expand Down
2 changes: 2 additions & 0 deletions src/apps/treasury/util/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

# Colors
KLIMA_GREEN = '#00cc33'

CACHE_TIMEOUT = 60 * 60 # 1 hour

0 comments on commit 9cd7c1c

Please sign in to comment.