Hardhat Upgrades #5342
-
Hello, I just wanted to see source code of hardhat upgrades to better understand what functions stands behind calling for example async function main() {
const proxyAddress = "FILL_ME_IN"
const BoxV2 = await ethers.getContractFactory("BoxV2")
console.log("Preparing upgrade...")
// Extending Box contract with BoxV2 contract functions
const boxV2Address = await upgrades.prepareUpgrade(proxyAddress, BoxV2)
console.log("BoxV2 at:", boxV2Address)
}
async function main() {
const gnosisSafe = "FILL_ME_IN"
console.log("Transferring ownership of ProxyAdmin...")
// The owner of the ProxyAdmin can upgrade our contracts
// We are setting new admin
await upgrades.admin.transferProxyAdminOwnership(gnosisSafe)
console.log("Transferred ownership of ProxyAdmin to:", gnosisSafe)
} First we are setting proxyAddress to "FILL_ME_IN" and then we are transfering ownership to same string and I have no idea why. Could someone please explain above code to me and also pass link to hardhat upgrades source code so I can view such functions as Hardhat library is so big that I was unable to find it myself and ctrl+click in VsCode doesn't help with this... as it leads to nowhere like below and there is no upgrades script/code whatsoever After getting into hardhat library we get: import debug from "debug";
import { HardhatRuntimeEnvironment } from "../../types";
import { HardhatContext } from "../context";
import { loadConfigAndTasks } from "../core/config/config-loading";
import { HardhatError } from "../core/errors";
import { ERRORS } from "../core/errors-list";
import { getEnvHardhatArguments } from "../core/params/env-variables";
import { HARDHAT_PARAM_DEFINITIONS } from "../core/params/hardhat-params";
import { Environment } from "../core/runtime-environment";
let ctx: HardhatContext;
let env: HardhatRuntimeEnvironment;
if (HardhatContext.isCreated()) {
ctx = HardhatContext.getHardhatContext();
// The most probable reason for this to happen is that this file was imported
// from the config file
if (ctx.environment === undefined) {
throw new HardhatError(ERRORS.GENERAL.LIB_IMPORTED_FROM_THE_CONFIG);
}
env = ctx.environment;
} else {
ctx = HardhatContext.createHardhatContext();
const hardhatArguments = getEnvHardhatArguments(
HARDHAT_PARAM_DEFINITIONS,
process.env
);
if (hardhatArguments.verbose) {
debug.enable("hardhat*");
}
const { resolvedConfig, userConfig } = loadConfigAndTasks(hardhatArguments);
env = new Environment(
resolvedConfig,
hardhatArguments,
ctx.tasksDSL.getTaskDefinitions(),
ctx.extendersManager.getExtenders(),
ctx.experimentalHardhatNetworkMessageTraceHooks,
userConfig
);
ctx.setHardhatRuntimeEnvironment(env);
}
export = env; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
@Neftyr You can find it here LINK I think you got this code from the openzepplin tutorial on hardhat-upgrades, right? Here they are using the Gnosis Safe to upgrade the contract. There they first create the V1 of the implementation contract and deploy using hardhat upgrades; example; async function main() {
const Box = await ethers.getContractFactory("Box");
console.log("Deploying Box...");
const box = await upgrades.deployProxy(Box, [42], { initializer: 'store' });
console.log("Box deployed to:", box.address);
} then they transfer the ownership of ProxyAdmin to Gnosis Safe, if you have read about this, you must know that using hardhat-upgrades only the owner of ProxyAdmin can upgrade the proxy implementation contract. async function main() {
const gnosisSafe = '0x1c14600daeca8852BA559CC8EdB1C383B8825906';
console.log("Transferring ownership of ProxyAdmin...");
// The owner of the ProxyAdmin can upgrade our contracts
await upgrades.admin.transferProxyAdminOwnership(gnosisSafe);
console.log("Transferred ownership of ProxyAdmin to:", gnosisSafe);
} After that, they create a new implementation of Box contract name BoxV2 and just prepare the upgrade; Note: Preparation is not actual upgrading the contract, it is just a way to get the implemenation address so the ProxyAdmin can perform the upgradability. async function main() {
const proxyAddress = '0xFF60fd044dDed0E40B813DC7CE11Bed2CCEa501F';
const BoxV2 = await ethers.getContractFactory("BoxV2");
console.log("Preparing upgrade...");
const boxV2Address = await upgrades.prepareUpgrade(proxyAddress, BoxV2);
console.log("BoxV2 at:", boxV2Address);
} And in the next step using the UI they perform the upgradability using Gnosis Safe as admin. |
Beta Was this translation helpful? Give feedback.
-
I have figured out what was wrong with that jumping into source code not working. We need simply add below in our require("@openzeppelin/hardhat-upgrades") After that we can ctrl+click everything we want from that library :) |
Beta Was this translation helpful? Give feedback.
@Neftyr You can find it here LINK
I think you got this code from the openzepplin tutorial on hardhat-upgrades, right? Here they are using the Gnosis Safe to upgrade the contract.
There they first create the V1 of the implementation contract and deploy using hardhat upgrades; example;
then they transfer the ownership of ProxyAdmin to Gnosis Safe, if you have read about this, you must know that using hardhat-upgrades only the owner of ProxyAdmin can upg…