Skip to content

Commit

Permalink
feat: compare and upgrade all contracts in batch transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
blockgroot committed Jun 25, 2024
1 parent abea2e0 commit 30c5d0e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
18 changes: 18 additions & 0 deletions scripts/safe-scripts/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { ethers, upgrades } = require("hardhat");
import { artifacts } from "hardhat";

export async function getDeployedBytecode(address: string, provider: any) {
const contractImpl = await upgrades.erc1967.getImplementationAddress(address);
const response = await provider.getCode(contractImpl);
return response;
}

export async function getArtifact(name: string) {
const artifact = await artifacts.readArtifact(name);
return artifact.deployedBytecode;
}

export async function forceImportDeployedProxies(contractAddress: string, contractName: string) {
const contractArtifact = await ethers.getContractFactory(contractName);
await upgrades.forceImport(contractAddress, contractArtifact, { kind: "transparent" });
}
20 changes: 2 additions & 18 deletions scripts/safe-scripts/upgradeAll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { ethers, upgrades } = require("hardhat");
const { ethers } = require("hardhat");
import upgradeHelper from "./helpers/upgrade";
import networkAddresses from "./address.json";
import proposeTransaction from "./helpers/proposeTransaction";
import { artifacts } from "hardhat";
import {getArtifact, getDeployedBytecode, forceImportDeployedProxies} from "./helpers/utils";

async function main(networks: { [networkName: string]: { contracts: { name: string; address: string }[] } }) {
const provider = ethers.provider;
Expand Down Expand Up @@ -38,22 +38,6 @@ async function main(networks: { [networkName: string]: { contracts: { name: stri
}
}

async function getDeployedBytecode(address: string, provider: any) {
const contractImpl = await upgrades.erc1967.getImplementationAddress(address);
const response = await provider.getCode(contractImpl);
return response;
}

async function getArtifact(name: string) {
const artifact = await artifacts.readArtifact(name);
return artifact.deployedBytecode;
}

async function forceImportDeployedProxies(contractAddress: string, contractName: string) {
const contractArtifact = await ethers.getContractFactory(contractName);
await upgrades.forceImport(contractAddress, contractArtifact, { kind: "transparent" });
}

main(networkAddresses)
.then(() => process.exit(0))
.catch((error) => {
Expand Down
37 changes: 31 additions & 6 deletions scripts/safe-scripts/upgradeAllBatch.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
import upgradeHelper from "./helpers/upgrade";
import networkAddresses from "./address.json";
import proposeTransactions from "./helpers/proposeTransactions";
import { MetaTransactionData } from "@safe-global/safe-core-sdk-types";
const { ethers } = require("hardhat");
import { MetaTransactionData } from "@safe-global/safe-core-sdk-types";
import { getArtifact, getDeployedBytecode, forceImportDeployedProxies } from "./helpers/utils";

async function main(networks: { [networkName: string]: { contracts: { name: string; address: string }[] } }) {
const provider = ethers.provider;
const networkName = await hre.network.name;
const networkContracts = networks[networkName].contracts;

console.log(`Checking contracts on network "${networkName}":`, "\n");
const targets = [];
const values = [];
const params = [];
console.log(`Checking contracts on network "${networkName}":`, "\n");

for (let { name, address } of networkContracts) {
const { to, value, data } = await upgradeHelper(address, name);
targets.push(to);
values.push(value);
params.push(data);
console.log(`Checking contract "${name}" at address ${address}`);

const deployedBytecode = await getDeployedBytecode(address, provider);
if (!deployedBytecode) {
console.error(`Failed to retrieve deployed bytecode for "${name}". Skipping.`);
continue;
}
try {
// Uncomment below line if network files are lost and need to be force import.
// await forceImportDeployedProxies(address, name);
const compiledBytecode = await getArtifact(name);

if (deployedBytecode !== compiledBytecode) {
console.warn(`Contract "${name}" is out of date!`);
console.log(`Upgrading to latest version...`);
const { to, value, data } = await upgradeHelper(address, name);
targets.push(to);
values.push(value);
params.push(data);
} else {
console.log(`"${name}" is already up to date on network "${networkName}".`);
}
} catch (error) {
console.error(`Error checking or upgrading "${name}" on network "${networkName}":`, error);
}
}
const multisigData = await buildMultiSigTx(targets, values, params);
await proposeTransactions(multisigData);
Expand Down

0 comments on commit 30c5d0e

Please sign in to comment.