-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent_Trigger.sol
84 lines (63 loc) · 2.46 KB
/
Event_Trigger.sol
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
81
82
83
84
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.0;
contract Ownable {
address payable _owner;
constructor() public {
_owner = msg.sender;
}
modifier onlyOwner() {
require(isOwner(), "You are not the owner");
_;
}
function isOwner() public view returns(bool) {
return (msg.sender == _owner);
}
}
contract Item {
uint public priceInWei;
uint public pricePaid;
uint public index;
ItemManager parentContract;
constructor(ItemManager _parentContract, uint _priceInWei, uint _index) public {
priceInWei = _priceInWei;
index = _index;
parentContract = _parentContract;
}
receive() external payable {
require(pricePaid == 0, "Item is paid already");
require(priceInWei == msg.value, "Only full payments allowed");
pricePaid += msg.value;
(bool success, ) = address(parentContract).call.value(msg.value)(abi.encodeWithSignature("triggerPayment(uint256)",index));
require(success, "The transaction was not successful, canceling");
}
fallback() external {}
}
contract ItemManager is Ownable {
enum SupplyChainState{Created, Paid, Delivered}
struct S_Item {
string _identifier;
uint _itemPrice;
ItemManager.SupplyChainState _state;
}
mapping(uint => S_Item) public items;
uint itemIndex;
event SupplyChainStep(uint _itemIndex, uint _step);
function createItem(string memory _identifier, uint _itemPrice) public onlyOwner {
items[itemIndex]._identifier = _identifier;
items[itemIndex]._itemPrice = _itemPrice;
items[itemIndex]._state = SupplyChainState.Created;
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 == SupplyChainState.Created, "Item is further in the chain");
items[_itemIndex]._state = SupplyChainState.Paid;
emit SupplyChainStep(_itemIndex, uint(items[_itemIndex]._state));
}
function triggerDelivery(uint _itemIndex) public onlyOwner {
require(items[_itemIndex]._state == SupplyChainState.Paid, "Item is further in the chain");
items[_itemIndex]._state = SupplyChainState.Delivered;
emit SupplyChainStep(_itemIndex, uint(items[_itemIndex]._state));
}
}