-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoreManager.sol
131 lines (101 loc) · 3.25 KB
/
StoreManager.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
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract Product{
enum Status{
Activate,
Deactivate
}
struct ProductDetails{
string name;
uint totalAvail;
uint price;
Status status;
}
struct OrderDetails{
string name;
uint orderQuantity;
uint price;
uint time;
}
mapping(uint=>ProductDetails) products;
mapping(address=>OrderDetails) orders;
address owner;
address parentContract;
bytes32 categoryHash;
uint productCount;
uint sellCount;
uint balance;
modifier onlyOwner{
require(owner==msg.sender,"Only Owner is Allowed");
_;
}
constructor(address _owner,address _parentContract,bytes32 _categoryHash){
owner=_owner;
parentContract=_parentContract;
categoryHash=_categoryHash;
}
function addProduct(string memory _name,uint _price,uint _quantity) external onlyOwner{
productCount++;
products[productCount].name=_name;
products[productCount].totalAvail=_quantity;
products[productCount].price=_price;
products[productCount].status=Status.Activate;
}
function purchaseProduct(uint _id,uint _quantity) external payable{
require(products[_id].totalAvail>=_quantity ,"Not Enougth Products left");
uint totalAmt = products[_id].price*_quantity;
require(msg.value==totalAmt,"Pay exact amount");
balance +=msg.value;
products[_id].totalAvail -=_quantity;
OrderDetails memory order = OrderDetails(products[_id].name,_quantity,msg.value,block.timestamp);
orders[msg.sender] = order;
(bool success,)=parentContract.call(abi.encodeWithSignature("addBalance(uint)",balance));
require(success);
payable(owner).transfer(address(this).balance);
}
function getBalance() external view onlyOwner returns(uint){
return balance;
}
function orderSummary()external view returns(OrderDetails[] memory){
}
}
contract ItemStore{
enum State{
Activated,
Deactivated
}
struct Category{
string name;
Product productAddress;
State state;
}
mapping(bytes32=>Category) hashToCategory;
mapping(uint=>bytes32) categories;
uint categoryCount;
address owner;
uint balance;
modifier onlyOwner{
require(msg.sender==owner,"Only Owner is Allowed");
_;
}
constructor(){
owner=msg.sender;
}
function addCategory(string memory _name)external onlyOwner{
bytes32 hash = keccak256(abi.encodePacked(_name,msg.sender));
categoryCount++;
categories[categoryCount]=hash;
hashToCategory[hash].name=_name;
hashToCategory[hash].productAddress=new Product(owner,address(this),hash);
hashToCategory[hash].state = State.Activated;
}
function getCategoryHash(uint _id) external view returns(bytes32){
return categories[_id];
}
function getCategoryByHash(bytes32 _hash) external view returns(Product,string memory){
return(hashToCategory[_hash].productAddress,hashToCategory[_hash].name);
}
function addBalance(uint _amt) external{
balance+=_amt;
}
}