Created
May 8, 2022 16:45
-
-
Save emo-eth/63239524b75e11bed6f6179f18f30a8e to your computer and use it in GitHub Desktop.
calculate and list all function signatures based on a foundry project's out/ directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import json | |
from typing import List | |
from web3 import Web3 | |
def calculateSelector(sig): | |
result = Web3.keccak(text=sig) | |
selector = (Web3.toHex(result))[:10] | |
return selector | |
def getTypeFromInput(ipt: dict) -> str: | |
components: List[dict] | None = ipt.get('components') | |
if components: | |
return f"({','.join([getTypeFromInput(c) for c in components])})" | |
return ipt['type'] | |
for fname in glob.glob("out/*/*.json"): | |
print(fname) | |
with open(fname, "r") as f: | |
js = json.loads(f.read()) | |
abi = js["abi"] | |
abi = [x for x in abi if x.get('name')] | |
for sig in abi: | |
name = sig['name'] | |
inputs = sig.get('inputs') | |
signature = f"{name}({','.join([getTypeFromInput(x) for x in inputs])})" | |
selector = calculateSelector(signature) | |
print(signature, selector) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment