-
Notifications
You must be signed in to change notification settings - Fork 11
/
StabilityPoolStorage.sol
101 lines (78 loc) · 3.9 KB
/
StabilityPoolStorage.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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
import "./Interfaces/IBorrowerOperations.sol";
import "./Interfaces/IStabilityPool.sol";
import "./Interfaces/IBorrowerOperations.sol";
import "./Interfaces/ITroveManager.sol";
import "./Interfaces/IZUSDToken.sol";
import "./Interfaces/ISortedTroves.sol";
import "./Interfaces/ICommunityIssuance.sol";
import "./Dependencies/Ownable.sol";
import "./Dependencies/BaseMath.sol";
contract StabilityPoolStorage is Ownable, BaseMath {
string public constant NAME = "StabilityPool";
IBorrowerOperations public borrowerOperations;
ITroveManager public troveManager;
IZUSDToken public zusdToken;
// Needed to check if there are pending liquidations
ISortedTroves public sortedTroves;
ICommunityIssuance public communityIssuance;
uint256 internal ETH; // deposited ether tracker
// Tracker for ZUSD held in the pool. Changes when users deposit/withdraw, and when Trove debt is offset.
uint256 internal totalZUSDDeposits;
// --- Data structures ---
struct FrontEnd {
uint256 kickbackRate;
bool registered;
}
struct Deposit {
uint256 initialValue;
address frontEndTag;
}
struct Snapshots {
uint256 S;
uint256 P;
uint256 G;
uint128 scale;
uint128 epoch;
}
mapping(address => Deposit) public deposits; // depositor address -> Deposit struct
mapping(address => Snapshots) public depositSnapshots; // depositor address -> snapshots struct
mapping(address => FrontEnd) public frontEnds; // front end address -> FrontEnd struct
mapping(address => uint256) public frontEndStakes; // front end address -> last recorded total deposits, tagged with that front end
mapping(address => Snapshots) public frontEndSnapshots; // front end address -> snapshots struct
/* Product 'P': Running product by which to multiply an initial deposit, in order to find the current compounded deposit,
* after a series of liquidations have occurred, each of which cancel some ZUSD debt with the deposit.
*
* During its lifetime, a deposit's value evolves from d_t to d_t * P / P_t , where P_t
* is the snapshot of P taken at the instant the deposit was made. 18-digit decimal.
*/
uint256 public P;
uint256 public constant SCALE_FACTOR = 1e9;
// Each time the scale of P shifts by SCALE_FACTOR, the scale is incremented by 1
uint128 public currentScale;
// With each offset that fully empties the Pool, the epoch is incremented by 1
uint128 public currentEpoch;
/* ETH Gain sum 'S': During its lifetime, each deposit d_t earns an ETH gain of ( d_t * [S - S_t] )/P_t, where S_t
* is the depositor's snapshot of S taken at the time t when the deposit was made.
*
* The 'S' sums are stored in a nested mapping (epoch => scale => sum):
*
* - The inner mapping records the sum S at different scales
* - The outer mapping records the (scale => sum) mappings, for different epochs.
*/
mapping(uint128 => mapping(uint128 => uint256)) public epochToScaleToSum;
/*
* Similarly, the sum 'G' is used to calculate ZERO gains. During it's lifetime, each deposit d_t earns a ZERO gain of
* ( d_t * [G - G_t] )/P_t, where G_t is the depositor's snapshot of G taken at time t when the deposit was made.
*
* ZERO reward events occur are triggered by depositor operations (new deposit, topup, withdrawal), and liquidations.
* In each case, the ZERO reward is issued (i.e. G is updated), before other state changes are made.
*/
mapping(uint128 => mapping(uint128 => uint256)) public epochToScaleToG;
// Error tracker for the error correction in the ZERO issuance calculation
uint256 public lastZEROError;
// Error trackers for the error correction in the offset calculation
uint256 public lastETHError_Offset;
uint256 public lastZUSDLossError_Offset;
}