From 99e6816ec90dd0c197e437922bde8bd854647391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20M=C3=BCller?= Date: Sun, 13 Oct 2024 15:18:33 +0300 Subject: [PATCH] task to get calldata to upgrade core contracts using DAO --- hardhat.config.ts | 1 + tasks/getContractUpgradeCalldata.ts | 57 +++++++++++++++++++++++++++++ tasks/index.ts | 1 + 3 files changed, 59 insertions(+) create mode 100644 tasks/getContractUpgradeCalldata.ts diff --git a/hardhat.config.ts b/hardhat.config.ts index 2aca9104..b862f911 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -13,6 +13,7 @@ import 'solidity-docgen'; import 'hardhat-tracer'; import './tasks/make_spec'; +import './tasks/getContractUpgradeCalldata'; const getMnemonic = () => { diff --git a/tasks/getContractUpgradeCalldata.ts b/tasks/getContractUpgradeCalldata.ts new file mode 100644 index 00000000..651ac90f --- /dev/null +++ b/tasks/getContractUpgradeCalldata.ts @@ -0,0 +1,57 @@ +import { task, types } from 'hardhat/config'; +import { attachProxyAdminV5 } from '@openzeppelin/hardhat-upgrades/dist/utils'; + +let KnownContracts = new Map([ + ["ValidatorSetHbbft", "0x1000000000000000000000000000000000000001"], + ["BlockRewardHbbft", "0x2000000000000000000000000000000000000001"], + ["RandomHbbft", "0x3000000000000000000000000000000000000001"], + ["TxPermissionHbbft", "0x4000000000000000000000000000000000000001"], + ["CertifierHbbft", "0x5000000000000000000000000000000000000001"], + ["KeyGenHistory", "0x7000000000000000000000000000000000000001"], + ["StakingHbbft", "0x1100000000000000000000000000000000000001"], + ["ConnectivityTrackerHbbft", "0x1200000000000000000000000000000000000001"], + ["BonusScoreSystem", "0x1300000000000000000000000000000000000001"], +]); + + +task("getUpgradeCalldata", "Get contract upgrade calldata to use in DAO proposal") + .addParam("contract", "The core contract address to upgrade") + .addOptionalParam("impl", "Address of new core contract implementation", undefined, types.string) + .setAction(async (taskArgs, hre) => { + const { contract, impl } = taskArgs; + + if (!KnownContracts.has(contract)) { + throw new Error(`${contract} is unknown`); + } + + const proxyAddress = KnownContracts.get(contract)!; + + let implementationAddress: string = impl; + if (impl == undefined) { + const [deployer] = await hre.ethers.getSigners(); + const contractFactory = await hre.ethers.getContractFactory(contract, deployer); + + const result = await hre.upgrades.deployImplementation( + contractFactory, + { + getTxResponse: false, + redeployImplementation: 'always', + } + ); + implementationAddress = result as string; + } + + const proxyAdminAddress = await hre.upgrades.erc1967.getAdminAddress(proxyAddress); + + const proxyAdmin = await attachProxyAdminV5(hre, proxyAdminAddress); + + const calldata = proxyAdmin.interface.encodeFunctionData("upgradeAndCall", [ + proxyAddress, + implementationAddress, + hre.ethers.hexlify(new Uint8Array()), + ]); + + console.log("contract:", contract); + console.log("calldata:", calldata); + console.log(" target:", proxyAdminAddress); + }); diff --git a/tasks/index.ts b/tasks/index.ts index 73c19f40..736b637c 100644 --- a/tasks/index.ts +++ b/tasks/index.ts @@ -1 +1,2 @@ import './make_spec.ts' +import './getContractUpgradeCalldata.ts' \ No newline at end of file