def display_defi_protocols(top: int, sortby: str, descend: bool, description: bool, export: str = "") -> None: """Display information about listed DeFi protocols, their current TVL and changes to it in the last hour/day/week. [Source: https://docs.llama.fi/api] Parameters ---------- top: int Number of records to display sortby: str Key by which to sort data descend: bool Flag to sort data descending description: bool Flag to display description of protocol export : str Export dataframe data to csv,json,xlsx file """ df = llama_model.get_defi_protocols() df_data = df.copy() df = df.sort_values(by=sortby, ascending=descend) df["tvl"] = df["tvl"].apply(lambda x: long_number_format(x)) if not description: df.drop(["description", "url"], axis=1, inplace=True) else: df = df[[ "name", "symbol", "category", "description", "url", ]] if gtff.USE_TABULATE_DF: print( tabulate( df.head(top), headers=df.columns, floatfmt=".2f", showindex=False, tablefmt="fancy_grid", ), "\n", ) else: print(df.to_string, "\n") export_data( export, os.path.dirname(os.path.abspath(__file__)), "llama", df_data, )
def test_get_defi_protocols(): df = llama_model.get_defi_protocols() # recorder not used # somehow there are some whitespace diff between captured/recorded assert isinstance(df, pd.DataFrame) assert not df.empty
def display_grouped_defi_protocols(num: int = 50, export: str = "") -> None: """Display top dApps (in terms of TVL) grouped by chain. [Source: https://docs.llama.fi/api] Parameters ---------- num: int Number of top dApps to display export : str Export dataframe data to csv,json,xlsx file """ df = llama_model.get_defi_protocols() fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI) df = df.sort_values("tvl", ascending=False).head(num) df = df.set_index("name") chains = df.groupby("chain").size().index.values.tolist() for chain in chains: chain_filter = df.loc[df.chain == chain] ax.bar(x=chain_filter.index, height=chain_filter.tvl, label=chain) ax.set_ylabel("Total Value Locked ($)") ax.set_xlabel("dApp name") ax.get_yaxis().set_major_formatter( ticker.FuncFormatter(lambda x, _: long_number_format(x))) fig.tight_layout(pad=8) ax.legend(ncol=2) ax.set_title(f"Top {num} dApp TVL grouped by chain") ax.grid(alpha=0.5) ax.tick_params(axis="x", labelrotation=90) if gtff.USE_ION: plt.ion() plt.show() print("") export_data( export, os.path.dirname(os.path.abspath(__file__)), "gdapps", df, )
def test_get_defi_protocols(recorder): df = llama_model.get_defi_protocols() recorder.capture(df)
def display_defi_protocols(top: int, sortby: str, descend: bool, description: bool, export: str = "") -> None: """Display information about listed DeFi protocols, their current TVL and changes to it in the last hour/day/week. [Source: https://docs.llama.fi/api] Parameters ---------- top: int Number of records to display sortby: str Key by which to sort data descend: bool Flag to sort data descending description: bool Flag to display description of protocol export : str Export dataframe data to csv,json,xlsx file """ df = llama_model.get_defi_protocols() df_data = df.copy() df = df.sort_values(by=sortby, ascending=descend) df = df.drop(columns="chain") df["tvl"] = df["tvl"].apply(lambda x: lambda_long_number_format(x)) if not description: df.drop(["description", "url"], axis=1, inplace=True) else: df = df[[ "name", "symbol", "category", "description", "url", ]] df.columns = [ lambda_replace_underscores_in_column_names(val) for val in df.columns ] df.rename( columns={ "Change 1H": "Change 1H (%)", "Change 1D": "Change 1D (%)", "Change 7D": "Change 7D (%)", "Tvl": "TVL ($)", }, inplace=True, ) print_rich_table(df.head(top), headers=list(df.columns), show_index=False) console.print("") export_data( export, os.path.dirname(os.path.abspath(__file__)), "ldapps", df_data, )
def display_grouped_defi_protocols( num: int = 50, export: str = "", external_axes: Optional[List[plt.Axes]] = None) -> None: """Display top dApps (in terms of TVL) grouped by chain. [Source: https://docs.llama.fi/api] Parameters ---------- num: int Number of top dApps to display export : str Export dataframe data to csv,json,xlsx file external_axes : Optional[List[plt.Axes]], optional External axes (1 axis is expected in the list), by default None """ df = llama_model.get_defi_protocols() df = df.sort_values("tvl", ascending=False).head(num) df = df.set_index("name") chains = df.groupby("chain").size().index.values.tolist() # This plot has 1 axis if not external_axes: _, ax = plt.subplots(figsize=(14, 8), dpi=PLOT_DPI) else: if len(external_axes) != 1: console.print("[red]Expected list of one axis item./n[/red]") return (ax, ) = external_axes colors = iter(cfg.theme.get_colors(reverse=True)) for chain in chains: chain_filter = df.loc[df.chain == chain] ax.barh( y=chain_filter.index, width=chain_filter.tvl, label=chain, height=0.5, color=next(colors), ) ax.set_ylabel("Total Value Locked ($)") ax.set_xlabel("dApp name") ax.get_yaxis().set_major_formatter( ticker.FuncFormatter(lambda x, _: lambda_long_number_format(x))) ax.set_title(f"Top {num} dApp TVL grouped by chain") cfg.theme.style_primary_axis(ax) ax.tick_params(axis="y", labelsize=8) ax.yaxis.set_label_position("left") ax.yaxis.set_ticks_position("left") ax.legend(loc="best") if not external_axes: cfg.theme.visualize_output() export_data( export, os.path.dirname(os.path.abspath(__file__)), "gdapps", df, )