-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColabXPayments.sol
630 lines (566 loc) · 18.1 KB
/
ColabXPayments.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.6;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./IColabXToken.sol";
import "./ControllerOwnable.sol";
import "./IColabXPayments.sol";
// import "@openzeppelin/contracts/utils/Strings.sol";
// import "hardhat/console.sol";
// console.log("ensureDeal");
// console.log(Strings.toString(uint256(SharedStructs.AgreementState.DealNegotiating)));
// console.log(Strings.toString(uint256(agreements[dealId].state)));
contract ColabXPayments is ControllerOwnable, Initializable {
/**
* Types
*/
// the address of the ColabXToken.sol contract
address private tokenAddress;
IColabXToken private tokenContract;
// used to cut off upgrades for the remote contract
bool private canChangeTokenAddress = true;
/**
* Enums
*/
enum PaymentReason {
// the money the JC puts up to pay for the job
PaymentCollateral,
// the money the RP puts up to attest it's results are correct
ResultsCollateral,
// the money the RP, JC and Mediator all put up to prevent timeouts
TimeoutCollateral,
// the money the RP gets paid for the job for running it successfully
JobPayment,
// the money the JC pays the mediator for resolving a dispute
MediationFee
}
enum PaymentDirection {
// money flowing into the contract
// i.e. we GET paid
PaidIn,
// money paid out to services
// i.e. we are PAYING
PaidOut,
// collateral that is locked up being refunded
Refunded,
// collateral that is locked up being slashed
Slashed
}
/**
* Events
*/
event Payment(
string dealId,
address payee,
uint256 amount,
PaymentReason reason,
PaymentDirection direction
);
/**
* Init
*/
// used for debugging
mapping(address => string) private accountNames;
// https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
function initialize(address _tokenAddress) public initializer {
setTokenAddress(_tokenAddress);
// this is only for debugging
// accountNames[address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)] = "admin";
// accountNames[address(0x70997970C51812dc3A010C7d01b50e0d17dc79C8)] = "faucet";
// accountNames[address(0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC)] = "solver";
// accountNames[address(0x90F79bf6EB2c4f870365E785982E1f101E93b906)] = "mediator";
// accountNames[address(0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65)] = "resource_provider";
// accountNames[address(0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc)] = "job_creator";
// accountNames[address(0x976EA74026E726554dB657fA54763abd0C3a0aa9)] = "directory";
}
// the ColabXToken.sol we are calling payinEscrow and payoutEscrow on
function setTokenAddress(address _tokenAddress) public onlyOwner {
require(
_tokenAddress != address(0),
"ColabXPayments: Token address must be defined"
);
require(
canChangeTokenAddress,
"ColabXToken: canChangeTokenAddress is disabled"
);
tokenAddress = _tokenAddress;
tokenContract = IColabXToken(_tokenAddress);
}
function getTokenAddress() public view returns (address) {
return tokenAddress;
}
// set for canChangePaymentsAddress
function disableChangeTokenAddress() public onlyOwner {
canChangeTokenAddress = false;
}
/**
* Controller handlers
*
* these methods are called by the controller to wrap various payment
* scenarios - hence they are all onlyOwner
*/
/**
* Agreements
*/
// * pay in the timeout collateral
function agreeResourceProvider(
string memory dealId,
address resourceProvider,
uint256 timeoutCollateral
) public onlyController {
// we check this here to double check who we are about to charge (the RP)
// is who signed the TX and so we can take the money
require(
tx.origin == resourceProvider,
"ColabXPayments: Can only be called by the RP"
);
_payEscrow(dealId, timeoutCollateral, PaymentReason.TimeoutCollateral);
}
// * pay in the payment collateral and timeout collateral
function agreeJobCreator(
string memory dealId,
address jobCreator,
uint256 paymentCollateral,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == jobCreator,
"ColabXPayments: Can only be called by the JC"
);
_payEscrow(dealId, paymentCollateral, PaymentReason.PaymentCollateral);
_payEscrow(dealId, timeoutCollateral, PaymentReason.TimeoutCollateral);
}
/**
* Results
*/
// * pay back the timeout collateral
// * pay in the results collateral
function addResult(
string memory dealId,
address resourceProvider,
uint256 resultsCollateral,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == resourceProvider,
"ColabXPayments: Can only be called by the RP"
);
_payEscrow(dealId, resultsCollateral, PaymentReason.ResultsCollateral);
_refundEscrow(
dealId,
resourceProvider,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
}
// * pay the RP the job cost
// * refund the RP the results collateral
// * refund the JC the job collateral minus the job cost
// * refund the JC the timeout collateral
function acceptResult(
string memory dealId,
address resourceProvider,
address jobCreator,
uint256 jobCost,
uint256 paymentCollateral,
uint256 resultsCollateral,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == jobCreator,
"ColabXPayments: Can only be called by the JC"
);
// what if the final job cost is more than the payment collateral?
// well - we have to cap the job cost at that collateral
// true - the RP has lost money but they agreed to the deal
uint256 actualPayment = jobCost;
uint256 jcRefund = 0;
if (jobCost > paymentCollateral) {
actualPayment = paymentCollateral;
} else {
jcRefund = paymentCollateral - jobCost;
}
// pay the RP the actualPayment
_payOut(
dealId,
jobCreator,
resourceProvider,
actualPayment,
PaymentReason.JobPayment
);
// if the job cost more than the payment collateral then we shold not go negative
// otherwise we are paying out more than the JC has put in
//
// the RP is loosing out a bit here but they agreed to doing the job
if (jcRefund > 0) {
_refundEscrow(
dealId,
jobCreator,
jcRefund,
PaymentReason.PaymentCollateral
);
}
// refund the JC timeout collateral
_refundEscrow(
dealId,
jobCreator,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
// refund the RP results collateral
_refundEscrow(
dealId,
resourceProvider,
resultsCollateral,
PaymentReason.ResultsCollateral
);
}
// * charge the JC the mediation fee
// * refund the JC the timeout collateral
function checkResult(
string memory dealId,
address jobCreator,
uint256 timeoutCollateral,
uint256 mediationFee
) public onlyController {
require(
tx.origin == jobCreator,
"ColabXPayments: Can only be called by the JC"
);
// the refund of the timeout collateral
_refundEscrow(
dealId,
jobCreator,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
// the payment of the mediation fee
_payEscrow(dealId, mediationFee, PaymentReason.MediationFee);
}
/**
* Mediation
*/
// * pay the RP the job cost
// * refund the RP the results collateral
// * refund the JC the job collateral minus the job cost
// * pay the mediator for mediating
function mediationAcceptResult(
string memory dealId,
address resourceProvider,
address jobCreator,
uint256 jobCost,
uint256 paymentCollateral,
uint256 resultsCollateral,
uint256 mediationFee
) public onlyController {
uint256 actualPayment = jobCost;
uint256 jcRefund = 0;
if (jobCost > paymentCollateral) {
actualPayment = paymentCollateral;
} else {
jcRefund = paymentCollateral - jobCost;
}
// pay the RP the job cost from the JC
_payOut(
dealId,
jobCreator,
resourceProvider,
actualPayment,
PaymentReason.JobPayment
);
// pay the mediator the fee from the JC
_payOut(
dealId,
jobCreator,
tx.origin,
mediationFee,
PaymentReason.MediationFee
);
// if the job cost more than the payment collateral then we shold not go negative
// otherwise we are paying out more than the JC has put in
//
// the RP is loosing out a bit here but they agreed to doing the job
if (jcRefund > 0) {
// refund the JC the diff between payment collateral and job cost
_refundEscrow(
dealId,
jobCreator,
jcRefund,
PaymentReason.PaymentCollateral
);
}
// refund the RP the results collateral
_refundEscrow(
dealId,
resourceProvider,
resultsCollateral,
PaymentReason.ResultsCollateral
);
}
// * refund the JC their payment collateral
// * slash the RP's results collateral
// * pay the mediator for mediating
function mediationRejectResult(
string memory dealId,
address resourceProvider,
address jobCreator,
uint256 paymentCollateral,
uint256 resultsCollateral,
uint256 mediationFee
) public onlyController {
// refund the JC their payment collateral
_refundEscrow(
dealId,
jobCreator,
paymentCollateral,
PaymentReason.PaymentCollateral
);
// pay the mediator the fee from the JC
_payOut(
dealId,
jobCreator,
tx.origin,
mediationFee,
PaymentReason.MediationFee
);
// slash the RP
_slashEscrow(
dealId,
resourceProvider,
resultsCollateral,
PaymentReason.ResultsCollateral
);
}
/**
* Timeouts
*/
function timeoutAgreeRefundResourceProvider(
string memory dealId,
address resourceProvider,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == resourceProvider,
"ColabXPayments: Can only be called by the RP"
);
// the refund of the job collateral to the JC
_refundEscrow(
dealId,
resourceProvider,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
}
function timeoutAgreeRefundJobCreator(
string memory dealId,
address jobCreator,
uint256 paymentCollateral,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == jobCreator,
"ColabXPayments: Can only be called by the JC"
);
// the refund of the job collateral to the JC
_refundEscrow(
dealId,
jobCreator,
paymentCollateral,
PaymentReason.PaymentCollateral
);
// the refund of the job collateral to the JC
_refundEscrow(
dealId,
jobCreator,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
}
// * pay back the JC's job collateral
// * pay back the JC's timeout collateral
// * slash the RP's results collateral
function timeoutSubmitResults(
string memory dealId,
address resourceProvider,
address jobCreator,
uint256 paymentCollateral,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == jobCreator,
"ColabXPayments: Can only be called by the JC"
);
// the refund of the job collateral to the JC
_refundEscrow(
dealId,
jobCreator,
paymentCollateral,
PaymentReason.PaymentCollateral
);
// the refund of the job collateral to the JC
_refundEscrow(
dealId,
jobCreator,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
// the slashing of the timeout collateral for the RP
_slashEscrow(
dealId,
resourceProvider,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
}
// * pay back the RP's results collateral
// * pay the RP the cost of the job
// * slash the JC's timeout collateral
// * slash the JC's job collateral
function timeoutJudgeResults(
string memory dealId,
address resourceProvider,
address jobCreator,
uint256 resultsCollateral,
uint256 timeoutCollateral
) public onlyController {
require(
tx.origin == resourceProvider,
"ColabXPayments: Can only be called by the RP"
);
// the refund of the results collateral to the RP
_refundEscrow(
dealId,
resourceProvider,
resultsCollateral,
PaymentReason.ResultsCollateral
);
// the slashing of the timeout collateral for the RP
_slashEscrow(
dealId,
jobCreator,
timeoutCollateral,
PaymentReason.TimeoutCollateral
);
}
// * pay back the RP's results collateral
// * pay back the JC's payment collateral
// * pay back the JC's mediation fee
function timeoutMediateResult(
string memory dealId,
address resourceProvider,
address jobCreator,
uint256 paymentCollateral,
uint256 resultsCollateral,
uint256 mediationFee
) public onlyController {
require(
tx.origin == resourceProvider || tx.origin == jobCreator,
"ColabXPayments: Can only be called by the RP or JC"
);
// the refund of the results collateral to the RP
_refundEscrow(
dealId,
resourceProvider,
resultsCollateral,
PaymentReason.ResultsCollateral
);
// the refund of the payment collateral to the JC
_refundEscrow(
dealId,
jobCreator,
paymentCollateral,
PaymentReason.PaymentCollateral
);
// the refund of the mediation fee to the JC
_refundEscrow(
dealId,
jobCreator,
mediationFee,
PaymentReason.MediationFee
);
}
/**
* Payment utils
*/
function _payEscrow(
string memory dealId,
uint256 amount,
PaymentReason reason
) private {
// we check they have that much in their token balance before moving to tokens to us
require(
tokenContract.balanceOf(tx.origin) >= amount,
"ColabXPayments: Insufficient balance"
);
// console.log("_payEscrow");
// console.log(accountNames[tx.origin]);
// console.log(amount);
bool success = tokenContract.payEscrow(amount);
require(success, "ColabXPayments: Pay escrow failed");
emit Payment(
dealId,
tx.origin,
amount,
reason,
PaymentDirection.PaidIn
);
}
function _refundEscrow(
string memory dealId,
address toAddress,
uint256 amount,
PaymentReason reason
) private {
// console.log("_refundEscrow");
// console.log(accountNames[toAddress]);
// console.log(amount);
bool success = tokenContract.refundEscrow(toAddress, amount);
require(success, "ColabXPayments: Refund escrow failed");
emit Payment(
dealId,
toAddress,
amount,
reason,
PaymentDirection.Refunded
);
}
function _payOut(
string memory dealId,
address fromAddress,
address toAddress,
uint256 amount,
PaymentReason reason
) private {
// console.log("_payJob");
// console.log(accountNames[fromAddress]);
// console.log(accountNames[toAddress]);
// console.log(amount);
bool success = tokenContract.payJob(fromAddress, toAddress, amount);
require(success, "ColabXPayments: Pay job failed");
emit Payment(
dealId,
toAddress,
amount,
reason,
PaymentDirection.PaidOut
);
}
function _slashEscrow(
string memory dealId,
address slashedAddress,
uint256 amount,
PaymentReason reason
) private {
// console.log("_slashEscrow");
// console.log(accountNames[slashedAddress]);
// console.log(amount);
bool success = tokenContract.slashEscrow(slashedAddress, amount);
require(success, "ColabXPayments: Slash escrow failed");
emit Payment(
dealId,
slashedAddress,
amount,
reason,
PaymentDirection.Slashed
);
}
}