| 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 |
63×
63×
63×
63×
63×
63×
63×
63×
63×
63×
63×
41×
41×
41×
3×
66×
109×
109×
109×
29×
28×
27×
26×
26×
25×
25×
25×
25×
25×
25×
25×
25×
25×
25×
25×
2×
2×
2×
5×
5×
5×
5×
6×
5×
5×
6×
5×
5×
5×
5×
11×
9×
9×
8×
8×
8×
8×
12×
12×
8×
8×
8×
8×
8×
8×
8×
8×
12×
12×
8×
8×
8×
8×
8×
8×
8×
8×
8×
6×
6×
2×
2×
8×
8×
8×
8×
8×
8×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
12×
8×
41×
49×
39×
21×
17×
8×
6×
16×
16×
7×
7×
18×
18×
11×
11×
54×
54×
| // SPDX-License-Identifier: MIT
pragma solidity >=0.6.2;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
import './interfaces/IEscrow.sol';
import './utils/SafeMath.sol';
contract Escrow is IEscrow, ReentrancyGuard {
using SafeMath for uint256;
bytes4 private constant FUNC_SELECTOR_BALANCE_OF =
bytes4(keccak256('balanceOf(address)'));
string constant ERROR_ZERO_ADDRESS = 'Escrow: zero address';
uint256 private constant BULK_MAX_VALUE = 1e9 * (10 ** 18);
uint32 private constant BULK_MAX_COUNT = 100;
event TrustedHandlerAdded(address _handler);
event IntermediateStorage(string _url, string _hash);
event Pending(string manifest, string hash);
event BulkTransfer(
uint256 indexed _txId,
address[] _recipients,
uint256[] _amounts,
bool _isPartial
);
event Cancelled();
event Completed();
EscrowStatuses public override status;
address public reputationOracle;
address public recordingOracle;
address public exchangeOracle;
address public launcher;
address payable public canceler;
address public escrowFactory;
uint8 public reputationOracleFeePercentage;
uint8 public recordingOracleFeePercentage;
uint8 public exchangeOracleFeePercentage;
address public token;
string public manifestUrl;
string public manifestHash;
string public intermediateResultsUrl;
string public finalResultsUrl;
string public finalResultsHash;
uint256 public duration;
mapping(address => bool) public areTrustedHandlers;
constructor(
address _token,
address _launcher,
address payable _canceler,
uint256 _duration,
address[] memory _handlers
) {
Erequire(_token != address(0), ERROR_ZERO_ADDRESS);
Erequire(_canceler != address(0), ERROR_ZERO_ADDRESS);
token = _token;
status = EscrowStatuses.Launched;
duration = _duration.add(block.timestamp); // solhint-disable-line not-rely-on-time
launcher = _launcher;
canceler = _canceler;
escrowFactory = msg.sender;
areTrustedHandlers[_launcher] = true;
areTrustedHandlers[_canceler] = true;
_addTrustedHandlers(_handlers);
}
function getBalance() public view returns (uint256) {
(bool success, bytes memory returnData) = token.staticcall(
abi.encodeWithSelector(FUNC_SELECTOR_BALANCE_OF, address(this))
);
Eif (success) {
return abi.decode(returnData, (uint256));
}
return 0;
}
function addTrustedHandlers(
address[] memory _handlers
) public override trusted {
_addTrustedHandlers(_handlers);
}
function _addTrustedHandlers(address[] memory _handlers) internal {
for (uint256 i = 0; i < _handlers.length; i++) {
Erequire(_handlers[i] != address(0), ERROR_ZERO_ADDRESS);
areTrustedHandlers[_handlers[i]] = true;
emit TrustedHandlerAdded(_handlers[i]);
}
}
// The escrower puts the Token in the contract without an agentless
// and assigsn a reputation oracle to payout the bounty of size of the
// amount specified
function setup(
address _reputationOracle,
address _recordingOracle,
address _exchangeOracle,
uint8 _reputationOracleFeePercentage,
uint8 _recordingOracleFeePercentage,
uint8 _exchangeOracleFeePercentage,
string memory _url,
string memory _hash
) external override trusted EnotExpired {
require(
_reputationOracle != address(0),
'Invalid reputation oracle address'
);
require(
_recordingOracle != address(0),
'Invalid recording oracle address'
);
require(
_exchangeOracle != address(0),
'Invalid exchange oracle address'
);
uint256 _totalFeePercentage = uint256(_reputationOracleFeePercentage) +
uint256(_recordingOracleFeePercentage) +
uint256(_exchangeOracleFeePercentage);
require(_totalFeePercentage <= 100, 'Percentage out of bounds');
Erequire(
status == EscrowStatuses.Launched,
'Escrow not in Launched status state'
);
reputationOracle = _reputationOracle;
recordingOracle = _recordingOracle;
exchangeOracle = _exchangeOracle;
reputationOracleFeePercentage = _reputationOracleFeePercentage;
recordingOracleFeePercentage = _recordingOracleFeePercentage;
exchangeOracleFeePercentage = _exchangeOracleFeePercentage;
manifestUrl = _url;
manifestHash = _hash;
status = EscrowStatuses.Pending;
emit Pending(manifestUrl, manifestHash);
}
function abort() external override trusted EnotComplete EnotPaid {
Eif (getBalance() != 0) {
cancel();
}
selfdestruct(canceler);
}
function cancel()
public
override
trusted
EnotBroke
EnotComplete
EnotPaid
EnonReentrant
returns (bool)
{
_safeTransfer(canceler, getBalance());
status = EscrowStatuses.Cancelled;
emit Cancelled();
return true;
}
function complete() external override EnotExpired trustedOrReputationOracle {
require(status == EscrowStatuses.Paid, 'Escrow not in Paid state');
status = EscrowStatuses.Complete;
emit Completed();
}
function storeResults(
string memory _url,
string memory _hash
) external override trustedOrRecordingOracle EnotExpired {
require(
status == EscrowStatuses.Pending ||
status == EscrowStatuses.Partial,
'Escrow not in Pending or Partial status state'
);
Erequire(bytes(_url).length != 0, "URL can't be empty");
Erequire(bytes(_hash).length != 0, "Hash can't be empty");
intermediateResultsUrl = _url;
emit IntermediateStorage(_url, _hash);
}
/**
* @dev Performs bulk payout to multiple workers
* Escrow needs to be complted / cancelled, so that it can be paid out.
* Every recipient is paid with the amount after reputation and recording oracle fees taken out.
* If the amount is less than the fee, the recipient is not paid.
* If the fee is zero, reputation, and recording oracle are not paid.
* Payout will fail if any of the transaction fails.
* If the escrow is fully paid out, meaning that the balance of the escrow is 0, it'll set as Paid.
* If the escrow is partially paid out, meaning that the escrow still has remaining balance, it'll set as Partial.
* This contract is only callable if the contract is not broke, not launched, not paid, not expired, by trusted parties.
*
* @param _recipients Array of recipients
* @param _amounts Array of amounts to be paid to each recipient.
* @param _url URL storing results as transaction details
* @param _hash Hash of the results
* @param _txId Transaction ID
*/
function bulkPayOut(
address[] memory _recipients,
uint256[] memory _amounts,
string memory _url,
string memory _hash,
uint256 _txId
)
external
override
trustedOrReputationOracle
EnotBroke
EnotLaunched
EnotPaid
EnotExpired
EnonReentrant
{
require(
_recipients.length == _amounts.length,
"Amount of recipients and values don't match"
);
Erequire(_amounts.length > 0, 'Amounts should not be empty');
require(_recipients.length < BULK_MAX_COUNT, 'Too many recipients');
Erequire(
status != EscrowStatuses.Complete &&
status != EscrowStatuses.Cancelled,
'Invalid status'
);
uint256 balance = getBalance();
uint256 aggregatedBulkAmount = 0;
for (uint256 i; i < _amounts.length; i++) {
Erequire(_amounts[i] > 0, 'Amount should be greater than zero');
aggregatedBulkAmount = aggregatedBulkAmount.add(_amounts[i]);
}
Erequire(aggregatedBulkAmount < BULK_MAX_VALUE, 'Bulk value too high');
Erequire(aggregatedBulkAmount <= balance, 'Not enough balance');
Erequire(bytes(_url).length != 0, "URL can't be empty");
Erequire(bytes(_hash).length != 0, "Hash can't be empty");
finalResultsUrl = _url;
finalResultsHash = _hash;
(
uint256[] memory finalAmounts,
uint256 reputationOracleFee,
uint256 recordingOracleFee,
uint256 exchangeOracleFee
) = finalizePayouts(_amounts);
for (uint256 i = 0; i < _recipients.length; ++i) {
Eif (finalAmounts[i] > 0) {
_safeTransfer(_recipients[i], finalAmounts[i]);
}
}
Eif (reputationOracleFee > 0) {
_safeTransfer(reputationOracle, reputationOracleFee);
}
Eif (recordingOracleFee > 0) {
_safeTransfer(recordingOracle, recordingOracleFee);
}
Eif (exchangeOracleFee > 0) {
_safeTransfer(exchangeOracle, exchangeOracleFee);
}
balance = getBalance();
bool isPartial;
if (balance == 0) {
status = EscrowStatuses.Paid;
isPartial = false;
} else {
status = EscrowStatuses.Partial;
isPartial = true;
}
emit BulkTransfer(_txId, _recipients, finalAmounts, isPartial);
}
function finalizePayouts(
uint256[] memory _amounts
) internal view returns (uint256[] memory, uint256, uint256, uint256) {
uint256[] memory finalAmounts = new uint256[](_amounts.length);
uint256 reputationOracleFee = 0;
uint256 recordingOracleFee = 0;
uint256 exchangeOracleFee = 0;
for (uint256 j; j < _amounts.length; j++) {
uint256 amount = _amounts[j];
uint256 amountFee = 0;
{
uint256 singleReputationOracleFee = uint256(
reputationOracleFeePercentage
).mul(amount).div(100);
reputationOracleFee = reputationOracleFee.add(
singleReputationOracleFee
);
amountFee = amountFee.add(singleReputationOracleFee);
}
{
uint256 singleRecordingOracleFee = uint256(
recordingOracleFeePercentage
).mul(_amounts[j]).div(100);
recordingOracleFee = recordingOracleFee.add(
singleRecordingOracleFee
);
amountFee = amountFee.add(singleRecordingOracleFee);
}
{
uint256 singleExchangeOracleFee = uint256(
exchangeOracleFeePercentage
).mul(_amounts[j]).div(100);
exchangeOracleFee = exchangeOracleFee.add(
singleExchangeOracleFee
);
amountFee = amountFee.add(singleExchangeOracleFee);
}
finalAmounts[j] = amount.sub(amountFee);
}
return (
finalAmounts,
reputationOracleFee,
recordingOracleFee,
exchangeOracleFee
);
}
function _safeTransfer(address to, uint256 value) internal {
SafeERC20.safeTransfer(IERC20(token), to, value);
}
modifier trusted() {
require(areTrustedHandlers[msg.sender], 'Address calling not trusted');
_;
}
modifier trustedOrReputationOracle() {
require(
areTrustedHandlers[msg.sender] || msg.sender == reputationOracle,
'Address calling not trusted'
);
_;
}
modifier trustedOrRecordingOracle() {
require(
areTrustedHandlers[msg.sender] || msg.sender == recordingOracle,
'Address calling not trusted'
);
_;
}
modifier notBroke() {
Erequire(getBalance() != 0, 'Token contract out of funds');
_;
}
modifier notComplete() {
Erequire(
status != EscrowStatuses.Complete,
'Escrow in Complete status state'
);
_;
}
modifier notPaid() {
Erequire(status != EscrowStatuses.Paid, 'Escrow in Paid status state');
_;
}
modifier notLaunched() {
Erequire(
status != EscrowStatuses.Launched,
'Escrow in Launched status state'
);
_;
}
modifier notExpired() {
Erequire(duration > block.timestamp, 'Contract expired'); // solhint-disable-line not-rely-on-time
_;
}
}
|