我有下面的映射,为用户的利害关系在坚实。
mapping(address => AllUserStakedTimestamp) internal allUserStakes;
struct AllUserStakedTimestamp {
bool[] _wasUnstaked;
bool[] _autoRenewal;
uint[] _amountStaked;
uint[] _timeOfStake;
uint[] _timesOfRelease;
uint[] _optionReleaseSelected; // 0-1-2
uint[] _epochDuration;
uint[] _rewardPerCycle;
uint[] _finalStakeReward;
}我现在已经完成了契约的编写,需要从前端/ web3.js中检索其中的一些数据,但是刚才我意识到我会遇到这样的错误:
TypeError: Internal or recursive type is not allowed for public state variables.
--> 11 - NewPublic.sol:72:5:72 | mapping(address => AllUserStakedTimestamp) public allUserStakes;如何使数据“公开”而不获取此错误,因为这个结构被许多其他的可靠函数使用,修改它或将其分割成较小的部分,从一开始就需要对函数和bug搜索进行全面重写。
有暗示吗?
发布于 2022-09-16 13:34:48
找到了解决方案,而不是重写整个数据结构,而是创建一个公共函数来检索数据,如下所示:
function checkStakeByIndex(uint _stakeIndexNo) public view returns(
string memory, // - 0 legend below:
bool, // - 1 WU - was unstaked
bool, // - 2 AR - automatic renewal
uint, // - 3 AS - amount staked
uint, // - 4 TOS - time of stake
uint // - 5 TOR - time of release // - 7 ED - epoch duration
){return(
"1 - WU | 2 - AR | 3 - AS | 4 - TOS | 5 - TOR :", // - 0 legend below:
allUserStakes[msg.sender]._wasUnstaked[_stakeIndexNo], // - 1 WU - was unstaked
allUserStakes[msg.sender]._autoRenewal[_stakeIndexNo], // - 2 AR - automatic renewal
allUserStakes[msg.sender]._amountStaked[_stakeIndexNo], // - 3 AS - amount staked
allUserStakes[msg.sender]._timeOfStake[_stakeIndexNo], // - 4 TOS - time of stake
allUserStakes[msg.sender]._timesOfRelease[_stakeIndexNo]); // - 5 TOR - time of release
}它不是最漂亮的,但比更改从该数据结构继承的所有代码和其他所有东西要好。
发布于 2022-09-16 12:29:43
您可以通过实现显式getter函数来实现这一点,而不需要进行重大修改。参见此示例:
mapping(address => AllUserStakedTimestamp) internal allUserStakes;
struct AllUserStakedTimestamp {
bool[] _wasUnstaked;
bool[] _autoRenewal;
uint[] _amountStaked;
uint[] _timeOfStake;
uint[] _timesOfRelease;
uint[] _optionReleaseSelected; // 0-1-2
uint[] _epochDuration;
uint[] _rewardPerCycle;
uint[] _finalStakeReward;
}
function getUsersStakes(address user) external view returns (AllUserStakedTimestamp memory) {
return allUserStakes[user];
}发布于 2022-09-16 12:57:31
mapping(address => AllUserStakedTimestamp) public allUserStakes;
struct AllUserStakedTimestamp {
bool[] _wasUnstaked;
bool[] _autoRenewal;
uint[] _amountStaked;
uint[] _timeOfStake;
uint[] _timesOfRelease;
uint[] _optionReleaseSelected; // 0-1-2
uint[] _epochDuration;
uint[] _rewardPerCycle;
uint[] _finalStakeReward;
}那个可以。在这里,你必须公开而不是内部。
只需知道在solidity中有四种类型的函数可见性:
公开:无论是合同内部还是合同外,任何人都可以调用这个函数。
私有:该函数只能在合同中使用。当您分配这个可访问性时,该函数甚至无法继承。
内部:就像私有的,但它支持继承。您可以继承该函数。
外部:函数只能由外部契约访问。如果函数可见性是外部的,这意味着它在内部不可访问。
您可以阅读更多有关函数可见性这里的信息。
告诉我是否有用!
https://ethereum.stackexchange.com/questions/135749
复制相似问题