-
Notifications
You must be signed in to change notification settings - Fork 6
/
abi.py
68 lines (49 loc) · 2.32 KB
/
abi.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
import os
import json
from pathlib import Path
from typing import List, Dict
DEFAULT_EXPORT_FOLDER="crytic-export"
DEFAULT_DIR=Path(DEFAULT_EXPORT_FOLDER)
def get_functions() -> (List, Dict):
functions = {}
contract_set = set()
for artifact in DEFAULT_DIR.glob("*.json"):
with open(artifact.resolve().as_posix()) as crytic_out:
out_info = json.load(crytic_out)
c_units = list(out_info["compilation_units"].keys())
for unit in c_units:
contracts = out_info["compilation_units"][unit]["contracts"][unit]
contract_names = list(contracts.keys())
for contract in contracts:
contract_set.add(contract)
functions[contract] = [data for data in contracts[contract]["abi"] if data["type"] == "function" and data["stateMutability"] != "view"]
# If the internalType of an input starts with `contract` we should save it,
# and look for it in the other abis, then deduce which functions are available to us
return (contract_names, functions)
def get_abi_and_bytecode():
abi = {}
bytecode = {}
contract_set = set()
for artifact in DEFAULT_DIR.glob("*.json"):
with open(artifact.resolve().as_posix()) as crytic_out:
out_info = json.load(crytic_out)
c_units = list(out_info["compilation_units"].keys())
for unit in c_units:
contracts = out_info["compilation_units"][unit]["contracts"][unit]
contract_names = list(contracts.keys())
for contract in contracts:
contract_set.add(contract)
abi[contract] = contracts[contract]["abi"]
bytecode[contract] = contracts[contract]["bin"]
return (abi, bytecode)
def get_abi_by_name(contract_name):
abi = {}
for artifact in DEFAULT_DIR.glob("*.json"):
with open(artifact.resolve().as_posix()) as crytic_out:
out_info = json.load(crytic_out)
c_units = list(out_info["compilation_units"].keys())
for unit in c_units:
contracts = out_info["compilation_units"][unit]["contracts"][unit]
return contracts[contract_name]["abi"]
if __name__ == "__main__":
print(get_abi_and_bytecode())