From 971849d5407e324508672aae58b9326c67492907 Mon Sep 17 00:00:00 2001 From: Nicola Miotto Date: Fri, 14 Jul 2023 18:20:03 +0200 Subject: [PATCH] autocompile for each upgrade --- tasks/upgrade.ts | 97 ++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/tasks/upgrade.ts b/tasks/upgrade.ts index ca0f49d..bd6894d 100644 --- a/tasks/upgrade.ts +++ b/tasks/upgrade.ts @@ -1,45 +1,36 @@ +import { Contract, ContractFactory } from "ethers"; import { task } from "hardhat/config"; +import { HardhatRuntimeEnvironment } from "hardhat/types"; -import { - GovernanceToken, - GovernanceToken__factory, - InternalMarket__factory, - ProxyAdmin, - ProxyAdmin__factory, - ResolutionManager__factory, -} from "../typechain"; +import { ProxyAdmin } from "../typechain"; import { NeokingdomDAOHardhat } from "../lib"; +import { NeokingdomContracts } from "../lib/internal/types"; import { question } from "../lib/utils"; -task("upgrade:resolution", "Upgrade ResolutionManager", async (_, hre) => { - const resolutionFactory = (await hre.ethers.getContractFactory( - "ResolutionManager" - )) as ResolutionManager__factory; +function toPascalCase(str: string): string { + let words = str.split(/(?=[A-Z])/); - const neokingdom = await NeokingdomDAOHardhat.initialize(hre); - const contracts = await neokingdom.loadContracts(); - console.log("Upgrade ResolutionManager"); - console.log(" Network:", hre.network.name); + // Capitalize the first letter of each word, leave the rest as it is + let pascalCase = words + .map((word) => word[0].toUpperCase() + word.slice(1)) + .join(""); - const resolutionContract = await hre.upgrades.upgradeProxy( - contracts.resolutionManager.address, - resolutionFactory - ); - await resolutionContract.deployed(); - - console.log(" Address:", resolutionContract.address); - console.log("Resolution upgraded"); -}); - -task("upgrade:market", "Upgrade Internal Market", async (_, hre) => { - const internalMarketFactory = (await hre.ethers.getContractFactory( - "InternalMarket" - )) as InternalMarket__factory; + return pascalCase; +} +async function upgrade( + hre: HardhatRuntimeEnvironment, + contractType: keyof NeokingdomContracts +) { + await hre.run("compile", { force: true }); const neokingdom = await NeokingdomDAOHardhat.initialize(hre); const contracts = await neokingdom.loadContracts(); - console.log(`Upgrade InternalMarket ${contracts.internalMarket.address}`); + + const contractTypeStr = toPascalCase(contractType); + const contractFactory = await hre.ethers.getContractFactory(contractTypeStr); + const proxyAddress = contracts[contractType].address; + console.log(`Upgrade ${contractTypeStr} ${proxyAddress}`); console.log(" Network:", hre.network.name); const answer = await question( @@ -47,41 +38,27 @@ task("upgrade:market", "Upgrade Internal Market", async (_, hre) => { ); if (answer == "GO") { - const internalMarketContract = await hre.upgrades.upgradeProxy( - contracts.internalMarket.address, - internalMarketFactory + const contractInstance = await hre.upgrades.upgradeProxy( + proxyAddress, + contractFactory ); - await internalMarketContract.deployed(); + await contractInstance.deployed(); - console.log(" Address:", internalMarketContract.address); - console.log("InternalMarket upgraded"); + console.log(" Address:", contractInstance.address); + console.log(`${contractTypeStr} upgraded`); } -}); - -task("upgrade:governance", "Upgrade Governance Token", async (_, hre) => { - const governanceTokenFactory = (await hre.ethers.getContractFactory( - "GovernanceToken" - )) as GovernanceToken__factory; +} - const neokingdom = await NeokingdomDAOHardhat.initialize(hre); - const contracts = await neokingdom.loadContracts(); - console.log(`Upgrade GovernanceToken ${contracts.governanceToken.address}`); - console.log(" Network:", hre.network.name); +task("upgrade:resolution", "Upgrade ResolutionManager", async (_, hre) => { + await upgrade(hre, "resolutionManager"); +}); - const answer = await question( - "This action is irreversible. Please type 'GO' to continue.\n" - ); +task("upgrade:market", "Upgrade Internal Market", async (_, hre) => { + await upgrade(hre, "internalMarket"); +}); - if (answer == "GO") { - const governanceTokenContract = (await hre.upgrades.upgradeProxy( - contracts.governanceToken.address, - governanceTokenFactory - )) as GovernanceToken; - await governanceTokenContract.deployed(); - - console.log(" Address:", governanceTokenContract.address); - console.log("GovernanceToken upgraded"); - } +task("upgrade:governance", "Upgrade Governance Token", async (_, hre) => { + await upgrade(hre, "governanceToken"); }); task("impl", "Get Proxy Impl")