cryo
, Paradigm’s powerful command-line interface tool. As you might recall, this tool is a beacon for developers, researchers, and blockchain enthusiasts, optimizing the process of extracting data from various blockchain networks. Our initial journey through cryo
revealed its data formatting efficiency and seamless integration with Chainstack Global Nodes.
cryo
and how it works with cryo: Your gateway to blockchain data.cryo
with the world of Python. This guide will show you how to use the Python wrapper made for the cryo
CLI by covering setup, basic usage, and data extraction and manipulation using common Python libraries.
cryo
’s Rust-based efficiency for data extraction with Python’s data manipulation capabilities. This integration enhances the analytical power at your fingertips, allowing you to leverage Python’s rich library ecosystem for in-depth data analysis, visualization, and machine learning.
cryo
tool with Python. This process involves ensuring that your system has the necessary tools and libraries and installing the Python wrapper for cryo
.
.env
file in your root directory and place the endpoint in it.
cryo
to work, the Python integration is a lightweight wrapper for the cryo
CLI, so you’ll still need to meet the app’s requirements.cryo_python
depends on several libraries, make sure to install the following libraries,
python-dotenv web3 matplotlib
libraries are not strictly required to run cryo_python
, but we’ll use them along the guide.
cryo
repository from GitHub. If you don’t have git installed, you can download it from git.
cryo_python
:
maturin
build command:
.whl
file generated by maturin. It will be located in the target/wheels
directory.<PATH_TO_WHEEL_FILE>
with the actual path to the .whl
file generated, it will look like this:
cryo.collect()
and cryo.freeze()
. Here’s an improved version:
cryo_python
cryo_python
serves as a lightweight wrapper for the cryo
CLI offers a seamless Python interface to the powerful CLI commands. With cryo_python
users can access two principal functions that mirror their CLI counterparts:
cryo.collect()
extracts blockchain data and returns it as a Python-friendly data frame, enabling direct use within scripts for real-time analysis and manipulation.cryo.freeze()
fetches data and saves it to a file, facilitating subsequent use or long-term storage.cryo.collect()
Main Aspectscryo.collect()
includes both async_collect
and collect
methods, designed to operate asynchronously. This feature is vital for efficiently handling large datasets or high-throughput tasks, ensuring optimal resource utilization and performance.
cryo.collect()
allows you to organize data in various Python-friendly formats for diverse scenarios:
cryo.freeze()
Main Aspectscryo.freeze()
can handle single and multiple data types, showcasing its versatility in accommodating various data collection needs.cryo.collect()
, cryo.freeze()
also parses additional keyword arguments (*kwargs
), enhancing the customization possibilities in data collection and storage.cryo_python
, let’s get into practical examples to demonstrate its usage. Throughout this guide, we’ll consistently retrieve the RPC endpoint from a .env
file.
.env
file for these examples.cryo.collect
basic examplemain.py
and paste the following code:
os
for environment variable management, cryo
for accessing blockchain data, and load_dotenv
from the dotenv
package to load environment variables from a .env
file.load_dotenv()
, which reads the .env
file and sets the variables.ETH_RPC
variable, which contains the URL to an Ethereum RPC endpoint, is fetched from the environment variables using os.getenv("ETH_RPC")
.cryo.collect
:
cryo.collect
function has specific parameters to fetch data from the Ethereum blockchain.datatype
: Set to "blocks"
, indicating that the function should fetch data about blockchain blocks.blocks
: Specifies the range of blocks to fetch data for (in this case, from block 18734050
to 18735050
).rpc
: The Ethereum RPC endpoint URL, passed as eth_rpc
.output_format
: Set to "pandas"
, indicating that the data should be returned as a Pandas DataFrame.hex
: The boolean parameter set to True
will return the data already converted to hexadecimal.data
, a Pandas DataFrame.data
, showing the fetched blockchain data.block_hash
, author
, block_number
, gas_used
, extra_data
, timestamp
, base_fee_per_gas
, and chain_id
.
Here is an example of the output in the console:
cryo
CLI directly:
cryo.collect
can be adjusted using the requests_per_second
parameter:
cryo.freeze
basic examplecryo.freeze
is quite similar to cryo.collect
. In a new file, paste this code:
cryo.freeze
to fetch and save the same block data as a JSON file in the specified directory. The logic and syntax closely follow the cryo
CLI. The result is a JSON file containing data for the blocks in the root/blocks_data/
directory.
Since both cryo.freeze
and cryo.collect
are just wrappers around the CLI; you can use the same commands. Let’s explore a few more examples.
cryo
cryo_python
to retrieve ERC-20 token balances from specified addresses and contracts. We’ll get the balance of the APECoin token in the Binance address in a range of 10,000 blocks.
Start by creating a new Python file and paste the following code:
erc20_balances
efficiently organizes ERC-20 balances by block, offering a clear and accessible format for data analysis.
cryo
documentation to find what other datasets you can fetch.cryo_python
, let’s now get into a more advanced application by integrating it with essential Python libraries for data manipulation and visualization.
cryo_python
, pandas
, and matplotlib
.
In a Python file, paste the following:
os
, time
, pandas
, matplotlib.pyplot
, and Web3
, along with cryo
. Then, we define constants for the RPC URL, the number of blocks to look back on, and the number of top authors to display.cryo.collect
to get data on these blocks and returns it as a pandas DataFrame. We track the time taken for this operation, offering insights into the performance of our data retrieval process.matplotlib
to create a bar chart, showcasing the top authors based on the number of blocks mined.main
function, we initialize a Web3 instance, connect to the Ethereum node, fetch the block data, and, if successful, visualize the top authors. We handle potential errors, such as missing environment variables or connection issues, to ensure robustness.cryo
with other Python tools to fetch, process, and visualize Ethereum blockchain data, providing valuable insights into blockchain activity.
Here is an example of the console output and chart. The console will output something like the following:
erc20_balances
dataset used in one of the previous examples. This time, we’ll fetch and visualize how much WETH is in theWETH-USDT
pool from Uniswap V2.
In a new file, paste the following code:
cryo.collect
function is called, and the data is returned in a pandas DataFrame format.None
values to avoid errors during conversion.matplotlib
. The x-axis represents block numbers, and the y-axis represents the balance in Ether.cryo
to fetch data and manipulate it with Python, a very powerful combo.
cryo
with Python is a significant advancement for blockchain data analysis. It combines cryo
’s efficient data extraction capabilities with Python’s powerful data processing and visualization tools. This synergy, coupled with high-performance Chainstack Global Nodes, enables users to easily extract, analyze, and visualize blockchain data, making it an invaluable resource for developers, researchers, and enthusiasts in the blockchain community. The practical examples demonstrate this integration’s real-world utility, highlighting its potential to yield insightful and actionable information from complex blockchain datasets. In essence, cryo
and Python offer an effective and accessible platform for in-depth blockchain data exploration.