This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcontract.sol
228 lines (191 loc) · 8.75 KB
/
contract.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
pragma solidity ^0.5.0;
contract WorldSkills {
struct Estate {
uint estate_id;
address owner;
string info;
uint squere;
uint useful_squere;
address renter_address;
bool present_status;
bool sale_status;
bool rent_status;
}
struct Present {
uint estate_id;
address address_from;
address address_to;
bool finished;
}
struct Sale {
uint estate_id;
address owner;
uint price;
address payable[] customers;
uint[] prices;
bool finished;
}
struct Rent {
uint estate_id;
address payable owner_address;
address payable renter_address;
uint time;
uint money;
uint deadline;
bool finished;
}
Estate[] estates;
Present[] presents;
Sale[] sales;
Rent[] rents;
address admin = msg.sender;
address payable default_address = 0x0000000000000000000000000000000000000000;
function iam_admin() public view returns(bool) {
return msg.sender == admin;
}
function get_estates_number() public view returns(uint) {
return estates.length;
}
function get_presents_number() public view returns(uint) {
return presents.length;
}
function get_sales_number() public view returns(uint) {
return sales.length;
}
function get_rents_number() public view returns(uint) {
return rents.length;
}
function get_estates(uint estate_number) public view returns(uint, address, string memory, uint, uint, address) {
return(estates[estate_number].estate_id, estates[estate_number].owner, estates[estate_number].info, estates[estate_number].squere, estates[estate_number].useful_squere, estates[estate_number].renter_address);
}
function get_estates_statuses(uint estate_number) public view returns(bool, bool, bool) {
return(estates[estate_number].present_status, estates[estate_number].sale_status, estates[estate_number].rent_status);
}
function get_presents(uint present_number) public view returns(uint, address, address, bool) {
return(presents[present_number].estate_id, presents[present_number].address_from, presents[present_number].address_to, presents[present_number].finished);
}
function get_sales(uint sale_number) public view returns(uint, address, uint, address payable[] memory, uint[] memory prices, bool) {
return(sales[sale_number].estate_id, sales[sale_number].owner, sales[sale_number].price, sales[sale_number].customers, sales[sale_number].prices, sales[sale_number].finished);
}
function get_rents(uint rent_number) public view returns(uint, address, address, uint, uint, uint, bool) {
return(rents[rent_number].estate_id, rents[rent_number].owner_address, rents[rent_number].renter_address, rents[rent_number].time, rents[rent_number].money, rents[rent_number].deadline, rents[rent_number].finished);
}
modifier status_OK(uint estate_id) {
require(estates[estate_id].present_status == false);
require(estates[estate_id].sale_status == false);
require(estates[estate_id].rent_status == false);
_;
}
modifier is_owner(uint estate_id) {
require(msg.sender == estates[estate_id].owner);
_;
}
modifier is_admin {
require(msg.sender == admin);
_;
}
function create_estate(address owner, string memory info, uint squere, uint useful_squere) public is_admin{
estates.push(Estate(estates.length, owner, info, squere, useful_squere, 0x0000000000000000000000000000000000000000, false, false, false));
}
function create_present(uint estate_id, address address_to) public status_OK(estate_id) is_owner(estate_id) {
presents.push(Present(estate_id, msg.sender, address_to, false));
estates[estate_id].present_status = true;
}
function cancel_present(uint present_number) payable public {
require(msg.sender == presents[present_number].address_from);
require(presents[present_number].finished == false);
estates[presents[present_number].estate_id].present_status = false;
presents[present_number].finished = true;
}
function confirm_present(uint present_number) payable public {
require(msg.sender == presents[present_number].address_to);
require(presents[present_number].finished == false);
estates[presents[present_number].estate_id].owner = presents[present_number].address_to;
estates[presents[present_number].estate_id].present_status = false;
presents[present_number].finished = true;
}
function create_sale(uint estate_id, uint price) public status_OK(estate_id) is_owner(estate_id){
address payable[] memory customers;
uint[] memory prices;
sales.push(Sale(estate_id, msg.sender, price, customers, prices, false));
estates[estate_id].sale_status = true;
}
function cancel_sale(uint sale_number) public {
require(msg.sender == sales[sale_number].owner);
require(sales[sale_number].finished == false);
for (uint i = 0; i < sales[sale_number].customers.length; i++){
(sales[sale_number].customers[i]).transfer(sales[sale_number].prices[i]);
}
estates[sales[sale_number].estate_id].sale_status = false;
sales[sale_number].finished = true;
}
function check_to_buy(uint sale_number) public payable {
require(msg.sender != sales[sale_number].owner);
require(msg.value >= sales[sale_number].price);
require(sales[sale_number].finished == false);
uint status = 0;
for (uint i=0; i < sales[sale_number].customers.length; i++) {
if (sales[sale_number].customers[i] == msg.sender) {
status = 1;
break;
}
}
require(status == 0);
sales[sale_number].customers.push(msg.sender);
sales[sale_number].prices.push(msg.value);
}
function cancel_to_buy(uint sale_number) public payable {
require(sales[sale_number].finished == false);
for (uint i=0; i<sales[sale_number].customers.length; i++){
if (sales[sale_number].customers[i] == msg.sender){
msg.sender.transfer(sales[sale_number].prices[i]);
delete sales[sale_number].prices[i];
}
}
}
function confirm_sale(uint sale_number, uint sale_to) public payable {
require(msg.sender == sales[sale_number].owner);
require(sales[sale_number].prices[sale_to] != 0);
require(sales[sale_number].finished == false);
estates[sales[sale_number].estate_id].owner = sales[sale_number].customers[sale_to];
msg.sender.transfer(sales[sale_number].prices[sale_to]);
for (uint i=0; i<sales[sale_number].customers.length; i++){
if (i != sale_to) {
sales[sale_number].customers[i].transfer(sales[sale_number].prices[i]);
}
else {
sales[sale_number].prices[sale_to] = 0;
}
}
estates[sales[sale_number].estate_id].sale_status = false;
sales[sale_number].finished = true;
}
function create_rent(uint estate_id, uint time, uint money) public is_owner(estate_id) status_OK(estate_id){
rents.push(Rent(estate_id, msg.sender, default_address, time, money, 0, false));
estates[estate_id].rent_status=true;
}
function to_rent(uint rent_id) public payable{
require(rents[rent_id].finished == false);
require(rents[rent_id].renter_address == default_address);
require(rents[rent_id].owner_address != msg.sender);
require(rents[rent_id].money == msg.value);
rents[rent_id].renter_address = msg.sender;
estates[rents[rent_id].estate_id].renter_address = msg.sender;
rents[rent_id].deadline = now + rents[rent_id].time*86400;
rents[rent_id].owner_address.transfer(rents[rent_id].money);
}
function cancel_rent(uint rent_id) public {
require(rents[rent_id].finished == false);
require(rents[rent_id].owner_address == msg.sender);
require(rents[rent_id].renter_address == default_address);
estates[rents[rent_id].estate_id].rent_status=false;
rents[rent_id].finished = true;
}
function finish_rent(uint rent_id) public is_owner(rents[rent_id].estate_id) {
require(rents[rent_id].finished == false);
require(rents[rent_id].deadline < now);
estates[rents[rent_id].estate_id].renter_address = default_address;
estates[rents[rent_id].estate_id].rent_status=false;
rents[rent_id].finished = true;
}
}