Analyses and Visualizations of CryptoCurrency Historical Price on AAVE using Covalent API and Python
Covalent Blockchain Data API provides a unified API to all assets secured by a blockchain network. There are two classes of endpoints:
- Class A — endpoints that return enriched blockchain data applicable to all blockchain networks, eg: balances, transactions, log events, etc.
- Class B — endpoints for a specific protocol on a blockchain, eg: AAVE is Ethereum-only and is not applicable to other blockchain networks.
In this article, we will explore the Class B endpoint to get Aave network assets and look for the cryptocurrency historical price history.
Step 1: Get Aave Network Assets
Before we start to retrieve cryptocurrency price history, first we need to get Aave Network Assets by calling Covalent API and normalize the results to Pandas Dataframe:
import subprocess
command = "curl https://api.covalenthq.com/v1/1/networks/aave/assets/"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()import pandas
import json
output_json = json.loads(output.decode("utf-8"))
output_df = pd.json_normalize(output_json)
Next, we will analyze and preprocess the output to get the symbol and address of currency on Aave:
contract_ticker_symbol_address_map = {}
for i in range(len(output_df["data.items"][0])):
symbol = output_df["data.items"][0][i]["underlying"]["contract_ticker_symbol"]
address = output_df["data.items"][0][i]["underlying"]["contract_address"]
contract_ticker_symbol_address_map[symbol] = address
Sample output:
{'DAI': '0x6b175474e89094c44da98b954eedeac495271d0f',
'TUSD': '0x0000000000085d4780b73119b644ae5ecd22b376',
'USDC': '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
'USDT': '0xdac17f958d2ee523a2206206994597c13d831ec7',
'sUSD': '0x57ab1ec28d129707052df4df418d58a2d46d5f51',
'LEND': '0x80fb784b7ed66730e8b1dbd9820afd29931aab03',
'BAT': '0x0d8775f648430679a709e98d2b0cb6250d2887ef',
'ETH': '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
'LINK': '0x514910771af9ca656af840dff83e8264ecf986ca',
'KNC': '0xdd974d5c2e2928dea5f71b9825b8b646686bd200',
'REP': '0x1985365e9f78359a9b6ad760e32412f4a445e862',
'MKR': '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2',
'MANA': '0x0f5d2fb29fb7d3cfee444a200298f468908cc942',
'ZRX': '0xe41d2489571d322189246dafa5ebde1f4699f498',
'SNX': '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f',
'WBTC': '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
'BUSD': '0x4fabb145d64652a948d72533023f6e7a623c7c53',
'ENJ': '0xf629cbd94d3791c9250152bd8dfbdf380e2a3b9c',
'REN': '0x408e41876cccdc0f92210600ef50372656052a38',
'YFI': '0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e',
'AAVE': '0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9',
'UNI': '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'}
Step 2: Get Historical Price of Aave Network Assets
After we know the addresses, we now can start looking for the historical prices of these cryptocurrencies. Using the following Python code, we will have a list of data points, that each data point represents the cryptocurrency price information at each timestamp since Jan 01, 2019:
data = []
for k in contract_ticker_symbol_address_map.keys():
v = contract_ticker_symbol_address_map[k]
if v == "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee":
continue
address = v
command = "curl https://api.covalenthq.com/v1/pricing/historical_by_address/1/USD/"+address+"/?from=2019-01-01"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
output_json = json.loads(output.decode("utf-8"))
output_df = pd.json_normalize(output_json)
for i in range(len(output_df["data.prices"][0])):
row = []
date = output_df["data.prices"][0][i]["date"]
price = output_df["data.prices"][0][i]["price"]
row.append(k)
row.append(date)
row.append(price)
data.append(row)
Sample output (only showing the first 10 rows):
[['DAI', '2021-04-26', 1.0047135],
['DAI', '2021-04-25', 1.0741913],
['DAI', '2021-04-24', 0.9921342],
['DAI', '2021-04-23', 1.0110247],
['DAI', '2021-04-22', 1.006573],
['DAI', '2021-04-21', 0.99438775],
['DAI', '2021-04-20', 0.9992315],
['DAI', '2021-04-19', 0.992394],
['DAI', '2021-04-18', 0.9946256],
['DAI', '2021-04-17', 0.9927106],...]
Next, we will transform the list into Pandas Dataframe for further visualization:
df = pd.DataFrame(data, columns = ['ticker_symbol', 'date', 'price']).sort_values(by='date')df.head()
Step 3: Visualize Historical Price of Aave Network Assets
In this section, we will use the resulted Pandas Dataframe to visualize price changing over time. Note the price is in USD.
import seaborn as sns
from datetime import datetime
import matplotlib.dates as md
import numpy as np
for k in contract_ticker_symbol_address_map.keys():
if k == "ETH":
continue
df_v = df[df.ticker_symbol == k]
fig, ax = plt.subplots(figsize = (15,15))
ax = sns.lineplot(data=df_v, x='date', y='price', ax = ax)
plt.xticks(np.arange(0, len(df_v.date)+1, 20))
plt.setp(ax.get_xticklabels(), rotation=90)
ax.set_title(k + " Price History")
plt.xlabel('Date')
plt.ylabel('Price (USD)')
About Covalent
Covalent leverages big-data technologies to create meaning from hundreds of billions of data points, delivering actionable insights to investors and allowing developers to allocate resources to higher-utility goals within their organization. Instead of pain-stakingly sourcing data from a small handful of chains, Covalent aggregates information from across dozens of sources including nodes, chains and data feeds. The Covalent API then sources end users with individualized data by wallet, including current and historical investment performance across all types of digital assets. Most importantly, Covalent returns this data in a rapid and consistent manner, incorporating all relevant data within one API interface.
Twitter: @Covalent_HQ
Telegram: https://www.covalenthq.com/telegram/
Discord: https://www.covalenthq.com/discord