[英]Design implementation: Using Structs inside Libraries with interface and dynamic lib address
基于一些关于使用带有结构的库的文章。
以下是示例文章中的示例代码。
计数溶胶
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;
}
}
数学溶胶
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
访问它。
但是我很困惑我应该如何写它,因为没有关于它的文章可以再对此进行研究。
计数溶胶
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];
}
}
数学溶胶
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, uint _a, bool _good) external returns (bool);
function showGood(Count.hold calldata s, uint _a) external view returns (bool);
}
上面的代码有几个问题,我卡住了。
首先,我必须增加Count.sol
的函数数量,因为 struct 不容易直接访问。 没关系,如果需要的话。
其次,我尝试使用库的地址并将其存储在h
并将其转换为ICount(h).function
并将其用作函数。 但是有很多问题。
第三,如ICount.sol
所示,我不想导入Count.sol
,但我需要类型Count
用于函数参数类型声明。
做上述所有事情的目的以及我试图实现的目标。
Count
以便调用函数。Count.sol
是因为我的Math.sol
变得太大而无法编译。 我需要将具有相同目的的相关代码移动到Count.sol
请告知并抱歉修改后的代码无法编译,因为我完全被卡住了。
更新: 本文提供了一些提示,说明为了获得可替换的库地址可能应该做什么。 但是因为它有点风险所以我认为它不适合在实际生产案例中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.