/
contract
80 lines (64 loc) · 2.81 KB
/
contract
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
pragma solidity ^0.6.0;
contract ItemManager{
enum SupplyChainSteps{Created, Paid, Delivered}
struct S_Item {
ItemManager.SupplyChainSteps _step;
string _identifier;
uint _priceInWei;
}
mapping(uint => S_Item) public items;
uint index;
event SupplyChainStep(uint _itemIndex, uint _step);
function createItem(string memory _identifier, uint _priceInWei) public {
items[index]._priceInWei = _priceInWei;
items[index]._step = SupplyChainSteps.Created;
items[index]._identifier = _identifier;
emit SupplyChainStep(index, uint(items[index]._step));
index++;
}
function triggerPayment(uint _index) public payable {
require(items[_index]._priceInWei <= msg.value, "Not fully paid");
require(items[_index]._step == SupplyChainSteps.Created, "Item is further in the supply chain");
items[_index]._step = SupplyChainSteps.Paid;
emit SupplyChainStep(_index, uint(items[_index]._step));
}
function triggerDelivery(uint _index) public {
require(items[_index]._step == SupplyChainSteps.Paid, "Item is further in the supply chain");
items[_index]._step = SupplyChainSteps.Delivered;
emit SupplyChainStep(_index, uint(items[_index]._step));
}
}
pragma solidity ^0.6.0;
import "./Item.sol";
contract ItemManager {
struct S_Item {
Item _item;
ItemManager.SupplyChainSteps _step;
string _identifier;
}
mapping(uint => S_Item) public items;
uint index;
enum SupplyChainSteps {Created, Paid, Delivered}
event SupplyChainStep(uint _itemIndex, uint _step, address _address);
function createItem(string memory _identifier, uint _priceInWei) public {
Item item = new Item(this, _priceInWei, index);
items[index]._item = item;
items[index]._step = SupplyChainSteps.Created;
items[index]._identifier = _identifier;
emit SupplyChainStep(index, uint(items[index]._step), address(item));
index++;
}
function triggerPayment(uint _index) public payable {
Item item = items[_index]._item;
require(address(item) == msg.sender, "Only items are allowed to update themselves");
require(item.priceInWei() == msg.value, "Not fully paid yet");
require(items[_index]._step == SupplyChainSteps.Created, "Item is further in the supply chain");
items[_index]._step = SupplyChainSteps.Paid;
emit SupplyChainStep(_index, uint(items[_index]._step), address(item));
}
function triggerDelivery(uint _index) public {
require(items[_index]._step == SupplyChainSteps.Paid, "Item is further in the supply chain");
items[_index]._step = SupplyChainSteps.Delivered;
emit SupplyChainStep(_index, uint(items[_index]._step), address(items[_index]._item));
}
}