【翻译】用 Hardhat 进行升级部署(Using with Hardhat)

该软件包为您的Hardhat脚本添加了功能,以便您可以为您的合同部署和升级代理。取决于ethers.js。

# 和Hardhat一起使用 该软件包为您的Hardhat脚本添加了功能,以便您可以为您的合同部署和升级代理。取决于ethers.js。 > 提示:查看[分步教程](https://forum.openzeppelin.com/t/openzeppelin-buidler-upgrades-step-by-step-tutorial/3580),展示从创建、测试和部署,一直到使用 Gnosis Safe 进行升级。 --- ## 安装 ``` $ npm install --save-dev @openzeppelin/hardhat-upgrades $ npm install --save-dev @nomiclabs/hardhat-ethers ethers # peer dependencies ``` 并在您的 [hardhat.config.js](https://hardhat.org/config) 中注册插件: ``` require('@openzeppelin/hardhat-upgrades'); ``` --- ## 在脚本中的使用 您可以在 [Hardhat脚本](https://hardhat.org/guides/scripts.html)中使用此插件,通过 `deployProxy` 函数部署其中一个合约的可升级实例: ``` // 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.deployed(); console.log("Box deployed to:", box.address); } main(); ``` 这将自动检查`Box`合约是否升级安全,设置代理管理员(如果需要),为`Box`合约部署一个实现合约(除非之前的部署已经有一个),创建一个代理并初始化它 通过调用`initialize(42)`。 然后,在另一个脚本中,您可以使用 `upgradeProxy` 函数将部署的实例升级到新版本。新版本可以是不同的合约(比如`BoxV2`),或者你可以修改现有的`Box`合约并重新编译它—插件会注意到它发生了变化。 ``` // scripts/upgrade-box.js const { ethers, upgrades } = require("hardhat"); async function main() { const BoxV2 = await ethers.getContractFactory("BoxV2"); const box = await upgrades.upgradeProxy(BOX_ADDRESS, BoxV2); console.log("Box upgraded"); } main(); ``` > 注意:虽然此插件会跟踪您在每个网络上部署的所有实现合同,但为了重用它们并验证存储兼容性,它不会跟踪您已部署的代理。这意味着您需要手动跟踪每个部署地址,以便在需要时将这些地址提供给升级功能。 该插件将负责将`BoxV2`与前一个进行比较,以确保它们与升级兼容,部署新的`BoxV2`实现合约(除非先前部署中已经存在),并将现有代理升级到新实现。 --- ## 测试中的使用 您还可以使用Hardhat tests中的`deployProxy`和`upgradeProxy`函数,以防您想添加测试以升级您的合约(您应该这样做!)。API 与脚本中的相同。 ``` const { expect } = require("chai"); describe("Box", function() { it('works', async () => { const Box = await ethers.getContractFactory("Box"); const BoxV2 = await ethers.getContractFactory("BoxV2"); const instance = await upgrades.deployProxy(Box, [42]); const upgraded = await upgrades.upgradeProxy(instance.address, BoxV2); const value = await upgraded.value(); expect(value.toString()).to.equal('42'); }); }); ```

和Hardhat一起使用

该软件包为您的Hardhat脚本添加了功能,以便您可以为您的合同部署和升级代理。取决于ethers.js。

提示:查看分步教程,展示从创建、测试和部署,一直到使用 Gnosis Safe 进行升级。

安装

$ npm install --save-dev @openzeppelin/hardhat-upgrades
$ npm install --save-dev @nomiclabs/hardhat-ethers ethers # peer dependencies

并在您的 hardhat.config.js 中注册插件:

require('@openzeppelin/hardhat-upgrades');

在脚本中的使用

您可以在 Hardhat脚本中使用此插件,通过 deployProxy 函数部署其中一个合约的可升级实例:

// 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.deployed();
  console.log("Box deployed to:", box.address);
}

main();

这将自动检查Box合约是否升级安全,设置代理管理员(如果需要),为Box合约部署一个实现合约(除非之前的部署已经有一个),创建一个代理并初始化它 通过调用initialize(42)。 然后,在另一个脚本中,您可以使用 upgradeProxy 函数将部署的实例升级到新版本。新版本可以是不同的合约(比如BoxV2),或者你可以修改现有的Box合约并重新编译它—插件会注意到它发生了变化。

// scripts/upgrade-box.js
const { ethers, upgrades } = require("hardhat");

async function main() {
  const BoxV2 = await ethers.getContractFactory("BoxV2");
  const box = await upgrades.upgradeProxy(BOX_ADDRESS, BoxV2);
  console.log("Box upgraded");
}

main();

注意:虽然此插件会跟踪您在每个网络上部署的所有实现合同,但为了重用它们并验证存储兼容性,它不会跟踪您已部署的代理。这意味着您需要手动跟踪每个部署地址,以便在需要时将这些地址提供给升级功能。

该插件将负责将BoxV2与前一个进行比较,以确保它们与升级兼容,部署新的BoxV2实现合约(除非先前部署中已经存在),并将现有代理升级到新实现。

测试中的使用

您还可以使用Hardhat tests中的deployProxyupgradeProxy函数,以防您想添加测试以升级您的合约(您应该这样做!)。API 与脚本中的相同。

const { expect } = require("chai");

describe("Box", function() {
  it('works', async () => {
    const Box = await ethers.getContractFactory("Box");
    const BoxV2 = await ethers.getContractFactory("BoxV2");

    const instance = await upgrades.deployProxy(Box, [42]);
    const upgraded = await upgrades.upgradeProxy(instance.address, BoxV2);

    const value = await upgraded.value();
    expect(value.toString()).to.equal('42');
  });
});

本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

  • 发表于 2021-08-27 11:35
  • 阅读 ( 591 )
  • 学分 ( 17 )
  • 分类:Solidity

评论