-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_pools_and_gauges.py
144 lines (119 loc) · 4.39 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import json
import pandas as pd
import requests
from bal_tools import BalPoolsGauges
from bal_tools import Subgraph
def query_swap_enabled_pools(chain, skip=0, step_size=100) -> list:
url = Subgraph(chain).get_subgraph_url("core")
query = f"""{{
pools(
skip: {skip}
first: {step_size}
where: {{swapEnabled: true}}
) {{
address
symbol
}}
}}"""
r = requests.post(url, json={"query": query})
r.raise_for_status()
try:
result = r.json()["data"]["pools"]
except KeyError:
result = []
if len(result) > 0:
# didnt reach end of results yet, collect next page
result += query_swap_enabled_pools(chain, skip + step_size, step_size)
return result
def process_query_swap_enabled_pools(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.set_index("symbol")["address"].to_dict()
def process_query_preferential_gauges(result) -> dict:
df = pd.DataFrame(result)
if len(df) == 0:
return
# assert no duplicate addresses exist
assert len(df["id"].unique()) == len(df)
# solve issue of duplicate gauge symbols
df["symbol"] = df["symbol"] + "-" + df["id"].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.set_index("symbol")["id"].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)
for chain in chains["BALANCER_PRODUCTION_CHAINS"]:
print(f"Generating pools and gauges for {chain}...")
gauge_info = BalPoolsGauges(chain)
# pools
# TODO: consider moving to query object??
result = process_query_swap_enabled_pools(query_swap_enabled_pools(chain))
if result:
pools[chain] = result
# gauges
result = process_query_preferential_gauges(
gauge_info.query_preferential_gauges()
)
if result:
gauges[chain] = result
# cache mainnet BalPoolsGauges
if chain == "mainnet":
gauge_info_mainnet = 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)
with open("outputs/gauges.json", "w") as f:
json.dump(gauges, f, indent=2)
with open("outputs/root_gauges.json", "w") as f:
json.dump(root_gauges, f, indent=2)
if __name__ == "__main__":
main()