如何解决Solidity Enum 结构内部没有在 2/3 函数中更新......在 1/3
我有以下测试智能合约:
pragma solidity ^0.8.4;
contract Test {
mapping(uint => S_Item) public items;
uint itemIndex;
enum State {
Created,Paid,Delivered
}
struct S_Item {
string _identifier;
uint _itemPrice;
State _state;
}
State public state;
event SupplyChainStep(uint _itemIndex,uint _step);
function createItem(string memory _identifier,uint _itemPrice) public {
items[itemIndex]._identifier = _identifier;
items[itemIndex]._itemPrice = _itemPrice;
state = State.Created;
items[itemIndex]._state = state;
emit SupplyChainStep(itemIndex,uint(items[itemIndex]._state));
itemIndex++;
}
function triggerPayment(uint _itemIndex) public payable {
require(items[_itemIndex]._itemPrice == msg.value,"Only full payments accepted");
require(items[_itemIndex]._state == State.Created,"Item is further in the chain");
state = State.Paid;
items[itemIndex]._state = state;
emit SupplyChainStep(itemIndex,uint(items[itemIndex]._state));
}
function triggerDelivery(uint _itemIndex) public {
require(items[_itemIndex]._state == State.Paid,"Item is further in the chain");
state = State.Delivered;
items[itemIndex]._state = state;
emit SupplyChainStep(itemIndex,uint(items[itemIndex]._state));
}
}
createItem 函数正确更新了项目映射 _identifier 和 _itemPrice 属性。 _state 属性的默认值为 0,但如果我更改顺序以使 Created 在枚举中占据位置 1 而不是 0,则 item _state 枚举属性会像我预期的那样更新为数字 1。
另外两个函数不会在item映射中推进状态。它保持在项目映射中的任何当前值,其代码与在 createItem 函数中工作的代码相同。
注意...
state = State.XXX
...这些函数中的每一个中的赋值 - 我将其用于故障排除 - 当运行 triggerPayment 或 triggerDelivery 函数时,状态会正确填充到名为 state 的公共变量中。这三个函数有效,但项目映射 _state 属性拒绝以相同的方式更新,这就是我需要工作的。
谁能帮助我理解我可以做些什么来让所有函数按预期推进项目映射 _state 属性,就像 createItems 函数似乎没有问题一样?
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。