This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProduct.sol
608 lines (508 loc) · 22.4 KB
/
Product.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.19;
import "@equilibria/root/control/unstructured/UInitializable.sol";
import "@equilibria/root/control/unstructured/UReentrancyGuard.sol";
import "../controller/UControllerProvider.sol";
import "./UPayoffProvider.sol";
import "./UParamProvider.sol";
import "./types/position/AccountPosition.sol";
import "./types/accumulator/AccountAccumulator.sol";
/**
* @title Product
* @notice Manages logic and state for a single product market.
* @dev Cloned by the Controller contract to launch new product markets.
*/
contract Product is IProduct, UInitializable, UParamProvider, UPayoffProvider, UReentrancyGuard {
/// @dev Whether or not the product is closed
BoolStorage private constant _closed = BoolStorage.wrap(keccak256("equilibria.perennial.Product.closed"));
function closed() public view returns (bool) {
return _closed.read();
}
/// @dev The name of the product
string public name;
/// @dev The symbol of the product
string public symbol;
/// @dev The individual position state for each account
mapping(address => AccountPosition) private _positions;
/// @dev The global position state for the product
VersionedPosition private _position;
/// @dev The individual accumulator state for each account
mapping(address => AccountAccumulator) private _accumulators;
/// @dev The global accumulator state for the product
VersionedAccumulator private _accumulator;
/**
* @notice Initializes the contract state
* @param productInfo_ Product initialization params
*/
function initialize(ProductInfo calldata productInfo_) external initializer(1) {
__UControllerProvider__initialize(IController(msg.sender));
__UPayoffProvider__initialize(productInfo_.oracle, productInfo_.payoffDefinition);
__UReentrancyGuard__initialize();
__UParamProvider__initialize(
productInfo_.maintenance,
productInfo_.fundingFee,
productInfo_.makerFee,
productInfo_.takerFee,
productInfo_.positionFee,
productInfo_.makerLimit,
productInfo_.utilizationCurve
);
name = productInfo_.name;
symbol = productInfo_.symbol;
}
/**
* @notice Surfaces global settlement externally
*/
function settle() external nonReentrant notPaused {
_settle();
}
/**
* @notice Core global settlement flywheel
* @dev
* a) last settle oracle version
* b) latest pre position oracle version
* c) current oracle version
*
* Settles from a->b then from b->c if either interval is non-zero to account for a change
* in position quantity at (b).
*
* Syncs each to instantaneously after the oracle update.
*/
function _settle() private returns (IOracleProvider.OracleVersion memory currentOracleVersion) {
IController _controller = controller();
// Get current oracle version
currentOracleVersion = _sync();
// Get latest oracle version
uint256 _latestVersion = latestVersion();
if (_latestVersion == currentOracleVersion.version) return currentOracleVersion; // short circuit entirely if a == c
IOracleProvider.OracleVersion memory latestOracleVersion = atVersion(_latestVersion);
// Get settle oracle version
uint256 _settleVersion = _position.pre.settleVersion(currentOracleVersion.version);
IOracleProvider.OracleVersion memory settleOracleVersion = _settleVersion == currentOracleVersion.version
? currentOracleVersion // if b == c, don't re-call provider for oracle version
: atVersion(_settleVersion);
// Initiate
_controller.incentivizer().sync(currentOracleVersion);
UFixed18 boundedFundingFee = _boundedFundingFee();
// value a->b
UFixed18 accumulatedFee = _accumulator.accumulate(
boundedFundingFee, _position, latestOracleVersion, settleOracleVersion);
// position a->b
_position.settle(_latestVersion, settleOracleVersion);
// Apply any pending fee updates if present
_settleFeeUpdates();
// short-circuit from a->c if b == c
if (settleOracleVersion.version != currentOracleVersion.version) {
// value b->c
accumulatedFee = accumulatedFee.add(
_accumulator.accumulate(boundedFundingFee, _position, settleOracleVersion, currentOracleVersion)
);
// position b->c (every accumulator version needs a position stamp)
_position.settle(settleOracleVersion.version, currentOracleVersion);
}
// settle collateral
_controller.collateral().settleProduct(accumulatedFee);
emit Settle(settleOracleVersion.version, currentOracleVersion.version);
}
/**
* @notice Surfaces account settlement externally
* @param account Account to settle
*/
function settleAccount(address account) external nonReentrant notPaused {
IOracleProvider.OracleVersion memory currentOracleVersion = _settle();
_settleAccount(account, currentOracleVersion);
}
/**
* @notice Core account settlement flywheel
* @param account Account to settle
* @dev
* a) last settle oracle version
* b) latest pre position oracle version
* c) current oracle version
*
* Settles from a->b then from b->c if either interval is non-zero to account for a change
* in position quantity at (b).
*
* Syncs each to instantaneously after the oracle update.
*/
function _settleAccount(address account, IOracleProvider.OracleVersion memory currentOracleVersion) private {
IController _controller = controller();
// Get latest oracle version
if (latestVersion(account) == currentOracleVersion.version) return; // short circuit entirely if a == c
// Get settle oracle version
uint256 _settleVersion = _positions[account].pre.settleVersion(currentOracleVersion.version);
IOracleProvider.OracleVersion memory settleOracleVersion = _settleVersion == currentOracleVersion.version
? currentOracleVersion // if b == c, don't re-call provider for oracle version
: atVersion(_settleVersion);
// sync incentivizer before accumulator
_controller.incentivizer().syncAccount(account, settleOracleVersion);
// value a->b
Fixed18 accumulated = _accumulators[account].syncTo(
_accumulator, _positions[account], settleOracleVersion.version).sum();
// position a->b
_positions[account].settle(settleOracleVersion);
// short-circuit from a->c if b == c
if (settleOracleVersion.version != currentOracleVersion.version) {
// sync incentivizer before accumulator
_controller.incentivizer().syncAccount(account, currentOracleVersion);
// value b->c
accumulated = accumulated.add(
_accumulators[account].syncTo(_accumulator, _positions[account], currentOracleVersion.version).sum()
);
}
// settle collateral
_controller.collateral().settleAccount(account, accumulated);
emit AccountSettle(account, settleOracleVersion.version, currentOracleVersion.version);
}
/**
* @notice Opens a taker position for `msg.sender`
* @param amount Amount of the position to open
*/
function openTake(UFixed18 amount) external {
openTakeFor(msg.sender, amount);
}
/**
* @notice Opens a taker position for `account`. Deducts position fee based on notional value at `latestVersion`
* @param account Account to open the position for
* @param amount Amount of the position to open
*/
function openTakeFor(address account, UFixed18 amount)
public
nonReentrant
notPaused
notClosed
onlyAccountOrMultiInvoker(account)
settleForAccount(account)
maxUtilizationInvariant
positionInvariant(account)
liquidationInvariant(account)
maintenanceInvariant(account)
{
IOracleProvider.OracleVersion memory latestOracleVersion = atVersion(latestVersion());
_positions[account].pre.openTake(latestOracleVersion.version, amount);
_position.pre.openTake(latestOracleVersion.version, amount);
UFixed18 positionFee = amount.mul(latestOracleVersion.price.abs()).mul(takerFee());
if (!positionFee.isZero()) {
controller().collateral().settleAccount(account, Fixed18Lib.from(-1, positionFee));
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
}
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
emit TakeOpened(account, latestOracleVersion.version, amount);
}
/**
* @notice Closes a taker position for `msg.sender`
* @param amount Amount of the position to close
*/
function closeTake(UFixed18 amount) external {
closeTakeFor(msg.sender, amount);
}
/**
* @notice Closes a taker position for `account`. Deducts position fee based on notional value at `latestVersion`
* @param account Account to close the position for
* @param amount Amount of the position to close
*/
function closeTakeFor(address account, UFixed18 amount)
public
nonReentrant
notPaused
onlyAccountOrMultiInvoker(account)
settleForAccount(account)
closeInvariant(account)
liquidationInvariant(account)
{
_closeTake(account, amount);
}
function _closeTake(address account, UFixed18 amount) private {
IOracleProvider.OracleVersion memory latestOracleVersion = atVersion(latestVersion());
_positions[account].pre.closeTake(latestOracleVersion.version, amount);
_position.pre.closeTake(latestOracleVersion.version, amount);
UFixed18 positionFee = amount.mul(latestOracleVersion.price.abs()).mul(takerFee());
if (!positionFee.isZero()) {
controller().collateral().settleAccount(account, Fixed18Lib.from(-1, positionFee));
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
}
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
emit TakeClosed(account, latestOracleVersion.version, amount);
}
/**
* @notice Opens a maker position for `msg.sender`
* @param amount Amount of the position to open
*/
function openMake(UFixed18 amount) external {
openMakeFor(msg.sender, amount);
}
/**
* @notice Opens a maker position for `account`. Deducts position fee based on notional value at `latestVersion`
* @param account Account to open position for
* @param amount Amount of the position to open
*/
function openMakeFor(address account, UFixed18 amount)
public
nonReentrant
notPaused
notClosed
onlyAccountOrMultiInvoker(account)
settleForAccount(account)
nonZeroVersionInvariant
makerInvariant
positionInvariant(account)
liquidationInvariant(account)
maintenanceInvariant(account)
{
IOracleProvider.OracleVersion memory latestOracleVersion = atVersion(latestVersion());
_positions[account].pre.openMake(latestOracleVersion.version, amount);
_position.pre.openMake(latestOracleVersion.version, amount);
UFixed18 positionFee = amount.mul(latestOracleVersion.price.abs()).mul(makerFee());
if (!positionFee.isZero()) {
controller().collateral().settleAccount(account, Fixed18Lib.from(-1, positionFee));
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
}
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
emit MakeOpened(account, latestOracleVersion.version, amount);
}
/**
* @notice Closes a maker position for `msg.sender`
* @param amount Amount of the position to close
*/
function closeMake(UFixed18 amount) external {
closeMakeFor(msg.sender, amount);
}
/**
* @notice Closes a maker position for `account`. Deducts position fee based on notional value at `latestVersion`
* @param account Account to close the position for
* @param amount Amount of the position to close
*/
function closeMakeFor(address account, UFixed18 amount)
public
nonReentrant
notPaused
onlyAccountOrMultiInvoker(account)
settleForAccount(account)
takerInvariant
closeInvariant(account)
liquidationInvariant(account)
{
_closeMake(account, amount);
}
function _closeMake(address account, UFixed18 amount) private {
IOracleProvider.OracleVersion memory latestOracleVersion = atVersion(latestVersion());
_positions[account].pre.closeMake(latestOracleVersion.version, amount);
_position.pre.closeMake(latestOracleVersion.version, amount);
UFixed18 positionFee = amount.mul(latestOracleVersion.price.abs()).mul(makerFee());
if (!positionFee.isZero()) {
controller().collateral().settleAccount(account, Fixed18Lib.from(-1, positionFee));
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
}
emit PositionFeeCharged(account, latestOracleVersion.version, positionFee);
emit MakeClosed(account, latestOracleVersion.version, amount);
}
/**
* @notice Closes all open and pending positions, locking for liquidation
* @dev Only callable by the Collateral contract as part of the liquidation flow
* @param account Account to close out
*/
function closeAll(address account) external onlyCollateral notClosed settleForAccount(account) {
AccountPosition storage accountPosition = _positions[account];
Position memory p = accountPosition.position.next(_positions[account].pre);
// Close all positions
_closeMake(account, p.maker);
_closeTake(account, p.taker);
// Mark liquidation to lock position
accountPosition.liquidation = true;
}
/**
* @notice Returns the maintenance requirement for `account`
* @param account Account to return for
* @return The current maintenance requirement
*/
function maintenance(address account) external view returns (UFixed18) {
return _positions[account].maintenance();
}
/**
* @notice Returns the maintenance requirement for `account` after next settlement
* @dev Assumes no price change and no funding, used to protect user from over-opening
* @param account Account to return for
* @return The next maintenance requirement
*/
function maintenanceNext(address account) external view returns (UFixed18) {
return _positions[account].maintenanceNext();
}
/**
* @notice Returns whether `account` has a completely zero'd position
* @param account Account to return for
* @return The the account is closed
*/
function isClosed(address account) external view returns (bool) {
return _positions[account].isClosed();
}
/**
* @notice Returns whether `account` is currently locked for an in-progress liquidation
* @param account Account to return for
* @return Whether the account is in liquidation
*/
function isLiquidating(address account) external view returns (bool) {
return _positions[account].liquidation;
}
/**
* @notice Returns `account`'s current position
* @param account Account to return for
* @return Current position of the account
*/
function position(address account) external view returns (Position memory) {
return _positions[account].position;
}
/**
* @notice Returns `account`'s current pending-settlement position
* @param account Account to return for
* @return Current pre-position of the account
*/
function pre(address account) external view returns (PrePosition memory) {
return _positions[account].pre;
}
/**
* @notice Returns the global latest settled oracle version
* @return Latest settled oracle version of the product
*/
function latestVersion() public view returns (uint256) {
return _accumulator.latestVersion;
}
/**
* @notice Returns the global position at oracleVersion `oracleVersion`
* @dev Only valid for the version at which a global settlement occurred
* @param oracleVersion Oracle version to return for
* @return Global position at oracle version
*/
function positionAtVersion(uint256 oracleVersion) public view returns (Position memory) {
return _position.positionAtVersion(oracleVersion);
}
/**
* @notice Returns the current global pending-settlement position
* @return Global pending-settlement position
*/
function pre() external view returns (PrePosition memory) {
return _position.pre;
}
/**
* @notice Returns the global accumulator value at oracleVersion `oracleVersion`
* @dev Only valid for the version at which a global settlement occurred
* @param oracleVersion Oracle version to return for
* @return Global accumulator value at oracle version
*/
function valueAtVersion(uint256 oracleVersion) external view returns (Accumulator memory) {
return _accumulator.valueAtVersion(oracleVersion);
}
/**
* @notice Returns the global accumulator share at oracleVersion `oracleVersion`
* @dev Only valid for the version at which a global settlement occurred
* @param oracleVersion Oracle version to return for
* @return Global accumulator share at oracle version
*/
function shareAtVersion(uint256 oracleVersion) external view returns (Accumulator memory) {
return _accumulator.shareAtVersion(oracleVersion);
}
/**
* @notice Returns `account`'s latest settled oracle version
* @param account Account to return for
* @return Latest settled oracle version of the account
*/
function latestVersion(address account) public view returns (uint256) {
return _accumulators[account].latestVersion;
}
/**
* @notice Returns The per-second rate based on the provided `position`
* @dev Handles 0-maker/taker edge cases
* @param position_ Position to base utilization on
* @return The per-second rate
*/
function rate(Position calldata position_) public view returns (Fixed18) {
UFixed18 utilization = position_.taker.unsafeDiv(position_.maker);
Fixed18 annualizedRate = utilizationCurve().compute(utilization);
return annualizedRate.div(Fixed18Lib.from(365 days));
}
/**
* @notice Returns the minimum funding fee parameter with a capped range for safety
* @dev Caps controller.minFundingFee() <= fundingFee() <= 1
* @return Safe minimum funding fee parameter
*/
function _boundedFundingFee() private view returns (UFixed18) {
return fundingFee().max(controller().minFundingFee());
}
/**
* @notice Updates product closed state
* @dev only callable by product owner. Settles the product before flipping the flag
* @param newClosed new closed value
*/
function updateClosed(bool newClosed) external nonReentrant notPaused onlyProductOwner {
IOracleProvider.OracleVersion memory oracleVersion = _settle();
_closed.store(newClosed);
emit ClosedUpdated(newClosed, oracleVersion.version);
}
/**
* @notice Updates underlying product oracle
* @dev only callable by product owner
* @param newOracle new oracle address
*/
function updateOracle(IOracleProvider newOracle) external onlyProductOwner {
_updateOracle(address(newOracle), latestVersion());
}
/// @dev Limit total maker for guarded rollouts
modifier makerInvariant() {
_;
Position memory next = positionAtVersion(latestVersion()).next(_position.pre);
if (next.maker.gt(makerLimit())) revert ProductMakerOverLimitError();
}
/// @dev Limit maker short exposure to the range 0.0-1.0x of their position. Does not apply when in closeOnly state
modifier takerInvariant() {
_;
if (closed()) return;
Position memory next = positionAtVersion(latestVersion()).next(_position.pre);
UFixed18 socializationFactor = next.socializationFactor();
if (socializationFactor.lt(UFixed18Lib.ONE)) revert ProductInsufficientLiquidityError(socializationFactor);
}
/// @dev Limit utilization to (1 - utilizationBuffer)
modifier maxUtilizationInvariant() {
_;
if (closed()) return;
Position memory next = positionAtVersion(latestVersion()).next(_position.pre);
UFixed18 utilization = next.taker.unsafeDiv(next.maker);
if (utilization.gt(UFixed18Lib.ONE.sub(utilizationBuffer())))
revert ProductInsufficientLiquidityError(utilization);
}
/// @dev Ensure that the user has only taken a maker or taker position, but not both
modifier positionInvariant(address account) {
_;
if (_positions[account].isDoubleSided()) revert ProductDoubleSidedError();
}
/// @dev Ensure that the user hasn't closed more than is open
modifier closeInvariant(address account) {
_;
if (_positions[account].isOverClosed()) revert ProductOverClosedError();
}
/// @dev Ensure that the user will have sufficient margin for maintenance after next settlement
modifier maintenanceInvariant(address account) {
_;
if (controller().collateral().liquidatableNext(account, IProduct(this)))
revert ProductInsufficientCollateralError();
}
/// @dev Ensure that the user is not currently being liquidated
modifier liquidationInvariant(address account) {
if (_positions[account].liquidation) revert ProductInLiquidationError();
_;
}
/// @dev Helper to fully settle an account's state
modifier settleForAccount(address account) {
IOracleProvider.OracleVersion memory _currentVersion = _settle();
_settleAccount(account, _currentVersion);
_;
}
/// @dev Ensure we have bootstraped the oracle before creating positions
modifier nonZeroVersionInvariant() {
if (latestVersion() == 0) revert ProductOracleBootstrappingError();
_;
}
/// @dev Ensure the product is not closed
modifier notClosed() {
if (closed()) revert ProductClosedError();
_;
}
}