Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GA script #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
import update_data



logging.basicConfig(level=logging.INFO)



def main():
streamlit.title("DeRisk")

Expand Down Expand Up @@ -93,7 +91,8 @@ def main():
if histogram_data.empty:
histogram_data = protocol_histogram_data
else:
histogram_data = pandas.concat([histogram_data, protocol_histogram_data])
histogram_data = pandas.concat(
[histogram_data, protocol_histogram_data])
if loans_data.empty:
loans_data = protocol_loans_data
else:
Expand All @@ -108,9 +107,11 @@ def main():
)
streamlit.plotly_chart(figure_or_data=figure, use_container_width=True)

collateral_token_price = src.swap_amm.Prices().prices.values[collateral_token]
collateral_token_price = src.swap_amm.Prices(
).prices.values[collateral_token]
example_row = main_chart_data[
main_chart_data['collateral_token_price'] > decimal.Decimal("0.5") * collateral_token_price
main_chart_data['collateral_token_price'] > decimal.Decimal(
"0.5") * collateral_token_price
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you splitting the lines? they are not that long?

].sort_values('collateral_token_price').iloc[0]

def _get_risk_level(debt_to_supply_ratio: float) -> str:
Expand All @@ -122,11 +123,12 @@ def _get_risk_level(debt_to_supply_ratio: float) -> str:
'high'
return 'very high'

debt_to_supply_ratio = example_row['liquidable_debt_at_interval'] / example_row['debt_token_supply']
debt_to_supply_ratio = example_row['liquidable_debt_at_interval'] / \
example_row['debt_token_supply']
streamlit.subheader(
f":warning: At price of {int(example_row['collateral_token_price']):,}, the risk of acquiring bad debt for "
f"lending protocols is {_get_risk_level(debt_to_supply_ratio)}."
)
)
streamlit.write(
f"The ratio of liquidated debt to available supply is {round(debt_to_supply_ratio * 100)}%.Debt worth of "
f"{int(example_row['liquidable_debt_at_interval']):,} USD will be liquidated while the AMM swaps capacity "
Expand All @@ -151,11 +153,16 @@ def _get_risk_level(debt_to_supply_ratio: float) -> str:
)

streamlit.header("Comparison of lending protocols")
streamlit.dataframe(pandas.read_parquet(f"gs://{src.helpers.GS_BUCKET_NAME}/data/general_stats.parquet"))
streamlit.dataframe(pandas.read_parquet(f"gs://{src.helpers.GS_BUCKET_NAME}/data/utilization_stats.parquet"))
supply_stats = pandas.read_parquet(f"gs://{src.helpers.GS_BUCKET_NAME}/data/supply_stats.parquet")
collateral_stats = pandas.read_parquet(f"gs://{src.helpers.GS_BUCKET_NAME}/data/collateral_stats.parquet")
debt_stats = pandas.read_parquet(f"gs://{src.helpers.GS_BUCKET_NAME}/data/debt_stats.parquet")
streamlit.dataframe(pandas.read_parquet(
f"gs://{src.helpers.GS_BUCKET_NAME}/data/general_stats.parquet"))
streamlit.dataframe(pandas.read_parquet(
f"gs://{src.helpers.GS_BUCKET_NAME}/data/utilization_stats.parquet"))
supply_stats = pandas.read_parquet(
f"gs://{src.helpers.GS_BUCKET_NAME}/data/supply_stats.parquet")
collateral_stats = pandas.read_parquet(
f"gs://{src.helpers.GS_BUCKET_NAME}/data/collateral_stats.parquet")
debt_stats = pandas.read_parquet(
f"gs://{src.helpers.GS_BUCKET_NAME}/data/debt_stats.parquet")

columns = streamlit.columns(6)
for column, token in zip(columns, src.settings.TOKEN_SETTINGS.keys()):
Expand Down Expand Up @@ -188,11 +195,13 @@ def _get_risk_level(debt_to_supply_ratio: float) -> str:
streamlit.header("Loan size distribution")
src.histogram.visualization(data=histogram_data)

last_update = src.persistent_state.load_pickle(path=src.persistent_state.LAST_UPDATE_FILENAME)
last_update = src.persistent_state.load_pickle(
path=src.persistent_state.LAST_UPDATE_FILENAME)
last_timestamp = last_update["timestamp"]
last_block_number = last_update["block_number"]
date_str = datetime.datetime.utcfromtimestamp(int(last_timestamp))
streamlit.write(f"Last updated {date_str} UTC, last block: {last_block_number}.")
streamlit.write(
f"Last updated {date_str} UTC, last block: {last_block_number}.")


if __name__ == "__main__":
Expand All @@ -201,6 +210,10 @@ def _get_risk_level(debt_to_supply_ratio: float) -> str:
page_title="DeRisk by Carmine Finance",
page_icon="https://carmine.finance/assets/logo.svg",
)
# Append Google Analytics script
with open("src/analytics.html", "r", encoding="utf-8") as f:
analytics_html = f.read()
streamlit.markdown(analytics_html, unsafe_allow_html=True)

if os.environ.get("UPDATE_RUNNING") is None:
os.environ["UPDATE_RUNNING"] = "True"
Expand Down
9 changes: 9 additions & 0 deletions src/analytics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BFQKNJ61QM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());

gtag('config', 'G-BFQKNJ61QM');
</script>