-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleSupplyChain.sol
64 lines (47 loc) · 1.99 KB
/
SimpleSupplyChain.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
pragma solidity ^0.6.0;
import "./Whitelistable.sol";
import "./PaymentReceiver.sol";
contract SimpleSupplyChain is Whitelistable {
address constant private ADDRESS_ZERO = address(0);
mapping(uint => Item) public items;
uint public itemsCount;
event ItemListed(uint indexed itemId, uint price, string name, address indexed paymentReceiver, address listedBy);
event ItemPaid(uint indexed itemId);
event ItemSent(uint indexed itemId, address indexed sentBy);
enum ItemState{ NotExisting, Listed, Paid, Sent }
struct Item {
PaymentReceiver paymentReceiver;
string name;
uint price;
ItemState state;
}
function listItem(string memory _name, uint _price) public onlyWhitelisted{
Item memory newItem = Item({
name: _name,
price: _price,
state: ItemState.Listed,
paymentReceiver: new PaymentReceiver(this, _price, itemsCount)
});
emit ItemListed(itemsCount, _price, _name, address(newItem.paymentReceiver), msg.sender);
items[itemsCount++] = newItem;
}
function payForItem(uint _itemId) payable public {
require(items[_itemId].state == ItemState.Listed, "Only listed items can be paid");
require(msg.value == items[_itemId].price, "Pay exact price");
items[_itemId].state = ItemState.Paid;
emit ItemPaid(_itemId);
}
function sendItem(uint _itemId) public onlyWhitelisted {
require(items[_itemId].state == ItemState.Paid, "You can send only paid items");
items[_itemId].state = ItemState.Sent;
emit ItemSent(_itemId, msg.sender);
}
receive() payable external {
revert("We dont want your money");
}
fallback() payable external {
// TODO: allow to recieve funds with itemId in msg.data
// https://solidity.readthedocs.io/en/v0.6.6/contracts.html#fallback-function
revert("Not implemented yet");
}
}