mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-27 03:33:24 +00:00
* Fixes * Update stocks_helper.py * update git-actions set-output to new format * Update stocks_helper.py * Update terminal_helper.py * removed LineAnnotateDrawer from qa_view * lint * few changes * updates * sdk auto gen modules done * Update stocks_helper.py * updates to changed imports, and remove first sdk_modules * Update generate_sdk.py * Update generate_sdk.py * pylint * revert stocks_helper * Update generate_sdk.py * Update sdk.py * Update generate_sdk.py * full auto generation, added sdk.py/controllers creation * missed enable forecasting * added running black in subprocess after sdk files generation completes * removed deleted sdk_arg_logger * comment out tests * property doc fix * clean up * Update generate_sdk.py * make trailmap classes useable for doc generation * Update generate_sdk.py * added lineon to trailmap class for linking to func in markdown * changed lineon to dict * added full_path to trailmap for linking in docs * updated portfolio * feat: initial files * feat: added meta head * feat: added funcdef * added func_def to trailmap attributes for markdown in docs, added missing type hints to covid functions * feat: added view and merged with jaun * Update generate_sdk.py * Update generate_sdk.py * Update generate_sdk.py * Update generate_sdk.py * init * fix returns * fix: random stuff * fix: random * fixed encoding issue on windows * fix: generate tabs * update * Update generate_sdk_markdown.py * Create .pydocstyle.ini * added type hint classes for views * fixes * alt, ba * alt-economy * Update finviz_compare_model.py * fixs * Update substack_model.py * Update generate_sdk.py * last of my section * porfolio * po * Update optimizer_model.py * fixing more things * few more * keys done * update * fixes * Update generate_sdk_markdown.py * Update generate_sdk_markdown.py * mypy forecast fix * Update generate_sdk_markdown.py * Update generate_sdk_markdown.py * Update generate_sdk_markdown.py * fixes * forecast fixes * one more fix * Update coinbase_model.py * Update generate_sdk_markdown.py Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Co-authored-by: James Maslek <jmaslek11@gmail.com> Co-authored-by: jose-donato <zmcdonato@gmail.com> Co-authored-by: andrewkenreich <andrew.kenreich@gmail.com>
219 lines
5.4 KiB
Python
219 lines
5.4 KiB
Python
"""Shroom model"""
|
|
import logging
|
|
from typing import List
|
|
import json
|
|
import time
|
|
import pandas as pd
|
|
import requests
|
|
|
|
from openbb_terminal.decorators import log_start_end, check_api_key
|
|
from openbb_terminal import config_terminal as cfg
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
TTL_MINUTES = 15
|
|
|
|
# return up to 100,000 results per GET request on the query id
|
|
PAGE_SIZE = 100000
|
|
|
|
# return results of page 1
|
|
PAGE_NUMBER = 1
|
|
|
|
|
|
def create_query(query: str):
|
|
r = requests.post(
|
|
"https://node-api.flipsidecrypto.com/queries",
|
|
data=json.dumps({"sql": query, "ttlMinutes": TTL_MINUTES}),
|
|
headers={
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
"x-api-key": cfg.API_SHROOM_KEY,
|
|
},
|
|
)
|
|
if r.status_code != 200:
|
|
raise Exception(
|
|
f"Error creating query, got response: {r.text} with status code: {str(r.status_code)}"
|
|
)
|
|
|
|
return json.loads(r.text)
|
|
|
|
|
|
def get_query_results(token):
|
|
r = requests.get(
|
|
f"https://node-api.flipsidecrypto.com/queries/{token}?pageNumber={PAGE_NUMBER}&pageSize={PAGE_SIZE}",
|
|
headers={
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
"x-api-key": cfg.API_SHROOM_KEY,
|
|
},
|
|
)
|
|
if r.status_code != 200:
|
|
raise Exception(
|
|
f"Error creating query, got response: {r.text} with status code: {str(r.status_code)}"
|
|
)
|
|
|
|
data = json.loads(r.text)
|
|
if data["status"] == "running":
|
|
time.sleep(10)
|
|
return get_query_results(token)
|
|
|
|
return data
|
|
|
|
|
|
DAPP_STATS_PLATFORM_CHOICES = ["uniswap-v3", "uniswap-v2", "sushiswap", "curve"]
|
|
|
|
|
|
def get_shroom_data(sql: str):
|
|
"""Get shroom data"""
|
|
query = create_query(sql)
|
|
token = query["token"]
|
|
data = get_query_results(token)
|
|
return data
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
@check_api_key(["API_SHROOM_KEY"])
|
|
def get_dapp_stats(
|
|
platform: str = "curve",
|
|
) -> pd.DataFrame:
|
|
"""Get dapp stats
|
|
|
|
Parameters
|
|
----------
|
|
platform: str
|
|
platform to get stats for
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame
|
|
dapp stats
|
|
"""
|
|
sql = f"""select
|
|
date_trunc('week', s.block_timestamp) as date,
|
|
sum(t.fee_usd) as fee,
|
|
count(distinct(s.from_address)) as n_users,
|
|
count(distinct(s.amount_usd)) as volume
|
|
from ethereum.dex_swaps s
|
|
join ethereum.transactions t
|
|
on s.tx_id = t.tx_id
|
|
where platform = '{platform}'
|
|
group by date
|
|
order by date asc
|
|
"""
|
|
data = get_shroom_data(sql)
|
|
|
|
df = pd.DataFrame(
|
|
data["results"], columns=["timeframe", "fees", "n_users", "volume"]
|
|
)
|
|
|
|
df["timeframe"] = pd.to_datetime(df["timeframe"])
|
|
df = df.set_index("timeframe")
|
|
|
|
return df
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
@check_api_key(["API_SHROOM_KEY"])
|
|
def get_daily_transactions(symbols: List[str]) -> pd.DataFrame:
|
|
"""Get daily transactions for certain symbols in ethereum blockchain
|
|
[Source: https://sdk.flipsidecrypto.xyz/shroomdk]
|
|
|
|
Parameters
|
|
----------
|
|
symbols : List[str]
|
|
List of symbols to get transactions for
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame
|
|
DataFrame with transactions for each symbol
|
|
"""
|
|
extra_sql = ""
|
|
for symbol in symbols:
|
|
extra_sql += (
|
|
f"sum(case when symbol = '{symbol}' then amount_usd end) as {symbol},"
|
|
)
|
|
extra_sql = extra_sql[:-1]
|
|
sql = f"""
|
|
select
|
|
date_trunc('day', block_timestamp) as timeframe,
|
|
{extra_sql}
|
|
from ethereum.udm_events
|
|
where
|
|
block_timestamp >= '2020-06-01'
|
|
-- and amount0_usd > '0'
|
|
group by 1
|
|
order by timeframe desc
|
|
"""
|
|
|
|
data = get_shroom_data(sql)
|
|
|
|
df = pd.DataFrame(data["results"], columns=["timeframe"] + symbols)
|
|
df["timeframe"] = pd.to_datetime(df["timeframe"])
|
|
df.set_index("timeframe", inplace=True)
|
|
|
|
return df
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
@check_api_key(["API_SHROOM_KEY"])
|
|
def get_total_value_locked(
|
|
user_address: str,
|
|
address_name: str,
|
|
symbol: str = "USDC",
|
|
interval: int = 1,
|
|
) -> pd.DataFrame:
|
|
|
|
"""
|
|
Get total value locked for a user/name address and symbol
|
|
TVL measures the total amount of a token that is locked in a contract.
|
|
[Source: https://sdk.flipsidecrypto.xyz/shroomdk]
|
|
|
|
Parameters
|
|
----------
|
|
user_address : str
|
|
User address
|
|
address_name : str
|
|
Name of the address
|
|
symbol : str
|
|
Symbol of the token
|
|
interval : int
|
|
Interval in months
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame
|
|
DataFrame with total value locked
|
|
"""
|
|
|
|
if not (user_address or address_name):
|
|
raise Exception("No user address or address name provided")
|
|
if user_address:
|
|
extra_sql = f"user_address = '{user_address}' and"
|
|
else:
|
|
extra_sql = f"address_name = '{address_name}' and"
|
|
|
|
sql = f"""
|
|
SELECT
|
|
date_trunc('day', balance_date) as metric_date,
|
|
symbol,
|
|
amount_usd/1000000 as amount_usd
|
|
FROM
|
|
ethereum.erc20_balances
|
|
WHERE
|
|
{extra_sql}
|
|
symbol = '{symbol}' AND
|
|
balance_date >= getdate() - interval '{interval} month'
|
|
ORDER BY
|
|
metric_date asc
|
|
"""
|
|
|
|
data = get_shroom_data(sql)
|
|
|
|
df = pd.DataFrame(data["results"], columns=["metric_date", "symbol", "amount_usd"])
|
|
df["metric_date"] = pd.to_datetime(df["metric_date"])
|
|
df.set_index("metric_date", inplace=True)
|
|
|
|
return df
|