Add a luna crash custom reports (#1816)

* bring dashboards and reports menus one step above

* remove checkpoints

* Add a custom_report for luna_crash

* add Minh updated text and report

* Make a few change to the text

Co-authored-by: Minh Hoang <nminh.hoang1023@gmail.com>
This commit is contained in:
didierlopes.eth
2022-05-16 18:53:59 +01:00
committed by GitHub
parent 54a1b6f545
commit bb6bdcc661
9 changed files with 659 additions and 7 deletions

2
.gitignore vendored
View File

@@ -21,6 +21,7 @@ openbb_terminal/config_terminal.py
openbb_terminal/feature_flags.py
openbb_terminal/reports/stored/*
openbb_terminal/dashboards/.ipynb_checkpoints
openbb_terminal/custom_reports/*/.ipynb_checkpoints
openbb_terminal/reports/.ipynb_checkpoints
openbb_terminal/reports/widgets/.ipynb_checkpoints
!openbb_terminal/reports/stored/.gitkeep
@@ -32,6 +33,7 @@ openbb_terminal/portfolio/portfolios/*
*.http
bots/interactive/*.html
.coverage.*
!openbb_terminal/custom_reports/*/.ipynb
# pyinstaller artifacts
*.pyo

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 593 KiB

View File

@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "70ba2c8a",
"metadata": {},
"source": [
"## Notebook setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "driven-billion",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import io\n",
"import warnings\n",
"import datetime\n",
"import numpy as np\n",
"import pandas as pd\n",
"import base64\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib_inline.backend_inline\n",
"\n",
"import sys\n",
"sys.path.append('../../../')\n",
"\n",
"from openbb_terminal import api as openbb\n",
"from openbb_terminal.helper_classes import TerminalStyle\n",
"\n",
"%matplotlib inline\n",
"matplotlib_inline.backend_inline.set_matplotlib_formats(\"svg\")\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3fe7db5-ec6a-42cf-9e66-52dc1de22370",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" theme = TerminalStyle(\"dark\", \"dark\", \"dark\")\n",
"except:\n",
" pass\n",
"stylesheet = openbb.widgets.html_report_stylesheet()\n",
"# with open(\"./OpenBB_reports_logo.png\", \"rb\") as image_file:\n",
"with open(\"../../reports/OpenBB_reports_logo.png\", \"rb\") as image_file:\n",
" openbb_image_encoded = base64.b64encode(image_file.read())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6aa8b34-aa60-45ce-8ba0-0182addffab8",
"metadata": {},
"outputs": [],
"source": [
"author = \"Minh Hoang\"\n",
"report_title = f\"LUNATICS REPORT\"\n",
"report_date = datetime.datetime.now().strftime('%d %B, %Y')\n",
"report_time = datetime.datetime.now().strftime('%H:%M') \n",
"report_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo\n",
"report_name = \"luna_crash\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abfe37e4-ea38-42c3-8fb5-7cceb233fb12",
"metadata": {},
"outputs": [],
"source": [
"body = \"\"\n",
"\n",
"img = f'<img src=\"data:image/png;base64,{openbb_image_encoded.decode()}\" alt=\"OpenBB\" style=\"width:144px;\">'\n",
"body += openbb.widgets.header(img, author, report_date, report_time, report_timezone, f'<b>LUNA CRASH EVENTS</b>')\n",
"\n",
"body += openbb.widgets.tablinks([\"Overview\", \"Before\", \"During\", \"After\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "381b353b-3e01-4db7-ab3d-429b5785e98e",
"metadata": {},
"outputs": [],
"source": [
"# OVERVIEW\n",
"text = \"\"\"Over the past few days, we witnessed the bank run on the largest decentralized stablecoin, UST, together with the demise of LUNA. \n",
"</br></br> \n",
"Its effect has spread over the entire Terra ecosystem as well as the entire crypto market. \n",
"\"\"\"\n",
"htmlcode = openbb.widgets.p(text, \"width:1200px; margin-bottom:8px\")\n",
"body += openbb.widgets.add_tab(\"Overview\", htmlcode, False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adc40a71-f245-4542-a303-fbf0970b9759",
"metadata": {},
"outputs": [],
"source": [
"# BEFORE\n",
"htmlcode = openbb.widgets.add_external_fig(\"./luna_supply.png\", \"width:500px; margin-bottom:16px\")\n",
"text = \"\"\"Just a few weeks ago, LUNA and the Terra ecosystem were the darlings of crypto investors.</br> \n",
"Experiencing a 20,000% increase since the beginning of 2021, LUNA reached its ATH of $119 in January 2021.\n",
"\"\"\"\n",
"htmlcode += openbb.widgets.p(text, \"width:1200px; margin-bottom:8px\")\n",
"htmlcode += openbb.widgets.add_external_fig(\"./coin_highs.png\", \"width:500px; margin-bottom:16px\")\n",
"text = \"\"\"Overall, before the crisis, Terra was the second biggest smart contract platform in terms of total value locked, with almost $22 billion.</br>\n",
"Its biggest dApp is Anchor, a lending and borrowing protocol with the (in)famous APY of 20%.</br>\n",
"This lucrative return is too hard to ignore, even within the crypto market. Within a year, its TVL has shot up from $500M to almost $17.5B.\n",
"\"\"\"\n",
"htmlcode += openbb.widgets.p(text, \"width:1200px; margin-bottom:8px\")\n",
"htmlcode += openbb.widgets.add_external_fig(\"./tvl.png\", \"width:500px; margin-bottom:16px\")\n",
"body += openbb.widgets.add_tab(\"Before\", htmlcode, False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e08062d0-cc29-4308-87c6-b54d5d7339a7",
"metadata": {},
"outputs": [],
"source": [
"# DURING\n",
"text = \"\"\"\n",
"The bloodbath initially started with UST losing its peg against USD. \n",
"After that, both LUNA and UST have been completely destroyed. \n",
"You might be wondering, how did this happen in less than a week? \n",
"To explain this, we first have to understand the stablizing mechanism of the Terra ecosystem.\n",
"</br>\n",
"</br> \n",
"A centralized stablecoin such as USDT would need actual backed assets, such as BTC, to consistently track the dollar. \n",
" As UST is a decentralized, and algorithmic stablecoin, the mint and burnt mechanism between LUNA & UST play a crucial role in sustaining the peg.\n",
"</br>\n",
"</br> \n",
"<b>In essence, Terra protocol allow its users to burn 1$ worth of LUNA to mint 1 UST and vice versa, burn UST to mint 1$ worth of LUNA.</b>\n",
"</br>\n",
"</br> \n",
"This creates an arbitrage opportunity whenever UST loses its peg against the dollar. \n",
"Let’s say when UST is traded at $0.90, you can buy UST and burn it for 1$ worth of LUNA, making a profit of 10 cent. \n",
"By burning UST, UST supply would decrease and therefore, raising price back to $1 as in Economics 101.\n",
"</br>\n",
"</br> \n",
"The other way around, when UST is traded at $1.10, you can burn $1 worth of LUNA to mint UST. \n",
"This would flood more UST into the market, forcing the price to go back to $1.\n",
"Whenever there’s a price discrepancy between USD and UST, Terra relies on this genius arbitrage incentives to bring price back to $1.\n",
"\"\"\"\n",
"htmlcode = openbb.widgets.p(text, \"width:1200px; margin-bottom:8px; margin-top:16px\")\n",
"htmlcode += openbb.widgets.add_external_fig(\"./ust_terra.png\", \"width:500px; margin-left:2em\")\n",
"\n",
"text = \"\"\"\n",
"But why can’t the burn-and-mint mechanism save the day? Let’s go back to what happened.\n",
"</br>\n",
"</br> \n",
"The crash started it off with UST losing its dollar peg repeatedly. \n",
"This causes investors, and whales in specific, to lose their confidence in the security of an algorithmic stablecoin.\n",
"The peak of uncertainty was on the weekend of the 7th and 8th of May.\n",
"During two days, a massive amount of UST was withdrawn from the Anchor protocol.\n",
"On Friday, the 6th of May, there were still around 14 billion UST locked in the Anchor protocol as deposits.\n",
"Then on Monday, this figure was down to 10.8 billion. This means that around 3.2 billion of UST were taken out over the weekend. \n",
"Inevitably, this created massive selling pressure on UST and lead to UST further losing its peg. \n",
"</br>\n",
"</br> \n",
"As the UST peg is too far away from 1 dollar, a massive amount of LUNA needs to be minted to bring the price back. \n",
"With such a large amount of LUNA dumped into the market, the circulating supply shot up and caused LUNA's price to fall. \n",
" Let’s keep in mind that this happened during a market crash, and hence the selling pressure has doubled up.\n",
"\"\"\"\n",
"htmlcode += openbb.widgets.p(text, \"width:1200px; margin-bottom:8px; margin-top:16px\")\n",
"htmlcode += openbb.widgets.add_external_fig(\"./luna_terra.png\", \"width:500px;\")\n",
"\n",
"text = \"\"\"\n",
"This was reflected in the LUNA’s circulating supply.\n",
"In just a few days, the number of Luna went to the moon, from less than 400 million tokens to over 6 trillion. \n",
"</br>\n",
"</br> \n",
"Eventually, with the enormous selling pressure, Luna's price has fallen below the $1 mark, even lower than UST’s supposed value.\n",
"This continues until Luna’s market cap became smaller than UST’s.\n",
"With this, the peg stabilization mechanism can no longer work, ending the hope of many investors that its price can still go back up.\n",
"This continues until Luna’s market cap became smaller than UST’s. With this, the peg stabilization mechanism can no longer work, ending the hope of many investors that its price can still go back up.\n",
"\"\"\"\n",
"htmlcode += openbb.widgets.p(text, \"width:1200px; margin-bottom:8px; margin-top:16px\")\n",
"body += openbb.widgets.add_tab(\"During\", htmlcode, False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "660ea74b-316a-45b0-af95-ee0a4ec162ef",
"metadata": {},
"outputs": [],
"source": [
"# AFTER\n",
"text = \"\"\"\n",
"As a revival plan, Do Kwon proposed to hard-fork Luna to a completely new chain, with a snapshot before the attack. \n",
"</br>\n",
"</br> \n",
"This new chain would have maximum supply of 1 billion coins. Out of these:</br>\n",
" * 400M would be given to Luna holders before the depeg happened</br>\n",
" * 400M to UST holders at the time of the upgrade</br>\n",
" * 100M to Luna holders when the blockchain stops functioning</br>\n",
" * 100M for community pool\n",
"</br>\n",
"</br> \n",
"He also mentioned changing the mint-and-burnt mechanism to a collateralized version, similar to what MakerDAO does with their DAI stablecoin.\n",
"Ironically, just early this year, he posted on Twitter: “By my hand $DAI will die.” When UST was struggling to maintain its peg, $DAI stood still with its peg. \n",
"</br>\n",
"</br> \n",
"The biggest question is: do investors have the courage to give Do Kwon and Terra’s team their trust one more time? \n",
"This bank run has put an end to a once $40 billion cryptocurrency and once the top 3 biggest stablecoins by market caps, wiping out fortunes of investors and validators. \n",
"\"\"\"\n",
"htmlcode = openbb.widgets.p(text, \"width:1200px; margin-bottom:8px; margin-top:16px\")\n",
"body += openbb.widgets.add_tab(\"After\", htmlcode, False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ddfca2f-658c-41ae-be37-e588cd2daa37",
"metadata": {},
"outputs": [],
"source": [
"body += openbb.widgets.tab_clickable_evt()\n",
"\n",
"report = openbb.widgets.html_report(title=report_name, stylesheet=stylesheet, body=body)\n",
"\n",
"# to save the results\n",
"with open(report_name + \".html\", \"w\") as fh:\n",
" fh.write(report)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e15cef06-0d10-4153-b6d3-9762be0ed1dd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Tags",
"interpreter": {
"hash": "aa1c4d436101635411b2afa2ce061827695b6e3d0efa7f05338cbf11ad26165e"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 652 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 455 KiB

View File

@@ -6,6 +6,7 @@ The library includes both python code and html/css/js elements that can be found
"""
import os
from typing import List
import base64
from jinja2 import Template
@@ -96,20 +97,22 @@ def h(level: int, text: str) -> str:
return f"<h{str(level)}>{text}</h{level}>"
def p(text: str) -> str:
def p(text: str, style: str = "") -> str:
"""Wrap text into an HTML `p` tag.
Parameters
----------
text : str
Contents for HTML `p` tag
style: str
Div style
Returns
-------
str
HTML code as string
"""
return f"<p>{text}</p>"
return f'<p style="{style}">{text}</p>'
def row(elements: List) -> str:
@@ -163,7 +166,7 @@ def kpi(thresholds: List[float], sentences: List[str], value: float) -> str:
return ""
def add_tab(title: str, htmlcode: str) -> str:
def add_tab(title: str, htmlcode: str, comment_cell: bool = True) -> str:
"""Add new tab section for the report. By default adds an opinion editable box at the start.
Parameters
@@ -172,18 +175,22 @@ def add_tab(title: str, htmlcode: str) -> str:
Title associated with this tab / section
htmlcode : str
All HTML code contain within this section
comment_cell : bool
Comment cell
Returns
-------
str
HTML code as string
"""
return f"""<div id="{title}" class="tabcontent"></br>
<p style="border:3px; border-style:solid;
html_text = f'<div id="{title}" class="tabcontent"></br>'
if comment_cell:
html_text += """<p style="border:3px; border-style:solid;
border-color:#000000; padding: 1em; width: 1050px;" contentEditable="true">
No comment.
</p>{htmlcode}
</div>"""
</p>"""
html_text += f"{htmlcode}</div>"
return html_text
def tab_clickable_evt() -> str:
@@ -277,3 +284,26 @@ def header(img, author, report_date, report_time, report_tz, title) -> str:
<p>{title}</p>
</div>
</div>"""
def add_external_fig(figloc: str, style: str = "") -> str:
"""Add external figure to HTML
Parameters
----------
figloc : str
Relative figure location
style: str
Div style
Returns
-------
str
HTML code for figure
"""
try:
with open(figloc, "rb") as image_file:
img = base64.b64encode(image_file.read()).decode()
return f'<img src="data:image/{figloc.split(".")[1]};base64,{img}"style="{style}">'
except Exception:
return ""