如何使用透明度限制二氧化碳作为透明代理

发布于 2025-01-23 21:24:19 字数 3813 浏览 0 评论 0原文

我是牢固的新手。

我正在学习如何使用Openzeppelin的透明顾问合同来实施透明的代理,但遇到了一些问题。

步骤1:我尝试部署简单的合同MyConv0,然后实现部署和调用方法,一切都很好。

// File MyConV0.sol
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract MyConV0 {    
    string private _name;
    string private _symbol;

    constructor(string memory name_, string memory symbol_) public {            
      _name = name_;
      _symbol = symbol_;    
      console.log(_symbol);
    }
    
    function symbol() public view returns (string memory) {
        console.log(_symbol);
        return _symbol;
    }

    function name() public view returns (string memory) {
        console.log('Name: ');
        console.log(_name);
        return _name;
    }

    function getVersion() pure external returns(uint256) {
        return 0;
    }    
}

步骤2:我试图升级到MyConv1,以便能够使用透明度限制proxy升级,但失败了。

// File: MyConV1
pragma solidity ^0.8.0;

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

contract MyConV1 is Initializable, OwnableUpgradeable {   
    string private _name;
    string private _symbol;

    function initialize(string memory name_, string memory symbol_) initializer public {      
      __Ownable_init();
      _name = name_;
      _symbol = symbol_;
      
      console.log(_symbol);
    }

    
    function symbol() public view returns (string memory) {
        console.log('Symbol: ');
        console.log(_symbol);
        return _symbol;
    }

    function name() public view returns (string memory) {
        console.log('Name: ');
        console.log(_name);
        return _name;
    }

    function getVersion() pure external returns(uint256) {
        return 1;
    }
    
}

请参阅thresparentupgradableproxy:

// The test file in JS using ethers.js and Hardhat environment
async function main() {

  let t0, t1, t2, t3, t4, v1, v2, v3, v4;
  
  const [owner, proxyAdmin, user, other] = await ethers.getSigners();
  
  // --- 0. Deploy MyConV0
  let input0 = [ 'TotoName', 'Toto' ];
  let con0 = await deploy_verify_contract("MyConV0", input0);  
  t0 = await con0.symbol() ;
  console.log(t0); // worked -> Toto

  // --- 1. Deploy MyConV1
  let input1 = [ ];
  let con1 = await deploy_verify_contract("MyConV1", input1);  


   // --- 2. get data
  let abi = [ `function initialize(   string name_,
                                      string symbol_,                                      
                                  )` ];
  let iface = new ethers.utils.Interface(abi);
  let data = iface.encodeFunctionData("initialize", [ 'TotoName', 'Toto' ]);  
  
  
  // --- 3. deploy trans proxy
  let input2 = [ con1.address, owner.address, data ];  
  let con2 = await deploy_verify_contract("TransparentUpgradeableProxy1", input2);  

  // --- 4. call proxy method
  t2 = await con2.implementation();
  console.log(t2); // DO NOT WORK, t2 is object tx, and do not contains the results like step 0

  // --- 5. call MyConV1 contact via proxy -> ERROR: "TypeError: con2.symbol is not a function"
  t3 = await con2.symbol();
  console.log(t3);

}

async function deploy_verify_contract(contractName, input, lib = {}){
  
  const _contract = await hre.ethers.getContractFactory(contractName, lib);
  const contract = await _contract.deploy(...input);

  await contract.deployed();

  console.log( contractName + " deployed to:", contract.address );

  return contract;

}

​正确代理。特别是使用上述代码,我不明白几点:

  • 我是否正确部署了代理?
  • 为什么我无法获得代理实现()函数的正确返回数据?
  • 为什么我不能通过代理来调用MyConv1的功能?

希望你们可以帮助我如何纠正代码,几天我无法解决这个问题。

I'm Solidity Newbie.

I'm learning how to implement Transparent Proxy using Openzeppelin's TransparentUpgradeableProxy contract, but am having some problems.

Step 1: I tried to deploy a simple contract MyConV0, then implemented deploy and call method, everything is fine.

// File MyConV0.sol
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract MyConV0 {    
    string private _name;
    string private _symbol;

    constructor(string memory name_, string memory symbol_) public {            
      _name = name_;
      _symbol = symbol_;    
      console.log(_symbol);
    }
    
    function symbol() public view returns (string memory) {
        console.log(_symbol);
        return _symbol;
    }

    function name() public view returns (string memory) {
        console.log('Name: ');
        console.log(_name);
        return _name;
    }

    function getVersion() pure external returns(uint256) {
        return 0;
    }    
}

Step 2: I tried to upgrade to MyConV1 to be able to Upgradable with TransparentUpgradeableProxy but failed.

// File: MyConV1
pragma solidity ^0.8.0;

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

contract MyConV1 is Initializable, OwnableUpgradeable {   
    string private _name;
    string private _symbol;

    function initialize(string memory name_, string memory symbol_) initializer public {      
      __Ownable_init();
      _name = name_;
      _symbol = symbol_;
      
      console.log(_symbol);
    }

    
    function symbol() public view returns (string memory) {
        console.log('Symbol: ');
        console.log(_symbol);
        return _symbol;
    }

    function name() public view returns (string memory) {
        console.log('Name: ');
        console.log(_name);
        return _name;
    }

    function getVersion() pure external returns(uint256) {
        return 1;
    }
    
}

Refer TransparentUpgradableProxy: https://docs.openzeppelin.com/contracts/4.x/api/proxy#TransparentUpgradeableProxy

// The test file in JS using ethers.js and Hardhat environment
async function main() {

  let t0, t1, t2, t3, t4, v1, v2, v3, v4;
  
  const [owner, proxyAdmin, user, other] = await ethers.getSigners();
  
  // --- 0. Deploy MyConV0
  let input0 = [ 'TotoName', 'Toto' ];
  let con0 = await deploy_verify_contract("MyConV0", input0);  
  t0 = await con0.symbol() ;
  console.log(t0); // worked -> Toto

  // --- 1. Deploy MyConV1
  let input1 = [ ];
  let con1 = await deploy_verify_contract("MyConV1", input1);  


   // --- 2. get data
  let abi = [ `function initialize(   string name_,
                                      string symbol_,                                      
                                  )` ];
  let iface = new ethers.utils.Interface(abi);
  let data = iface.encodeFunctionData("initialize", [ 'TotoName', 'Toto' ]);  
  
  
  // --- 3. deploy trans proxy
  let input2 = [ con1.address, owner.address, data ];  
  let con2 = await deploy_verify_contract("TransparentUpgradeableProxy1", input2);  

  // --- 4. call proxy method
  t2 = await con2.implementation();
  console.log(t2); // DO NOT WORK, t2 is object tx, and do not contains the results like step 0

  // --- 5. call MyConV1 contact via proxy -> ERROR: "TypeError: con2.symbol is not a function"
  t3 = await con2.symbol();
  console.log(t3);

}

async function deploy_verify_contract(contractName, input, lib = {}){
  
  const _contract = await hre.ethers.getContractFactory(contractName, lib);
  const contract = await _contract.deploy(...input);

  await contract.deployed();

  console.log( contractName + " deployed to:", contract.address );

  return contract;

}

I used Hardhat's console.log function and it seems to have successfully deployed the Proxy, and sent the correct data to the MyConV1.initialize function, but don't know how to call the proxy properly. Specifically with the above code, I don't understand a few points:

  • Have I deployed the proxy correctly?
  • Why can't I get the correct return data of the proxy's implementation() function?
  • Why can't I call MyConV1's function through the proxy?

Hope you guys can help me how to correct the code, I have not been able to solve this problem for a few days.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2025-01-30 21:24:19

您没有在代理合同上拨打ustgradeto()来设置实施合同的地址。在代理中设置实施合同后,您必须仅通过代理合同初始化实施合同!没有任何其他方式或直接在实施合同上调用它(它不起作用!!)

You are not calling UpgradeTo() on proxy contract to set the implementation contract's address. After you have set the implementation contract in proxy you have to initialise the implementation contract via proxy contract only! not any other way or directly calling it on implementation contract (it wont work!!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文