This project implements a risk scoring system for DeFi wallets interacting with the Compound V2 and V3 protocols. It is an extension of my existing Risk Scoring system: ScoreFi. The current system analyzes live on-chain transaction patterns, behavioral indicators, and risk management practices to assign risk scores between 0-1000, where higher scores indicate higher risk.
For a detailed explanation of the data collection, feature selection, scoring method, and justification of risk indicators, please see approach.md.
- Data Collection: The script fetches transaction data for each wallet address from the Etherscan API, filtering for interactions with Compound protocol contracts.
- Feature Engineering: For each wallet, features are engineered to capture transaction types, volume, activity patterns, portfolio diversity, and behavioral ratios relevant to risk.
- Risk Scoring: Features are grouped into five risk categories (liquidity, activity, volatility, diversification, behavioral). Each category is weighted based on data-driven analysis, and a final risk score (0-1000) is computed for each wallet.
- Output: Results are saved as a CSV file with columns
wallet_idandscore. Model artifacts (scaler and weights) are saved ascompound_risk_model.joblib.
This project requires access to the Etherscan API (for fetching on-chain transaction data) and Infura (for Ethereum node access if needed). You must set your API keys in the main execution block of compound_risk_scorer.py:
ETHERSCAN_API_KEY = "YOUR_ETHERSCAN_API_KEY"
INFURA_URL = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
Replace the placeholders with your actual API keys before running the script. You can obtain free API keys by registering at Etherscan and Infura.
pip install web3 pandas numpy matplotlib seaborn scikit-learn scipy requests joblib- Place your wallet addresses in a CSV file named
Wallet id - Sheet1.csvin the project directory. The file should contain a column with wallet addresses (e.g.,wallet_id). - Run
compound_risk_scorer.py:python compound_risk_scorer.py
- The script will fetch data, compute features, score wallets, and save results to
enhanced_wallet_scores.csvand model artifacts tocompound_risk_model.joblib.
enhanced_wallet_scores.csv: Wallet-level risk scorescompound_risk_model.joblib: Model scaler and weights
- 0-399: Low risk
- 400-749: Medium risk
- 750-1000: High risk
The following code snippet demonstrates how raw model outputs (if using a machine learning model) can be normalized to a 0-1000 scale:
preds = model.predict(X_scaled)
scores = 1000 * (preds - preds.min()) / (preds.max() - preds.min())
features_df['credit_score'] = scores.astype(int)In this project, the risk scoring logic is implemented directly in the script, but this normalization approach is standard for converting raw scores to a 0-1000 range.
- To use a different wallet CSV, update the filename in the script or rename your file accordingly.
- API keys for Etherscan and Infura are set in the script; update them as needed.
Open source under MIT License.