`

UUPS

 
阅读更多
a.Proxy直接把所有的请求都通过delegatecall丢给Implementation(如果是升级,Implementation的升级函数会确认一下是否为管理员)
b.需要实现_authorizeUpgrade函数以确保只有管理员账户可以进行升级
c.升级函数upgradeTo在Implementation合约中

// SPDX-License-Identifier: MIT
pragma solidity >= 0.8.0;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "hardhat/console.sol";

contract UUPSImplement is Initializable, UUPSUpgradeable, OwnableUpgradeable {
    uint256 public num;

    function initialize() initializer public {
      __Ownable_init();
      __UUPSUpgradeable_init();
    }

    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() initializer {
    }

    function _authorizeUpgrade(address) internal override onlyOwner {}

    function setNum(uint256 _num) external{
        num = _num;
    }

    function fun() external{
        console.log("fun");
    }
}


// SPDX-License-Identifier: MIT
pragma solidity >= 0.8.0;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "hardhat/console.sol";

contract UUPSImplementNew is Initializable, UUPSUpgradeable, OwnableUpgradeable {
    uint256 public num;
    address public addr;

    function initialize() initializer public {
      __Ownable_init();
      __UUPSUpgradeable_init();
      num = 3;
    }

    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() initializer {
        addr = msg.sender;
    }

    function _authorizeUpgrade(address) internal override onlyOwner {
        console.log("_authorizeUpgrade");
        console.log(owner());
    }

    function setAddr(address _addr) external {
        addr = _addr;
    }

    function fun() external{
        console.log("new fun");
    }
}


test
const { ethers, upgrades } = require('hardhat');

contractImpl = "UUPSImplement";
contractImplNew = "UUPSImplementNew";


describe("contractProxy", function () {
  it("UUPS ", async function () {
    const ContractImpl = await ethers.getContractFactory(contractImpl);
    const impl = (await upgrades.deployProxy(ContractImpl, {kind: 'uups'}));
    await impl.fun();
    await impl.setNum(8);
    console.log("num:",await impl.num());

    const ContractImplNew = await ethers.getContractFactory(contractImplNew);
    implNew = (await upgrades.upgradeProxy(impl, ContractImplNew));
    await implNew.fun();
    await implNew.setAddr(implNew.address);
    console.log("num:",await implNew.num());
    console.log("addr:",await implNew.addr());
  });
});

需要在hardhat.config.js最上面加上
require('@openzeppelin/hardhat-upgrades');

contractProxy
fun
num: BigNumber { value: "8" }
new fun
num: BigNumber { value: "8" }
addr: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics