image_output_1.png
import datetime

import json

import matplotlib.pyplot as plt

import pandas as pd

import requests

import seaborn as sns

import time

token_address = '0xd7efb00d12c2c13131fd319336fdf952525da2af'
base_url = 'https://api.ethplorer.io'
url = base_url + f'/getTokenInfo/{token_address}?apiKey=freekey'
response = requests.get(url)
if response.status_code == 200:
    token_info_response = json.loads(response.text)
token_info_response

url = base_url + f"/getTopTokenHolders/{token_address}?apiKey=freekey&limit=100"

response = requests.get(url)
if response.status_code == 200:
    token_holders_response = json.loads(response.text)
    
token_holders_df = pd.DataFrame(token_holders_response["holders"])
n_top_holders_list = ['1', '2', '3', '5', '10', '25', '100']
shares_list = [round(token_holders_df["share"].values[:int(n)].sum(),2) for n in n_top_holders_list]

plt.figure(figsize=(12,6))
ax = sns.barplot(x=n_top_holders_list, y=shares_list, alpha=0.8, color=color[1])
ax.bar_label(ax.containers[0])
# plt.xticks(rotation='vertical')
plt.xlabel('Top N Wallets', fontsize=12)
plt.ylabel('Cumulative percentage of Token share', fontsize=12)
plt.title('Percentage of tokens hodl by top N wallets', fontsize=16)
plt.show()

url = base_url + f"/getTokenPriceHistoryGrouped/{token_address}?apiKey=freekey&period=90"

response = requests.get(url)
if response.status_code == 200:
    token_price_history_response = json.loads(response.text)
    
def get_date_from_id(value):
    return datetime.date(value["year"], value["month"], value["day"])
    
token_price_history_df  = pd.DataFrame(token_price_history_response["history"]["countTxs"])
token_price_history_df["date"] = token_price_history_df["_id"].apply(lambda x: get_date_from_id(x))

plt.figure(figsize=(25,6))
ax = sns.barplot(x=token_price_history_df["date"], y=token_price_history_df["cnt"], alpha=0.8, color=color[1])
ax.bar_label(ax.containers[0])
plt.xticks(rotation='vertical')
plt.xlabel('Date', fontsize=12)
plt.ylabel('Number of transactions', fontsize=12)
plt.title('Number of transactions in the last 90 days', fontsize=16)
plt.show()

url = base_url + f"/getTokenHistory/{token_address}?apiKey=freekey&limit=100"
response = requests.get(url)
if response.status_code == 200:
    token_transaction_history_response = json.loads(response.text)

token_transaction_history_df = pd.DataFrame(token_transaction_history_response["operations"])
token_transaction_history_df["date"] = token_transaction_history_df["timestamp"].apply(lambda x: datetime.datetime.fromtimestamp(x).date())
cutoff_date = (datetime.datetime.today() - datetime.timedelta(days=5)).date()
# print(cutoff_date)

for _ in range(100):
    min_df_date = token_transaction_history_df["date"].min()
    offset_time = token_transaction_history_df["timestamp"].values[-1] - 1
#     print(min_df_date, offset_time)
    if min_df_date < cutoff_date:
        break
    else:
        time.sleep(3) # Honor the free limit
        url = base_url + f"/getTokenHistory/{token_address}?apiKey=freekey&limit=100&timestamp={offset_time}"
        response = requests.get(url)
        if response.status_code == 200:
            temp_token_transaction_history_response = json.loads(response.text)
            temp_token_transaction_history_df = pd.DataFrame(temp_token_transaction_history_response["operations"])
            temp_token_transaction_history_df["date"] = temp_token_transaction_history_df["timestamp"].apply(lambda x: datetime.datetime.fromtimestamp(x).date())
            token_transaction_history_df = pd.concat([token_transaction_history_df, temp_token_transaction_history_df])
            
tth_df = token_transaction_history_df[token_transaction_history_df["date"] >= cutoff_date]
tth_df = tth_df[tth_df["date"] < datetime.date.today()].reset_index(drop=True)
tth_df["value"] = tth_df["value"].astype(int)
temp_df = tth_df.groupby("date")["value"].sum().reset_index()
decimal_value = int(tth_df["tokenInfo"].values[0]["decimals"])
temp_df["value"] = temp_df["value"] / (10**decimal_value)

plt.figure(figsize=(12,6))
ax = sns.barplot(x=temp_df["date"], y=temp_df["value"], alpha=0.8, color=color[1])
ax.bar_label(ax.containers[0])
plt.xticks(rotation='vertical')
plt.xlabel('Date', fontsize=12)
plt.ylabel('Number of Tokens Transferred', fontsize=12)
plt.title('Number of Tokens Transferred in the last 5 days', fontsize=16)
plt.show()

date_value_list = []
total_addresses_list = []
for day_value in range(1, 6):
    date_value = datetime.date.today() - datetime.timedelta(days=day_value)
    temp_df = tth_df[tth_df['date'] == date_value]
    from_list = list(temp_df['from'].values)
    to_list = list(temp_df['to'].values)
    total_addresses = len(set(from_list + to_list))
    date_value_list.append(date_value)
    total_addresses_list.append(total_addresses)
plt.figure(figsize=(12, 6))
ax = sns.barplot(x=date_value_list[::-1], y=total_addresses_list[::-1], alpha=0.8, color=color[1])
ax.bar_label(ax.containers[0])
plt.xticks(rotation='vertical')
plt.xlabel('Date', fontsize=12)
plt.ylabel('Number of unique addresses involved in transfers', fontsize=12)
plt.title('Number of unique addresses in the last 5 days', fontsize=16)
plt.show()