-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecurityPoolForker.sol
More file actions
728 lines (678 loc) · 28.9 KB
/
Copy pathSecurityPoolForker.sol
File metadata and controls
728 lines (678 loc) · 28.9 KB
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
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.35;
import { IERC20 } from '../IERC20.sol';
import { ReputationToken } from '../ReputationToken.sol';
import { SafeERC20Ops } from '../SafeERC20Ops.sol';
import { Zoltar } from '../Zoltar.sol';
import { IUniformPriceDualCapBatchAuction } from './interfaces/IUniformPriceDualCapBatchAuction.sol';
import { UniformPriceDualCapBatchAuction } from './UniformPriceDualCapBatchAuction.sol';
import { ISecurityPool, SystemState } from './interfaces/ISecurityPool.sol';
import { EscalationGame, MERKLE_MOUNTAIN_RANGE_MAX_PEAKS } from './EscalationGame.sol';
import { BinaryOutcomes } from './BinaryOutcomes.sol';
import { SecurityPoolUtils } from './SecurityPoolUtils.sol';
import { SecurityPoolMigrationProxy } from './SecurityPoolMigrationProxy.sol';
import { SecurityPoolForkerVaultMigrationDelegate } from './SecurityPoolForkerVaultMigrationDelegate.sol';
import { EscalationGameForker } from './EscalationGameForker.sol';
import { SecurityPoolForkerVaultMigrationBase } from './SecurityPoolForkerVaultMigrationBase.sol';
import { SecurityPoolForkerForkData } from './SecurityPoolForkerTypes.sol';
contract SecurityPoolForker is SecurityPoolForkerVaultMigrationBase {
using SafeERC20Ops for IERC20;
uint256 constant ESCALATION_TIME_LENGTH = 4233600; // 7 weeks
address private immutable vaultMigrationDelegate;
address private immutable escalationGameForkerDelegate;
event InitiateSecurityPoolFork(uint256 auctionableRepAtFork);
event TruthAuctionStarted(uint256 completeSetCollateralAmount, uint256 repMigrated, uint256 auctionableRepAtFork);
event TruthAuctionFinalized();
event ClaimAuctionProceeds(
address vault,
uint256 amount,
uint256 poolOwnershipAmount,
uint256 poolOwnershipDenominator
);
event FinalizeAuction(
uint256 repAvailable,
uint256 migratedRep,
uint256 repPurchased,
uint256 poolOwnershipDenominator,
uint256 completeSetCollateralAmount
);
function forkData(
ISecurityPool securityPool
)
public
view
returns (
uint256 auctionableRepAtFork,
UniformPriceDualCapBatchAuction truthAuction,
uint256 truthAuctionStarted,
uint256 migratedRep,
uint256 auctionedSecurityBondAllowance,
uint256 escalationElapsedAtFork,
uint256 escalationStartBondAtFork,
uint256 escalationNonDecisionThresholdAtFork,
bool ownFork,
bool unresolvedEscalationAtFork,
uint8 outcomeIndex
)
{
SecurityPoolForkerForkData storage data = forkDataByPool[securityPool];
return (
data.auctionableRepAtFork,
data.truthAuction,
data.truthAuctionStarted,
data.migratedRep,
data.auctionedSecurityBondAllowance,
data.escalationElapsedAtFork,
data.escalationStartBondAtFork,
data.escalationNonDecisionThresholdAtFork,
data.ownFork,
data.unresolvedEscalationAtFork,
data.outcomeIndex
);
}
function getMigratedRep(ISecurityPool securityPool) public view returns (uint256) {
return forkDataByPool[securityPool].migratedRep;
}
function getOwnForkRepBuckets(
ISecurityPool securityPool
) public view returns (uint256 vaultRepAtFork, uint256 unallocatedEscrowChildRep, uint256 escrowSourceRepAtFork) {
SecurityPoolForkerForkData storage repBuckets = forkDataByPool[securityPool];
return (
repBuckets.vaultRepAtFork,
_unallocatedEscrowChildRep(securityPool, repBuckets),
repBuckets.escalationSourceRepAtFork
);
}
function getOwnForkMigrationStatus(
ISecurityPool securityPool
)
public
view
returns (
bool ownFork,
uint256 auctionableRepAtFork,
uint256 vaultRepAtFork,
uint256 unallocatedEscrowChildRep,
uint256 escrowSourceRepAtFork
)
{
SecurityPoolForkerForkData storage data = forkDataByPool[securityPool];
return (
data.ownFork,
data.auctionableRepAtFork,
data.vaultRepAtFork,
_unallocatedEscrowChildRep(securityPool, data),
data.escalationSourceRepAtFork
);
}
function _unallocatedEscrowChildRep(
ISecurityPool securityPool,
SecurityPoolForkerForkData storage data
) private view returns (uint256) {
uint256 escrowChildRepUsed;
for (uint8 outcomeIndex = 0; outcomeIndex < 3; outcomeIndex++) {
escrowChildRepUsed += ownForkChildRepAllocationByPoolAndOutcome[securityPool][outcomeIndex]
.escrowChildRepUsed;
}
uint256 escalationChildRepAtFork = data.escalationChildRepAtFork;
if (escalationChildRepAtFork <= escrowChildRepUsed) return 0;
return escalationChildRepAtFork - escrowChildRepUsed;
}
constructor(Zoltar _zoltar) SecurityPoolForkerVaultMigrationBase(_zoltar) {
vaultMigrationDelegate = address(new SecurityPoolForkerVaultMigrationDelegate(_zoltar));
escalationGameForkerDelegate = address(new EscalationGameForker(_zoltar));
}
function _forkOccurredBeforeEscalationSettled(EscalationGame escalationGame) private view returns (bool) {
if (address(escalationGame) == address(0x0)) return false;
return escalationGame.getQuestionResolution() == BinaryOutcomes.BinaryOutcome.None;
}
function _getEscalationElapsedAtFork(
EscalationGame escalationGame,
uint256 forkTime
) private view returns (uint256 elapsedAtFork) {
if (escalationGame.forkContinuation()) {
elapsedAtFork = escalationGame.forkElapsedAtStart();
if (escalationGame.forkContinuationResumed()) {
uint256 resumedAt = escalationGame.forkResumedAt();
if (forkTime > resumedAt) {
elapsedAtFork += forkTime - resumedAt;
}
}
} else {
uint256 activationTime = escalationGame.activationTime();
if (forkTime <= activationTime) return 0;
elapsedAtFork = forkTime - activationTime;
}
if (elapsedAtFork > ESCALATION_TIME_LENGTH) {
elapsedAtFork = ESCALATION_TIME_LENGTH;
}
}
function _snapshotEscalationAtFork(
SecurityPoolForkerForkData storage data,
EscalationGame escalationGame,
uint256 forkTime
) private {
if (!_forkOccurredBeforeEscalationSettled(escalationGame)) return;
data.unresolvedEscalationAtFork = true;
data.escalationStartBondAtFork = escalationGame.startBond();
data.escalationNonDecisionThresholdAtFork = escalationGame.nonDecisionThreshold();
data.escalationElapsedAtFork = _getEscalationElapsedAtFork(escalationGame, forkTime);
}
function _getForkData(ISecurityPool securityPool) private view returns (SecurityPoolForkerForkData storage data) {
data = forkDataByPool[securityPool];
}
function _prepareForkState(
ISecurityPool securityPool,
EscalationGame escalationGame
) private returns (SecurityPoolForkerForkData storage data) {
uint248 universe = securityPool.universeId();
uint256 forkTime = zoltar.getForkTime(universe);
require(forkTime > 0, 'e7');
require(securityPool.systemState() != SystemState.PoolForked, 'e8');
require(securityPool.systemState() == SystemState.Operational, 'e9');
require(
address(escalationGame) == address(0x0) ||
escalationGame.getQuestionResolution() == BinaryOutcomes.BinaryOutcome.None,
'ea'
);
data = forkDataByPool[securityPool];
_snapshotEscalationAtFork(data, escalationGame, forkTime);
}
function _getMigrationProxySalt(ISecurityPool securityPool) private pure returns (bytes32) {
return keccak256(abi.encode(address(securityPool)));
}
function getMigrationProxyAddress(ISecurityPool securityPool) public view returns (address) {
bytes32 salt = _getMigrationProxySalt(securityPool);
bytes32 initCodeHash = keccak256(
abi.encodePacked(
type(SecurityPoolMigrationProxy).creationCode,
abi.encode(zoltar, securityPool.repToken(), securityPool.universeId(), address(this))
)
);
return address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xff), address(this), salt, initCodeHash)))));
}
// Lazily deploy one proxy per parent pool so that all Zoltar migration calls for
// that pool use a unique `msg.sender`. CREATE2 keeps the proxy address stable
// and predictable from the pool address before deployment.
function _getOrDeployMigrationProxy(
ISecurityPool securityPool
) private returns (SecurityPoolMigrationProxy migrationProxy) {
migrationProxy = migrationProxyByPool[securityPool];
if (address(migrationProxy) != address(0x0)) return migrationProxy;
migrationProxy = new SecurityPoolMigrationProxy{ salt: _getMigrationProxySalt(securityPool) }(
zoltar,
securityPool.repToken(),
securityPool.universeId(),
address(this)
);
migrationProxyByPool[securityPool] = migrationProxy;
}
function _initializeChildForkedEscalationGameIfNeeded(ISecurityPool parent, ISecurityPool child) internal override {
SecurityPoolForkerForkData storage parentForkData = forkDataByPool[parent];
if (!parentForkData.unresolvedEscalationAtFork) return;
if (address(child.escalationGame()) == address(0x0)) {
child.initializeForkedEscalationGame(
parentForkData.escalationStartBondAtFork,
parentForkData.escalationNonDecisionThresholdAtFork,
parentForkData.escalationElapsedAtFork
);
}
EscalationGame parentEscalationGame = parent.escalationGame();
EscalationGame childEscalationGame = child.escalationGame();
if (
address(parentEscalationGame) != address(0x0) &&
address(childEscalationGame) != address(0x0) &&
!childEscalationGame.forkCarrySnapshotInitialized()
) {
(
bytes32[MERKLE_MOUNTAIN_RANGE_MAX_PEAKS][3] memory inheritedCarryPeaks,
uint256[3] memory inheritedCarryLeafCounts,
uint256[3] memory inheritedCarryTotals,
bytes32[3] memory inheritedNullifierRoots
) = parentEscalationGame.getForkCarrySnapshot();
if (parentForkData.ownFork) {
uint256[3] memory inheritedResolutionBalances;
child.initializeForkCarrySnapshotWithResolutionBalances(
inheritedCarryPeaks,
inheritedCarryLeafCounts,
inheritedCarryTotals,
inheritedResolutionBalances,
inheritedNullifierRoots
);
} else {
child.initializeForkCarrySnapshot(
inheritedCarryPeaks,
inheritedCarryLeafCounts,
inheritedCarryTotals,
inheritedNullifierRoots
);
}
}
if (child.systemState() == SystemState.Operational) {
child.resumeForkedEscalationGame();
}
}
function initializeChildForkedEscalationGameIfNeeded(ISecurityPool parent, ISecurityPool child) external {
require(msg.sender == address(this), 'os');
_initializeChildForkedEscalationGameIfNeeded(parent, child);
}
function initiateSecurityPoolFork(ISecurityPool securityPool) public {
EscalationGame escalationGame = securityPool.escalationGame();
SecurityPoolForkerForkData storage data = _prepareForkState(securityPool, escalationGame);
ReputationToken rep = securityPool.repToken();
uint248 universe = securityPool.universeId();
uint256 repBalanceBefore = rep.balanceOf(address(this));
securityPool.activateForkMode();
SecurityPoolMigrationProxy migrationProxy = _getOrDeployMigrationProxy(securityPool);
uint256 previousMigrationBalance = zoltar.getMigrationRepBalance(address(migrationProxy), universe);
uint256 repBalanceAfter = rep.balanceOf(address(this));
uint256 repToLock = repBalanceAfter - repBalanceBefore;
if (repToLock > 0) IERC20(address(rep)).safeTransfer(address(migrationProxy), repToLock);
uint256 proxyRepBalance = rep.balanceOf(address(migrationProxy));
if (proxyRepBalance > 0) migrationProxy.lockRep(proxyRepBalance);
data.auctionableRepAtFork = zoltar.getMigrationRepBalance(address(migrationProxy), universe);
require(data.auctionableRepAtFork >= previousMigrationBalance, 'migration balance regressed');
emit InitiateSecurityPoolFork(data.auctionableRepAtFork);
// TODO: we could pay the caller basefee*2 out of Open interest. We have to reward caller
}
function migrateRepToZoltar(ISecurityPool securityPool, uint256[] calldata outcomeIndices) external {
SecurityPoolMigrationProxy migrationProxy = migrationProxyByPool[securityPool];
require(address(migrationProxy) != address(0x0), 'e3');
require(securityPool.systemState() == SystemState.PoolForked, 'parent pool not forked');
SecurityPoolForkerForkData storage data = forkDataByPool[securityPool];
uint256 migrationAmount = data.ownFork ? data.vaultRepAtFork : data.auctionableRepAtFork;
if (migrationAmount > 0) {
for (uint256 index = 0; index < outcomeIndices.length; index++) {
uint256 outcomeIndex = outcomeIndices[index];
require(outcomeIndex <= type(uint8).max, 'eb');
uint8 normalizedOutcomeIndex = uint8(outcomeIndex);
ISecurityPool child = childrenByPoolAndOutcome[securityPool][normalizedOutcomeIndex];
if (address(child) != address(0x0)) {
require(child.systemState() == SystemState.ForkMigration, 'child branch already priced');
}
require(
block.timestamp <= zoltar.getForkTime(securityPool.universeId()) + SecurityPoolUtils.MIGRATION_TIME,
'migration window closed'
);
_splitMigrationRepToChild(securityPool, normalizedOutcomeIndex, migrationAmount, data.ownFork, false);
pendingChildRepByPoolAndOutcome[securityPool][normalizedOutcomeIndex] += migrationAmount;
_sweepChildRepToPool(securityPool, normalizedOutcomeIndex);
}
}
}
function _delegateMigrationCall(address delegate) private returns (bytes memory returnData) {
(bool success, bytes memory data) = delegate.delegatecall(msg.data);
if (!success) {
assembly {
revert(add(data, 0x20), mload(data))
}
}
return data;
}
function createChildUniverse(ISecurityPool, uint8) public {
_delegateVaultMigration();
}
function claimForkedEscalationDeposits(
ISecurityPool,
address vault,
BinaryOutcomes.BinaryOutcome,
uint256[] calldata
) external {
require(msg.sender == vault, 'ov');
_delegateEscalationGameForker();
}
// migrates vault into outcome universe after fork
function migrateVault(ISecurityPool, uint8) public {
_delegateVaultMigration();
}
function migrateVaultWithUnresolvedEscalation(
ISecurityPool,
address vault,
uint8
) public returns (bool moreToMigrate) {
require(msg.sender == vault, 'ov');
bytes memory returnData = _delegateEscalationGameForker();
return abi.decode(returnData, (bool));
}
function _delegateVaultMigration() private returns (bytes memory returnData) {
return _delegateMigrationCall(vaultMigrationDelegate);
}
function _delegateEscalationGameForker() private returns (bytes memory returnData) {
return _delegateMigrationCall(escalationGameForkerDelegate);
}
function startTruthAuction(ISecurityPool securityPool) public {
SecurityPoolForkerForkData storage data;
SecurityPoolForkerForkData storage parentData;
ISecurityPool parent;
uint256 parentCollateral;
(data, parentData, parent, parentCollateral) = _loadTruthAuctionState(securityPool);
uint256 poolAuctionableRepAtFork = _getPoolAuctionableRepAtFork(parentData);
emit TruthAuctionStarted(parentCollateral, data.migratedRep, poolAuctionableRepAtFork);
_startTruthAuctionOrFinalize(securityPool, data, parentData, parentCollateral);
}
function _loadTruthAuctionState(
ISecurityPool securityPool
)
private
returns (
SecurityPoolForkerForkData storage data,
SecurityPoolForkerForkData storage parentData,
ISecurityPool parent,
uint256 parentCollateral
)
{
require(securityPool.systemState() == SystemState.ForkMigration, 'f2');
require(
block.timestamp > zoltar.getForkTime(securityPool.universeId()) + SecurityPoolUtils.MIGRATION_TIME,
'f3'
);
data = _getForkData(securityPool);
securityPool.setSystemState(SystemState.ForkTruthAuction);
data.truthAuctionStarted = block.timestamp;
parent = securityPool.parent();
parent.updateCollateralAmount();
securityPool.setTotalShares(parent.shareTokenSupply());
parentData = _getForkData(parent);
parentCollateral =
parentData.ownFork ? parentData.ownForkCollateralAtFork : parent.completeSetCollateralAmount();
}
function _startTruthAuctionOrFinalize(
ISecurityPool securityPool,
SecurityPoolForkerForkData storage data,
SecurityPoolForkerForkData storage parentData,
uint256 parentCollateral
) private {
if (_isAllRepMigrated(data, parentData)) {
// we have acquired all the ETH already, no need for truthAuction
_finalizeTruthAuction(securityPool);
return;
}
uint256 ethToBuy = _computeRepNeededForAuction(parentCollateral, data, parentData);
if (ethToBuy == 0) {
_finalizeTruthAuction(securityPool);
return;
}
// Sell effectively all REP for ETH while leaving only a tiny migrated-rep residue unsold.
// This intentionally parses as `repAtFork - (migratedRep / divisor)`: the parent
// pool keeps its full REP-at-fork anchor, and only the tiny unsold residue is scaled
// down by the haircut divisor. We cannot sell literally all REP because
// `poolOwnershipDenominator` still needs a finite anchor.
data.truthAuction.startAuction(ethToBuy, _getTruthAuctionCap(data, parentData));
}
function _isAllRepMigrated(
SecurityPoolForkerForkData storage data,
SecurityPoolForkerForkData storage parentData
) private view returns (bool) {
return data.migratedRep >= _getPoolAuctionableRepAtFork(parentData);
}
function _computeRepNeededForAuction(
uint256 parentCollateral,
SecurityPoolForkerForkData storage data,
SecurityPoolForkerForkData storage parentData
) private view returns (uint256 ethToBuy) {
uint256 poolAuctionableRepAtFork = _getPoolAuctionableRepAtFork(parentData);
if (poolAuctionableRepAtFork == 0 || data.migratedRep >= poolAuctionableRepAtFork) return 0;
ethToBuy = parentCollateral - (parentCollateral * data.migratedRep) / poolAuctionableRepAtFork;
}
function _getTruthAuctionCap(
SecurityPoolForkerForkData storage data,
SecurityPoolForkerForkData storage parentData
) private view returns (uint256) {
uint256 poolAuctionableRepAtFork = _getPoolAuctionableRepAtFork(parentData);
uint256 migratedRepHaircut = data.migratedRep / SecurityPoolUtils.MAX_AUCTION_VAULT_HAIRCUT_DIVISOR;
if (migratedRepHaircut >= poolAuctionableRepAtFork) return 0;
return poolAuctionableRepAtFork - migratedRepHaircut;
}
function _getPoolAuctionableRepAtFork(
SecurityPoolForkerForkData storage parentData
) private view returns (uint256) {
return parentData.ownFork ? parentData.vaultRepAtFork : parentData.auctionableRepAtFork;
}
function _finalizeTruthAuction(ISecurityPool securityPool) private {
require(securityPool.systemState() == SystemState.ForkTruthAuction, 'f4');
SecurityPoolForkerForkData storage data = _getForkData(securityPool);
SecurityPoolForkerForkData storage parentData = _getForkData(securityPool.parent());
ISecurityPool parent = securityPool.parent();
uint256 repPurchased = _consumeTruthAuctionRep(securityPool, data);
_captureUnclaimedCollateralForAuction(securityPool, parent, data);
_finalizeOwnershipAfterAuction(securityPool, parentData, repPurchased);
_finalizeEscalationStateAfterAuction(securityPool, parentData);
_emitFinalizeAuctionEvent(securityPool, parentData, data, repPurchased);
securityPool.updateRetentionRate();
}
function _consumeTruthAuctionRep(
ISecurityPool securityPool,
SecurityPoolForkerForkData storage data
) private returns (uint256 repPurchased) {
if (data.truthAuction.auctionStarted() != 0) {
uint256 balanceBeforeFinalize = address(this).balance;
data.truthAuction.finalize();
uint256 ethReceived = address(this).balance - balanceBeforeFinalize;
if (ethReceived > 0) {
(bool sent, ) = payable(address(securityPool)).call{ value: ethReceived }('');
require(sent, 'truth auction ETH transfer failed');
}
repPurchased = data.truthAuction.totalRepPurchased();
}
securityPool.setSystemState(SystemState.Operational);
}
function _captureUnclaimedCollateralForAuction(
ISecurityPool securityPool,
ISecurityPool parent,
SecurityPoolForkerForkData storage data
) private {
uint256 balance = address(securityPool).balance;
uint256 feesOwed = securityPool.totalFeesOwedToVaults();
uint256 collateralAmount = balance >= feesOwed ? balance - feesOwed : 0;
uint256 parentTotalSecurityBondAllowance = parent.totalSecurityBondAllowance();
data.auctionedSecurityBondAllowance = parentTotalSecurityBondAllowance - data.migratedSecurityBondAllowance;
securityPool.setPoolFinancials(collateralAmount, parentTotalSecurityBondAllowance);
}
function _finalizeOwnershipAfterAuction(
ISecurityPool securityPool,
SecurityPoolForkerForkData storage parentData,
uint256 repPurchased
) private {
uint256 repAvailable = _getPoolAuctionableRepAtFork(parentData);
if (repAvailable > 0) {
uint256 unsoldRep = repAvailable - repPurchased;
if (unsoldRep > 0) {
// Preserve the current vault-ownership distribution while scaling it down to the
// unsold REP that remains redeemable by migrated parent vaults. Auction buyers
// will mint into the reserved gap via `claimAuctionProceeds`.
uint256 currentOwnershipDenominator = securityPool.poolOwnershipDenominator();
securityPool.setOwnershipDenominator((currentOwnershipDenominator * repAvailable) / unsoldRep);
} else {
// All rep purchased; avoid division by zero by using repAvailable directly
securityPool.setOwnershipDenominator(repAvailable * SecurityPoolUtils.PRICE_PRECISION);
}
}
if (securityPool.poolOwnershipDenominator() == 0) {
// wipe all rep holders in vaults
securityPool.setOwnershipDenominator(repAvailable * SecurityPoolUtils.PRICE_PRECISION);
}
}
function _finalizeEscalationStateAfterAuction(
ISecurityPool securityPool,
SecurityPoolForkerForkData storage parentData
) private {
if (!parentData.unresolvedEscalationAtFork) return;
securityPool.setAwaitingForkContinuation(false);
EscalationGame childEscalationGame = securityPool.escalationGame();
if (
address(childEscalationGame) != address(0x0) &&
childEscalationGame.forkContinuation() &&
!childEscalationGame.forkContinuationResumed()
) {
securityPool.resumeForkedEscalationGame();
}
}
function _emitFinalizeAuctionEvent(
ISecurityPool securityPool,
SecurityPoolForkerForkData storage parentData,
SecurityPoolForkerForkData storage data,
uint256 repPurchased
) private {
emit FinalizeAuction(
_getPoolAuctionableRepAtFork(parentData),
data.migratedRep,
repPurchased,
securityPool.poolOwnershipDenominator(),
securityPool.completeSetCollateralAmount()
);
}
function finalizeTruthAuction(ISecurityPool securityPool) public {
require(
block.timestamp > _getForkData(securityPool).truthAuctionStarted + SecurityPoolUtils.AUCTION_TIME,
'f5'
);
_finalizeTruthAuction(securityPool);
}
function forkZoltarWithOwnEscalationGame(ISecurityPool securityPool) public {
EscalationGame escalationGame = securityPool.escalationGame();
require(address(escalationGame) != address(0x0) && escalationGame.nonDecisionTimestamp() > 0, 'f6');
require(securityPool.systemState() != SystemState.PoolForked, 'e8');
require(securityPool.systemState() == SystemState.Operational, 'e9');
ReputationToken rep = securityPool.repToken();
uint256 poolRepToFork = rep.balanceOf(address(securityPool));
uint256 repBalanceBefore = rep.balanceOf(address(this));
securityPool.activateForkMode();
uint256 escalationRepToFork = escalationGame.drainAllRep(address(this));
SecurityPoolForkerForkData storage data = forkDataByPool[securityPool];
data.ownFork = true;
SecurityPoolMigrationProxy migrationProxy = _getOrDeployMigrationProxy(securityPool);
uint256 repBalanceAfter = rep.balanceOf(address(this));
uint256 repToFork = repBalanceAfter - repBalanceBefore;
if (repToFork > 0) IERC20(address(rep)).safeTransfer(address(migrationProxy), repToFork);
migrationProxy.forkUniverse(securityPool.questionId());
uint256 leftoverProxyRep = rep.balanceOf(address(migrationProxy));
if (leftoverProxyRep > 0) migrationProxy.lockRep(leftoverProxyRep);
uint256 forkTime = zoltar.getForkTime(securityPool.universeId());
require(forkTime > 0, 'e7');
_snapshotEscalationAtFork(data, escalationGame, forkTime);
uint256 auctionableRepAtFork = zoltar.getMigrationRepBalance(
address(migrationProxy),
securityPool.universeId()
);
uint256 totalRepBeforeBurn = poolRepToFork + escalationRepToFork;
uint256 vaultRepAtFork =
totalRepBeforeBurn == 0 ? 0 : (poolRepToFork * auctionableRepAtFork) / totalRepBeforeBurn;
_initializeOwnForkRepBuckets(
securityPool,
vaultRepAtFork,
auctionableRepAtFork - vaultRepAtFork,
escalationRepToFork
);
data.auctionableRepAtFork = auctionableRepAtFork;
data.ownForkCollateralAtFork = securityPool.completeSetCollateralAmount();
data.ownForkMigratedRepCollateralized = 0;
data.ownForkCollateralTransferred = 0;
emit InitiateSecurityPoolFork(data.auctionableRepAtFork);
}
// Settles finalized truth-auction bids through the forker-owned auction.
// Winning and partial bids credit purchased REP into the vault and assign the
// corresponding share of auctioned allowance. Finalized losing bids may still
// settle here as ETH-only refunds, in which case no vault accounting changes.
// Anyone can call this so that settlement is not blocked on the bidder.
function claimAuctionProceeds(
ISecurityPool securityPool,
address vault,
IUniformPriceDualCapBatchAuction.TickIndex[] calldata tickIndices
) external {
_claimAuctionProceeds(securityPool, vault, tickIndices);
}
// settleAuctionBids lets callers submit both claim and refund batches in a single
// transaction. Before finalization, only refundable bids can be settled.
// After finalization, both sets are withdrawn as settlement payouts from the auction.
function settleAuctionBids(
ISecurityPool securityPool,
address vault,
IUniformPriceDualCapBatchAuction.TickIndex[] calldata claimTickIndices,
IUniformPriceDualCapBatchAuction.TickIndex[] calldata refundTickIndices
) external {
require(claimTickIndices.length > 0 || refundTickIndices.length > 0, 'f7');
if (forkDataByPool[securityPool].truthAuction.finalized()) {
IUniformPriceDualCapBatchAuction.TickIndex[]
memory allTickIndices = new IUniformPriceDualCapBatchAuction.TickIndex[](
claimTickIndices.length + refundTickIndices.length
);
for (uint256 i = 0; i < claimTickIndices.length; i += 1) {
allTickIndices[i] = claimTickIndices[i];
}
for (uint256 i = 0; i < refundTickIndices.length; i += 1) {
allTickIndices[claimTickIndices.length + i] = refundTickIndices[i];
}
_claimAuctionProceeds(securityPool, vault, allTickIndices);
return;
}
require(claimTickIndices.length == 0, 'f8');
_refundLosingAuctionBidsForSettlement(securityPool, vault, refundTickIndices);
}
function _claimAuctionProceeds(
ISecurityPool securityPool,
address vault,
IUniformPriceDualCapBatchAuction.TickIndex[] memory tickIndices
) private {
SecurityPoolForkerForkData storage data = forkDataByPool[securityPool];
require(data.truthAuction.finalized(), 'f9');
(uint256 amount, ) = data.truthAuction.withdrawBids(vault, tickIndices);
if (amount == 0) return;
uint256 poolOwnershipAmount = repToPoolOwnership(securityPool, amount);
(uint256 poolOwnership, uint256 currentSecurityBondAllowance, , uint256 currentFeeIndex) = securityPool
.securityVaults(vault);
uint256 newSecurityBondAllowance = _calculateAuctionedSecurityBondAllowance(data, amount);
data.claimedAuctionRepPurchased += amount;
data.claimedAuctionedSecurityBondAllowance += newSecurityBondAllowance;
uint256 nextFeeIndex = currentSecurityBondAllowance > 0 ? currentFeeIndex : securityPool.feeIndex();
securityPool.configureVault(
vault,
poolOwnership + poolOwnershipAmount,
currentSecurityBondAllowance + newSecurityBondAllowance,
nextFeeIndex
);
emit ClaimAuctionProceeds(vault, amount, poolOwnershipAmount, securityPool.poolOwnershipDenominator());
}
function _calculateAuctionedSecurityBondAllowance(
SecurityPoolForkerForkData storage data,
uint256 amount
) private view returns (uint256 newSecurityBondAllowance) {
uint256 totalRepPurchased = data.truthAuction.totalRepPurchased();
if (data.claimedAuctionRepPurchased + amount == totalRepPurchased) {
newSecurityBondAllowance = data.auctionedSecurityBondAllowance - data.claimedAuctionedSecurityBondAllowance;
} else {
newSecurityBondAllowance = (data.auctionedSecurityBondAllowance * amount) / totalRepPurchased;
}
}
function _refundLosingAuctionBidsForSettlement(
ISecurityPool securityPool,
address vault,
IUniformPriceDualCapBatchAuction.TickIndex[] calldata tickIndices
) private {
forkDataByPool[securityPool].truthAuction.refundLosingBidsFor(vault, tickIndices);
}
function getQuestionOutcome(
ISecurityPool securityPool
) external view returns (BinaryOutcomes.BinaryOutcome outcome) {
SystemState systemState = securityPool.systemState();
if (systemState == SystemState.PoolForked) return BinaryOutcomes.BinaryOutcome.None;
ISecurityPool parent = securityPool.parent();
if (address(parent) != address(0x0)) {
SecurityPoolForkerForkData storage parentData = _getForkData(parent);
SecurityPoolForkerForkData storage childData = _getForkData(securityPool);
if (parentData.ownFork) return BinaryOutcomes.BinaryOutcome(childData.outcomeIndex);
}
if (systemState == SystemState.Operational) {
EscalationGame escalationGame = securityPool.escalationGame();
uint256 forkTime = zoltar.getForkTime(securityPool.universeId());
if (address(escalationGame) != address(0x0)) {
uint256 escalationEndDate = escalationGame.getEscalationGameEndDate();
if (block.timestamp > escalationEndDate && (forkTime == 0 || escalationEndDate < forkTime))
return escalationGame.getQuestionResolution();
}
}
return BinaryOutcomes.BinaryOutcome.None;
}
receive() external payable {
require(trustedAuctionAddresses[msg.sender], 'fa');
}
}