-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNullSettlementChallenge.sol
498 lines (446 loc) · 21.1 KB
/
NullSettlementChallenge.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
/*
* 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 {BalanceTrackable} from "./BalanceTrackable.sol";
import {WalletLockable} from "./WalletLockable.sol";
import {SafeMathIntLib} from "./SafeMathIntLib.sol";
import {SafeMathUintLib} from "./SafeMathUintLib.sol";
import {NullSettlementDispute} from "./NullSettlementDispute.sol";
import {MonetaryTypesLib} from "./MonetaryTypesLib.sol";
import {NahmiiTypesLib} from "./NahmiiTypesLib.sol";
import {SettlementTypesLib} from "./SettlementTypesLib.sol";
/**
* @title NullSettlementChallenge
* @notice Where null settlements are started and challenged
*/
contract NullSettlementChallenge is Ownable, Challenge, BalanceTrackable, WalletLockable {
using SafeMathIntLib for int256;
using SafeMathUintLib for uint256;
//
// Variables
// -----------------------------------------------------------------------------------------------------------------
NullSettlementDispute public nullSettlementDispute;
uint256 public nonce;
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;
//
// Events
// -----------------------------------------------------------------------------------------------------------------
event SetNullSettlementDisputeEvent(NullSettlementDispute oldNullSettlementDispute,
NullSettlementDispute newNullSettlementDispute);
event StartChallengeEvent(address wallet, int256 amount, address stageCurrencyCt,
uint stageCurrencyId);
event StartChallengeByProxyEvent(address proxy, address wallet, int256 amount,
address stageCurrencyCt, uint stageCurrencyId);
event DisqualifyProposalEvent(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 newNullSettlementDispute The (address of) NullSettlementDispute contract instance
function setNullSettlementDispute(NullSettlementDispute newNullSettlementDispute)
public
onlyDeployer
notNullAddress(newNullSettlementDispute)
{
NullSettlementDispute oldNullSettlementDispute = nullSettlementDispute;
nullSettlementDispute = newNullSettlementDispute;
emit SetNullSettlementDisputeEvent(oldNullSettlementDispute, nullSettlementDispute);
}
/// @notice Get the number of challenge wallets, i.e. wallets that have started null 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 Start settlement challenge
/// @param amount The concerned amount to stage
/// @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 startChallenge(int256 amount, address currencyCt, uint256 currencyId)
public
{
// Require that wallet is not locked
require(!walletLocker.isLocked(msg.sender));
// Start challenge for wallet
_startChallenge(msg.sender, amount, currencyCt, currencyId, true);
// Emit event
emit StartChallengeEvent(msg.sender, amount, currencyCt, currencyId);
}
/// @notice Start settlement challenge for the given wallet
/// @param wallet The address of the concerned wallet
/// @param amount The concerned amount to stage
/// @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 startChallengeByProxy(address wallet, int256 amount, address currencyCt, uint256 currencyId)
public
onlyOperator
{
// Start challenge for wallet
_startChallenge(wallet, amount, currencyCt, currencyId, false);
// Emit event
emit StartChallengeByProxyEvent(msg.sender, wallet, amount, currencyCt, currencyId);
}
/// @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 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 expirationTime The end time value
function setProposalExpirationTime(address wallet, address currencyCt, uint256 currencyId,
uint256 expirationTime)
public
onlyNullSettlementDispute
{
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 status The status value
function setProposalStatus(address wallet, address currencyCt, uint256 currencyId,
SettlementTypesLib.Status status)
public
onlyNullSettlementDispute
{
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
onlyNullSettlementDispute
{
// 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 Challenge the settlement by providing order candidate
/// @param order The order candidate that challenges the null
function challengeByOrder(NahmiiTypesLib.Order order)
public
onlyOperationalModeNormal
{
nullSettlementDispute.challengeByOrder(order, 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 null
function challengeByTrade(address wallet, NahmiiTypesLib.Trade trade)
public
onlyOperationalModeNormal
{
nullSettlementDispute.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 null
function challengeByPayment(address wallet, NahmiiTypesLib.Payment payment)
public
onlyOperationalModeNormal
{
nullSettlementDispute.challengeByPayment(wallet, payment, msg.sender);
}
//
// Private functions
// -----------------------------------------------------------------------------------------------------------------
function _startChallenge(address wallet, int256 stageAmount, address currencyCt, uint256 currencyId,
bool balanceReward)
private
{
// Require that current block number is beyond the earliest settlement challenge block number
require(block.number >= configuration.earliestSettlementBlockNumber());
// Require that wallet has no overlap with active proposal
require(
hasProposalExpired(
wallet, currencyCt, currencyId
)
);
// Create proposal
_addProposal(wallet, stageAmount, currencyCt, currencyId, balanceReward);
// Add wallet to store of challenge wallets
_addToChallengeWallets(wallet);
}
function _addProposal(address wallet, int256 stageAmount, address currencyCt, uint256 currencyId,
bool balanceReward)
private
{
// Require that stage amount is positive
require(stageAmount.isPositiveInt256());
// Get the last logged active balance amount and block number
(int256 activeBalanceAmount, uint256 activeBalanceBlockNumber) = _activeBalanceLogEntry(
wallet, currencyCt, currencyId
);
// Require that balance amount is not less than stage amount
require(activeBalanceAmount >= stageAmount);
// Create proposal
proposals.length++;
// Populate proposal
proposals[proposals.length - 1].wallet = wallet;
proposals[proposals.length - 1].nonce = ++nonce;
proposals[proposals.length - 1].blockNumber = activeBalanceBlockNumber;
proposals[proposals.length - 1].expirationTime = block.timestamp.add(configuration.settlementChallengeTimeout());
proposals[proposals.length - 1].status = SettlementTypesLib.Status.Qualified;
proposals[proposals.length - 1].currency = MonetaryTypesLib.Currency(currencyCt, currencyId);
proposals[proposals.length - 1].stageAmount = stageAmount;
proposals[proposals.length - 1].targetBalanceAmount = activeBalanceAmount.sub(stageAmount);
proposals[proposals.length - 1].balanceReward = balanceReward;
// Store proposal index
proposalIndexByWalletCurrency[wallet][currencyCt][currencyId] = proposals.length;
proposalIndicesByWallet[wallet].push(proposals.length);
}
function _activeBalanceLogEntry(address wallet, address currencyCt, uint256 currencyId)
private
view
returns (int256 amount, uint256 blockNumber)
{
// Get last log record of deposited and settled balances
(int256 depositedAmount, uint256 depositedBlockNumber) = balanceTracker.lastFungibleRecord(
wallet, balanceTracker.depositedBalanceType(), currencyCt, currencyId
);
(int256 settledAmount, uint256 settledBlockNumber) = balanceTracker.lastFungibleRecord(
wallet, balanceTracker.settledBalanceType(), currencyCt, currencyId
);
// Set amount as the sum of deposited and settled
amount = depositedAmount.add(settledAmount);
// Set block number as the latest of deposited and settled
blockNumber = depositedBlockNumber > settledBlockNumber ? depositedBlockNumber : settledBlockNumber;
}
function _addToChallengeWallets(address wallet)
private
{
if (!challengeByWallets[wallet]) {
challengeByWallets[wallet] = true;
challengeWallets.push(wallet);
}
}
//
// Modifiers
// -----------------------------------------------------------------------------------------------------------------
modifier onlyNullSettlementDispute() {
require(msg.sender == address(nullSettlementDispute));
_;
}
}