gpt4 book ai didi

solidity - Require() 函数在下面的代码中无法正常工作

转载 作者:行者123 更新时间:2023-12-02 20:03:57 31 4
gpt4 key购买 nike

在 Rinkeyby 测试网络的 InjectedWeb3 环境中的 Remix IDE 上部署此合约。

我尝试删除 require 中的错误消息语句,然后它不会抛出错误,但仍然无法正常工作,即无论任何 require 条件如何,该函数都会被执行。

pragma solidity >=0.4.22 <0.7.0;
contract RegisterLand{

struct land{
uint area;
string location;
uint floorsAllowed;
mapping(uint => address) owner;
uint count;
bool idExists;
}
mapping(uint => land) lands;
function Register(uint id,uint area, string memory location, uint
floorsAllowed) public
{
require(
!lands[id].idExists,
"ID already exists"
);
lands[id] = land(area, location, floorsAllowed,0,true);
lands[id].owner[lands[id].count] = msg.sender;
}
function ViewLand(uint id) public view returns(address currentOwner,
uint
landArea, string memory landLocation, uint landFloors )
{
require(lands[id].idExists,
"Id doesn't exist.");
currentOwner = lands[id].owner[lands[id].count];
landArea = lands[id].area;
landLocation = lands[id].location;
landFloors = lands[id].floorsAllowed;
}
}

错误:

Failed to decode output: Error: overflow (operation="setValue", fault="overflow", details="Number can only safely store up to 53 bits", version=4.0.32)

最佳答案

有一个已知问题,要求 View /纯函数不会在公共(public)网络上恢复: https://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-networks/1211

如果您使用 Remix JavaScript VM,则使用不存在的 id 调用 ViewLand 会按预期恢复。

如果您对 dapp 开发有疑问,也可以在 OpenZeppelin 社区论坛中提问:https://forum.openzeppelin.com/

披露:我是 OpenZeppelin 的社区经理

关于solidity - Require() 函数在下面的代码中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770312/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com