This script fetches information about validators on the Polygon network and their corresponding checkpoints within a specified range. It highlights the latest checkpoints for each validator, displaying information such as checkpoint ID, timestamp, validator name, and total staked amount. To use it, you need to run the script in a Python environment and ensure that necessary libraries, such as requests, are installed. Also, make sure you have permission to access the APIs mentioned in the script.
import requestsfrom datetime import datetime# Desired start and end checkpoint IDs start_checkpoint_id = 54360end_checkpoint_id = 54371validator_api_url = "https://staking-api.polygon.technology/api/v2/validators?limit=0&offset=1"last_checkpoints = {}validator_response = requests.get(validator_api_url)if validator_response.status_code == 200:validator_data = validator_response.json() proposer_info = {validator["signer"]: {"name": validator["name"], "totalStaked": validator["totalStaked"]} for validator in validator_data["result"]}
for checkpoint_id in range(start_checkpoint_id, end_checkpoint_id + 1):api_url = f"https://heimdall-api.polygon.technology/checkpoints/{checkpoint_id}"response = requests.get(api_url)if response.status_code == 200:json_data = response.json()proposer = json_data["result"]["proposer"]if proposer in last_checkpoints:if last_checkpoints[proposer]["timestamp"] < json_data["result"]["timestamp"]:last_checkpoints[proposer] = {"id":json_data["result"]["id"],"timestamp":json_data["result"]["timestamp"],"name":proposer_info.get(proposer, {}).get("name", "Name not found"),"totalStaked":proposer_info.get(proposer, {}).get("totalStaked", "TotalStaked not found"), }else:last_checkpoints[proposer] = {"id":json_data["result"]["id"],"timestamp":json_data["result"]["timestamp"],"name":proposer_info.get(proposer, {}).get("name", "Name not found"),"totalStaked":proposer_info.get(proposer, {}).get("totalStaked", "TotalStaked not found"), }# Print the final resultsfor proposer, info in last_checkpoints.items():checkpoint_id = info["id"]timestamp = info["timestamp"]name = info["name"]total_staked = int(float(info["totalStaked"]))datetime_obj = datetime.utcfromtimestamp(timestamp)formatted_date = datetime_obj.strftime('%Y-%m-%d %H:%M:%S UTC')formatted_total_staked = str(total_staked)[:-18]formatted_total_staked += " MATIC"print(f"Proposer: {proposer}") print(f"ID: {checkpoint_id}") print(f"Timestamp: {formatted_date}") print(f"Validator Name: {name}") print(f"Total Staked: {formatted_total_staked}") print("---")
Python and Requests Installation:
Make sure you have Python installed on your system. Open the terminal or command prompt. Execute the following command to install the requests library:
pipinstallrequests
Script Execution:
Save the Python script (Your_script.py) in the directory of your choice. Place a file named "addresses.txt" in the same directory as the script, containing the addresses of the validators.
Run the Script:
In the terminal or command prompt, navigate to the directory where the script is located. Execute the script using the following command:
python3your_script.py
Results:
The script will process the last checkpoint of each validator on a Polygon network (formerly Matic).