|
2 | 2 | pragma solidity ^0.8.20; |
3 | 3 |
|
4 | 4 | import "forge-std/Script.sol"; |
| 5 | +import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; |
| 6 | +import "../src/Router.sol"; |
5 | 7 | import "../src/AgentRegistry.sol"; |
6 | 8 | import "../src/AgentLedger.sol"; |
7 | 9 | import "../src/LocationLedger.sol"; |
8 | 10 | import "../src/InboxLedger.sol"; |
| 11 | +import "../src/EvaluationLedger.sol"; |
9 | 12 | import "../src/GameEngine.sol"; |
10 | 13 |
|
| 14 | +/// @notice Upgrade all implementations behind existing proxies. |
| 15 | +/// Only requires ROUTER_ADDRESS - resolves everything else on-chain. |
| 16 | +/// If EvaluationLedger doesn't exist yet, deploys a new proxy for it. |
11 | 17 | contract UpgradeScript is Script { |
12 | 18 | function run() external { |
13 | 19 | uint256 deployerKey = vm.envUint("PRIVATE_KEY"); |
14 | | - address registryProxy = vm.envAddress("AGENT_REGISTRY_ADDRESS"); |
15 | | - address agentLedgerProxy = vm.envAddress("AGENT_LEDGER_ADDRESS"); |
16 | | - address locationLedgerProxy = vm.envAddress("LOCATION_LEDGER_ADDRESS"); |
17 | | - address inboxLedgerProxy = vm.envAddress("INBOX_LEDGER_ADDRESS"); |
18 | | - address engineProxy = vm.envAddress("GAME_ENGINE_ADDRESS"); |
| 20 | + address routerProxy = vm.envAddress("ROUTER_ADDRESS"); |
| 21 | + |
| 22 | + // Read addresses via low-level call to handle both old (5-return) and new (6-return) Router |
| 23 | + address registryProxy; |
| 24 | + address agentLedgerProxy; |
| 25 | + address locationLedgerProxy; |
| 26 | + address inboxLedgerProxy; |
| 27 | + address engineProxy; |
| 28 | + address evalLedgerProxy; |
| 29 | + |
| 30 | + // Try the new 6-return getAddresses first |
| 31 | + (bool ok, bytes memory data) = routerProxy.staticcall(abi.encodeWithSignature("getAddresses()")); |
| 32 | + require(ok, "getAddresses() failed"); |
| 33 | + |
| 34 | + if (data.length >= 192) { |
| 35 | + // New Router: 6 addresses |
| 36 | + (registryProxy, agentLedgerProxy, locationLedgerProxy, inboxLedgerProxy, engineProxy, evalLedgerProxy) |
| 37 | + = abi.decode(data, (address, address, address, address, address, address)); |
| 38 | + } else { |
| 39 | + // Old Router: 5 addresses - EvaluationLedger missing |
| 40 | + (registryProxy, agentLedgerProxy, locationLedgerProxy, inboxLedgerProxy, engineProxy) |
| 41 | + = abi.decode(data, (address, address, address, address, address)); |
| 42 | + } |
| 43 | + |
| 44 | + console.log("Router: ", routerProxy); |
| 45 | + console.log("AgentRegistry: ", registryProxy); |
| 46 | + console.log("AgentLedger: ", agentLedgerProxy); |
| 47 | + console.log("LocationLedger: ", locationLedgerProxy); |
| 48 | + console.log("InboxLedger: ", inboxLedgerProxy); |
| 49 | + console.log("GameEngine: ", engineProxy); |
| 50 | + console.log("EvaluationLedger:", evalLedgerProxy); |
19 | 51 |
|
20 | 52 | vm.startBroadcast(deployerKey); |
21 | 53 |
|
| 54 | + // 1. Upgrade Router first (to add evaluationLedger slot if missing) |
| 55 | + Router(routerProxy).upgradeToAndCall(address(new Router()), ""); |
| 56 | + |
| 57 | + // 2. Deploy EvaluationLedger proxy if it doesn't exist |
| 58 | + if (evalLedgerProxy == address(0)) { |
| 59 | + console.log("EvaluationLedger not found - deploying new proxy..."); |
| 60 | + EvaluationLedger evalImpl = new EvaluationLedger(); |
| 61 | + ERC1967Proxy evalProxy = new ERC1967Proxy( |
| 62 | + address(evalImpl), |
| 63 | + abi.encodeCall(EvaluationLedger.initialize, (registryProxy)) |
| 64 | + ); |
| 65 | + evalLedgerProxy = address(evalProxy); |
| 66 | + Router(routerProxy).setEvaluationLedger(evalLedgerProxy); |
| 67 | + console.log("EvaluationLedger (new):", evalLedgerProxy); |
| 68 | + } else { |
| 69 | + EvaluationLedger(evalLedgerProxy).upgradeToAndCall(address(new EvaluationLedger()), ""); |
| 70 | + } |
| 71 | + |
| 72 | + // 3. Upgrade remaining contracts |
22 | 73 | AgentRegistry(registryProxy).upgradeToAndCall(address(new AgentRegistry()), ""); |
23 | 74 | AgentLedger(agentLedgerProxy).upgradeToAndCall(address(new AgentLedger()), ""); |
24 | 75 | LocationLedger(locationLedgerProxy).upgradeToAndCall(address(new LocationLedger()), ""); |
25 | 76 | InboxLedger(inboxLedgerProxy).upgradeToAndCall(address(new InboxLedger()), ""); |
26 | 77 | GameEngine(engineProxy).upgradeToAndCall(address(new GameEngine()), ""); |
27 | 78 |
|
| 79 | + // 4. Wire up new contracts if needed (setAgentLedger, setEvaluationLedger on GameEngine) |
| 80 | + GameEngine engine = GameEngine(engineProxy); |
| 81 | + // Only set if not already wired |
| 82 | + (bool hasEval,) = engineProxy.staticcall(abi.encodeWithSignature("evaluationLedger()")); |
| 83 | + if (hasEval) { |
| 84 | + (bool okEval, bytes memory evalData) = engineProxy.staticcall(abi.encodeWithSignature("evaluationLedger()")); |
| 85 | + if (okEval && evalData.length >= 32) { |
| 86 | + address currentEval = abi.decode(evalData, (address)); |
| 87 | + if (currentEval == address(0)) { |
| 88 | + engine.setEvaluationLedger(evalLedgerProxy); |
| 89 | + console.log("Wired EvaluationLedger on GameEngine"); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Initialize World Bible if not yet done |
| 95 | + (bool hasWb, bytes memory wbData) = engineProxy.staticcall(abi.encodeWithSignature("worldBibleLocationId()")); |
| 96 | + if (hasWb && wbData.length >= 32) { |
| 97 | + uint256 wbLocId = abi.decode(wbData, (uint256)); |
| 98 | + if (wbLocId == 0) { |
| 99 | + engine.initWorldBible(); |
| 100 | + console.log("Initialized World Bible"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
28 | 104 | vm.stopBroadcast(); |
| 105 | + |
| 106 | + console.log("All contracts upgraded successfully"); |
29 | 107 | } |
30 | 108 | } |
0 commit comments