本文介绍了如何将 Hardhat Upgrades 插件与 OpenZeppelin Defender 集成,以便利用 Defender 的气体定价、重新提交和自动字节码及源代码验证等功能。
Hardhat Upgrades 包可以使用 OpenZeppelin Defender 进行部署,而不是使用 ethers.js,这允许诸如 gas 价格估算、重新提交以及自动字节码和源代码验证等功能。
在 OpenZeppelin Defender 上创建一个部署环境,并在 hardhat.config.js
或 hardhat.config.ts
文件的 defender
下提供 Team API Key 和 secret:
module.exports = {
defender: {
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
}
}
上述 API 密钥必须至少具有管理部署的功能(可选地,需要管理 Relayers 才能使用 Relayer 创建审批流程)。 你可以在 https://defender.openzeppelin.com/#/settings/api-keys 中配置你的 API 密钥。 |
与 OpenZeppelin Defender 一起使用的网络由 Hardhat 连接到的网络决定。
如果要确保 Defender 使用特定的网络,请在 hardhat.config.js
或 hardhat.config.ts
文件的 defender
部分设置 network
字段:
module.exports = {
defender: {
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
network: "my-mainnet-fork",
}
}
如果设置了此项,则它必须是 Defender 中的公共、私有或 fork 网络的名称。 如果在设置此项时 Hardhat 连接到不同的网络,则不会发生部署,而是会引发错误。
如果你在 Defender 中有多个具有相同 chainId 的 fork 网络,则这是必需的,在这种情况下,将使用名称与 network 字段匹配的网络。 |
当使用 Hardhat Upgrades API 函数 时,使用以下任何一种方式启用 OpenZeppelin Defender 部署。
只有在其 API 参考中具有 useDefenderDeploy 选项的函数才支持通过 OpenZeppelin Defender 进行部署。 如果你启用以下选项,但使用不支持 useDefenderDeploy 的函数,则以下第一种方式将导致这些函数使用 ethers.js 进行部署,而第二种和第三种方式将导致这些函数给出错误。 |
hardhat.config.js
或 hardhat.config.ts
中,在 defender
下设置 useDefenderDeploy: true
。 例如:module.exports = {
defender: {
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
useDefenderDeploy: true,
}
}
// scripts/create-box.js
const { ethers, upgrades } = require("hardhat");
async function main() {
const Box = await ethers.getContractFactory("Box");
const box = await upgrades.deployProxy(Box, [42]);
await box.waitForDeployment();
console.log("Box deployed to:", await box.getAddress());
}
main();
defender
模块而不是 Hardhat Runtime Environment 中的 upgrades
。 如果你想确保使用 Defender 并且希望在函数不支持 Defender 时看到错误,请使用此选项。 例如:// scripts/create-box.js
const { ethers, defender } = require("hardhat");
async function main() {
const Box = await ethers.getContractFactory("Box");
const box = await defender.deployProxy(Box, [42]);
await box.waitForDeployment();
console.log("Box deployed to:", await box.getAddress());
}
main();
useDefenderDeploy
常见选项。 设置此选项将覆盖特定函数的上述内容。 例如:// scripts/create-box.js
const { ethers, upgrades } = require("hardhat");
async function main() {
const Box = await ethers.getContractFactory("Box");
const box = await upgrades.deployProxy(Box, [42], { useDefenderDeploy: true });
await box.waitForDeployment();
console.log("Box deployed to:", await box.getAddress());
}
main();
- 原文链接: docs.openzeppelin.com/up...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!