diff options
author | jose-donato <43375532+jose-donato@users.noreply.github.com> | 2021-09-08 22:08:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 22:08:56 +0100 |
commit | 49c6430a0c80acd97c7205b1257d5a6809eb6954 (patch) | |
tree | 8a644d329345ce4483c375e7a96d9eba1b760ce9 | |
parent | 1491ddce301ee62493fbf6282b00fc4724286ca5 (diff) |
implement gwei feature (ethereum gas fees) (#736)
* implement gwei feature (ethereum gas fees)
* fix linter issues
* fixing another linting issues
* typo and linter issue
* added model and view for gwei feature
* added extra folder for reports
* added new line after gwei info
* lint
* changed tx label and table, lint
* lint fixes
6 files changed, 268 insertions, 11 deletions
diff --git a/exports/cryptocurrency/onchain/.gitkeep b/exports/cryptocurrency/onchain/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/exports/cryptocurrency/onchain/.gitkeep diff --git a/gamestonk_terminal/cryptocurrency/crypto_controller.py b/gamestonk_terminal/cryptocurrency/crypto_controller.py index 851dbbac310..ec7c58d492b 100644 --- a/gamestonk_terminal/cryptocurrency/crypto_controller.py +++ b/gamestonk_terminal/cryptocurrency/crypto_controller.py @@ -38,6 +38,7 @@ from gamestonk_terminal.cryptocurrency.cryptocurrency_helpers import ( plot_chart, ) from gamestonk_terminal.cryptocurrency.report import report_controller +from gamestonk_terminal.cryptocurrency.onchain import onchain_controller from gamestonk_terminal.cryptocurrency.due_diligence.binance_model import ( show_available_pairs_for_given_symbol, ) @@ -60,13 +61,7 @@ class CryptoController: "find", ] - CHOICES_MENUS = [ - "ta", - "dd", - "ov", - "disc", - "report", - ] + CHOICES_MENUS = ["ta", "dd", "ov", "disc", "report", "onchain"] SOURCES = { "bin": "Binance", @@ -122,10 +117,11 @@ Note: Some of CoinGecko commands can fail. Team is working on fix. find alternate way to search for coins finbrain crypto sentiment from 15+ major news headlines -> disc discover trending cryptocurrencies, e.g.: top gainers, losers, top sentiment -> ov overview of the cryptocurrencies, e.g.: market cap, DeFi, latest news, top exchanges, stables -> dd due-diligence for loaded coin, e.g.: coin information, social media, market stats -> ta technical analysis for loaded coin. e.g.: ema, macd, rsi, adx, bbands, obv +> disc discover trending cryptocurrencies, e.g.: top gainers, losers, top sentiment +> ov overview of the cryptocurrencies, e.g.: market cap, DeFi, latest news, top exchanges, stables +> dd due-diligence for loaded coin, e.g.: coin information, social media, market stats +> ta technical analysis for loaded coin, e.g.: ema, macd, rsi, adx, bbands, obv +> onchain information on different blockchains, e.g.: eth gas fees > report generate automatic report """ print(help_text) @@ -586,6 +582,15 @@ Note: Some of CoinGecko commands can fail. Team is working on fix. else: return True + def call_onchain(self, _): + """Process onchain command""" + ret = onchain_controller.menu() + + if ret is False: + self.print_help() + else: + return True + def call_find(self, other_args): """Process find command""" parser = argparse.ArgumentParser( diff --git a/gamestonk_terminal/cryptocurrency/onchain/__init__.py b/gamestonk_terminal/cryptocurrency/onchain/__init__.py new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/gamestonk_terminal/cryptocurrency/onchain/__init__.py diff --git a/gamestonk_terminal/cryptocurrency/onchain/gasnow_model.py b/gamestonk_terminal/cryptocurrency/onchain/gasnow_model.py new file mode 100644 index 00000000000..8a538672635 --- /dev/null +++ b/gamestonk_terminal/cryptocurrency/onchain/gasnow_model.py @@ -0,0 +1,40 @@ +import pandas as pd +import requests + + +def get_gwei_fees() -> pd.DataFrame: + """Returns the most recent Ethereum gas fees in gwei + [Source: https://www.gasnow.org] + + Parameters + ---------- + + Returns + ------- + pd.DataFrame + four gas fees and durations + (fees for slow, average, fast and + fastest transactions in gwei and + its average durations in seconds) + """ + + r = requests.get("https://www.gasnow.org/api/v3/gas/price") + + if r.status_code == 200: + try: + data = r.json()["data"] + return pd.DataFrame( + data=[ + ["Fastest", int(data["rapid"] / 1_000_000_000), "~15 sec"], + ["Fast", int(data["fast"] / 1_000_000_000), "~1 min"], + ["Standard", int(data["standard"] / 1_000_000_000), "~3 min"], + ["Slow", int(data["slow"] / 1_000_000_000), ">10 min"], + ], + columns=["Tx Type", "Fee (gwei)", "Duration"], + ) + except TypeError: + print("Error in gasnow JSON response.\n") + return pd.DataFrame() + else: + print("Error in gasnow GET request\n") + return pd.DataFrame() diff --git a/gamestonk_terminal/cryptocurrency/onchain/gasnow_view.py b/gamestonk_terminal/cryptocurrency/onchain/gasnow_view.py new file mode 100644 index 00000000000..83d3b362ad6 --- /dev/null +++ b/gamestonk_terminal/cryptocurrency/onchain/gasnow_view.py @@ -0,0 +1,42 @@ +import os + +from tabulate import tabulate +from gamestonk_terminal.helper_funcs import export_data +from gamestonk_terminal.cryptocurrency.onchain.gasnow_model import get_gwei_fees +from gamestonk_terminal import feature_flags as gtff + + +def display_gwei_fees(export: str) -> None: + """Current gwei fees + [Source: https://www.gasnow.org] + + Parameters + ---------- + export : str + Export dataframe data to csv,json,xlsx file + """ + + df_fees = get_gwei_fees() + + print("Current ETH gas fees (gwei):") + + if gtff.USE_TABULATE_DF: + print( + tabulate( + df_fees.head(4), + headers=df_fees.columns, + floatfmt=".1f", + showindex=False, + tablefmt="fancy_grid", + ), + "\n", + ) + else: + print(df_fees.to_string(index=False), "\n") + + export_data( + export, + os.path.dirname(os.path.abspath(__file__)), + "gwei", + df_fees, + ) diff --git a/gamestonk_terminal/cryptocurrency/onchain/onchain_controller.py b/gamestonk_terminal/cryptocurrency/onchain/onchain_controller.py new file mode 100644 index 00000000000..32793ee6294 --- /dev/null +++ b/gamestonk_terminal/cryptocurrency/onchain/onchain_controller.py @@ -0,0 +1,170 @@ +"""Onchain Controller Module""" +__docformat__ = "numpy" + +import os +import argparse +from typing import List +from prompt_toolkit.completion import NestedCompleter + +from gamestonk_terminal import feature_flags as gtff +from gamestonk_terminal.menu import session +from gamestonk_terminal.helper_funcs import get_flair, parse_known_args_and_warn + +from gamestonk_terminal.cryptocurrency.onchain import gasnow_view + +# pylint: disable=R1732 + + +class OnchainController: + """Onchain Controller class""" + + CHOICES = [ + "cls", + "?", + "help", + "q", + "quit", + ] + + CHOICES_COMMANDS = [ + "gwei", + ] + + CHOICES += CHOICES_COMMANDS + + def __init__(self): + """Constructor""" + self.onchain_parser = argparse.ArgumentParser(add_help=False, prog="onchain") + self.onchain_parser.add_argument( + "cmd", + choices=self.CHOICES, + ) + + def switch(self, an_input: str): + """Process and dispatch input + + Parameters + ------- + an_input : str + string with input arguments + + Returns + ------- + True, False or None + False - quit the menu + True - quit the program + None - continue in the menu + """ + + # Empty command + if not an_input: + print("") + return None + + (known_args, other_args) = self.onchain_parser.parse_known_args( + an_input.split() + ) + + # Help menu again + if known_args.cmd == "?": + print_help() + return None + + # Clear screen + if known_args.cmd == "cls": + os.system("cls||clear") + return None + + return getattr( + self, "call_" + known_args.cmd, lambda: "Command not recognized!" + )(other_args) + + def call_help(self, *_): + """Process Help command""" + print_help() + + def call_q(self, _): + """Process Q command - quit the menu""" + return False + + def call_quit(self, _): + """Process Quit command - quit the program""" + return True + + def call_gwei(self, other_args: List[str]): + """Process gwei command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="onchain", + description=""" + Display ETH gas fees + [Source: https://www.gasnow.org] + """, + ) + + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + + try: + ns_parser = parse_known_args_and_warn(parser, other_args) + + if not ns_parser: + return + + gasnow_view.display_gwei_fees(export=ns_parser.export) + + except Exception as e: + print(e, "\n") + + +def print_help(): + """Print help""" + print( + "https://github.com/GamestonkTerminal/GamestonkTerminal/tree/main/gamestonk_terminal/cryptocurrency/onchain" + ) + print("\nOnchain:") + print(" cls clear screen") + print(" ?/help show this menu again") + print(" q quit this menu, and shows back to main menu") + print(" quit quit to abandon program") + print("") + print(" gwei check current eth gas fees") + print("") + + +def menu(): + """Onchain Menu""" + onchain_controller = OnchainController() + onchain_controller.call_help(None) + + while True: + # Get input command from user + if session and gtff.USE_PROMPT_TOOLKIT: + completer = NestedCompleter.from_nested_dict( + {c: None for c in onchain_controller.CHOICES} + ) + an_input = session.prompt( + f"{get_flair()} (crypto)>(onchain)> ", + completer=completer, + ) + else: + an_input = input(f"{get_flair()} (crypto)>(onchain)> ") + + try: + process_input = onchain_controller.switch(an_input) + except SystemExit: + print("The command selected doesn't exist\n") + continue + + if process_input is False: + return False + + if process_input is True: + return True |