From 015948bc00647dc2a0a7005c9aa5424fbe454e10 Mon Sep 17 00:00:00 2001 From: tringuyenskymavis Date: Wed, 3 Jul 2024 11:32:25 +0700 Subject: [PATCH] chore: restructure factories scripts --- .../governors-configs.s.sol | 8 +- .../factory-maptoken-mainchain.s.sol | 185 ------------------ .../factory-maptoken-roninchain.s.sol | 171 ---------------- .../factory-maptoken-ronin-mainnet.s.sol | 4 +- 4 files changed, 5 insertions(+), 363 deletions(-) delete mode 100644 script/factories/factory-maptoken-mainchain.s.sol delete mode 100644 script/factories/factory-maptoken-roninchain.s.sol diff --git a/script/20240626-maptoken-anima/governors-configs.s.sol b/script/20240626-maptoken-anima/governors-configs.s.sol index 09877741..04241b3d 100644 --- a/script/20240626-maptoken-anima/governors-configs.s.sol +++ b/script/20240626-maptoken-anima/governors-configs.s.sol @@ -7,10 +7,10 @@ contract Migration__Governors_Config { constructor() { // TODO: replace by address of the testnet governors - governors[3] = 0xd24D87DDc1917165435b306aAC68D99e0F49A3Fa; - governors[2] = 0xb033ba62EC622dC54D0ABFE0254e79692147CA26; - governors[0] = 0x087D08e3ba42e64E3948962dd1371F906D1278b9; - governors[1] = 0x52ec2e6BBcE45AfFF8955Da6410bb13812F4289F; + governors[3] = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266; + governors[2] = 0x90F79bf6EB2c4f870365E785982E1f101E93b906; + governors[0] = 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC; + governors[1] = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8; // TODO: replace by private key of the testnet governors governorPks[3] = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80; governorPks[2] = 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6; diff --git a/script/factories/factory-maptoken-mainchain.s.sol b/script/factories/factory-maptoken-mainchain.s.sol deleted file mode 100644 index 9cc7f660..00000000 --- a/script/factories/factory-maptoken-mainchain.s.sol +++ /dev/null @@ -1,185 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.19; - -import "@ronin/contracts/libraries/Ballot.sol"; -import { console2 } from "forge-std/console2.sol"; -import { StdStyle } from "forge-std/StdStyle.sol"; -import { RoninBridgeManager } from "@ronin/contracts/ronin/gateway/RoninBridgeManager.sol"; -import { IMainchainGatewayV3 } from "@ronin/contracts/interfaces/IMainchainGatewayV3.sol"; -import { GlobalProposal } from "@ronin/contracts/libraries/GlobalProposal.sol"; -import { Proposal } from "@ronin/contracts/libraries/Proposal.sol"; -import { LibTokenInfo, TokenStandard } from "@ronin/contracts/libraries/LibTokenInfo.sol"; -import { Contract } from "../utils/Contract.sol"; -import { Migration } from "../Migration.s.sol"; -import { Network, TNetwork } from "../utils/Network.sol"; -import { IGeneralConfigExtended } from "../interfaces/IGeneralConfigExtended.sol"; -import { SignatureConsumer } from "@ronin/contracts/interfaces/consumers/SignatureConsumer.sol"; -import { MapTokenInfo } from "../libraries/MapTokenInfo.sol"; -import { LibCompanionNetwork } from "script/shared/libraries/LibCompanionNetwork.sol"; -import { MainchainBridgeAdminUtils } from "test/helpers/MainchainBridgeAdminUtils.t.sol"; -import { MainchainBridgeManager } from "@ronin/contracts/mainchain/MainchainBridgeManager.sol"; -import { LibProposal } from "script/shared/libraries/LibProposal.sol"; - -abstract contract Factory__MapTokensMainchain is Migration { - using LibCompanionNetwork for *; - - RoninBridgeManager internal _roninBridgeManager; - address internal _mainchainGatewayV3; - address internal _mainchainBridgeManager; - address internal _governor; - address[] internal _governors; - uint256[] internal _governorPKs; - - function setUp() public override { - super.setUp(); - if (block.chainid == 2020 || block.chainid == 2021) { - _roninBridgeManager = RoninBridgeManager(config.getAddressFromCurrentNetwork(Contract.RoninBridgeManager.key())); - _mainchainGatewayV3 = config.getAddress(network().companionNetwork(), Contract.MainchainGatewayV3.key()); - _mainchainBridgeManager = config.getAddress(network().companionNetwork(), Contract.MainchainBridgeManager.key()); - } - - _governor = _initCaller(); - } - - function run() public virtual { - Proposal.ProposalDetail memory proposal = _createAndVerifyProposal(); - _propose(proposal); - } - - function _initCaller() internal virtual returns (address); - function _initTokenList() internal virtual returns (uint256 totalToken, MapTokenInfo[] memory infos); - - function _propose(Proposal.ProposalDetail memory proposal) internal virtual { - vm.broadcast(_governor); - _roninBridgeManager.propose( - proposal.chainId, proposal.expiryTimestamp, proposal.executor, proposal.targets, proposal.values, proposal.calldatas, proposal.gasAmounts - ); - } - - function _relayProposal(Proposal.ProposalDetail memory proposal) internal { - MainchainBridgeAdminUtils mainchainProposalUtils = - new MainchainBridgeAdminUtils(2021, _governorPKs, MainchainBridgeManager(_mainchainBridgeManager), _governors[0]); - - Ballot.VoteType[] memory supports_ = new Ballot.VoteType[](_governors.length); - require(_governors.length > 0 && _governors.length == _governorPKs.length, "Invalid governors information"); - - for (uint256 i; i < _governors.length; ++i) { - supports_[i] = Ballot.VoteType.For; - } - - SignatureConsumer.Signature[] memory signatures = mainchainProposalUtils.generateSignatures(proposal, _governorPKs); - - uint256 gasAmounts = 1_000_000; - for (uint256 i; i < proposal.gasAmounts.length; ++i) { - gasAmounts += proposal.gasAmounts[i]; - } - - vm.broadcast(_governors[0]); - MainchainBridgeManager(_mainchainBridgeManager).relayProposal{ gas: gasAmounts }(proposal, supports_, signatures); - } - - function _createAndVerifyProposal() internal returns (Proposal.ProposalDetail memory proposal) { - (address[] memory mainchainTokens, address[] memory roninTokens, TokenStandard[] memory standards, uint256[][4] memory thresholds) = - _prepareMapTokensAndThresholds(); - bytes memory innerData = abi.encodeCall(IMainchainGatewayV3.mapTokensAndThresholds, (mainchainTokens, roninTokens, standards, thresholds)); - bytes memory proxyData = abi.encodeWithSignature("functionDelegateCall(bytes)", innerData); - - uint256 expiredTime = block.timestamp + 14 days; - address[] memory targets = new address[](1); - uint256[] memory values = new uint256[](1); - bytes[] memory calldatas = new bytes[](1); - uint256[] memory gasAmounts = new uint256[](1); - - targets[0] = _mainchainGatewayV3; - values[0] = 0; - calldatas[0] = proxyData; - gasAmounts[0] = 1_000_000; - - // Verify gas when call from mainchain. - LibProposal.verifyProposalGasAmount(address(_mainchainBridgeManager), targets, values, calldatas, gasAmounts); - - // // Verify gas when call from ronin. - // (uint256 companionChainId, TNetwork companionNetwork) = network().companionNetworkData(); - // address companionManager = config.getAddress(companionNetwork, Contract.MainchainBridgeManager.key()); - // LibProposal.verifyMainchainProposalGasAmount(companionNetwork, companionManager, targets, values, calldatas, gasAmounts); - - uint256 chainId; - uint256 nonce; - if (block.chainid == 2020 || block.chainid == 2021) { - nonce = 0; - (chainId,) = network().companionNetworkData(); - } else { - nonce = MainchainBridgeManager(_mainchainBridgeManager).round(11155111) + 1; - chainId = block.chainid; - } - - proposal = Proposal.ProposalDetail({ - nonce: nonce, - chainId: chainId, - expiryTimestamp: expiredTime, - executor: address(0), - targets: targets, - values: values, - calldatas: calldatas, - gasAmounts: gasAmounts - }); - } - - function _prepareMapTokensAndThresholds() - internal - returns (address[] memory mainchainTokens, address[] memory roninTokens, TokenStandard[] memory standards, uint256[][4] memory thresholds) - { - // function mapTokensAndThresholds( - // address[] calldata _mainchainTokens, - // address[] calldata _roninTokens, - // TokenStandard.ERC20[] calldata _standards, - // uint256[][4] calldata _thresholds - // ) - (uint256 N, MapTokenInfo[] memory tokenInfos) = _initTokenList(); - - mainchainTokens = new address[](N); - roninTokens = new address[](N); - standards = new TokenStandard[](N); - - thresholds[0] = new uint256[](N); - thresholds[1] = new uint256[](N); - thresholds[2] = new uint256[](N); - thresholds[3] = new uint256[](N); - - for (uint256 i; i < N; ++i) { - mainchainTokens[i] = tokenInfos[i].mainchainToken; - roninTokens[i] = tokenInfos[i].roninToken; - standards[i] = TokenStandard.ERC20; - thresholds[0][i] = tokenInfos[i].highTierThreshold; - thresholds[1][i] = tokenInfos[i].lockedThreshold; - thresholds[2][i] = tokenInfos[i].unlockFeePercentages; - thresholds[3][i] = tokenInfos[i].dailyWithdrawalLimit; - } - } - - function _cheatStorage(address[] memory governors) internal { - bytes32 governorsSlot = keccak256(abi.encode(0xc648703095712c0419b6431ae642c061f0a105ac2d7c3d9604061ef4ebc38300)); - uint256 length = governors.length; - //cheat governors addresses - for (uint256 i; i < length; ++i) { - bytes32 governorSlotId = bytes32(uint256(governorsSlot) + uint256(i)); - vm.store(_mainchainBridgeManager, governorSlotId, bytes32(uint256(uint160(governors[i])))); - } - //after cheat - for (uint256 i; i < length; ++i) { - bytes32 governorSlotId = bytes32(uint256(governorsSlot) + uint256(i)); - bytes32 afterCheatData = vm.load(_mainchainBridgeManager, bytes32(uint256(governorsSlot) + uint256(i))); - - assertEq(afterCheatData, bytes32(uint256(uint160(governors[i])))); - } - - //cheat governors weights - bytes32 governorsWeightSlot = bytes32(uint256(0xc648703095712c0419b6431ae642c061f0a105ac2d7c3d9604061ef4ebc38300) + uint256(2)); - for (uint256 i; i < length; ++i) { - address key = governors[i]; - bytes32 valueSlot = keccak256(abi.encode(key, governorsWeightSlot)); - vm.store(_mainchainBridgeManager, valueSlot, bytes32(uint256(uint96(100)))); - } - bytes32 governorsWeightData = vm.load(_mainchainBridgeManager, keccak256(abi.encode(0x087D08e3ba42e64E3948962dd1371F906D1278b9, governorsWeightSlot))); - } -} diff --git a/script/factories/factory-maptoken-roninchain.s.sol b/script/factories/factory-maptoken-roninchain.s.sol deleted file mode 100644 index 35f92d42..00000000 --- a/script/factories/factory-maptoken-roninchain.s.sol +++ /dev/null @@ -1,171 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.19; - -import { console2 } from "forge-std/console2.sol"; -import { StdStyle } from "forge-std/StdStyle.sol"; - -import { RoninBridgeManager } from "@ronin/contracts/ronin/gateway/RoninBridgeManager.sol"; -import { IRoninGatewayV3 } from "@ronin/contracts/interfaces/IRoninGatewayV3.sol"; -import { MinimumWithdrawal } from "@ronin/contracts/extensions/MinimumWithdrawal.sol"; -import { LibTokenInfo, TokenStandard } from "@ronin/contracts/libraries/LibTokenInfo.sol"; -import { Ballot } from "@ronin/contracts/libraries/Ballot.sol"; -import { GlobalProposal } from "@ronin/contracts/libraries/GlobalProposal.sol"; -import { Proposal } from "@ronin/contracts/libraries/Proposal.sol"; -import { Contract } from "../utils/Contract.sol"; -import { Migration } from "../Migration.s.sol"; -import { Network } from "../utils/Network.sol"; -import { Contract } from "../utils/Contract.sol"; -import { IGeneralConfigExtended } from "../interfaces/IGeneralConfigExtended.sol"; -import { LibProposal } from "script/shared/libraries/LibProposal.sol"; -import { MapTokenInfo } from "../libraries/MapTokenInfo.sol"; -import { LibCompanionNetwork } from "script/shared/libraries/LibCompanionNetwork.sol"; - -abstract contract Factory__MapTokensRoninchain is Migration { - using LibCompanionNetwork for *; - - RoninBridgeManager internal _roninBridgeManager; - address internal _roninGatewayV3; - address private _governor; - address[] internal _governors; - - function setUp() public virtual override { - super.setUp(); - _roninBridgeManager = RoninBridgeManager(config.getAddressFromCurrentNetwork(Contract.RoninBridgeManager.key())); - _roninGatewayV3 = config.getAddressFromCurrentNetwork(Contract.RoninGatewayV3.key()); - - _governor = _initCaller(); - _cheatWeightOperator(_governor); - } - - function run() public virtual { - Proposal.ProposalDetail memory proposal = _createAndVerifyProposal(); - _propose(proposal); - } - - function _initCaller() internal virtual returns (address); - function _initTokenList() internal virtual returns (uint256 totalToken, MapTokenInfo[] memory infos); - - function _proposeAndExecute(Proposal.ProposalDetail memory proposal) internal { - proposal.executor = _governors[0]; - _propose(proposal); - - uint256 minVoteWeight = _roninBridgeManager.minimumVoteWeight(); - uint256 sumVoteWeight; - uint256 amountGovernorsNeedToVote; - - for (uint256 i; i < _governors.length; ++i) { - sumVoteWeight += _roninBridgeManager.getGovernorWeight(_governors[i]); - amountGovernorsNeedToVote++; - if (sumVoteWeight >= minVoteWeight) break; - } - require(sumVoteWeight > 0 && amountGovernorsNeedToVote > 0); - - for (uint256 i; i < amountGovernorsNeedToVote; ++i) { - vm.broadcast(_governors[i]); - _roninBridgeManager.castProposalVoteForCurrentNetwork(proposal, Ballot.VoteType.For); - } - - vm.broadcast(_governors[0]); - _roninBridgeManager.execute{ gas: 2_000_000 }(proposal); - } - - function _propose(Proposal.ProposalDetail memory proposal) internal virtual { - vm.broadcast(_governor); - _roninBridgeManager.propose( - proposal.chainId, proposal.expiryTimestamp, proposal.executor, proposal.targets, proposal.values, proposal.calldatas, proposal.gasAmounts - ); - } - - function _createAndVerifyProposal() internal returns (Proposal.ProposalDetail memory proposal) { - (address[] memory roninTokens, address[] memory mainchainTokens, uint256[] memory chainIds, TokenStandard[] memory standards) = _prepareMapToken(); - (address[] memory roninTokensToSetMinThreshold, uint256[] memory minThresholds) = _prepareSetMinThreshold(); - - address[] memory targets = new address[](2); - uint256[] memory values = new uint256[](2); - bytes[] memory calldatas = new bytes[](2); - uint256[] memory gasAmounts = new uint256[](2); - - bytes memory innerData = abi.encodeCall(IRoninGatewayV3.mapTokens, (roninTokens, mainchainTokens, chainIds, standards)); - bytes memory proxyData = abi.encodeWithSignature("functionDelegateCall(bytes)", innerData); - - uint256 expiredTime = block.timestamp + 14 days; - targets[0] = _roninGatewayV3; - values[0] = 0; - calldatas[0] = proxyData; - gasAmounts[0] = 1_000_000; - - innerData = abi.encodeCall(MinimumWithdrawal.setMinimumThresholds, (roninTokensToSetMinThreshold, minThresholds)); - proxyData = abi.encodeWithSignature("functionDelegateCall(bytes)", innerData); - - targets[1] = _roninGatewayV3; - values[1] = 0; - calldatas[1] = proxyData; - gasAmounts[1] = 1_000_000; - - LibProposal.verifyProposalGasAmount(address(_roninBridgeManager), targets, values, calldatas, gasAmounts); - - proposal = Proposal.ProposalDetail({ - nonce: RoninBridgeManager(_roninBridgeManager).round(2021) + 1, - chainId: block.chainid, - expiryTimestamp: expiredTime, - executor: address(0), - targets: targets, - values: values, - calldatas: calldatas, - gasAmounts: gasAmounts - }); - } - - function _cheatWeightOperator(address gov) internal { - bytes32 $ = keccak256(abi.encode(gov, 0x88547008e60f5748911f2e59feb3093b7e4c2e87b2dd69d61f112fcc932de8e3)); - bytes32 opAndWeight = vm.load(address(_roninBridgeManager), $); - - uint256 totalWeight = _roninBridgeManager.getTotalWeight(); - bytes32 newOpAndWeight = bytes32((totalWeight << 160) + uint160(uint256(opAndWeight))); - vm.store(address(_roninBridgeManager), $, newOpAndWeight); - } - - function _prepareMapToken() - internal - returns (address[] memory roninTokens, address[] memory mainchainTokens, uint256[] memory chainIds, TokenStandard[] memory standards) - { - // function mapTokens( - // address[] calldata _roninTokens, - // address[] calldata _mainchainTokens, - // uint256[] calldata chainIds, - // TokenStandard[] calldata _standards - // ) - (uint256 N, MapTokenInfo[] memory tokenInfos) = _initTokenList(); - - roninTokens = new address[](N); - mainchainTokens = new address[](N); - chainIds = new uint256[](N); - standards = new TokenStandard[](N); - - // ============= MAP TOKENS =========== - - for (uint256 i; i < N; ++i) { - roninTokens[i] = tokenInfos[i].roninToken; - mainchainTokens[i] = tokenInfos[i].mainchainToken; - chainIds[i] = network().companionChainId(); - standards[i] = TokenStandard.ERC20; - } - } - - function _prepareSetMinThreshold() internal returns (address[] memory roninTokensToSetMinThreshold, uint256[] memory minThresholds) { - (uint256 N, MapTokenInfo[] memory tokenInfos) = _initTokenList(); - - // ============= SET MIN THRESHOLD ============ - // function setMinimumThresholds( - // address[] calldata _tokens, - // uint256[] calldata _thresholds - // ); - roninTokensToSetMinThreshold = new address[](N); - minThresholds = new uint256[](N); - - for (uint256 i; i < N; ++i) { - roninTokensToSetMinThreshold[i] = tokenInfos[i].roninToken; - minThresholds[i] = tokenInfos[i].minThreshold; - } - } -} diff --git a/script/factories/roninchain/factory-maptoken-ronin-mainnet.s.sol b/script/factories/roninchain/factory-maptoken-ronin-mainnet.s.sol index 3be12de4..b3896b82 100644 --- a/script/factories/roninchain/factory-maptoken-ronin-mainnet.s.sol +++ b/script/factories/roninchain/factory-maptoken-ronin-mainnet.s.sol @@ -9,7 +9,7 @@ import { Contract } from "../../utils/Contract.sol"; import { MainchainBridgeManager } from "@ronin/contracts/mainchain/MainchainBridgeManager.sol"; import "./factory-maptoken-roninchain.s.sol"; -abstract contract Factory__MapTokensRonin_Testnet is Factory__MapTokensRoninchain { +abstract contract Factory__MapTokensRonin_Mainnet is Factory__MapTokensRoninchain { using LibCompanionNetwork for *; function setUp() public override { @@ -19,8 +19,6 @@ abstract contract Factory__MapTokensRonin_Testnet is Factory__MapTokensRoninchai _governor = _initCaller(); } - function _initGovernors() internal virtual returns (address[] memory); - function run() public virtual override { Proposal.ProposalDetail memory proposal = _createAndVerifyProposal(); _propose(proposal);