-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_pools_and_gauges.py
124 lines (101 loc) · 3.97 KB
/
gen_pools_and_gauges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import json
import pandas as pd
from bal_tools import BalPoolsGauges
def process_query_pools(result) -> dict:
flattened_result = []
for pool_data in result:
flattened_result.append(
{"address": pool_data.address, "symbol": pool_data.symbol}
)
df = pd.DataFrame(flattened_result)
if len(df) == 0:
return
# assert no duplicate addresses exist
assert len(df["address"].unique()) == len(df)
# solve issue of duplicate gauge symbols
df["symbol"] = df["symbol"] + "-" + df["address"].str[2:6]
# confirm no duplicate symbols exist, raise if so
if len(df["symbol"].unique()) != len(df):
print("Found duplicate symbols!")
print(df[df["symbol"].duplicated(keep=False)].sort_values("symbol"))
raise
return df.sort_values("address").set_index("symbol")["address"].to_dict()
def process_query_gauges(result) -> dict:
df = pd.DataFrame(result)
if len(df) == 0:
return
# assert no duplicate addresses exist
assert len(df["address"].unique()) == len(df)
# solve issue of duplicate gauge symbols
df["symbol"] = df["symbol"] + "-" + df["address"].str[2:6]
# confirm no duplicate symbols exist, raise if so
if len(df["symbol"].unique()) != len(df):
print("Found duplicate symbols!")
print(df[df["symbol"].duplicated(keep=False)].sort_values("symbol"))
raise
return df.sort_values("address").set_index("symbol")["address"].to_dict()
def process_query_root_gauges(result, gauges) -> dict:
# map to child gauges
df = []
for root_gauge in result:
for chain in gauges:
for symbol, gauge in gauges[chain].items():
if "chain" not in root_gauge:
# mainnet root gauge == child gauge
continue
if root_gauge["recipient"] == gauge:
root_gauge["symbol"] = symbol[:-4].replace(
"-gauge-", f"-{root_gauge['chain'].lower()}-root-"
)
root_gauge["symbol"] += f"{root_gauge['id'][2:6]}"
df.append(root_gauge)
if len(df) == 0:
return
df = pd.DataFrame(df)
# drop duplicates
df = df[~df.duplicated()]
# assert no duplicate addresses exist
assert len(df["id"].unique()) == len(df)
# confirm no duplicate symbols exist, raise if so
if len(df["symbol"].unique()) != len(df):
print("Found duplicate symbols!")
print(df[df["symbol"].duplicated(keep=False)].sort_values("symbol"))
raise
return df.set_index("symbol")["id"].to_dict()
def main():
pools = {}
gauges = {}
root_gauges = {}
with open("extras/chains.json", "r") as f:
chains = json.load(f)
# adding optimism because balancer has root gauges there that should be included
for chain in chains["BALANCER_PRODUCTION_CHAINS"] + ["optimism"]:
print(f"Generating pools and gauges for {chain}...")
pool_gauge_info = BalPoolsGauges(chain)
# pools
result = process_query_pools(pool_gauge_info.query_all_pools())
if result:
pools[chain] = result
# gauges
result = process_query_gauges(pool_gauge_info.query_all_gauges())
if result:
gauges[chain] = result
# cache mainnet BalPoolsGauges
if chain == "mainnet":
gauge_info_mainnet = pool_gauge_info
# root gauges; only on mainnet
result = process_query_root_gauges(gauge_info_mainnet.query_root_gauges(), gauges)
if result:
root_gauges["mainnet"] = result
# dump all collected dicts to json files
with open(f"outputs/pools.json", "w") as f:
json.dump(pools, f, indent=2)
f.write("\n")
with open("outputs/gauges.json", "w") as f:
json.dump(gauges, f, indent=2)
f.write("\n")
with open("outputs/root_gauges.json", "w") as f:
json.dump(root_gauges, f, indent=2)
f.write("\n")
if __name__ == "__main__":
main()