-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDriipSettlementChallenge.sol
752 lines (669 loc) · 31.6 KB
/
DriipSettlementChallenge.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
* Hubii Nahmii
*
* Compliant with the Hubii Nahmii specification v0.12.
*
* Copyright (C) 2017-2018 Hubii AS
*/
pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;
import {Ownable} from "./Ownable.sol";
import {Challenge} from "./Challenge.sol";
import {Validatable} from "./Validatable.sol";
import {WalletLockable} from "./WalletLockable.sol";
import {SafeMathIntLib} from "./SafeMathIntLib.sol";
import {SafeMathUintLib} from "./SafeMathUintLib.sol";
import {DriipSettlementDispute} from "./DriipSettlementDispute.sol";
import {MonetaryTypesLib} from "./MonetaryTypesLib.sol";
import {NahmiiTypesLib} from "./NahmiiTypesLib.sol";
import {SettlementTypesLib} from "./SettlementTypesLib.sol";
/**
* @title DriipSettlementChallenge
* @notice Where driip settlements are started and challenged
*/
contract DriipSettlementChallenge is Ownable, Challenge, Validatable, WalletLockable {
using SafeMathIntLib for int256;
using SafeMathUintLib for uint256;
//
// Variables
// -----------------------------------------------------------------------------------------------------------------
DriipSettlementDispute public driipSettlementDispute;
address[] public challengeWallets;
mapping(address => bool) challengeByWallets;
SettlementTypesLib.Proposal[] public proposals;
mapping(address => mapping(address => mapping(uint256 => uint256))) public proposalIndexByWalletCurrency;
mapping(address => uint256[]) public proposalIndicesByWallet;
bytes32[] public challengeTradeHashes;
mapping(address => uint256[]) public challengeTradeHashIndicesByWallet;
bytes32[] public challengePaymentHashes;
mapping(address => uint256[]) public challengePaymentHashIndicesByWallet;
//
// Events
// -----------------------------------------------------------------------------------------------------------------
event SetDriipSettlementDisputeEvent(DriipSettlementDispute oldDriipSettlementDispute,
DriipSettlementDispute newDriipSettlementDispute);
event StartChallengeFromTradeEvent(address wallet, bytes32 tradeHash,
int256 intendedStageAmount, int256 conjugateStageAmount);
event StartChallengeFromTradeByProxyEvent(address proxy, address wallet, bytes32 tradeHash,
int256 intendedStageAmount, int256 conjugateStageAmount);
event StartChallengeFromPaymentEvent(address wallet, bytes32 paymentHash, int256 stageAmount);
event StartChallengeFromPaymentByProxyEvent(address proxy, address wallet, bytes32 paymentHash,
int256 stageAmount);
event DisqualifyProposalEvent(address challengedWallet, address currencyCt, uint256 currencyId,
address challengerWallet, bytes32 candidateHash, SettlementTypesLib.CandidateType candidateType);
event QualifyProposalEvent(address challengedWallet, address currencyCt, uint256 currencyId,
address challengerWallet, bytes32 candidateHash, SettlementTypesLib.CandidateType candidateType);
//
// Constructor
// -----------------------------------------------------------------------------------------------------------------
constructor(address deployer) Ownable(deployer) public {
}
//
// Functions
// -----------------------------------------------------------------------------------------------------------------
/// @notice Set the settlement dispute contract
/// @param newDriipSettlementDispute The (address of) DriipSettlementDispute contract instance
function setDriipSettlementDispute(DriipSettlementDispute newDriipSettlementDispute)
public
onlyDeployer
notNullAddress(newDriipSettlementDispute)
{
DriipSettlementDispute oldDriipSettlementDispute = driipSettlementDispute;
driipSettlementDispute = newDriipSettlementDispute;
emit SetDriipSettlementDisputeEvent(oldDriipSettlementDispute, driipSettlementDispute);
}
/// @notice Get the number of challenge wallets, i.e. wallets that have started driip settlement challenge
/// @return The number of challenge wallets
function challengeWalletsCount()
public
view
returns (uint256)
{
return challengeWallets.length;
}
/// @notice Get the number of proposals
/// @return The number of proposals
function proposalsCount()
public
view
returns (uint256)
{
return proposals.length;
}
/// @notice Get the number of challenged trade hashes
/// @return The count of challenged trade hashes
function challengeTradeHashesCount()
public
view
returns (uint256)
{
return challengeTradeHashes.length;
}
/// @notice Get the number of challenged payment hashes
/// @return The count of challenged payment hashes
function challengePaymentHashesCount()
public
view
returns (uint256)
{
return challengePaymentHashes.length;
}
/// @notice Get the number of current and past settlement challenges from trade for given wallet
/// @param wallet The wallet for which to return count
/// @return The count of settlement challenges from trade
function walletChallengeTradeHashIndicesCount(address wallet)
public
view
returns (uint256)
{
return challengeTradeHashIndicesByWallet[wallet].length;
}
/// @notice Get the number of current and past settlement challenges from payment for given wallet
/// @param wallet The wallet for which to return count
/// @return The count of settlement challenges from payment
function walletChallengePaymentHashIndicesCount(address wallet)
public
view
returns (uint256)
{
return challengePaymentHashIndicesByWallet[wallet].length;
}
/// @notice Start settlement challenge on trade
/// @param trade The challenged trade
/// @param intendedStageAmount Amount of intended currency to be staged
/// @param conjugateStageAmount Amount of conjugate currency to be staged
function startChallengeFromTrade(NahmiiTypesLib.Trade trade, int256 intendedStageAmount,
int256 conjugateStageAmount)
public
{
// Require that wallet is not temporarily disqualified
require(!walletLocker.isLocked(msg.sender));
// Start challenge
_startChallengeFromTrade(msg.sender, trade, intendedStageAmount, conjugateStageAmount, true);
// Emit event
emit StartChallengeFromTradeEvent(msg.sender, trade.seal.hash, intendedStageAmount, conjugateStageAmount);
}
/// @notice Start settlement challenge on trade by proxy
/// @param wallet The concerned party
/// @param trade The challenged trade
/// @param intendedStageAmount Amount of intended currency to be staged
/// @param conjugateStageAmount Amount of conjugate currency to be staged
function startChallengeFromTradeByProxy(address wallet, NahmiiTypesLib.Trade trade, int256 intendedStageAmount,
int256 conjugateStageAmount)
public
onlyOperator
{
// Start challenge for wallet
_startChallengeFromTrade(wallet, trade, intendedStageAmount, conjugateStageAmount, false);
// Emit event
emit StartChallengeFromTradeByProxyEvent(msg.sender, wallet, trade.seal.hash, intendedStageAmount, conjugateStageAmount);
}
/// @notice Start settlement challenge on payment
/// @param payment The challenged payment
/// @param stageAmount Amount of payment currency to be staged
function startChallengeFromPayment(NahmiiTypesLib.Payment payment, int256 stageAmount)
public
{
// Require that wallet is not temporarily disqualified
require(!walletLocker.isLocked(msg.sender));
// Start challenge for wallet
_startChallengeFromPayment(msg.sender, payment, stageAmount, true);
// Emit event
emit StartChallengeFromPaymentEvent(msg.sender, payment.seals.operator.hash, stageAmount);
}
/// @notice Start settlement challenge on payment
/// @param wallet The concerned party
/// @param payment The challenged payment
/// @param stageAmount Amount of payment currency to be staged
function startChallengeFromPaymentByProxy(address wallet, NahmiiTypesLib.Payment payment, int256 stageAmount)
public
onlyOperator
{
// Start challenge for wallet
_startChallengeFromPayment(wallet, payment, stageAmount, false);
// Emit event
emit StartChallengeFromPaymentByProxyEvent(msg.sender, wallet, payment.seals.operator.hash, stageAmount);
}
/// @notice Gauge whether the proposal for the given wallet and currency has expired
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return true if proposal has expired, else false
function hasProposalExpired(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (bool)
{
// 1-based index
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
return (
0 == index ||
0 == proposals[index - 1].nonce ||
block.timestamp >= proposals[index - 1].expirationTime
);
}
/// @notice Get the challenge nonce of the given wallet
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The challenge nonce
function proposalNonce(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (uint256)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].nonce;
}
/// @notice Get the settlement proposal block number of the given wallet
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The settlement proposal block number
function proposalBlockNumber(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (uint256)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].blockNumber;
}
/// @notice Get the settlement proposal end time of the given wallet
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The settlement proposal end time
function proposalExpirationTime(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (uint256)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].expirationTime;
}
/// @notice Get the challenge status of the given wallet
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The challenge status
function proposalStatus(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (SettlementTypesLib.Status)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].status;
}
/// @notice Get the settlement proposal stage amount of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The settlement proposal stage amount
function proposalStageAmount(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (int256)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].stageAmount;
}
/// @notice Get the settlement proposal target balance amount of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The settlement proposal target balance amount
function proposalTargetBalanceAmount(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (int256)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].targetBalanceAmount;
}
/// @notice Get the settlement proposal driip hash of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The settlement proposal driip hash
function proposalDriipHash(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (bytes32)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].driipHash;
}
/// @notice Get the settlement proposal driip type of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The settlement proposal driip type
function proposalDriipType(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (NahmiiTypesLib.DriipType)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].driipType;
}
/// @notice Get the balance reward of the given wallet's settlement proposal
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The balance reward of the settlement proposal
function proposalBalanceReward(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (bool)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].balanceReward;
}
/// @notice Get the disqualification challenger of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The challenger of the settlement disqualification
function proposalDisqualificationChallenger(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (address)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].disqualification.challenger;
}
/// @notice Get the disqualification block number of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The block number of the settlement disqualification
function proposalDisqualificationBlockNumber(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (uint256)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].disqualification.blockNumber;
}
/// @notice Get the disqualification candidate type of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The candidate type of the settlement disqualification
function proposalDisqualificationCandidateType(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (SettlementTypesLib.CandidateType)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].disqualification.candidateType;
}
/// @notice Get the disqualification candidate hash of the given wallet and currency
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @return The candidate hash of the settlement disqualification
function proposalDisqualificationCandidateHash(address wallet, address currencyCt, uint256 currencyId)
public
view
returns (bytes32)
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
return proposals[index - 1].disqualification.candidateHash;
}
/// @notice Set settlement proposal end time property of the given wallet
/// @dev This function can only be called by this contract's dispute instance
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @param expirationTime The end time value
function setProposalExpirationTime(address wallet, address currencyCt, uint256 currencyId,
uint256 expirationTime)
public
onlyDriipSettlementDispute
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
proposals[index - 1].expirationTime = expirationTime;
}
/// @notice Set settlement proposal status property of the given wallet
/// @dev This function can only be called by this contract's dispute instance
/// @param wallet The address of the concerned wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @param status The status value
function setProposalStatus(address wallet, address currencyCt, uint256 currencyId,
SettlementTypesLib.Status status)
public
onlyDriipSettlementDispute
{
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
proposals[index - 1].status = status;
}
/// @notice Disqualify a proposal
/// @dev A call to this function will intentionally override previous disqualifications if existent
/// @param challengedWallet The address of the concerned challenged wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
/// @param challengerWallet The address of the concerned challenger wallet
/// @param blockNumber The disqualification block number
/// @param candidateHash The candidate hash
/// @param candidateType The candidate type
function disqualifyProposal(address challengedWallet, address currencyCt, uint256 currencyId, address challengerWallet,
uint256 blockNumber, bytes32 candidateHash, SettlementTypesLib.CandidateType candidateType)
public
onlyDriipSettlementDispute
{
// Get the proposal index
uint256 index = proposalIndexByWalletCurrency[challengedWallet][currencyCt][currencyId];
require(0 != index);
// Update proposal
proposals[index - 1].status = SettlementTypesLib.Status.Disqualified;
proposals[index - 1].expirationTime = block.timestamp.add(configuration.settlementChallengeTimeout());
proposals[index - 1].disqualification.challenger = challengerWallet;
proposals[index - 1].disqualification.blockNumber = blockNumber;
proposals[index - 1].disqualification.candidateHash = candidateHash;
proposals[index - 1].disqualification.candidateType = candidateType;
// Emit event
emit DisqualifyProposalEvent(
challengedWallet, currencyCt, currencyId, challengerWallet, candidateHash, candidateType
);
}
/// @notice (Re)Qualify a proposal
/// @param wallet The address of the concerned challenged wallet
/// @param currencyCt The address of the concerned currency contract (address(0) == ETH)
/// @param currencyId The ID of the concerned currency (0 for ETH and ERC20)
function qualifyProposal(address wallet, address currencyCt, uint256 currencyId)
public
onlyDriipSettlementDispute
{
// Get the proposal index
uint256 index = proposalIndexByWalletCurrency[wallet][currencyCt][currencyId];
require(0 != index);
// Emit event
emit QualifyProposalEvent(
wallet, currencyCt, currencyId,
proposals[index - 1].disqualification.challenger,
proposals[index - 1].disqualification.candidateHash,
proposals[index - 1].disqualification.candidateType
);
// Update proposal
proposals[index - 1].status = SettlementTypesLib.Status.Qualified;
proposals[index - 1].expirationTime = block.timestamp.add(configuration.settlementChallengeTimeout());
delete proposals[index - 1].disqualification;
}
/// @notice Challenge the settlement by providing order candidate
/// @param order The order candidate that challenges the challenged driip
function challengeByOrder(NahmiiTypesLib.Order order)
public
onlyOperationalModeNormal
{
driipSettlementDispute.challengeByOrder(order, msg.sender);
}
/// @notice Unchallenge settlement by providing trade that shows that challenge order candidate has been filled
/// @param order The order candidate that challenged driip
/// @param trade The trade in which order has been filled
function unchallengeOrderCandidateByTrade(NahmiiTypesLib.Order order, NahmiiTypesLib.Trade trade)
public
onlyOperationalModeNormal
{
driipSettlementDispute.unchallengeOrderCandidateByTrade(order, trade, msg.sender);
}
/// @notice Challenge the settlement by providing trade candidate
/// @param wallet The wallet whose settlement is being challenged
/// @param trade The trade candidate that challenges the challenged driip
function challengeByTrade(address wallet, NahmiiTypesLib.Trade trade)
public
onlyOperationalModeNormal
{
driipSettlementDispute.challengeByTrade(wallet, trade, msg.sender);
}
/// @notice Challenge the settlement by providing payment candidate
/// @param wallet The wallet whose settlement is being challenged
/// @param payment The payment candidate that challenges the challenged driip
function challengeByPayment(address wallet, NahmiiTypesLib.Payment payment)
public
onlyOperationalModeNormal
{
driipSettlementDispute.challengeByPayment(wallet, payment, msg.sender);
}
//
// Private functions
// -----------------------------------------------------------------------------------------------------------------
function _startChallengeFromTrade(address wallet, NahmiiTypesLib.Trade trade,
int256 intendedStageAmount, int256 conjugateStageAmount, bool balanceReward)
private
onlySealedTrade(trade)
{
// Require that current block number is beyond the earliest settlement challenge block number
require(block.number >= configuration.earliestSettlementBlockNumber());
// Require that given wallet is a trade party
require(validator.isTradeParty(trade, wallet));
// Require that wallet has no overlap with active proposals
require(hasProposalExpired(
wallet, trade.currencies.intended.ct, trade.currencies.intended.id
));
require(hasProposalExpired(
wallet, trade.currencies.conjugate.ct, trade.currencies.conjugate.id
));
// Create proposals
_addIntendedProposalFromTrade(wallet, trade, intendedStageAmount, balanceReward);
_addConjugateProposalFromTrade(wallet, trade, conjugateStageAmount, balanceReward);
// Add wallet to store of challenge wallets
_addToChallengeWallets(wallet);
// Store driip hashes
challengeTradeHashes.push(trade.seal.hash);
challengeTradeHashIndicesByWallet[wallet].push(challengeTradeHashes.length);
}
function _startChallengeFromPayment(address wallet, NahmiiTypesLib.Payment payment,
int256 stageAmount, bool balanceReward)
private
onlySealedPayment(payment)
{
// Require that current block number is beyond the earliest settlement challenge block number
require(block.number >= configuration.earliestSettlementBlockNumber());
// Require that given wallet is a payment party
require(validator.isPaymentParty(payment, wallet));
// Require that wallet has no overlap with active proposal
require(
hasProposalExpired(
wallet, payment.currency.ct, payment.currency.id
)
);
// Create proposal
_addProposalFromPayment(wallet, payment, stageAmount, balanceReward);
// Add wallet to store of challenge wallets
_addToChallengeWallets(wallet);
// Store driip hashes
challengePaymentHashes.push(payment.seals.operator.hash);
challengePaymentHashIndicesByWallet[wallet].push(challengePaymentHashes.length);
}
function _addIntendedProposalFromTrade(address wallet, NahmiiTypesLib.Trade trade, int256 stageAmount, bool balanceReward)
private
{
_addProposalFromTrade(
wallet, trade, stageAmount,
_tradeIntendedBalanceAmount(trade, wallet),
trade.currencies.intended, balanceReward
);
}
function _addConjugateProposalFromTrade(address wallet, NahmiiTypesLib.Trade trade, int256 stageAmount, bool balanceReward)
private
{
_addProposalFromTrade(
wallet, trade, stageAmount,
_tradeConjugateBalanceAmount(trade, wallet),
trade.currencies.conjugate, balanceReward
);
}
function _addProposalFromTrade(address wallet, NahmiiTypesLib.Trade trade, int256 stageAmount,
int256 balanceAmount, MonetaryTypesLib.Currency currency, bool balanceReward)
private
{
// Require that stage amount is positive
require(stageAmount.isPositiveInt256());
// Require that balance amount is not less than stage amount
require(balanceAmount >= stageAmount);
// Create proposal
proposals.length++;
// Populate proposal
proposals[proposals.length - 1].wallet = wallet;
proposals[proposals.length - 1].nonce = trade.nonce;
proposals[proposals.length - 1].blockNumber = trade.blockNumber;
proposals[proposals.length - 1].expirationTime = block.timestamp.add(configuration.settlementChallengeTimeout());
proposals[proposals.length - 1].status = SettlementTypesLib.Status.Qualified;
proposals[proposals.length - 1].currency = currency;
proposals[proposals.length - 1].stageAmount = stageAmount;
proposals[proposals.length - 1].targetBalanceAmount = balanceAmount.sub(stageAmount);
proposals[proposals.length - 1].driipHash = trade.seal.hash;
proposals[proposals.length - 1].driipType = NahmiiTypesLib.DriipType.Trade;
proposals[proposals.length - 1].balanceReward = balanceReward;
// Store proposal index
proposalIndexByWalletCurrency[wallet][currency.ct][currency.id] = proposals.length;
proposalIndicesByWallet[wallet].push(proposals.length);
}
function _addProposalFromPayment(address wallet, NahmiiTypesLib.Payment payment, int256 stageAmount,
bool balanceReward)
private
{
// Require that stage amount is positive
require(stageAmount.isPositiveInt256());
// Deduce the concerned balance amount
int256 balanceAmount = _paymentBalanceAmount(payment, wallet);
// Require that balance amount is not less than stage amount
require(balanceAmount >= stageAmount);
// Create proposal
proposals.length++;
// Populate proposal
proposals[proposals.length - 1].wallet = wallet;
proposals[proposals.length - 1].nonce = payment.nonce;
proposals[proposals.length - 1].blockNumber = payment.blockNumber;
proposals[proposals.length - 1].expirationTime = block.timestamp.add(configuration.settlementChallengeTimeout());
proposals[proposals.length - 1].status = SettlementTypesLib.Status.Qualified;
proposals[proposals.length - 1].currency = payment.currency;
proposals[proposals.length - 1].stageAmount = stageAmount;
proposals[proposals.length - 1].targetBalanceAmount = balanceAmount.sub(stageAmount);
proposals[proposals.length - 1].driipHash = payment.seals.operator.hash;
proposals[proposals.length - 1].driipType = NahmiiTypesLib.DriipType.Payment;
proposals[proposals.length - 1].balanceReward = balanceReward;
// Store proposal index
proposalIndexByWalletCurrency[wallet][payment.currency.ct][payment.currency.id] = proposals.length;
proposalIndicesByWallet[wallet].push(proposals.length);
}
function _tradeIntendedBalanceAmount(NahmiiTypesLib.Trade trade, address wallet)
private
view
returns (int256)
{
return validator.isTradeBuyer(trade, wallet) ?
trade.buyer.balances.intended.current :
trade.seller.balances.intended.current;
}
function _tradeConjugateBalanceAmount(NahmiiTypesLib.Trade trade, address wallet)
private
view
returns (int256)
{
return validator.isTradeBuyer(trade, wallet) ?
trade.buyer.balances.conjugate.current :
trade.seller.balances.conjugate.current;
}
function _paymentBalanceAmount(NahmiiTypesLib.Payment payment, address wallet)
private
view
returns (int256)
{
return validator.isPaymentSender(payment, wallet) ?
payment.sender.balances.current :
payment.recipient.balances.current;
}
function _addToChallengeWallets(address wallet)
private
{
if (!challengeByWallets[wallet]) {
challengeByWallets[wallet] = true;
challengeWallets.push(wallet);
}
}
//
// Modifiers
// -----------------------------------------------------------------------------------------------------------------
modifier onlyDriipSettlementDispute() {
require(msg.sender == address(driipSettlementDispute));
_;
}
}