This Python script fetches and tracks wallet balances for LINK and stLINK tokens, including staked and queued tokens in a priority pool, on the Ethereum mainnet. It retrieves data at specific blocks, including transaction blocks, and reward update blocks, and can output results in a human-readable format or as CSV. Also optional Google Sheets integration.
- Wallet Balance Tracking: Retrieves LINK and stLINK token balances for a specified wallet.
- Priority Pool Data: Fetches staked (lsd_tokens) and queued (queued_tokens) amounts from a staking contract.
- Reward Calculation: Calculates rewards for reward update blocks based on changes in staked and queued tokens.
- Block Selection:
- Token transfer transactions between the wallet and the staking contract.
- Weekly snapshots every Monday at 13:00 UTC.
- Reward update transactions on the rebase controller contract.
- Output Options: Supports human-readable output or CSV format for data analysis.
- IPFS Integration: Retrieves distribution and shares data from IPFS using the staking contract's IPFS hash.
- Etherscan API: Queries token transfers and reward update transactions.
- Configurable Dates: Allows specifying a start date for data retrieval (defaults to 2023-10-19).
- Python 3.8+
- Dependencies (install via
pip install -r requirements.txt):web3requestsbase58decimalpytz
- Ethereum RPC Endpoint: An Ethereum mainnet RPC URL (e.g., Alchemy).
- Etherscan API Key: For querying transaction data.
- Environment Variables (optional, override defaults in code):
RPC_URL: Ethereum RPC endpoint URL.ETHERSCAN_API_KEY: Etherscan API key.USER_WALLET_ADDRESS: Wallet address to track.
-
Clone the Repository:
git clone <repository-url> cd <repository-directory>
-
Install Dependencies:
pip install -r requirements.txt
-
Configure Environment Variables : Or create a stLink.sh script (see stLink.sh_EXAMPLE):
export RPC_URL="https://eth-mainnet.g.alchemy.com/v2/your-api-key" export ETHERSCAN_API_KEY="your-etherscan-api-key" export USER_WALLET_ADDRESS="0xYourWalletAddress" python stLink.py
Run the script with optional arguments:
python script.py [--datefrom YYYY-MM-DD] [--csv]--datefrom: Start date for data retrieval (format: YYYY-MM-DD, default: 2023-10-19).--csv: Output results as CSV to stdout and suppress other output.
-
Default Run (human-readable output, starts from 2023-10-19):
python script.py
-
Custom Start Date:
python script.py --datefrom 2024-01-01
-
CSV Output:
python script.py --csv > output.csv
-
Human-Readable: Displays balances and rewards for each block, including:
- Block number and date.
- Wallet balances (stLINK, LINK).
- Priority pool data (stLINK, queued LINK).
- Rewards for reward blocks.
-
CSV: Columns:
block_date,block,type,stlink_balance,link_balance,lsd_tokens,queued_tokens,reward_share.
- Contracts:
- Staking Contract:
0xDdC796a66E8b83d0BcCD97dF33A6CcFBA8fd60eA - LINK Token:
0x514910771AF9Ca656af840dff83E8264EcF986CA - stLINK Token:
0xb8b295df2cd735b15BE5Eb419517Aa626fc43cD5 - Rebase Controller:
0x1711e93eec78ba83D38C26f0fF284eB478bdbec4
- Staking Contract:
- Default Start Block: 18385225 (corresponding to 2023-10-19).
- Snapshot Time: 13:00:00 UTC every Monday (extra hour in case it's late).
- Etherscan API: Used for token transfers and reward updates.
- IPFS Gateway:
https://ipfs.io/ipfs/for fetching distribution data.
- Ensure your RPC endpoint and Etherscan API key are valid and have sufficient quotas.
- The script caches block timestamps to optimize performance.
- Errors (e.g., missing IPFS data, contract call failures) are logged unless
--csvis used. - The script stops processing after February 24, 2025, for Monday snapshots.
- Reward calculations assume previous block data is available for comparison.
- Connection Errors: Verify
RPC_URLand internet connectivity. - Etherscan API Errors: Check
ETHERSCAN_API_KEYand API rate limits. - IPFS Errors: Ensure the IPFS gateway is accessible and the contract's IPFS hash is valid.
- No Transactions Found: Confirm
USER_WALLET_ADDRESSandSTAKE_CONTRACT_ADDRESSare correct.
To allow the application to automatically update a Google Sheet, you must authorize it using a Google Cloud Service Account. This is a one-time setup process.
- Go to the Google Cloud Console.
- If you don't have a project, create a New Project. Give it a descriptive name (e.g.,
My App Integrations). - Once in your project, go to the APIs & Services dashboard.
- Click + ENABLE APIS AND SERVICES.
- Search for and enable the following two APIs:
- Google Sheets API
- Google Drive API (this is required for finding sheets by name/ID)
A service account is a special type of Google account intended to represent a non-human user that needs to authenticate and be authorized to access data in Google APIs.
- In the APIs & Services section, navigate to Credentials.
- Click + CREATE CREDENTIALS and select Service account.
- Fill in the details:
- Service account name: A short name (e.g.,
google-sheets-updater). - Service account ID: This will be automatically generated.
- Description: A clear description (e.g., "Service account to update project data in Google Sheets").
- Service account name: A short name (e.g.,
- Click CREATE AND CONTINUE.
- Grant access (Permissions): In the "Grant this service account access to project" step, select the Editor role for simplicity. This provides sufficient permissions. Click CONTINUE, then DONE.
You need a private key file for your application to authenticate as the service account.
- On the Credentials page, find the service account you just created in the "Service Accounts" list and click on it.
- Go to the KEYS tab.
- Click ADD KEY -> Create new key.
- Select JSON as the key type and click CREATE.
- A JSON file will be downloaded to your computer. This file is a secret credential—treat it like a password!
- Move this file into your project directory and rename it to
service-account-key.json(or another name of your choice). Do not commit this file to public Git repositories. Add it to your.gitignorefile.
Finally, you must give your new service account permission to edit the specific Google Sheet you want to update.
- Open your downloaded
service-account-key.jsonfile in a text editor. - Find the
client_emailaddress. It will look something like[email protected]. Copy this email address. - Open the target Google Sheet.
- Click the Share button in the top-right corner.
- Paste the
client_emailinto the "Add people and groups" field. - Ensure it is given Editor permissions.
- Click Send. You do not need to check the "Notify people" box.
Your setup is now complete. The application can use the JSON key to securely access and update the shared Google Sheet.
- Edit stLink.sh
- Add URL of your spreadsheet.
MIT License
Contributions are welcome! Please submit issues or pull requests for bug fixes or enhancements.