-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (64 loc) · 2.1 KB
/
main.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
import json
import subprocess
import sys
from wake.deployment import Address, chain, print
from env import EnvNotSet, getenv
from ipfs import GW3, PublicIPFS
from pytypes.contracts.ICSFeeDistributor import ICSFeeDistributor
from tree import CSMRewardTree
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
@chain.connect(getenv("RPC_URL"))
def main():
distributor = ICSFeeDistributor(getenv("DISTRIBUTOR_ADDRESS"))
chain.default_call_account = Address(0)
root = distributor.treeRoot()
cid = distributor.treeCid()
print(f"root={root.hex()} {cid=}")
if not cid:
print("No tree CID stored so far")
sys.exit(EXIT_SUCCESS)
try:
ipfs = GW3(
getenv("GW3_ACCESS_KEY"),
getenv("GW3_SECRET_KEY"),
)
except EnvNotSet:
ipfs = PublicIPFS()
tree = CSMRewardTree.load(json.loads(ipfs.fetch(cid)))
if tree.root != root:
print(f"tree.root={tree.root.hex()}")
print("error: Roots mismatch")
sys.exit(EXIT_FAILURE)
dump = tree.dump()
with open("tree.json", "w", encoding="utf-8") as fp:
json.dump(dump, fp, indent=2, default=default)
proofs = {
f"CSM Operator {v["value"][0]}": {
"cumulativeFeeShares": v["value"][1],
"proof": list(tree.get_proof(v["treeIndex"])),
}
for v in dump["values"]
}
with open("proofs.json", "w", encoding="utf-8") as fp:
json.dump(proofs, fp, indent=2, default=default)
# XXX: CI-only stuff below.
try:
github_output = getenv("GITHUB_OUTPUT")
except EnvNotSet:
sys.exit(EXIT_SUCCESS)
git_diff = subprocess.run(["git", "diff", "--exit-code", "--quiet", "tree.json"])
with open(github_output, "a", encoding="utf-8") as fp:
fp.write(
"\n".join(
[
"",
f"cid={cid}",
f"updated={git_diff.returncode != 0}",
]
)
)
def default(o):
if isinstance(o, bytes):
return f"0x{o.hex()}"
raise ValueError(f"Unexpected type for json encoding, got {repr(o)}")