forked from shimunmatic/decode-travel-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcursionContract.sol
211 lines (185 loc) · 5.87 KB
/
ExcursionContract.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract ExcursionContract {
uint256 immutable public tourId;
bytes32 immutable tourDescription;
address payable public operator;
address payable public guide;
ERC20 public token;
Customer[] customers;
Checkpoint[] checkpoints;
bool joruneyEnded;
uint256 unitPrice;
struct Checkpoint {
uint256 id;
address[] checkedInUsers;
bool endingCheckpoint;
}
struct Customer {
address id;
bool checkedIn;
}
event Log(string message);
event Log(uint256 message);
mapping(address => bool) public isValidCustomer;
constructor(
address _guide,
uint256 _tourId,
bytes32 _tourDescription,
uint256[] memory _checkpoints,
uint256 _endingCheckpoint,
uint256 _unitPrice,
address _token
) {
operator = payable(msg.sender);
token = ERC20(_token);
guide = payable(_guide);
tourId = _tourId;
tourDescription = _tourDescription;
unitPrice = _unitPrice;
for (uint256 i = 0; i < _checkpoints.length; i++) {
checkpoints.push(
Checkpoint(
_checkpoints[i],
new address[](0),
_checkpoints[i] == _endingCheckpoint
)
);
}
}
function data()
external
view
returns (
uint256,
address payable,
address payable,
bytes32,
ERC20,
Checkpoint[] memory,
bool
)
{
return (tourId, operator, guide, tourDescription, token, checkpoints, joruneyEnded);
}
function getCustomers() external view returns (Customer[] memory) {
return customers;
}
modifier isOperator() {
require(operator == msg.sender, "Not the operator");
_;
}
modifier isCustomer() {
require(isValidCustomer[msg.sender], "Not a customer");
_;
}
modifier isCheckedInCustomer() {
require(isValidCustomer[msg.sender], "Not a customer");
int256 customerIndex = getCustomerIndexByAddress(msg.sender);
require(customers[uint256(customerIndex)].checkedIn, "Not checked in");
_;
}
function addCustomer(address _customer) public isOperator {
require(
!isValidCustomer[_customer] ||
_customer == operator ||
_customer == guide,
"Customer invalid"
);
customers.push(Customer(_customer, false));
isValidCustomer[_customer] = true;
}
function balance() public view returns (uint256) {
return token.allowance(operator, address(this));
}
function neededBalance() public view returns (uint256) {
return customers.length * unitPrice;
}
function checkIn(uint256 id) public isCustomer {
// check there are enough funds
if (id > 0) {
checkpoint(id);
return;
}
require(
balance() >= neededBalance(),
"Not enough funds in the contract"
);
int256 customerIndex = getCustomerIndexByAddress(msg.sender);
require(
!customers[uint256(customerIndex)].checkedIn,
"Already checked in"
);
customers[uint256(customerIndex)].checkedIn = true;
}
function getCustomerIndexByAddress(address _customer)
internal
view
returns (int256)
{
int256 customerIndex = -1;
for (uint256 i = 0; i < customers.length; i++) {
if (customers[i].id == _customer) {
customerIndex = int256(i);
break;
}
}
return customerIndex;
}
function checkpoint(uint256 checkpointId) internal isCheckedInCustomer {
// get the checkpoint
int256 id = getCheckpointIndexById(checkpointId);
require(id >= 0, "Checkpoint not found");
// check that the customer is not already checked in on the checkpoint
bool checkedIn = false;
for (
uint256 i = 0;
i < checkpoints[uint256(id)].checkedInUsers.length;
i++
) {
if (checkpoints[uint256(id)].checkedInUsers[i] == msg.sender) {
checkedIn = true;
break;
}
}
require(!checkedIn, "Already checked in at the checkpoint");
// check in to the checkpoint
checkpoints[uint256(id)].checkedInUsers.push(msg.sender);
// if it is the last checkpoint and the number of the checked in users is equal to number of the users checked in for the tour - initiate the automatic end of the journey
if (
checkpoints[uint256(id)].endingCheckpoint &&
checkpoints[uint256(id)].checkedInUsers.length ==
getNumberOfCheckedInUsers()
) {
endJourney();
}
}
function endJourney() internal {
joruneyEnded = true;
executePayment();
}
function executePayment() internal {
uint256 valueToBeTransfered = getNumberOfCheckedInUsers() * unitPrice;
token.transferFrom(operator, guide, valueToBeTransfered);
}
function getNumberOfCheckedInUsers() internal view returns (uint256) {
uint256 count = 0;
for (uint256 i = 0; i < customers.length; i++) {
if (customers[i].checkedIn) {
count++;
}
}
return count;
}
function getCheckpointIndexById(uint256 id) internal view returns (int256) {
int256 index = -1;
for (uint256 i = 0; i < checkpoints.length; i++) {
if (checkpoints[i].id == id) {
index = int256(i);
break;
}
}
return index;
}
}