A comprehensive data analytics pipeline for the Mezo protocol, processing blockchain data from subgraphs, smart contracts, and block explorer APIs to generate insights and dashboards. It exports raw and transformed data to Mezo's BigQuery warehouse.
This repository contains scripts and utilities for:
- Fetching and processing data from Mezo mainnet subgraphs
- Querying smart contracts via Web3.py (note: these queries are performed directly in Hex)
- Retrieving transaction data from block explorer APIs
- Processing all major transactions on Mezo mainnet, including providing liquidity, swapping, vaulting assets, borrowing, and purchasing items in the MUSD market.
mezo-analytics/
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore rules
βββ README.md # Project documentation
βββ requirements.txt # Python dependencies
βββ setup.py # Package setup configuration
β
βββ mezo/ # Core library modules
β βββ __init__.py # Package initialization
β βββ clients.py # API clients (SubgraphClient, Web3Client, BigQueryClient)
β βββ queries.py # GraphQL queries for subgraphs
β βββ currency_config.py # Token mappings and currency configurations
β βββ currency_utils.py # Currency conversion and formatting utilities
β βββ datetime_utils.py # Date/time formatting and conversion
β βββ data_utils.py # Data processing helper functions
β βββ visual_utils.py # Progress indicators and error handling
β βββ smart_contracts/ # Contract ABIs and configurations
β βββ ActivePool.json
β βββ BorrowerOperations.json
β βββ MezoBridge.json
β βββ PoolFactory.json
β βββ PriceFeed.json
β βββ Router.json
β βββ StabilityPool.json
β βββ TroveManager.json
β
βββ scripts/ # Data processing pipelines
β βββ __init__.py
β β
β βββ # Main Processing Scripts
β βββ process_pools_data.py # Liquidity pool metrics and TVL
β βββ process_musd_data.py # MUSD loan and collateral processing
β βββ process_swaps_data.py # Swap volume and fee analytics
β βββ process_market_data.py # Market transactions and donations
β βββ process_bridge_data.py # Bridge in/out transaction processing
β βββ process_vaults_data.py # Vault deposits and withdrawals
β βββ process_dapp_data.py # DApp transaction processing
β β
β βββ # Data Fetching Scripts
β βββ fetch_market_transactions.py # Fetch from block explorer API
β βββ fetch_mezo_users.py # User data collection
β βββ get_raw_data.py # Raw subgraph data fetching
β β
β βββ # Utility Scripts
β βββ update_requirements.py # Auto-update requirements.txt
β βββ update_all_data.py # Orchestrate all data updates
β β
β βββ archive/ # Deprecated/archived scripts
β βββ get_raw_autobridge_data.py
β βββ get_raw_loan_data.py
β
βββ notebooks/ # Jupyter notebooks for analysis
β βββ exploratory_analysis.ipynb
β βββ pool_metrics_analysis.ipynb
β βββ user_behavior_analysis.ipynb
β
βββ tests/ # Test suite
β βββ __init__.py
β βββ test_clients.py
β βββ test_currency_utils.py
β βββ test_data_processing.py
β βββ test_queries.py
β
βββ docs/ # Additional documentation
βββ api_reference.md
βββ data_dictionary.md
βββ deployment_guide.md
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Subgraph APIs β β Smart Contracts β β Block Explorer β
β (Goldsky) β β (Web3.py) β β API β
ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β SubgraphClient β β Web3Client β β API Requests β
ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ
β β β
βββββββββββββββββββββββββΌβββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Raw Data Collection β
ββββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Data Processing Scripts β
β β’ process_pools_data β
β β’ process_musd_data β
β β’ process_swaps_data β
β β’ process_market_data β
ββββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Data Transformation β
β β’ Cleaning & Validationβ
β β’ Currency Conversion β
β β’ Metrics Calculation β
ββββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β BigQuery Storage β
βββββββββββββββββββββββββββ€
β β’ raw_data/ β
β β’ intermediate/ β
β β’ marts/ β
ββββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Hex Dashboards β
β β’ Executive KPIs β
β β’ Pool Analytics β
β β’ Risk Metrics β
βββββββββββββββββββββββββββ
All subgraphs are hosted on Goldsky and accessed via GraphQL queries:
Subgraph | URL | Purpose |
---|---|---|
Mezo Portal | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/mezo-portal-mainnet/1.0.0/gn |
Auto-bridge transactions from Mainnet |
Mezo Bridge | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/mezo-bridge-mainnet/1.0.0/gn |
Bridge transactions into Mezo |
Bridge Out | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/bridge-out-mezo/1.0.0/gn |
Bridge transactions out of Mezo |
Borrower Operations | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/borrower-operations-mezo/1.0.0/gn |
MUSD loan operations (open/close/adjust troves) |
MUSD Token | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/musd-token/1.0.0/gn |
MUSD token transfers and mints |
MUSD Market | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/market-mezo/1.0.0/gn |
MUSD market donations and purchases |
Stability Pool | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/musd-stability-pool/1.0.0/gn |
MUSD stability pool operations |
Trove Manager | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/musd-trove-manager/1.0.0/gn |
Trove liquidations and redemptions |
MUSD Pools | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/musd-pools-mezo/1.0.0/gn |
MUSD liquidity pools and swaps |
Tigris Pools | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/tigris-pools-mezo/1.0.0/gn |
Tigris protocol pools |
August Vaults | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/mezo-vaults-mezo/1.0.0/gn |
August protocol vaults |
Wormhole | https://api.goldsky.com/api/public/project_cm6ks2x8um4aj01uj8nwg1f6r/subgraphs/wormhole-bridge-mezo/1.0.0/gn |
Wormhole bridge operations |
The following contracts are queried directly using Web3.py:
Contract | Address | Methods Called |
---|---|---|
BorrowerOperations | 0x5fE95B9Bb60bE973cb6c90Cd07eC69b7E88fafE2 |
borrowingRate() , refinancingFeePercentage() |
TroveManager | 0x94AfB503dBca74aC3E4929BACEeDfCe19B93c193 |
liquidateTroves() , MUSD_GAS_COMPENSATION() , getCurrentICR() |
PCV | 0x391EcC7ffEFc48cff41D0F2Bb36e38b82180B993 |
debtToPay() |
RPC Endpoint: https://mainnet.mezo.public.validationcloud.io/
Base URL: https://api.explorer.mezo.org/api/v2/
Endpoints Used:
/addresses/{address}/transactions
- Fetch transactions for specific contracts/tokens/{token_address}/transfers
- Token transfer history/tokens/{token_address}/counters
- Token holder statistics/tokens/{token_address}/
- Token metadata and market data
Contracts called via Block Explorer:
- MUSD token:
0xdD468A1DDc392dcdbEf6db6e34E89AA338F9F186
- Pools Router:
0x16A76d3cd3C1e3CE843C6680d6B37E9116b5C706
- TroveManager:
0x94afb503dbca74ac3e4929bacededfce19b93c193
- August MUSD Vault:
0x221B2D9aD7B994861Af3f4c8A80c86C4aa86Bf53
# Subgraph queries via SubgraphClient
SubgraphClient.fetch_subgraph_data(query, method)
# Smart contract queries via Web3Client
Web3Client.load_contract().functions.method_name().call()
# Block explorer API
requests.get(f"https://api.explorer.mezo.org/api/v2/{endpoint}")
- Metrics Calculated:
- TVL (Total Value Locked) with growth rates
- Trading volume and efficiency ratios
- Fee revenue analytics
- User engagement metrics
- Pool health indicators with composite scores
- Net flows (deposits - withdrawals)
- Metrics Calculated:
- Loan originations and closures
- Collateralization ratios
- Liquidation risk categories
- Borrowing fees and interest rates
- Stability pool deposits
- Redemption volumes
- Metrics Calculated:
- Swap volumes by pool
- Price impact analysis
- Trading fee revenue
- Token pair liquidity
- Slippage metrics
- Metrics Calculated:
- Purchase volumes by product
- Donation amounts
- Transaction fees (gas costs)
- User participation rates
All processed data is stored in BigQuery with the following structure:
mezo-portal-data/
βββ raw_data/ # Raw subgraph and API data
βββ staging/ # Cleaned and normalized data
βββ intermediate/ # Transformed data
βββ marts/ # Aggregated metrics and analytics
class SubgraphClient:
def __init__(self, url, headers)
def fetch_subgraph_data(query, method) -> List[Dict]
def get_subgraph_data(subgraph_url, query, query_key) -> pd.DataFrame
class Web3Client:
def __init__(self, contract_name: str)
def load_abi() -> Dict
def load_contract() -> Contract
class BigQueryClient:
def __init__(self, key: str = None, project_id: str = None)
def create_dataset(self, dataset_id: str, location: str = "US")
def create_table(self, df: pd.DataFrame, dataset_id: str, table_id: str)
def table_exists(self, dataset_id: str, table_id: str) -> bool
def update_table(self, df: pd.DataFrame, dataset_id: str, table_id: str, id_column: str)
def upsert_table_by_id(self, df: pd.DataFrame, dataset_id: str, table_id: str, id_column: str)
Required environment variables (in .env
file):
# API Keys
COINGECKO_KEY=your_coingecko_api_key
GOOGLE_CLOUD_KEY=google_cloud_key_for_prod_warehouse
GOOGLE_CLOUD_KEY_DEV=google_cloud_key_for_dev_warehouse
# Database Connections
SUPABASE_URL_PROD=your_supabase_url
SUPABASE_KEY_PROD=your_supabase_key
SUPABASE_DATA_URL=your_data_url
SUPABASE_DATA_KEY=your_data_key
# Web3 Configuration
MEZO_RPC_URL=https://mainnet.mezo.public.validationcloud.io/
The processed data feeds into Hex dashboards with the following key metrics:
- Total Value Locked (TVL)
- 24h Trading Volume
- Total Users
- Protocol Revenue
- TVL by Pool (time series)
- Volume/TVL Efficiency Ratio
- Fee APY by Pool
- User Growth Trends
- Collateralization Ratios
- Liquidation Risk Distribution
- Stability Pool Coverage
- Bad Debt Tracking
# Clone the repository
git clone https://github.com/your-org/mezo-analytics.git
cd mezo-analytics
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
cp .env.example .env
# Edit .env with your credentials
# Process pool data
python scripts/process_pools_data.py
# Process MUSD loan and token
python scripts/process_musd_data.py
# Process swap data
python scripts/process_swaps_data.py
# Process bridging data (native)
python scripts/process_bridge_data.py
# Process vaulting data
python scripts/process_vaults_data
# Process ecosystem dapp data
python scripts/process_dapp_data
# Process MUSD market data
python scripts/process_market_data
Uses Github Actions to run updates periodically:
schedule:
# Run every 6 hours (at 00:00, 06:00, 12:00, 18:00 UTC)
- cron: '0 */6 * * *'
- Null value checking with fallback defaults
- Retry logic for API calls with exponential backoff
- Data validation before uploads
- Comprehensive logging of processing steps
- Deduplication by transaction hash
- Timestamp normalization to UTC
- Currency formatting consistency
- Cross-validation between data sources