-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.sol
More file actions
263 lines (241 loc) · 7.74 KB
/
Library.sol
File metadata and controls
263 lines (241 loc) · 7.74 KB
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
pragma solidity ^0.4.11;
contract Library {
mapping (bytes32 => address) public owner;
bytes32[] books;
address libaddress;
enum State {Stable, TBR, TBC, TBT} //default state is the first one.
// TBC - To be collected, TBT - To be transferred, TBR - To be returned
mapping (bytes32 => State) public status;
uint value;
mapping (address => string) message;
mapping (bytes32 => address) public preowner;
// mapping (address => bytes32) phone; - Later
modifier checkValue(uint newValue) { require(newValue == 2*value); _; }
modifier checkNotOwner(bytes32 book) {require(msg.sender != owner[book]); _;}
modifier checkOwner(bytes32 book) {require(msg.sender == owner[book]); _;}
modifier checkLibrary() {require(msg.sender == libaddress); _;}
modifier checkStatus(bytes32 book) {require(status[book] != State.Stable); _;}
modifier checkNonZeroValue(uint v) { require(v > 0); _; }
event NotAvailable();
event AllOccupied();
//event CollectBookFromLibrary();
//event YouDontHaveThisBook();
//event ReturnBookToLibrary();
//vent CollectBookFromUser();
event RecieveConfirmedByUser();
event RecieveConfirmedByLibrary();
event ContractDeployed();
event ReturnConfirmed();
event BookAlreadyPresent();
event NewBookAdded();
function Library(bytes32[] book_names)
checkNonZeroValue(msg.value)
public
payable
{
value = msg.value;
books = book_names;
libaddress = msg.sender;
for (uint i = 0; i < books.length; i++) {
owner[books[i]] = libaddress;
}
ContractDeployed();
}
function request_book(bytes32 book_name)
public
checkValue(msg.value)
checkNotOwner(book_name)
payable
{
if (check_book(book_name) == 0) {
NotAvailable();
msg.sender.transfer(2*value);
// message[msg.sender] = "Book not available."; // Book name - One time
return;
}
if (owner[book_name] != libaddress) {
if (status[book_name] == State.TBR) {
address add = owner[book_name];
preowner[book_name] = add;
owner[book_name] = msg.sender;
status[book_name] = State.TBT;
message[add] = "Coordinate with new owner with phone number : 999"; // All time
message[msg.sender] = "Collect book from member with phone number: 988";
//CollectBookFromUser();
return;
}
else {
AllOccupied();
msg.sender.transfer(2*value);
//message[msg.sender] = "Book occupied."; // Book name - One time
return;
}
}
else {
owner[book_name] = msg.sender;
status[book_name] = State.TBC;
//CollectBookFromLibrary();
preowner[book_name] = libaddress;
message[msg.sender] = "Collect book from library!"; // All time
}
}
function recieved_by_user(bytes32 book_name)
checkOwner(book_name)
checkStatus(book_name)
public
{
if(status[book_name] == State.TBC) {
msg.sender.transfer(value);
// message[msg.sender] = "Ethers have been transferred to your account."; // Mention value - One
// time
}
if(status[book_name] == State.TBT) {
msg.sender.transfer(value);
preowner[book_name].transfer(value);
//message[msg.sender] = "Recieve Confirmed! Ethers have been transferred to your account.";
message[preowner[book_name]] = "Recieve confirmed by the user. Ethers have been transferred to your account."; // Mention address of the user
}
status[book_name] = State.Stable;
RecieveConfirmedByUser();
message[msg.sender] = "";
//message[msg.sender] = "Recieve Confirmed! Your ethers have been transferred to your account.";
}
function return_book(bytes32 book_name)
checkOwner(book_name)
public
{
if (status[book_name] == State.TBC) {
status[book_name] = State.Stable;
owner[book_name] = libaddress;
msg.sender.transfer(2*value);
//message[msg.sender] = "Return Confirmed!"; // One time
ReturnConfirmed();
message[msg.sender] = "";
}
else if (status[book_name] == State.TBT) {
status[book_name] = State.TBR;
address add = owner[book_name];
owner[book_name] = preowner[book_name];
msg.sender.transfer(2*value); // All time
message[owner[book_name]] = "Owner doesn't want the book anymore. Return to Library";
//message[msg.sender] = "Return Confirmed! Your ethers have been transferred to your account.";
ReturnConfirmed(); // Problem
message[msg.sender] = "";
return;
}
else {
//ReturnBookToLibrary();
status[book_name] = State.TBR;
preowner[book_name] = msg.sender;
message[msg.sender] = "Return book to library"; // All time
return;
}
}
function recieved_by_library(bytes32 book_name)
checkLibrary()
checkStatus(book_name)
public
{
if (status[book_name] != State.TBR) {
throw;
}
status[book_name] = State.Stable;
address add = owner[book_name];
owner[book_name].transfer(value);
owner[book_name] = libaddress;
RecieveConfirmedByLibrary();
message[add] = "Library has recieved the book. Your ethers have been transferred to your account"; // One time
return; // Problem
}
function check_book(bytes32 book_name)
private
constant
returns(uint256)
{
uint256 i = 0;
while (i < books.length) {
if (books[i] == book_name)
return i+1;
i++;
}
return 0;
}
function get_owner(bytes32 book_name)
public
constant // changed
returns(address)
{
if (check_book(book_name) == 0)
throw;
return owner[book_name];
}
function access_message()
public
constant
returns(string)
{
return message[msg.sender];
}
function balance()
public
constant
returns(uint)
{
return msg.sender.balance;
}
function delete_message()
public
{
message[msg.sender] = "";
return;
}
function get_books()
checkLibrary()
public
constant
returns(bytes32[])
{
return books;
}
function add_book(bytes32 book_name)
public
checkLibrary()
{
if(check_book(book_name) != 0){
BookAlreadyPresent();
return;
}
books.push(book_name);
owner[book_name] = libaddress;
NewBookAdded();
}
function get_value()
public
constant
returns(uint)
{
return value;
}
/*function remove_book(bytes32 book_name)
public
checkLibrary()
{
int256 index = check_book(book_name);
if(index == -1){
return;
}
if(status[book_name] != State.Stable)
{
BookNotStable();
return;
}
if(owner[book_name] != libaddress)
{
BookOwnedBySomeUser();
return;
}
books.remove(index);
delete owner[book_name];
delete status[book_name];
}*/
}