I have the following test smart contract:
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));
}
}
The createItem function correctly updates the items mappings _identifier and _itemPrice properties. The default value is 0 for the _state property, but if I change the order to make Created occupy position 1 instead of 0 in the enum, the item _state enum property gets updated to the number 1 like I expect.
The other two functions will not advance the state in the item mapping. It stays at whatever the current value is in the item mapping with the same code that works in the createItem function.
Notice the...
state = State.XXX
...assignments in each one of these functions - i threw that in to troubleshoot - when the triggerPayment or triggerDelivery functions are ran, state is correctly populated in the public variable named state. in all three functions this works, but the item mappings _state property refuses to be updated in the same way, and thats what I need to work.
Can anyone help me understand what I can do to get all functions to advance the item mapping _state property as expected like the createItems function seems to be doing without issue?
Thank you!
