#!/usr/bin/env python3 """Score one model answer for a DeFiReplay task. Usage: python3 scripts/evaluate.py cat answer.sol | python3 scripts/evaluate.py is the model's completion: either a bare Solidity test-function body, or a ```solidity ...``` fenced block. Extra helper code may be appended after the marker lines `// === ATTACK_AUX_MEMBERS ===` and `// === ATTACK_AUX_CONTRACTS ===` (see the dataset card). Prints JSON: {"incident","score","reason"}. score is 1 (the exploit/sandwich reached the calibrated profit threshold) or 0. Requires Foundry (`forge`) on PATH and a mainnet RPC (ETH_RPC_URL, defaults to a public endpoint). The task's grading oracle lives in tasks//meta.json and is injected by the verifier — it is never model-controlled. """ import json, sys, os from pathlib import Path REPO = Path(__file__).resolve().parent.parent os.environ.setdefault("DEFIREPLAY_ROOT", str(REPO)) sys.path.insert(0, str(REPO / "verifier")) import foundry_verifier as fv # noqa: E402 def main(): if len(sys.argv) < 2: print(__doc__); sys.exit(2) incident = sys.argv[1] if len(sys.argv) >= 3: answer = Path(sys.argv[2]).read_text() else: answer = sys.stdin.read() task_dir = REPO / "tasks" / incident if not (task_dir / "template.sol").exists(): print(json.dumps({"incident": incident, "score": 0, "reason": f"unknown task (no tasks/{incident}/template.sol)"})) sys.exit(2) # tier only changes template selection for the "scratch" variant (not shipped # in v0.1); every v0.1 task uses template.sol, so tier is informational. ref = json.dumps({"incident": incident, "registry": f"tasks/{incident}", "tier": "scaffolded"}) if "```" not in answer: answer = "```solidity\n" + answer + "\n```" score, info = fv.evaluate_foundry(answer, ref) print(json.dumps({"incident": incident, "score": score, "reason": info["reason"][:300]})) if __name__ == "__main__": main()