微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

设计实现:在带有接口和动态库地址的库中使用结构体

如何解决设计实现:在带有接口和动态库地址的库中使用结构体

基于一些关于使用包含结构体的库的文章

  1. sample article
  2. eth doc for library shows the design pattern of using struct in library

以下是示例文章中的示例代码

Count.sol

pragma solidity ^0.6.4;

library Count{
    struct hold{
        uint a;
        mapping( uint => bool ) isGood;
    }
    
    function subUint(hold storage s,uint b) external view returns(uint){
        
        require(s.a >= b); // Make sure it doesn't return a negative value.
        return s.a - b;
        
    }

}

数学.sol

pragma solidity ^0.6.4;


import { Count } from  './Count.sol';

contract Math {
    
    using Count for Count.hold;
    
    Count.hold  h;
    address h1;
    
    constructor() public {
        h.a = 123;
        h.isGood[1] = true;
    }
    
    function subHold(uint a) public view  returns(uint){
        return h.subUint(a);
    }
    
    function show(uint a) public view returns ( bool){
        return h.isGood[a];
    }
}

问题:

我尝试向我的图书馆添加一个接口,并且因为我想在未来升级我的图书馆,所以我尝试将其存储为地址,以便我可以使用新地址 h 访问它。>

但是我很困惑我应该如何写它,因为没有关于它的文章可以再对此进行研究。

Count.sol

pragma solidity ^0.6.4;

library Count{
    struct hold{
        uint a;
        mapping( uint => bool ) isGood;
    }
    
    function subUint(hold storage s,uint b) external view returns (uint){
        require(s.a >= b); // Make sure it doesn't return a negative value.
        return s.a - b;
        
    }
    
    function setA(hold storage s,uint _a) external returns (bool){
        s.a = _a;
    }
    
    function setGood(hold storage s,uint _a,bool _good) external returns (bool){
        s.isGood[_a] = _good;
        
        return true; // successful
    }
    
    function showGood(hold storage s,uint _a) external view returns (bool) {
        return s.isGood[_a];
    }

}

数学.sol

pragma solidity ^0.6.4;


import { ICount } from  './ICount.sol';
import { Count } from  './Count.sol';

contract Math {
    
    using Count for ICount;
    
    address h;
    
    constructor() public {
        ICount(h).setA(123);
        ICount(h).setGood(1,true);
    }
    
    function subHold(uint a) public view  returns(uint){
        return ICount(h).subUint(a);
    }
    
    function show(uint a) public view returns ( bool){
        return ICount(h).showGood(a);
    }
}

ICount.sol

pragma solidity ^0.6.4;

import { Count } from  './Count.sol';  // this makes my code not dynamic,but I need the type declaration for functions below

interface ICount {
    function subUint(Count.hold calldata s,uint b) external view returns(uint);
    function setA(Count.hold calldata s,uint _a) external returns (bool);
    function setGood(Count.hold calldata s,bool _good) external returns (bool);
    function showGood(Count.hold calldata s,uint _a) external view returns (bool);
}

上面的代码有几个问题,我卡住了。

首先,我必须增加 Count.sol函数数量,因为 struct 不容易直接访问。没关系,如果需要的话。

其次,我尝试使用库地址并将其存储在 h 并将其转换为 with ICount(h).function 并将其用作函数。但是有很多问题。

第三,如ICount.sol所示,我不想导入Count.sol,但我需要类型Count进行函数参数类型声明。


完成上述所有工作的目的以及我尝试实现的目标。

  1. 接口旨在为未来和向后兼容性设定标准。
  2. 接口用于将地址转换为 Count 以便调用函数
  3. 使用
  4. Count.sol 是因为我的 Math.sol 变得太大而无法编译。我需要将具有相同目的的相关代码移至 Count.sol

请告知并抱歉修改后的代码无法编译,因为我完全被卡住了。

更新: This article 给出了一些提示,说明为了获得可替换的库地址可能应该做什么。但由于它有点风险,所以我认为不适合在实际生产案例中使用它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。