-
Notifications
You must be signed in to change notification settings - Fork 4
/
SupplyChain.sol
433 lines (371 loc) · 15.7 KB
/
SupplyChain.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
pragma solidity ^0.4.24;
import "../pharmaceuticalcore/Ownable.sol";
import "../pharmaceuticalaccesscontrol/ConsumerRole.sol";
import "../pharmaceuticalaccesscontrol/DistributorRole.sol";
import "../pharmaceuticalaccesscontrol/ManufacturerRole.sol";
import "../pharmaceuticalaccesscontrol/RetailerRole.sol";
// Define a contract 'Supplychain'
contract SupplyChain is Ownable, ConsumerRole, ManufacturerRole, RetailerRole, DistributorRole {
// Define a variable called 'upc' for Universal Product Code (UPC)
uint upc;
// Define a variable called 'sku' for Stock Keeping Unit (SKU)
uint sku;
// Define a public mapping 'items' that maps the UPC to an Item.
mapping (uint => Item) items;
// Define a public mapping 'itemsHistory' that maps the UPC to an array of TxHash,
// that track its journey through the supply chain -- to be sent from DApp.
mapping (uint => string[]) itemsHistory;
// Define enum 'State' with the following values:
enum State {
Manufactured, // 0
Packaged, // 1
ForSale, // 2
Sold, // 3
Shipped, // 4
Received, // 5
Purchased // 6
}
State constant defaultState = State.Manufactured;
// Define a struct 'Item' with the following fields:
struct Item {
uint sku; // Stock Keeping Unit (SKU)
uint upc; // Universal Product Code (UPC), generated by the Manufacturer, goes on the package, can be verified by the Consumer
address ownerID; // Metamask-Ethereum address of the current owner as the product moves through 8 stages
address originManufacturerID; // Metamask-Ethereum address of the Manufacturer
string originManufacturerName; // Manufacturer Name
string originManufacturerInformation; // Manufacturer Information
string originManufacturerLatitude; //Manufacturer Latitude
string originManufacturerLongitude; // Manufacturer Longitude
uint productID; // Product ID potentially a combination of upc + sku
string productNotes; // Product Notes
uint productPrice; // Product Price
State itemState; // Product State as represented in the enum above
address distributorID; // Metamask-Ethereum address of the Distributor
address retailerID; // Metamask-Ethereum address of the Retailer
address consumerID; // Metamask-Ethereum address of the Consumer
}
// Define 8 events with the same 8 state values and accept 'upc' as input argument
event Manufactured(uint upc);
event Packaged(uint upc);
event ForSale(uint upc);
event Sold(uint upc);
event Shipped(uint upc);
event Received(uint upc);
event Purchased(uint upc);
// Define a modifer that verifies the Caller
modifier verifyCaller (address _address) {
require(msg.sender == _address, "the sender address does not match");
_;
}
// Define a modifier that checks if the paid amount is sufficient to cover the price
modifier paidEnough(uint _price) {
require(msg.value >= _price, "insufficient payment");
_;
}
// Define a modifier that checks the price and refunds the remaining balance
modifier checkValue(uint _upc) {
_;
uint _price = items[_upc].productPrice;
uint amountToReturn = msg.value - _price;
items[_upc].consumerID.transfer(amountToReturn);
}
// Define a modifier that checks if an item.state of a upc is Manufactured
modifier manufactured(uint _upc) {
require(items[_upc].itemState == State.Manufactured, "item not manufactured");
_;
}
// Define a modifier that checks if an item.state of a upc is Packed
modifier packaged(uint _upc) {
require(items[_upc].itemState == State.Packaged, "item not packaged");
_;
}
// Define a modifier that checks if an item.state of a upc is ForSale
modifier forSale(uint _upc) {
require(items[_upc].itemState == State.ForSale, "item not for sale");
_;
}
// Define a modifier that checks if an item.state of a upc is Sold
modifier sold(uint _upc) {
require(items[_upc].itemState == State.Sold, "item not sold");
_;
}
// Define a modifier that checks if an item.state of a upc is Shipped
modifier shipped(uint _upc) {
require(items[_upc].itemState == State.Shipped, "item not shipped");
_;
}
// Define a modifier that checks if an item.state of a upc is Received
modifier received(uint _upc) {
require(items[_upc].itemState == State.Received, "item not received");
_;
}
// Define a modifier that checks if an item.state of a upc is Purchased
modifier purchased(uint _upc) {
require(items[_upc].itemState == State.Purchased, "item not purchased");
_;
}
// Define a modifier that checks if the msg.sender can sell the item.
modifier canSell(uint _upc) {
require(canAccountSellItem(msg.sender, _upc), "the sender cannot sell the item");
_;
}
// Define a modifier that checks if the msg.sender can ship the item
modifier canShip(uint _upc) {
require(canAccountShipItem(msg.sender, _upc), "the sender cannot ship the item");
_;
}
// Define a modifier that checks if the msg.sender can buy the item
modifier canBuy(uint _upc) {
require(canAccountBuyItem(msg.sender, _upc), "the sender cannot buy the item");
_;
}
// Define a modifier that checks if the msg.sender can package the item
modifier canPackage(uint _upc) {
require(canAccountPackageItem(msg.sender, _upc), "the sender cannot package the item");
_;
}
// Define a modifier that checks if the msg.sender can receive the item
modifier canReceive(uint _upc) {
require(canAccountReceiveItem(_upc), "the sender cannot receive the item");
_;
}
// In the constructor set 'owner' to the address that instantiated the contract
// and set 'sku' to 1
// and set 'upc' to 1
constructor() public payable {
transferOwnership(msg.sender);
upc = 1;
}
// Define a function 'kill' if required
function kill() public {
if (msg.sender == owner()) {
selfdestruct(owner());
}
}
// Define a function 'manufactureItem' that allows a manufacturer to mark an item 'Manufactured'
function manufactureItem(uint _upc, string _originManufacturerName, string _originManufacturerInformation,
string _originManufacturerLatitude, string _originManufacturerLongitude, string _productNotes) public
onlyManufacturer
{
// Add the new item as part of the Manufacturing process
// Increment sku
sku = sku + 1;
// Emit the appropriate event
emit Manufactured(_upc);
uint productId = sku + _upc;
// Add the new item to the inventory and mark it as manufactured.
items[_upc] = Item(
{sku: sku,
upc: _upc,
ownerID: msg.sender,
originManufacturerID: msg.sender,
originManufacturerName: _originManufacturerName,
originManufacturerInformation: _originManufacturerInformation,
originManufacturerLatitude: _originManufacturerLatitude,
originManufacturerLongitude: _originManufacturerLongitude,
productID: productId,
productNotes: _productNotes,
productPrice: 0,
itemState: State.Manufactured,
distributorID: 0,
retailerID: 0,
consumerID: 0});
}
// Define a function 'packItem' that allows a manufacturer or distributor to mark an item 'Packed'
function packageItem(uint _upc) public
// Call modifier to check if upc has passed previous supply chain stage
canPackage(_upc)
{
// Update the appropriate fields
items[_upc].itemState = State.Packaged;
// Emit the appropriate event
emit Packaged(_upc);
}
// Define a function 'sellItem' that allows a manufacturer to mark an item 'ForSale'
function sellItem(uint _upc, uint _price) public
// Call modifier to check if upc has passed previous supply chain stage and verify caller of this function
canSell(_upc)
{
// Update the appropriate fields
items[_upc].itemState = State.ForSale;
items[_upc].productPrice = _price;
// Emit the appropriate event
emit ForSale(upc);
}
// Define a function 'buyItem' that allows the disributor to mark an item 'Sold'
// Use the above defined modifiers to check if the item is available for sale, if the buyer has paid enough,
// and any excess ether sent is refunded back to the buyer
function buyItem(uint _upc) public payable
// Call modifier to check if upc has passed previous supply chain stage
canBuy(_upc)
// Call modifer to check if buyer has paid enough
paidEnough(items[_upc].productPrice)
// Call modifer to send any excess ether back to buyer
checkValue(_upc)
{
address buyer = msg.sender;
uint price = items[sku].productPrice;
// Update the appropriate fields - ownerID, distributorID, itemState
items[_upc].ownerID = buyer;
if(items[_upc].distributorID == address(0))
items[_upc].distributorID = buyer;
else if(items[_upc].retailerID == address(0))
items[_upc].retailerID = buyer;
else if(items[_upc].consumerID == address(0))
items[_upc].consumerID = buyer;
items[_upc].itemState = State.Sold;
// Transfer money to manufacturer
items[_upc].ownerID.transfer(price);
// emit the appropriate event
emit Sold(_upc);
}
// Define a function 'shipItem' that allows the distributor to mark an item 'Shipped'
// Use the above modifers to check if the item is sold
function shipItem(uint _upc) public
// Call modifier to check if upc has passed previous supply chain stage
canShip(_upc)
{
// Update the appropriate fields
items[_upc].itemState = State.Shipped;
// Emit the appropriate event
emit Shipped(_upc);
}
// Define a function 'receiveItem' that allows the retailer to mark an item 'Received'
// Use the above modifiers to check if the item is shipped
function receiveItem(uint _upc) public
canReceive(_upc)
{
// Update the appropriate fields - ownerID, retailerID, itemState
items[_upc].itemState = State.Received;
// Emit the appropriate event
emit Received(upc);
}
// Define a function that determines whether an account can ship the given item.
function canAccountShipItem(address _account, uint _upc) public view returns (bool) {
return (isSold(_upc) &&
((isManufacturer(_account, _upc) && items[_upc].ownerID == items[_upc].distributorID
|| isDistributor(_account, _upc) && items[_upc].ownerID == items[_upc].retailerID)));
}
// Define a function that determines whether an account can package the given item.
function canAccountPackageItem(address _account, uint _upc) public view returns (bool) {
return (isManufactured(_upc) && isManufacturer(_account, _upc));
}
// Define a function that determines whether an account can receive the given item.
function canAccountReceiveItem(uint _upc) public view returns (bool) {
return (isItemOwner(msg.sender, _upc) && isShipped(_upc));
}
// Define a function that determines whether an account can sell the given item.
function canAccountSellItem(address _account, uint _upc) public view returns (bool) {
return (isItemOwner(_account, _upc) &&
((isManufacturer(_account, _upc) && isPackaged(_upc)) ||
(isDistributor(_account, _upc) && isReceived(_upc)) ||
(isRetailer(_account, _upc) && isReceived(_upc))));
}
// Define a function that determines whether an account can sell the given item.
function canAccountBuyItem(address _account, uint _upc) public view returns (bool) {
return (isForSale(_upc) &&
((isDistributor(_account) &&
items[_upc].distributorID == address(0)) ||
(isRetailer(_account) &&
items[_upc].distributorID != address(0) &&
items[_upc].retailerID == address(0)) ||
(isConsumer(_account) &&
items[_upc].distributorID != address(0) &&
items[_upc].retailerID != address(0) &&
items[_upc].consumerID == address(0))));
}
// Define a function that determines whether the item is packaged.
function isItemOwner(address _account, uint _upc) public view returns (bool) {
return (items[_upc].ownerID == _account);
}
// Define a function that determines whether the item is packaged.
function isPackaged(uint _upc) public view returns (bool) {
return (items[_upc].itemState == State.Packaged);
}
// Define a function that determines whether the item is sold.
function isSold(uint _upc) public view returns (bool) {
return (items[_upc].itemState == State.Sold);
}
// Define a function that determines whether the item is shipped.
function isShipped(uint _upc) public view returns (bool) {
return (items[_upc].itemState == State.Shipped);
}
// Define a function that determines whether the item is for sale.
function isForSale(uint _upc) public view returns (bool) {
return (items[_upc].itemState == State.ForSale);
}
// Define a function that determines whether the item was received.
function isReceived(uint _upc) public view returns (bool) {
return (items[_upc].itemState == State.Received);
}
// Define a function that determines whether the item was manufactured.
function isManufactured(uint _upc) public view returns (bool) {
return (items[_upc].itemState == State.Manufactured);
}
// Define a function that determines whether the given account manufactured the
// given item.
function isManufacturer(address _account, uint _upc) internal view returns (bool) {
return (isManufacturer(_account) && _account == items[_upc].originManufacturerID);
}
// Define a function that determines whether the given account is the distributor
// of the given item.
function isDistributor(address _account, uint _upc) internal view returns (bool) {
return (isDistributor(_account) && _account == items[_upc].distributorID);
}
// Define a function that determines whether the given account is the retailer
// of the given item.
function isRetailer(address _account, uint _upc) internal view returns (bool) {
return (isRetailer(_account) && _account == items[_upc].retailerID);
}
// Define a function 'fetchItemBufferOne' that fetches the data
function fetchItemBufferOne(uint _upc) public view returns
(
uint itemSKU,
uint itemUPC,
address ownerID,
address originManufacturerID,
string originManufacturerName,
string originManufacturerInformation,
string originManufacturerLatitude,
string originManufacturerLongitude
)
{
// Assign values to the 8 parameters
itemSKU = items[_upc].sku;
itemUPC = items[_upc].upc;
ownerID = items[_upc].ownerID;
originManufacturerID = items[_upc].originManufacturerID;
originManufacturerName = items[_upc].originManufacturerName;
originManufacturerInformation = items[_upc].originManufacturerInformation;
originManufacturerLatitude = items[_upc].originManufacturerLatitude;
originManufacturerLongitude = items[_upc].originManufacturerLongitude;
return (itemSKU, itemUPC, ownerID, originManufacturerID, originManufacturerName, originManufacturerInformation,
originManufacturerLatitude, originManufacturerLongitude);
}
// Define a function 'fetchItemBufferTwo' that fetches the data
function fetchItemBufferTwo(uint _upc) public view returns
(
uint itemSKU,
uint itemUPC,
uint productID,
string productNotes,
uint productPrice,
uint itemState,
address distributorID,
address retailerID,
address consumerID
)
{
// Assign values to the 9 parameters
itemSKU = items[_upc].sku;
itemUPC = items[_upc].upc;
productID = items[_upc].productID;
productNotes = items[_upc].productNotes;
productPrice = items[_upc].productPrice;
itemState = uint(items[_upc].itemState);
distributorID = items[_upc].distributorID;
retailerID = items[_upc].retailerID;
consumerID = items[_upc].consumerID;
return (itemSKU, itemUPC, productID, productNotes, productPrice, itemState, distributorID, retailerID, consumerID);
}
}