-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFemPure.sol
565 lines (492 loc) · 23.9 KB
/
FemPure.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4 <0.9.0;
import "@openzeppelin/contracts/utils/Strings.sol";
contract FemPure {
// Structs
struct User {
string pinCode;
string residenceAddress;
}
struct Partner {
string[] pinCodes;
uint256 lastOrderID;
bool approved;
bool InTransit;
}
struct Product {
string name;
string description;
uint256 priceInWei;
}
struct OrderData {
uint256 orderInitiationTime;
uint256[] productIDs;
uint256[] supplyMap;
bool partnerAssigned;
bool orderDeliveredByPartner;
bool orderReceivedByUser;
bool orderCancelled;
uint256 otp;
}
// Events
event OrderAssignedToPartner(address indexed _user, address indexed _partner, uint256 indexed _orderId);
event OrderCancelled(address indexed _partner, uint256 indexed _orderId);
event NewOrder(string _pincode);
event OrderDeliveredByPartner(address indexed _user, uint256 indexed _orderId);
event newProductsAddedToLocation(string _pincode);
event ProductsRemovedFromLocation(string _pincode);
event NewPartnerRegistered();
// Modifiers
modifier onlyOwner {
require(msg.sender == owner, "Only Owner");
_;
}
modifier notPaused {
require(contractPaused == false, "Contract is Paused");
_;
}
modifier OnlyPartner {
require(partnerIDtoPartner[msg.sender].pinCodes.length > 0, "Not a Registered Partner");
_;
}
modifier OnlyApprovedPartner {
require(partnerIDtoPartner[msg.sender].pinCodes.length > 0, "Not a Registered Partner");
require(partnerIDtoPartner[msg.sender].approved, "Not an Approved Partner");
_;
}
modifier OnlyUser {
require(!StringCompare(userIDtoUser[msg.sender].pinCode, ""), "Not a Registered User");
_;
}
// State variables
address public owner;
uint256 public orderNum;
uint256 public productNum;
bool public contractPaused;
uint256 public logisticsCost;
string public productMetadataBaseURI;
// Constructor
constructor(uint256 _logisticsCostInWei, string memory _productMetadataBaseURI) {
owner = msg.sender;
orderNum = 1;
productNum = 0;
contractPaused = false;
logisticsCost = _logisticsCostInWei;
productMetadataBaseURI = _productMetadataBaseURI;
}
// Mapping and Arrays
mapping(string => uint256[]) public pincodeToAvailableProductIDs;
string[] public availableLocations;
mapping(uint256 => Product) public productIDtoProduct;
address[] private registeredUsers;
address[] private registeredPartners;
mapping(address => User) private userIDtoUser;
mapping(address => Partner) private partnerIDtoPartner;
mapping(uint256 => OrderData) private orderIDtoOrderData;
mapping(uint256 => address) private orderIDtoUserID;
mapping(uint256 => address) private orderIDtoPartnerID;
// Functions
// Registration and User Functions
function registerPartner(string[] memory pincodes) external notPaused {
require(partnerIDtoPartner[msg.sender].pinCodes.length == 0, "Partner already Registered");
require(pincodes.length > 0, "pincode not provided");
for (uint256 i = 0; i < pincodes.length; i++) {
require(!StringCompare(pincodes[i], ""), "Pincode cannot be blank");
}
partnerIDtoPartner[msg.sender] = Partner(pincodes, 0, false, false);
registeredPartners.push(msg.sender);
emit NewPartnerRegistered();
}
function isRegisteredPartner() external view returns (bool) {
return partnerIDtoPartner[msg.sender].pinCodes.length > 0;
}
function registerUser(string memory _pincode, string memory _residenceAddress) external notPaused {
require(StringCompare(userIDtoUser[msg.sender].pinCode, ""), "Already a Registered User");
require(!StringCompare(_pincode, ""), "pincode not provided");
require(!StringCompare(_residenceAddress, ""), "residence Address not provided");
userIDtoUser[msg.sender] = User(_pincode, _residenceAddress);
registeredUsers.push(msg.sender);
}
function isRegisteredUser() external view returns (bool) {
return !StringCompare(userIDtoUser[msg.sender].pinCode, "");
}
function getUserPincode() external OnlyUser view returns (string memory) {
return userIDtoUser[msg.sender].pinCode;
}
// Product Management Functions
function addNewProduct(string memory productName, string memory productDescription, uint256 productPriceInWei) external onlyOwner {
require(!StringCompare(productName, ""), "product name cannot be empty");
require(productPriceInWei > 0, "productPrice cannot be zero");
productIDtoProduct[productNum] = Product(productName, productDescription, productPriceInWei);
productNum++;
}
function addProductstoLocation(string memory _pincode, uint256[] memory productIDs) external onlyOwner {
for (uint256 i = 0; i < productIDs.length; i++) {
require(!StringCompare(productIDtoProduct[productIDs[i]].name, ""), "Some product IDs are undefined");
}
require(productIDs.length > 0, "No product ID provided");
require(!StringCompare(_pincode, ""), "pincode cannot be empty");
uint256[] memory presentproductIDs = pincodeToAvailableProductIDs[_pincode];
if (presentproductIDs.length > 0) {
for (uint256 i = 0; i < productIDs.length; i++) {
bool ispresent = false;
for (uint256 j = 0; j < presentproductIDs.length; j++) {
if (productIDs[i] == presentproductIDs[j]) {
ispresent = true;
break;
}
}
if (!ispresent) {
bool valueAdded = false;
for (uint256 j = 0; j < pincodeToAvailableProductIDs[_pincode].length; j++) {
if (pincodeToAvailableProductIDs[_pincode][j] == productIDs[i]) {
valueAdded = true;
break;
}
}
if (!valueAdded)
pincodeToAvailableProductIDs[_pincode].push(productIDs[i]);
}
}
} else {
for (uint256 i = 0; i < productIDs.length; i++) {
bool valueAdded = false;
for (uint256 j = 0; j < pincodeToAvailableProductIDs[_pincode].length; j++) {
if (pincodeToAvailableProductIDs[_pincode][j] == productIDs[i]) {
valueAdded = true;
break;
}
}
if (!valueAdded)
pincodeToAvailableProductIDs[_pincode].push(productIDs[i]);
}
availableLocations.push(_pincode);
}
emit newProductsAddedToLocation(_pincode);
}
function removeProductsFromLocation(string memory _pincode, uint256[] memory productIDs) external onlyOwner {
for (uint256 i = 0; i < productIDs.length; i++) {
require(!StringCompare(productIDtoProduct[productIDs[i]].name, ""), "Some product IDs are undefined");
}
require(productIDs.length > 0, "No product ID provided");
require(!StringCompare(_pincode, ""), "pincode cannot be empty");
uint256[] memory presentproductIDs = pincodeToAvailableProductIDs[_pincode];
require(presentproductIDs.length > 0, "pincode not present");
uint256 indexToRemoveSize = 0;
for (uint256 i = 0; i < productIDs.length; i++) {
for (uint256 j = 0; j < presentproductIDs.length; j++) {
if (productIDs[i] == presentproductIDs[j]) {
indexToRemoveSize++;
break;
}
}
}
uint256[] memory indexToRemove = new uint256[](indexToRemoveSize);
uint256 iterIndexToRemove = 0;
for (uint256 i = 0; i < productIDs.length; i++) {
for (uint256 j = 0; j < presentproductIDs.length; j++) {
if (productIDs[i] == presentproductIDs[j]) {
bool indexAdded = false;
for (uint256 k = 0; k < iterIndexToRemove; k++) {
if (indexToRemove[k] == j) {
indexAdded = true;
break;
}
}
if (!indexAdded) {
indexToRemove[iterIndexToRemove] = j;
iterIndexToRemove++;
}
break;
}
}
}
for (uint256 i = 0; i < iterIndexToRemove; i++) {
for (uint256 j = indexToRemove[i]; j < pincodeToAvailableProductIDs[_pincode].length - 1; j++) {
pincodeToAvailableProductIDs[_pincode][j] = pincodeToAvailableProductIDs[_pincode][j + 1];
}
for (uint256 k = i + 1; k < iterIndexToRemove; k++) {
if (indexToRemove[k] > indexToRemove[i])
indexToRemove[k]--;
}
pincodeToAvailableProductIDs[_pincode].pop();
}
if (pincodeToAvailableProductIDs[_pincode].length == 0) {
for (uint256 i = 0; i < availableLocations.length; i++) {
if (StringCompare(availableLocations[i], _pincode)) {
availableLocations[i] = availableLocations[availableLocations.length - 1];
availableLocations.pop();
}
}
}
emit ProductsRemovedFromLocation(_pincode);
}
// Partner Management Functions
function getUnapprovedPartners() external onlyOwner view returns (address[] memory, Partner[] memory) {
uint256 _unapprovedPartnerNum = 0;
for (uint256 i = 0; i < registeredPartners.length; i++) {
if (!partnerIDtoPartner[registeredPartners[i]].approved)
_unapprovedPartnerNum++;
}
address[] memory addresses = new address[](_unapprovedPartnerNum);
Partner[] memory partners = new Partner[](_unapprovedPartnerNum);
uint iterPartners = 0;
for (uint256 i = 0; i < registeredPartners.length; i++) {
if (!partnerIDtoPartner[registeredPartners[i]].approved) {
addresses[iterPartners] = registeredPartners[i];
partners[iterPartners] = partnerIDtoPartner[registeredPartners[i]];
iterPartners++;
}
}
return (addresses, partners);
}
function approvePartner(address partner) external onlyOwner {
Partner memory _partner = partnerIDtoPartner[partner];
require(_partner.pinCodes.length > 0, "Not a Registered Partner");
require(!_partner.approved, "Already Approved Partner");
partnerIDtoPartner[partner].approved = true;
}
function checkApprovalStatus() external OnlyPartner view returns (bool) {
return partnerIDtoPartner[msg.sender].approved;
}
// Order Management Functions
function placeOrder(uint256[] memory _productIDs, uint256[] memory _supplyMap) external payable OnlyUser notPaused {
require(_productIDs.length == _supplyMap.length, "Number of productID and supply should be same");
require(calculateTotalOrderItems(_productIDs, _supplyMap) > 0, "Product Input is empty");
string memory _pincode = userIDtoUser[msg.sender].pinCode;
require(checkIfRightProducts(_productIDs, _pincode), "Some Products are not Available");
uint256 _totalCost = this.calculateOrderCost(_productIDs, _supplyMap);
require(msg.value > _totalCost + logisticsCost, "Not enough balance");
uint256 _orderNum = orderNum;
orderIDtoOrderData[_orderNum] = OrderData(block.timestamp, _productIDs, _supplyMap, false, false, false, false, generateOTP(_productIDs, _supplyMap) % 10000);
orderIDtoUserID[_orderNum] = msg.sender;
emit NewOrder(_pincode);
address _partner = this.findAvailablePartner(_pincode);
require(_partner != address(0), "No Partner available for Delivery");
assignOrderToAPartner(_orderNum, _pincode, _partner);
orderNum++;
}
function markOrderDelivered() external OnlyApprovedPartner notPaused {
Partner memory partner = partnerIDtoPartner[msg.sender];
require(partner.InTransit, "Not Active Orders");
orderIDtoOrderData[partner.lastOrderID].orderDeliveredByPartner = true;
partnerIDtoPartner[msg.sender].InTransit = false;
emit OrderDeliveredByPartner(orderIDtoUserID[partner.lastOrderID], partner.lastOrderID);
}
function verifyOrderDelivery(uint256 _orderId, uint256 _otp) external OnlyUser notPaused {
require(orderIDtoUserID[_orderId] == msg.sender, "This is not your order");
OrderData memory order = orderIDtoOrderData[_orderId];
require(order.orderDeliveredByPartner && (!order.orderReceivedByUser) && (!order.orderCancelled), "Order is not delivered or has Already been received or has been cancelled");
require(order.otp == _otp, "OTP does not match");
orderIDtoOrderData[_orderId].orderReceivedByUser = true;
}
function cancelOrderAndRefund(uint256 _orderId) external OnlyUser notPaused {
require(orderIDtoUserID[_orderId] == msg.sender, "This is not your order");
OrderData memory order = orderIDtoOrderData[_orderId];
require((!order.orderDeliveredByPartner) && (!order.orderCancelled), "Order has been delivered or already cancelled");
orderIDtoOrderData[_orderId].orderCancelled = true;
partnerIDtoPartner[orderIDtoPartnerID[_orderId]].InTransit = false;
address payable user = payable(msg.sender);
uint256 _totalCost = this.calculateOrderCost(order.productIDs, order.supplyMap);
if (order.partnerAssigned) {
uint256 _logisticsCost = logisticsCost;
if (_totalCost - _logisticsCost > 0) {
(bool success, bytes memory returnData) = user.call{value: _totalCost - _logisticsCost}("");
require(success, string(returnData));
}
} else {
(bool success, bytes memory returnData) = user.call{value: _totalCost}("");
require(success, string(returnData));
}
emit OrderCancelled(orderIDtoPartnerID[_orderId], _orderId);
}
function assignOrderToAPartner(uint256 _orderNum, string memory _pincode, address partner) internal notPaused {
require(partnerIDtoPartner[partner].approved, "Partner is not Approved");
bool pincodePresent = false;
for (uint256 i = 0; i < partnerIDtoPartner[partner].pinCodes.length; i++) {
if (StringCompare(partnerIDtoPartner[partner].pinCodes[i], _pincode)) {
pincodePresent = true;
break;
}
}
require(pincodePresent, "Partner cannot deliver to this pincode");
partnerIDtoPartner[partner].lastOrderID = _orderNum;
partnerIDtoPartner[partner].InTransit = true;
orderIDtoPartnerID[_orderNum] = partner;
orderIDtoOrderData[_orderNum].partnerAssigned = true;
emit OrderAssignedToPartner(orderIDtoUserID[_orderNum], partner, _orderNum);
}
function getPendingOrderPartner() external OnlyApprovedPartner view returns (OrderData memory, uint256) {
require(partnerIDtoPartner[msg.sender].InTransit, "No Pending Orders");
return (orderIDtoOrderData[partnerIDtoPartner[msg.sender].lastOrderID], partnerIDtoPartner[msg.sender].lastOrderID);
}
function getDeliveryAddressPartner() external OnlyApprovedPartner view returns (string memory, string memory) {
require(partnerIDtoPartner[msg.sender].InTransit, "No Pending Orders");
return (userIDtoUser[orderIDtoUserID[partnerIDtoPartner[msg.sender].lastOrderID]].residenceAddress, userIDtoUser[orderIDtoUserID[partnerIDtoPartner[msg.sender].lastOrderID]].pinCode);
}
function getCompletedOrdersPartner() external OnlyApprovedPartner view returns (OrderData[] memory, uint256[] memory) {
uint256 completedOrdersNum = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoPartnerID[i] == msg.sender && orderIDtoOrderData[i].orderDeliveredByPartner)
completedOrdersNum++;
}
OrderData[] memory orders = new OrderData[](completedOrdersNum);
uint256[] memory orderIDs = new uint256[](completedOrdersNum);
uint256 iterOrders = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoPartnerID[i] == msg.sender && orderIDtoOrderData[i].orderDeliveredByPartner) {
orders[iterOrders] = orderIDtoOrderData[i];
orderIDs[iterOrders] = i;
iterOrders++;
}
}
return (orders, orderIDs);
}
function getCancelledOrdersPartner() external OnlyApprovedPartner view returns (OrderData[] memory, uint256[] memory) {
uint256 cancelledOrdersNum = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoPartnerID[i] == msg.sender && orderIDtoOrderData[i].orderCancelled)
cancelledOrdersNum++;
}
OrderData[] memory orders = new OrderData[](cancelledOrdersNum);
uint256[] memory orderIDs = new uint256[](cancelledOrdersNum);
uint256 iterOrders = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoPartnerID[i] == msg.sender && orderIDtoOrderData[i].orderCancelled) {
orders[iterOrders] = orderIDtoOrderData[i];
orderIDs[iterOrders] = i;
iterOrders++;
}
}
return (orders, orderIDs);
}
function getPendingOrdersUser() external OnlyUser view returns (OrderData[] memory, uint256[] memory) {
uint256 pendingOrdersNum = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoUserID[i] == msg.sender && (!orderIDtoOrderData[i].orderReceivedByUser) && (!orderIDtoOrderData[i].orderCancelled))
pendingOrdersNum++;
}
OrderData[] memory orders = new OrderData[](pendingOrdersNum);
uint256[] memory orderIDs = new uint256[](pendingOrdersNum);
uint256 iterOrders = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoUserID[i] == msg.sender && (!orderIDtoOrderData[i].orderReceivedByUser) && (!orderIDtoOrderData[i].orderCancelled)) {
orders[iterOrders] = orderIDtoOrderData[i];
orderIDs[iterOrders] = i;
iterOrders++;
}
}
return (orders, orderIDs);
}
function getCompletedOrdersUser() external OnlyUser view returns (OrderData[] memory, uint256[] memory) {
uint256 completedOrdersNum = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoUserID[i] == msg.sender && orderIDtoOrderData[i].orderReceivedByUser)
completedOrdersNum++;
}
OrderData[] memory orders = new OrderData[](completedOrdersNum);
uint256[] memory orderIDs = new uint256[](completedOrdersNum);
uint256 iterOrders = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoUserID[i] == msg.sender && orderIDtoOrderData[i].orderReceivedByUser) {
orders[iterOrders] = orderIDtoOrderData[i];
orderIDs[iterOrders] = i;
iterOrders++;
}
}
return (orders, orderIDs);
}
function getCancelledOrdersUser() external OnlyUser view returns (OrderData[] memory, uint256[] memory) {
uint256 cancelledOrdersNum = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoUserID[i] == msg.sender && orderIDtoOrderData[i].orderCancelled)
cancelledOrdersNum++;
}
OrderData[] memory orders = new OrderData[](cancelledOrdersNum);
uint256[] memory orderIDs = new uint256[](cancelledOrdersNum);
uint256 iterOrders = 0;
for (uint256 i = 0; i < orderNum; i++) {
if (orderIDtoUserID[i] == msg.sender && orderIDtoOrderData[i].orderCancelled) {
orders[iterOrders] = orderIDtoOrderData[i];
orderIDs[iterOrders] = i;
iterOrders++;
}
}
return (orders, orderIDs);
}
// Helper Functions
function calculateTotalOrderItems(uint256[] memory _productIDs, uint256[] memory _supplyMap) internal pure returns (uint256) {
uint256 totalItems = 0;
for (uint256 i = 0; i < _supplyMap.length; i++) {
totalItems += _supplyMap[i];
}
return totalItems;
}
function checkIfRightProducts(uint256[] memory _productIDs, string memory _pincode) internal view returns (bool) {
for (uint256 i = 0; i < _productIDs.length; i++) {
bool found = false;
for (uint256 j = 0; j < pincodeToAvailableProductIDs[_pincode].length; j++) {
if (_productIDs[i] == pincodeToAvailableProductIDs[_pincode][j]) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
function calculateOrderCost(uint256[] memory _productIDs, uint256[] memory _supplyMap) internal view returns (uint256) {
uint256 totalCost = 0;
for (uint256 i = 0; i < _productIDs.length; i++) {
totalCost += productIDtoProduct[_productIDs[i]].priceInWei * _supplyMap[i];
}
return totalCost;
}
function generateOTP(uint256[] memory _productIDs, uint256[] memory _supplyMap) internal pure returns (uint256) {
uint256 otp = 0;
for (uint256 i = 0; i < _productIDs.length; i++) {
otp += _productIDs[i] * _supplyMap[i];
}
return otp;
}
function findAvailablePartner(string memory _pincode) internal view returns (address) {
for (uint256 i = 0; i < registeredPartners.length; i++) {
if (partnerIDtoPartner[registeredPartners[i]].approved) {
for (uint256 j = 0; j < partnerIDtoPartner[registeredPartners[i]].pinCodes.length; j++) {
if (StringCompare(partnerIDtoPartner[registeredPartners[i]].pinCodes[j], _pincode)) {
if (!partnerIDtoPartner[registeredPartners[i]].InTransit) {
return registeredPartners[i];
}
}
}
}
}
return address(0);
}
function StringCompare(string memory a, string memory b) internal pure returns (bool) {
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
}
// Pause and Unpause Functions
function pauseContract() external onlyOwner {
contractPaused = true;
}
function unpauseContract() external onlyOwner {
contractPaused = false;
}
// Withdraw Functions
function withdrawBalance() external onlyOwner {
address payable ownerPayable = payable(owner);
ownerPayable.transfer(address(this).balance);
}
// Fallback function to receive ether
receive() external payable {}
// Other Owner Only Functions
function changeLogisticsCost(uint256 _logisticsCostInWei) external onlyOwner {
logisticsCost = _logisticsCostInWei;
}
function changeProductMetadataBaseURI(string memory _baseURI) external onlyOwner {
productMetadataBaseURI = _baseURI;
}
}