File size: 10,338 Bytes
88d2f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Deploy the **v2** ReputationRegistry contract to Arc testnet (W14-CONTRACT-PREP).

The v2 contract fixes two issues in the deployed v1 (`REPUTATION_REGISTRY_ADDRESS`):

  * Ξ²: ``_fillSignal`` now rescales 6-decimal USDC base units to 1e18 fixed-point
    before the ln() input, so ``fillSignal`` actually spans [0.5, 2.0] instead of
    collapsing to ``FILL_MIN=0.5`` for any realistic fee.
  * Ξ±: initial score on first touch is ``HALF=0.5e18`` instead of ``ONE=1e18`` so
    the first ``_recompute`` does not strictly decrease the score from a maxed-out
    prior.

Idempotency
-----------
By default the script **does not deploy** β€” it only prints what *would* happen.
Pass ``--confirm`` to actually broadcast the deployment transaction.

If ``REPUTATION_REGISTRY_V2_ADDRESS`` is set in the environment and the
contract exists at that address (non-empty bytecode), the script exits early β€”
the v2 contract is considered already deployed and the script is a no-op.

Authorization
-------------
After deployment, the script calls ``setAuthorized(operator, true)`` for the
operator EOA so the orchestrator can immediately push state. To wire the
downstream contracts (``TranslationAuction`` / ``BuilderFeeRouter`` /
``JudgePanel``), pass their addresses via the matching env vars (or CLI flags)
and the script will authorize each in turn.

Environment
-----------
* ``HACKATHON_WALLET_PRIVATE_KEY`` β€” operator private key (required)
* ``ARC_TESTNET_RPC``              β€” RPC URL (default https://rpc.testnet.arc.network)
* ``ARC_CHAIN_ID``                 β€” chain id (default 5042002)
* ``REPUTATION_REGISTRY_V2_ADDRESS`` β€” set to skip deploy (idempotency)
* ``TRANSLATION_AUCTION_ADDRESS``    β€” optional: authorize after deploy
* ``BUILDER_FEE_ROUTER_ADDRESS``     β€” optional: authorize after deploy
* ``JUDGE_PANEL_ADDRESS``            β€” optional: authorize after deploy

Usage::

    # dry-run (default)
    .venv/bin/python scripts/deploy_reputation_registry_v2.py

    # actually deploy
    .venv/bin/python scripts/deploy_reputation_registry_v2.py --confirm

    # deploy + explicit downstream authorizations
    .venv/bin/python scripts/deploy_reputation_registry_v2.py --confirm \\
        --authorize-auction 0x... --authorize-router 0x... --authorize-judge 0x...
"""

from __future__ import annotations

import argparse
import json
import os
import sys
import time
from pathlib import Path

from eth_account import Account
from web3 import Web3


REPO_ROOT = Path(__file__).resolve().parents[1]
FOUNDRY_OUT = REPO_ROOT / "contracts" / "out"
ARTIFACT_NAME = "ReputationRegistry"


def load_artifact(name: str) -> dict:
    """Load a Foundry artifact (ABI + bytecode) from contracts/out/."""
    path = FOUNDRY_OUT / f"{name}.sol" / f"{name}.json"
    if not path.exists():
        raise FileNotFoundError(
            f"Foundry artifact not found at {path}. Run `cd contracts && forge build` first."
        )
    with path.open("r", encoding="utf-8") as fh:
        art = json.load(fh)
    return {"abi": art["abi"], "bytecode": art["bytecode"]["object"]}


def wait_receipt(w3: Web3, tx_hash: bytes, timeout: int = 180) -> dict:
    receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=timeout)
    if receipt.status != 1:
        raise RuntimeError(f"tx failed: {tx_hash.hex()}")
    return receipt


def send_signed(w3: Web3, txn: dict, account) -> dict:
    signed = w3.eth.account.sign_transaction(txn, account.key)
    raw = getattr(signed, "raw_transaction", None) or signed.rawTransaction
    tx_hash = w3.eth.send_raw_transaction(raw)
    return wait_receipt(w3, tx_hash)


def deploy_registry(w3: Web3, account, chain_id: int) -> tuple[str, int]:
    art = load_artifact(ARTIFACT_NAME)
    contract = w3.eth.contract(abi=art["abi"], bytecode=art["bytecode"])
    nonce = w3.eth.get_transaction_count(account.address)
    txn = contract.constructor().build_transaction(
        {
            "from": account.address,
            "nonce": nonce,
            "chainId": chain_id,
            "gasPrice": w3.eth.gas_price,
            "gas": 4_500_000,
        }
    )
    print(f"  -> deploying {ARTIFACT_NAME} v2 ...")
    receipt = send_signed(w3, txn, account)
    addr = receipt.contractAddress
    print(f"     deployed at {addr} (gas {receipt.gasUsed})")
    return addr, receipt.gasUsed


def authorize(
    w3: Web3, account, chain_id: int, rep_address: str, rep_abi: list, who: str, label: str
) -> int:
    contract = w3.eth.contract(address=Web3.to_checksum_address(rep_address), abi=rep_abi)
    nonce = w3.eth.get_transaction_count(account.address)
    txn = contract.functions.setAuthorized(
        Web3.to_checksum_address(who), True
    ).build_transaction(
        {
            "from": account.address,
            "nonce": nonce,
            "chainId": chain_id,
            "gasPrice": w3.eth.gas_price,
            "gas": 120_000,
        }
    )
    print(f"  -> setAuthorized({label} = {who}, true) ...")
    receipt = send_signed(w3, txn, account)
    print(f"     authorized (gas {receipt.gasUsed})")
    return receipt.gasUsed


def already_deployed(w3: Web3, address: str | None) -> bool:
    """Return True if `address` is non-empty and contains contract code."""
    if not address:
        return False
    try:
        addr = Web3.to_checksum_address(address)
    except (ValueError, TypeError):
        return False
    code = w3.eth.get_code(addr)
    return code is not None and len(code) > 0 and code != b"\x00"


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Deploy v2 ReputationRegistry (Ξ² + Ξ± fix). Dry-run unless --confirm."
    )
    parser.add_argument(
        "--confirm",
        action="store_true",
        help="Actually broadcast deployment + authorization transactions. "
             "Without this flag the script is a dry-run.",
    )
    parser.add_argument(
        "--authorize-auction",
        default=os.environ.get("TRANSLATION_AUCTION_ADDRESS"),
        help="Address of TranslationAuction to authorize post-deploy.",
    )
    parser.add_argument(
        "--authorize-router",
        default=os.environ.get("BUILDER_FEE_ROUTER_ADDRESS"),
        help="Address of BuilderFeeRouter to authorize post-deploy.",
    )
    parser.add_argument(
        "--authorize-judge",
        default=os.environ.get("JUDGE_PANEL_ADDRESS"),
        help="Address of JudgePanel to authorize post-deploy.",
    )
    args = parser.parse_args()

    pk = os.environ.get("HACKATHON_WALLET_PRIVATE_KEY")
    if not pk:
        print("ERROR: HACKATHON_WALLET_PRIVATE_KEY is required", file=sys.stderr)
        return 2
    rpc_url = os.environ.get("ARC_TESTNET_RPC", "https://rpc.testnet.arc.network")
    chain_id = int(os.environ.get("ARC_CHAIN_ID", "5042002"))
    existing_v2 = os.environ.get("REPUTATION_REGISTRY_V2_ADDRESS")

    w3 = Web3(Web3.HTTPProvider(rpc_url))
    if not w3.is_connected():
        print(f"ERROR: cannot reach Arc RPC at {rpc_url}", file=sys.stderr)
        return 3

    account = Account.from_key(pk)
    print(f"deployer:        {account.address}")
    print(f"chain id:        {chain_id}")
    print(f"RPC:             {rpc_url}")
    print(f"existing v2 env: {existing_v2 or '(unset)'}")
    print(
        f"balance:         {w3.from_wei(w3.eth.get_balance(account.address), 'ether')} ETH-equivalent"
    )
    print()

    # Idempotency check
    if already_deployed(w3, existing_v2):
        print(f"v2 already deployed at {existing_v2} β€” no action taken (idempotent).")
        return 0

    if not args.confirm:
        print("DRY-RUN β€” would do the following (re-run with --confirm to broadcast):")
        print(f"  1. deploy ReputationRegistry (constructor takes no args)")
        print(f"  2. setAuthorized({account.address}, true)  # operator EOA")
        for label, addr in (
            ("TranslationAuction", args.authorize_auction),
            ("BuilderFeeRouter", args.authorize_router),
            ("JudgePanel", args.authorize_judge),
        ):
            if addr:
                print(f"  3. setAuthorized({addr}, true)  # {label}")
        return 0

    # 1. Deploy
    rep_addr, deploy_gas = deploy_registry(w3, account, chain_id)

    # 2. Authorize operator (constructor already does this β€” deployer is auto-authorized,
    #    but we re-issue defensively in case the wallet differs from msg.sender).
    rep_art = load_artifact(ARTIFACT_NAME)
    auth_gas = {}

    # The deployer is the operator EOA and is already authorized by the constructor,
    # so we skip a redundant setAuthorized(operator) call. But we still authorize
    # any downstream contracts the user passed in.
    for label, addr in (
        ("TranslationAuction", args.authorize_auction),
        ("BuilderFeeRouter", args.authorize_router),
        ("JudgePanel", args.authorize_judge),
    ):
        if addr:
            auth_gas[f"auth({label})"] = authorize(
                w3, account, chain_id, rep_addr, rep_art["abi"], addr, label
            )

    final_balance = w3.from_wei(w3.eth.get_balance(account.address), "ether")

    print()
    print("=== Deployment summary ===")
    print(f"  ReputationRegistry (v2): {rep_addr}  (deploy gas {deploy_gas})")
    for label, g in auth_gas.items():
        print(f"  {label}: {g}")
    print(f"  final balance:           {final_balance} ETH-equivalent")
    print()
    print("=== Suggested .env update ===")
    print(f"REPUTATION_REGISTRY_V2_ADDRESS={rep_addr}")
    print(
        "  # Once verified, update REPUTATION_REGISTRY_ADDRESS to point at this "
        "address and remove the V2 suffix."
    )

    # Dump a JSON artifact alongside the existing deployment_v2.json
    out_path = REPO_ROOT / "outputs" / "deployment_reputation_v2.json"
    out_path.parent.mkdir(parents=True, exist_ok=True)
    out_path.write_text(
        json.dumps(
            {
                "chain_id": chain_id,
                "deployer": account.address,
                "deployed_at": int(time.time()),
                "address": rep_addr,
                "deploy_gas": deploy_gas,
                "auth_gas": auth_gas,
                "notes": "W14-CONTRACT-PREP Ξ² + Ξ± fix",
            },
            indent=2,
        )
    )
    print(f"wrote {out_path}")
    return 0


if __name__ == "__main__":
    sys.exit(main())