solidity智能合约升级

智能合约升级
参考:https://edu.csdn.net/learn/31576/477299?spm=1002.2001.3001.4143

接口调用合约:https://www.ixigua.com/7028062852392944135?logTag=feab46e388c4bb13c72e
主-从式
逻辑+数据
代理+逻辑+数据

// 参考https://edu.csdn.net/learn/31576/477299?spm=1002.2001.3001.4143
// SPDX-License-Identifier:MIT;
pragma solidity ^0.6.1;
interface IA{
    function setName(string calldata _name) external;
    function getName()external view returns(string memory);
}
contract A is IA{
    // A继承IA,并且实现IA的方法
    string private name;
    function setName(string calldata _name) external override{
        name = _name;
    }
    function getName() external override view returns (string memory){
        return name;
    }
}
contract B is IA{
    // B继承IA,并且实现IA的方法
    string private name;
    function setName(string calldata _name) external override{
        name = _name;
    }
    function getName() external override view returns (string memory){
        return name;
    }
}
contract call_demo{
    IA a;
    function setIA(address addr) public{
        a = IA(addr);
    }
    function setName (string memory _name) public{
        a.setName(_name);
    }
    function getName() public view returns(string memory){
        return a.getName();
    }
}

第二种用interface接口
主合约:

//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol";

contract cat{
    using strings for *;
    string public name;
    function eat(string memory myname) public pure returns(string memory){
        return "cat eat fish".toSlice().concat(myname.toSlice());
    }

    function sleep() public pure returns(string memory){
         return "sleep";
    }

}


contract dog{
    using strings for *;
    string name;
   function eat(string memory myname) public pure returns(string memory){
        return "dog eat bone".toSlice().concat(myname.toSlice());
    }

    function swim() public pure returns(string memory){
         return "sleep";
    }

}

从合约:
接口继承is,只需主合约或者从合约,其中一方使用is

pragma solidity ^0.5.0;

// 调取外部的dog_cat合约

interface IanimalEat{
      function eat(string calldata myname) external returns(string memory);
}

contract animal is IanimalEat{
    //可以调取外部的合约方法
    function test(address _addr) public returns(string memory){
        IanimalEat   generalEat =  IanimalEat(_addr);
        return generalEat.eat("duanhuikun");
    }

    //可以改写
    function eat() external pure returns(string memory){
        return "eat eat eat";
    }
}
contract call_A {
    function setName(address _addr) public {
        IA   a =  IA(_addr);
        a.setName("duanhuikun");
    }
    // function getName() public view returns(string memory){
    //     return a.getName();
    // }

}

二、代理模式升级
1.UpgradeabilityProxy是2018年发布的代理的合约,通过call(回调address),logic contract(2022年部署的合约,或者2023年…,任意合约),通过address回调,其实就是中转了calldata
solidity智能合约升级_第1张图片

比如调用不匹配的方法,就触发fallback
solidity智能合约升级_第2张图片

你可能感兴趣的