This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FlightSuretyApp.sol
executable file
·233 lines (194 loc) · 5.5 KB
/
FlightSuretyApp.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
pragma solidity ^0.4.25;
import 'oz/SafeMath.sol';
// FlightSuretyData Interface contract
contract FlightSuretyData {
function registerAirline(address airlineAddress, address originAddress)
external;
function fund(address originAddress) external payable;
function registerFlight(
uint256 takeOff,
uint256 landing,
string flightRef,
uint256 price,
string from,
string to,
address originAddress
) external;
function book(
bytes32 flightKey,
uint256 amount,
address originAddress
) external payable;
function pay(address originAddress) external;
function getFlightPrice(bytes32 flightKey) external view returns (uint256);
function hasFunded(address airlineAddress) external view returns (bool);
function isRegistered(address airlineAddress) external view returns (bool);
function registeredAirlinesCount() external view returns (uint256);
function firstAirline() external view returns (address);
function paxOnFlight(bytes32 key, address passenger)
public
view
returns (bool onFlight);
}
contract FlightSuretyApp {
using SafeMath for uint256;
FlightSuretyData flightSuretyData;
address private contractOwner;
uint256 public minFund = 0.1 ether;
bool public operational;
// Multi-party consensus - votes count for to-be-added airline
mapping(address => address[]) internal votes;
event FlightRegistered(
string flightRef,
string from,
string to,
uint256 takeOff
);
event WithdrawRequest(address recipient);
modifier requireIsOperational() {
require(operational, 'Contract paused');
_;
}
// avoid spending unnecessary gas if requested state changed if the same as the current one
modifier differentModeRequest(bool status) {
require(status != operational, 'Contract already in the state requested');
_;
}
modifier requireContractOwner() {
require(msg.sender == contractOwner, 'Caller is not contract owner');
_;
}
modifier enoughFund() {
require(msg.value >= minFund, 'Value below minFund (0.1 ETH)');
_;
}
modifier valWithinRange(
uint256 val,
uint256 low,
uint256 up
) {
require(val < up, 'Value higher than max allowed');
require(val > low, 'Value lower than min allowed');
_;
}
modifier paidEnough(uint256 _price) {
require(msg.value >= _price, 'Value does not cover the price');
_;
}
modifier checkValue(uint256 _price) {
uint256 amountToReturn = msg.value - _price;
msg.sender.transfer(amountToReturn);
_;
}
modifier airlineRegistered() {
require(
flightSuretyData.isRegistered(msg.sender),
'Sender must be a registered airline'
);
_;
}
modifier airlineFunded() {
require(
flightSuretyData.hasFunded(msg.sender),
'Sender must have provided funding'
);
_;
}
constructor(address dataContractAddress) public {
operational = true;
contractOwner = msg.sender;
flightSuretyData = FlightSuretyData(dataContractAddress);
}
function setOperatingStatus(bool mode)
external
requireContractOwner
differentModeRequest(mode)
{
operational = mode;
}
function registerAirline(address airlineAddress)
external
requireIsOperational
airlineRegistered
airlineFunded
{
// only first Airline can register a new airline when less than 4 airlines are registered
if (flightSuretyData.registeredAirlinesCount() < 4) {
require(
flightSuretyData.firstAirline() == msg.sender,
'Less than 4 airlines registered: only first airline registered can register new ones'
);
flightSuretyData.registerAirline(airlineAddress, msg.sender);
} else {
// multi party consensus
bool isDuplicate = false;
for (uint256 i = 0; i < votes[airlineAddress].length; i++) {
if (votes[airlineAddress][i] == msg.sender) {
isDuplicate = true;
break;
}
}
require(!isDuplicate, 'Caller has already voted');
votes[airlineAddress].push(msg.sender);
if (votesLeft(airlineAddress) == 0) {
votes[airlineAddress] = new address[](0);
flightSuretyData.registerAirline(airlineAddress, msg.sender);
}
}
}
function fund()
external
payable
airlineRegistered
enoughFund
requireIsOperational
{
flightSuretyData.fund.value(msg.value)(msg.sender);
}
function registerFlight(
uint256 takeOff,
uint256 landing,
string flightRef,
uint256 price,
string from,
string to
) external requireIsOperational airlineFunded {
flightSuretyData.registerFlight(
takeOff,
landing,
flightRef,
price,
from,
to,
msg.sender
);
emit FlightRegistered(flightRef, from, to, takeOff);
}
function book(bytes32 key, uint256 amount)
external
payable
valWithinRange(amount, 0, 1.05 ether) // +0.05 to cover gas costs
paidEnough(flightSuretyData.getFlightPrice(key).add(amount))
checkValue(flightSuretyData.getFlightPrice(key).add(amount))
requireIsOperational
{
flightSuretyData.book.value(msg.value)(
key,
amount.mul(3).div(2),
msg.sender
);
}
function withdraw() external requireIsOperational {
flightSuretyData.pay(msg.sender);
emit WithdrawRequest(msg.sender);
}
function votesLeft(address airlineToBeAdded)
internal
view
returns (uint256 remainingVotes)
{
uint256 registeredVotes = votes[airlineToBeAdded].length;
uint256 threshold = flightSuretyData.registeredAirlinesCount().div(2);
remainingVotes = threshold.sub(registeredVotes);
}
}