Skip to content

Commit 2f70d24

Browse files
committed
add balances.py
1 parent 86ca085 commit 2f70d24

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

avgbalances/balances.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests
2+
3+
# Constants
4+
URL = "https://sp-api.dappnode.io/memory/validators"
5+
GWEI_IN_ETH = 1e9
6+
EFFECTIVE_BALANCE_THRESHOLD_GWEI = 32 * GWEI_IN_ETH
7+
OUTPUT_FILE = "results.txt"
8+
9+
def fetch_validator_data():
10+
response = requests.get(URL)
11+
response.raise_for_status()
12+
return response.json()
13+
14+
def analyze_validators(validators):
15+
# Count how many have effective balance > 32 ETH
16+
more_than_32_count = sum(
17+
int(v["beacon_effective_balance_gwei"]) > EFFECTIVE_BALANCE_THRESHOLD_GWEI
18+
for v in validators
19+
)
20+
21+
# Sort by effective balance in descending order
22+
sorted_validators = sorted(
23+
validators,
24+
key=lambda v: int(v["beacon_effective_balance_gwei"]),
25+
reverse=True
26+
)
27+
28+
top_10 = sorted_validators[:10]
29+
return more_than_32_count, top_10
30+
31+
def save_results(more_than_32_count, top_10):
32+
with open(OUTPUT_FILE, "w") as f:
33+
f.write(f"Validators with more than 32 ETH effective balance: {more_than_32_count}\n\n")
34+
f.write("Top 10 Validators by Effective Balance (in ETH):\n")
35+
for i, v in enumerate(top_10, 1):
36+
balance_eth = int(v["beacon_effective_balance_gwei"]) / GWEI_IN_ETH
37+
f.write(f"{i}. Validator Index: {v['validator_index']} - {balance_eth:.4f} ETH\n")
38+
39+
def main():
40+
validators = fetch_validator_data()
41+
more_than_32_count, top_10 = analyze_validators(validators)
42+
save_results(more_than_32_count, top_10)
43+
print(f"Results saved to {OUTPUT_FILE}")
44+
45+
if __name__ == "__main__":
46+
main()
421 KB
Loading

0 commit comments

Comments
 (0)