返回错误:VM Exception while processing transaction: revert



我试图使用映射结构体和数组创建合同。我的目标是允许公司在这个系统中保存一些数据。

我可以在Remix上部署它,但是当我想与功能saveStandard交互时,我遇到了Returned error: VM Exception while processing transaction: revert的问题。

然而,如果我去掉合约中的数组部分,它就会运行得很好。有人知道发生了什么吗?

下面是我的代码(带数组)
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Storage{

struct UserInfo {
uint index;
string[] time;
string[] source;
uint[] emission;
}

mapping (string => UserInfo) public users;
function saveStandard(uint input_material, uint weight, string memory input_time, string memory input_source) 
public returns (string memory, uint, uint, string memory){

uint id = users[input_source].index + 1;
users[input_source].index = id;

users[input_source].time[id] = input_time;
uint Combustion = weight * input_material / 10;
uint Process = weight * input_material / 10;
users[input_source].emission[id] = Combustion + Process;

return (input_source, Combustion, Process, input_time);
}

function search(string memory input_source) public view returns (uint, string memory) {
uint id = 0;
while (id <= users[input_source].index) {
id += 1;
return (users[input_source].emission[id], users[input_source].time[id]);
}
}
}

下面是一个没有数组的,这个工作得很好。

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Storage{

struct UserInfo {
string time;
string source;
uint emission;
}

mapping (string => UserInfo) public users;
function saveStandard(uint input_material, uint weight, string memory input_time, string memory input_source) 
public returns (string memory, uint, uint, string memory){

users[input_source].time = input_time;
uint Combustion = weight * input_material / 10;
uint Process = weight * input_material / 10;
users[input_source].emission = Combustion + Process;

return (input_source, Combustion, Process, input_time);
}

function search(string memory input_source) public view returns (uint, string memory) {
return (users[input_source].emission, users[input_source].time);
}
}
users[input_source].time[id] = input_time;
users[input_source].emission[id] = Combustion + Process;

就是这些行导致了回复。

您正在尝试分配给time[id]数组项,但id索引不存在。


您的用例将更适合使用mappings

struct UserInfo {
uint index;
mapping(uint => string) time;
mapping(uint => string) source;
mapping(uint => uint) emission;
}

然后你可以简单地通过id键赋值:

users[input_source].time[id] = input_time;
users[input_source].emission[id] = Combustion + Process;

相关内容

  • 没有找到相关文章

最新更新