【Solidity】合约升级1

合约升级主要是经过代理合约后面的逻辑合约给更改。


按照以下的代码范例,会有几个问题:
1. 经过interface的话,那所有的function量将被固定,无法增加
2. 数据是各自存在logic合约内的,如果改了logic合约,那么数据将不复存在

所以会在下一个教程当中,把合约升级写得更完美。

代码范例

Logic1.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


contract Logic1 {
    uint private number;

    function setNum(uint _number)public{
        number = _number + 1;
    }

    function checkNum()public view returns(uint){
        return number;
    }
}

Logic2.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


contract Logic2 {
    uint private number;

    function setNum(uint _number)public{
        number = _number + 2;
    }

    function checkNum()public view returns(uint){
        return number;
    }
}

Proxy.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

interface ILogic {
    function setNum(uint _number)external;
    function checkNum()external view returns(uint);
}

contract Proxy {
    ILogic public logic;

    function setLogic(address _address)public{
        logic = ILogic(_address);
    }

    function getLogicAddress()public view returns(address){
        return address(logic);
    }

    function setNumber(uint _num)public{
        logic.setNum(_num);
    }

    function checkNum()public view returns(uint){
        return logic.checkNum();
    }
}

Loading

Facebook评论