-
Notifications
You must be signed in to change notification settings - Fork 0
/
Booking.sol
214 lines (157 loc) · 5.81 KB
/
Booking.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
/**
* Copyright (C) 2018 Smartz, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND (express or implied).
*/
pragma solidity ^0.4.20;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
/**
* @title Booking
* @author Vladimir Khramov <vladimir.khramov@smartz.io>
*/
contract Booking is Ownable {
function Booking(
string _description,
string _fileUrl,
bytes32 _fileHash,
uint256 _price,
uint256 _cancellationFee,
uint256 _rentDateStart,
uint256 _rentDateEnd,
uint256 _noCancelPeriod,
uint256 _acceptObjectPeriod
) public payable {
require(_price > 0);
require(_price > _cancellationFee);
require(_rentDateStart > getCurrentTime());
require(_rentDateEnd > _rentDateStart);
require(_rentDateStart+_acceptObjectPeriod < _rentDateEnd);
require(_rentDateStart > _noCancelPeriod);
m_description = _description;
m_fileUrl = _fileUrl;
m_fileHash = _fileHash;
m_price = _price;
m_cancellationFee = _cancellationFee;
m_rentDateStart = _rentDateStart;
m_rentDateEnd = _rentDateEnd;
m_noCancelPeriod = _noCancelPeriod;
m_acceptObjectPeriod = _acceptObjectPeriod;
}
/************************** STRUCTS **********************/
enum State {OFFER, PAID, NO_CANCEL, RENT, CANCELED, FINISHED}
/************************** MODIFIERS **********************/
modifier onlyState(State _state) {
require(getCurrentState() == _state);
_;
}
modifier onlyClient() {
require(msg.sender == m_client);
_;
}
/************************** EVENTS **********************/
event StateChanged(State newState);
/************************** CONSTANTS **********************/
/************************** PROPERTIES **********************/
string public m_description;
string public m_fileUrl;
bytes32 public m_fileHash;
uint256 public m_price;
uint256 public m_cancellationFee;
uint256 public m_rentDateStart;
uint256 public m_rentDateEnd;
uint256 public m_noCancelPeriod;
uint256 public m_acceptObjectPeriod;
address public m_client;
State internal m_state;
/************************** FALLBACK **********************/
function() external payable onlyState(State.OFFER) {
require(msg.value >= m_price);
require(msg.sender != owner);
require(m_rentDateStart > getCurrentTime());
changeState(State.PAID);
m_client = msg.sender;
if (msg.value > m_price) {
msg.sender.transfer(msg.value-m_price);
}
}
/************************** EXTERNAL **********************/
function rejectPayment() external onlyOwner onlyState(State.PAID) {
refundWithoutCancellationFee();
}
function refund() external onlyClient onlyState(State.PAID) {
refundWithoutCancellationFee();
}
function startRent() external onlyClient onlyState(State.NO_CANCEL) {
require(getCurrentTime() > m_rentDateStart);
changeState(State.RENT);
owner.transfer(address(this).balance);
}
function cancelBooking() external onlyState(State.NO_CANCEL) {
if (getCurrentTime() >= m_rentDateStart+m_acceptObjectPeriod) {
require(msg.sender == owner);
} else {
require(msg.sender == m_client);
}
refundWithCancellationFee();
}
/************************** PUBLIC **********************/
function getCurrentState() public view returns(State) {
if (m_state == State.PAID) {
if (getCurrentTime() >= m_rentDateStart - m_noCancelPeriod) {
return State.NO_CANCEL;
} else {
return State.PAID;
}
} if (m_state == State.RENT) {
if (getCurrentTime() >= m_rentDateEnd) {
return State.FINISHED;
} else {
return State.RENT;
}
} else {
return m_state;
}
}
/************************** INTERNAL **********************/
function changeState(State _newState) internal {
State currentState = getCurrentState();
if (State.OFFER == _newState) {
assert(State.PAID == currentState || State.NO_CANCEL == currentState);
} else if (State.PAID == _newState) {
assert(State.OFFER == currentState);
assert(address(this).balance > 0);
} else if (State.NO_CANCEL == _newState) {
assert(false); // no direct change
} else if (State.CANCELED == _newState) {
assert(State.NO_CANCEL == currentState);
} else if (State.RENT == _newState) {
assert(State.NO_CANCEL == currentState);
} else if (State.FINISHED == _newState) {
assert(false); // no direct change
}
m_state = _newState;
emit StateChanged(_newState);
}
function getCurrentTime() public view returns (uint256) {
return now;
}
/************************** PRIVATE **********************/
function refundWithoutCancellationFee() private {
address client = m_client;
m_client = address(0);
changeState(State.OFFER);
client.transfer(address(this).balance);
}
function refundWithCancellationFee() private {
address client = m_client;
m_client = address(0);
changeState(State.CANCELED);
owner.transfer(m_cancellationFee);
client.transfer(address(this).balance);
}
}