/
SupplyChain.sol
539 lines (488 loc) 路 19.9 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../ownership/Ownable.sol";
import "../access/FarmerRole.sol";
import "../access/DistributorRole.sol";
import "../access/RetailerRole.sol";
import "../access/ConsumerRole.sol";
// Define a contract 'Supplychain'
contract SupplyChain is
Ownable,
FarmerRole,
DistributorRole,
RetailerRole,
ConsumerRole
{
// Define 'owner'
address owner;
// Define a variable called 'productCode' for Universal Product Code (productCode)
uint256 productCode;
// Define a variable called 'stockUnit' for Stock Keeping Unit (stockUnit)
uint256 stockUnit;
// Define a public mapping 'items' that maps the productCode to an Item.
mapping(uint256 => Item) items;
// Define a public mapping 'itemsHistory' that maps the productCode to an array of TxHash,
// that track its journey through the supply chain -- to be sent from DApp.
mapping(uint256 => Txblocks) itemsHistory;
// Define enum 'State' with the following values:
enum State {
ProduceByFarmer, // 0
ForSaleByFarmer, // 1
PurchasedByDistributor, // 2
ShippedByFarmer, // 3
ReceivedByDistributor, // 4
ProcessedByDistributor, // 5
PackageByDistributor, // 6
ForSaleByDistributor, // 7
PurchasedByRetailer, // 8
ShippedByDistributor, // 9
ReceivedByRetailer, // 10
ForSaleByRetailer, // 11
PurchasedByConsumer // 12
}
State constant defaultState = State.ProduceByFarmer;
// Define a struct 'Item' with the following fields:
struct Item {
uint256 stockUnit; // Stock Keeping Unit (stockUnit)
uint256 productCode; // Universal Product Code (productCode), generated by the Farmer, 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 farmerID; // Metamask-Ethereum address of the Farmer // ADDED PAYABLE
string farmName; // Farmer Name
string farmInformation; // Farmer Information
string farmLatitude; // Farm Latitude
string farmLongitude; // Farm Longitude
uint256 productID; // Product ID potentially a combination of productCode + stockUnit
string productNotes; // Product Notes
uint256 productDate; // Product Date NOTE: MIGHT NEED TO CHANGE type
uint256 productPrice; // Product Price
uint256 productSliced; // Parent product
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 // ADDED payable
}
// Block number stuct
struct Txblocks {
uint256 FTD; // blockfarmerToDistributor
uint256 DTR; // blockDistributorToRetailer
uint256 RTC; // blockRetailerToConsumer
}
event ProduceByFarmer(uint256 productCode); //1
event ForSaleByFarmer(uint256 productCode); //2
event PurchasedByDistributor(uint256 productCode); //3
event ShippedByFarmer(uint256 productCode); //4
event ReceivedByDistributor(uint256 productCode); //5
event ProcessedByDistributor(uint256 productCode); //6
event PackagedByDistributor(uint256 productCode); //7
event ForSaleByDistributor(uint256 productCode); //8
event PurchasedByRetailer(uint256 productCode); //9
event ShippedByDistributor(uint256 productCode); //10
event ReceivedByRetailer(uint256 productCode); //11
event ForSaleByRetailer(uint256 productCode); //12
event PurchasedByConsumer(uint256 productCode); //13
// Define a modifer that checks to see if _msgSender() == owner of the contract
modifier only_Owner() {
require(_msgSender() == owner);
_;
}
// Define a modifer that verifies the Caller
modifier verifyCaller(address _address) {
require(_msgSender() == _address);
_;
}
// Define a modifier that checks if the paid amount is sufficient to cover the price
modifier paidEnough(uint256 _price) {
require(msg.value >= _price);
_;
}
// Define a modifier that checks the price and refunds the remaining balance
modifier checkValue(uint256 _productCode, address payable addressToFund) {
uint256 _price = items[_productCode].productPrice;
uint256 amountToReturn = msg.value - _price;
addressToFund.transfer(amountToReturn);
_;
}
//Item State Modifiers
modifier producedByFarmer(uint256 _productCode) {
require(items[_productCode].itemState == State.ProduceByFarmer);
_;
}
modifier forSaleByFarmer(uint256 _productCode) {
require(items[_productCode].itemState == State.ForSaleByFarmer);
_;
}
modifier purchasedByDistributor(uint256 _productCode) {
require(items[_productCode].itemState == State.PurchasedByDistributor);
_;
}
modifier shippedByFarmer(uint256 _productCode) {
require(items[_productCode].itemState == State.ShippedByFarmer);
_;
}
modifier receivedByDistributor(uint256 _productCode) {
require(items[_productCode].itemState == State.ReceivedByDistributor);
_;
}
modifier processByDistributor(uint256 _productCode) {
require(items[_productCode].itemState == State.ProcessedByDistributor);
_;
}
modifier packagedByDistributor(uint256 _productCode) {
require(items[_productCode].itemState == State.PackageByDistributor);
_;
}
modifier forSaleByDistributor(uint256 _productCode) {
require(items[_productCode].itemState == State.ForSaleByDistributor);
_;
}
modifier shippedByDistributor(uint256 _productCode) {
require(items[_productCode].itemState == State.ShippedByDistributor);
_;
}
modifier purchasedByRetailer(uint256 _productCode) {
require(items[_productCode].itemState == State.PurchasedByRetailer);
_;
}
modifier receivedByRetailer(uint256 _productCode) {
require(items[_productCode].itemState == State.ReceivedByRetailer);
_;
}
modifier forSaleByRetailer(uint256 _productCode) {
require(items[_productCode].itemState == State.ForSaleByRetailer);
_;
}
modifier purchasedByConsumer(uint256 _productCode) {
require(items[_productCode].itemState == State.PurchasedByConsumer);
_;
}
// constructor setup owner stockUnit productCode
constructor() public payable {
owner = _msgSender();
stockUnit = 1;
productCode = 1;
}
// Define a function 'kill'
function kill() public {
if (_msgSender() == owner) {
address payable ownerAddressPayable = _make_payable(owner);
selfdestruct(ownerAddressPayable);
}
}
// allows you to convert an address into a payable address
function _make_payable(address x) internal pure returns (address payable) {
return payable(address(uint160(x)));
}
/*
1st step in supplychain
Allows farmer to create product
*/
function produceItemByFarmer(
uint256 _productCode,
string memory _farmName,
string memory _farmInformation,
string memory _farmLatitude,
string memory _farmLongitude,
string memory _productNotes,
uint256 _price
)
public
onlyFarmer // check address belongs to farmerRole
{
address distributorID; // Empty distributorID address
address retailerID; // Empty retailerID address
address consumerID; // Empty consumerID address
Item memory newProduce; // Create a new struct Item in memory
newProduce.stockUnit = stockUnit; // Stock Keeping Unit (stockUnit)
newProduce.productCode = _productCode; // Universal Product Code (productCode), generated by the Farmer, goes on the package, can be verified by the Consumer
newProduce.ownerID = _msgSender(); // Metamask-Ethereum address of the current owner as the product moves through 8 stages
newProduce.farmerID = _msgSender(); // Metamask-Ethereum address of the Farmer
newProduce.farmName = _farmName; // Farmer Name
newProduce.farmInformation = _farmInformation; // Farmer Information
newProduce.farmLatitude = _farmLatitude; // Farm Latitude
newProduce.farmLongitude = _farmLongitude; // Farm Longitude
newProduce.productID = _productCode + stockUnit; // Product ID
newProduce.productNotes = _productNotes; // Product Notes
newProduce.productPrice = _price; // Product Price
newProduce.productDate = block.timestamp;
newProduce.productSliced = 0;
newProduce.itemState = defaultState; // Product State as represented in the enum above
newProduce.distributorID = distributorID; // Metamask-Ethereum address of the Distributor
newProduce.retailerID = retailerID; // Metamask-Ethereum address of the Retailer
newProduce.consumerID = consumerID; // Metamask-Ethereum address of the Consumer // ADDED payable
items[_productCode] = newProduce; // Add newProduce to items struct by productCode
uint256 placeholder; // Block number place holder
Txblocks memory txBlock; // create new txBlock struct
txBlock.FTD = placeholder; // assign placeholder values
txBlock.DTR = placeholder;
txBlock.RTC = placeholder;
itemsHistory[_productCode] = txBlock; // add txBlock to itemsHistory mapping by productCode
// Increment stockUnit
stockUnit = stockUnit + 1;
// Emit the appropriate event
emit ProduceByFarmer(_productCode);
}
/*
2nd step in supplychain
Allows farmer to sell product
*/
function sellItemByFarmer(uint256 _productCode, uint256 _price)
public
onlyFarmer // check _msgSender() belongs to farmerRole
producedByFarmer(_productCode) // check items state has been produced
verifyCaller(items[_productCode].ownerID) // check _msgSender() is owner
{
items[_productCode].itemState = State.ForSaleByFarmer;
items[_productCode].productPrice = _price;
emit ForSaleByFarmer(_productCode);
}
/*
3rd step in supplychain
Allows distributor to purchase product
*/
function purchaseItemByDistributor(uint256 _productCode)
public
payable
onlyDistributor // check _msgSender() belongs to distributorRole
forSaleByFarmer(_productCode) // check items state is for ForSaleByFarmer
paidEnough(items[_productCode].productPrice) // check if distributor sent enough Ether for product
checkValue(_productCode, payable(_msgSender())) // check if overpayed return remaing funds back to _msgSender()
{
address payable ownerAddressPayable = _make_payable(
items[_productCode].farmerID
); // make originFarmID payable
ownerAddressPayable.transfer(items[_productCode].productPrice); // transfer funds from distributor to farmer
items[_productCode].ownerID = _msgSender(); // update owner
items[_productCode].distributorID = _msgSender(); // update distributor
items[_productCode].itemState = State.PurchasedByDistributor; // update state
itemsHistory[_productCode].FTD = block.number; // add block number
emit PurchasedByDistributor(_productCode);
}
/*
4th step in supplychain
Allows farmer to ship product purchased by distributor
*/
function shippedItemByFarmer(uint256 _productCode)
public
payable
onlyFarmer // check _msgSender() belongs to FarmerRole
purchasedByDistributor(_productCode)
verifyCaller(items[_productCode].farmerID) // check _msgSender() is originFarmID
{
items[_productCode].itemState = State.ShippedByFarmer; // update state
emit ShippedByFarmer(_productCode);
}
/*
5th step in supplychain
Allows distributor to receive product
*/
function receivedItemByDistributor(uint256 _productCode)
public
onlyDistributor // check _msgSender() belongs to DistributorRole
shippedByFarmer(_productCode)
verifyCaller(items[_productCode].ownerID) // check _msgSender() is owner
{
items[_productCode].itemState = State.ReceivedByDistributor; // update state
emit ReceivedByDistributor(_productCode);
}
/*
6th step in supplychain
Allows distributor to process product
*/
function processedItemByDistributor(uint256 _productCode, uint256 slices)
public
onlyDistributor // check _msgSender() belongs to DistributorRole
receivedByDistributor(_productCode)
verifyCaller(items[_productCode].ownerID) // check _msgSender() is owner
{
items[_productCode].itemState = State.ProcessedByDistributor; // update state
items[_productCode].productSliced = slices; // add slice amount
emit ProcessedByDistributor(_productCode);
}
/*
7th step in supplychain
Allows distributor to package product
*/
function packageItemByDistributor(uint256 _productCode)
public
onlyDistributor // check _msgSender() belongs to DistributorRole
processByDistributor(_productCode)
verifyCaller(items[_productCode].ownerID) // check _msgSender() is owner
{
items[_productCode].itemState = State.PackageByDistributor;
emit PackagedByDistributor(_productCode);
}
/*
8th step in supplychain
Allows distributor to sell product
*/
function sellItemByDistributor(uint256 _productCode, uint256 _price)
public
onlyDistributor // check _msgSender() belongs to DistributorRole
packagedByDistributor(_productCode)
verifyCaller(items[_productCode].ownerID) // check _msgSender() is owner
{
items[_productCode].itemState = State.ForSaleByDistributor;
items[_productCode].productPrice = _price;
emit ForSaleByDistributor(productCode);
}
/*
9th step in supplychain
Allows retailer to purchase product
*/
function purchaseItemByRetailer(uint256 _productCode)
public
payable
onlyRetailer // check _msgSender() belongs to RetailerRole
forSaleByDistributor(_productCode)
paidEnough(items[_productCode].productPrice)
checkValue(_productCode, payable(_msgSender()))
{
address payable ownerAddressPayable = _make_payable(
items[_productCode].distributorID
);
ownerAddressPayable.transfer(items[_productCode].productPrice);
items[_productCode].ownerID = _msgSender();
items[_productCode].retailerID = _msgSender();
items[_productCode].itemState = State.PurchasedByRetailer;
itemsHistory[_productCode].DTR = block.number;
emit PurchasedByRetailer(_productCode);
}
/*
10th step in supplychain
Allows Distributor to
*/
function shippedItemByDistributor(uint256 _productCode)
public
onlyDistributor // check _msgSender() belongs to DistributorRole
purchasedByRetailer(_productCode)
verifyCaller(items[_productCode].distributorID) // check _msgSender() is distributorID
{
items[_productCode].itemState = State.ShippedByDistributor;
emit ShippedByDistributor(_productCode);
}
/*
11th step in supplychain
*/
function receivedItemByRetailer(uint256 _productCode)
public
onlyRetailer // check _msgSender() belongs to RetailerRole
shippedByDistributor(_productCode)
verifyCaller(items[_productCode].ownerID) // check _msgSender() is ownerID
{
items[_productCode].itemState = State.ReceivedByRetailer;
emit ReceivedByRetailer(_productCode);
}
/*
12th step in supplychain
*/
function sellItemByRetailer(uint256 _productCode, uint256 _price)
public
onlyRetailer // check _msgSender() belongs to RetailerRole
receivedByRetailer(_productCode)
verifyCaller(items[_productCode].ownerID) // check _msgSender() is ownerID
{
items[_productCode].itemState = State.ForSaleByRetailer;
items[_productCode].productPrice = _price;
emit ForSaleByRetailer(_productCode);
}
/*
13th step in supplychain
*/
function purchaseItemByConsumer(uint256 _productCode)
public
payable
onlyConsumer // check _msgSender() belongs to ConsumerRole
forSaleByRetailer(_productCode)
paidEnough(items[_productCode].productPrice)
checkValue(_productCode, payable(_msgSender()))
{
items[_productCode].consumerID = _msgSender();
address payable ownerAddressPayable = _make_payable(
items[_productCode].retailerID
);
ownerAddressPayable.transfer(items[_productCode].productPrice);
items[_productCode].ownerID = _msgSender();
items[_productCode].consumerID = _msgSender();
items[_productCode].itemState = State.PurchasedByConsumer;
itemsHistory[_productCode].RTC = block.number;
emit PurchasedByConsumer(_productCode);
}
// Define a function 'fetchItemBufferOne' that fetches the data
function fetchItemBufferOne(uint256 _productCode)
public
view
returns (
uint256 itemstockUnit,
uint256 itemproductCode,
address ownerID,
address farmerID,
string memory farmName,
string memory farmInformation,
string memory farmLatitude,
string memory farmLongitude,
uint256 productDate,
uint256 productSliced
)
{
// Assign values to the 8 parameters
Item memory item = items[_productCode];
return (
item.stockUnit,
item.productCode,
item.ownerID,
item.farmerID,
item.farmName,
item.farmInformation,
item.farmLatitude,
item.farmLongitude,
item.productDate,
item.productSliced
);
}
// Define a function 'fetchItemBufferTwo' that fetches the data
function fetchItemBufferTwo(uint256 _productCode)
public
view
returns (
uint256 itemstockUnit,
uint256 itemproductCode,
uint256 productID,
string memory productNotes,
uint256 productPrice,
uint256 productDate,
State itemState,
address distributorID,
address retailerID,
address consumerID
)
{
// Assign values to the 9 parameters
Item memory item = items[_productCode];
return (
item.stockUnit,
item.productCode,
item.productID,
item.productNotes,
item.productPrice,
item.productDate,
item.itemState,
item.distributorID,
item.retailerID,
item.consumerID
);
}
// Define a function 'fetchItemHistory' that fetaches the data
function fetchitemHistory(uint256 _productCode)
public
view
returns (
uint256 blockfarmerToDistributor,
uint256 blockDistributorToRetailer,
uint256 blockRetailerToConsumer
)
{
// Assign value to the parameters
Txblocks memory txblock = itemsHistory[_productCode];
return (txblock.FTD, txblock.DTR, txblock.RTC);
}
}