-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPowerIndexPool.sol
253 lines (213 loc) · 8.07 KB
/
PowerIndexPool.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
/*
https://powerpool.finance/
wrrrw r wrr
ppwr rrr wppr0 prwwwrp prwwwrp wr0
rr 0rrrwrrprpwp0 pp pr prrrr0 pp 0r prrrr0 0rwrrr pp pr prrrr0 prrrr0 r0
rrp pr wr00rrp prwww0 pp wr pp w00r prwwwpr 0rw prwww0 pp wr pp wr r0
r0rprprwrrrp pr0 pp wr pr pp rwwr wr 0r pp wr pr wr pr r0
prwr wrr0wpwr 00 www0 0w0ww www0 0w 00 www0 www0 0www0
wrr ww0rrrr
*/
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.12;
import "./balancer-core/BPool.sol";
import "./interfaces/PowerIndexPoolInterface.sol";
import "@openzeppelin/contracts/proxy/Initializable.sol";
contract PowerIndexPool is BPool, Initializable {
/// @notice The event emitted when a dynamic weight set to token.
event SetDynamicWeight(
address indexed token,
uint256 fromDenorm,
uint256 targetDenorm,
uint256 fromTimestamp,
uint256 targetTimestamp
);
/// @notice The event emitted when weight per second bounds set.
event SetWeightPerSecondBounds(uint256 minWeightPerSecond, uint256 maxWeightPerSecond);
struct DynamicWeight {
uint256 fromTimestamp;
uint256 targetTimestamp;
uint256 targetDenorm;
}
/// @dev Mapping for storing dynamic weights settings. fromDenorm stored in _records mapping as denorm variable.
mapping(address => DynamicWeight) private _dynamicWeights;
/// @dev Min weight per second limit.
uint256 private _minWeightPerSecond;
/// @dev Max weight per second limit.
uint256 private _maxWeightPerSecond;
constructor() public BPool("", "") {}
function initialize(
string calldata name,
string calldata symbol,
address controller,
uint256 minWeightPerSecond,
uint256 maxWeightPerSecond
) external initializer {
_name = name;
_symbol = symbol;
_controller = controller;
_minWeightPerSecond = minWeightPerSecond;
_maxWeightPerSecond = maxWeightPerSecond;
}
/*** Controller Interface ***/
/**
* @notice Set minimum and maximum weight per second by controller.
* @param minWeightPerSecond Minimum weight per second.
* @param maxWeightPerSecond Maximum weight per second.
*/
function setWeightPerSecondBounds(uint256 minWeightPerSecond, uint256 maxWeightPerSecond) public _logs_ _lock_ {
_onlyController();
_minWeightPerSecond = minWeightPerSecond;
_maxWeightPerSecond = maxWeightPerSecond;
emit SetWeightPerSecondBounds(minWeightPerSecond, maxWeightPerSecond);
}
/**
* @notice Set dynamic weight for token by controller contract.
* @param token Token to change weight.
* @param targetDenorm Target weight. fromDenorm will fetch from current value of _getDenormWeight.
* @param fromTimestamp Start timestamp for changing weight.
* @param targetTimestamp Target timestamp for changing weight.
*/
function setDynamicWeight(
address token,
uint256 targetDenorm,
uint256 fromTimestamp,
uint256 targetTimestamp
) public _logs_ _lock_ {
_onlyController();
_requireTokenIsBound(token);
require(fromTimestamp > block.timestamp, "CANT_SET_PAST_TIMESTAMP");
require(targetTimestamp > fromTimestamp, "TIMESTAMP_INCORRECT_DELTA");
require(targetDenorm >= MIN_WEIGHT && targetDenorm <= MAX_WEIGHT, "TARGET_WEIGHT_BOUNDS");
uint256 fromDenorm = _getDenormWeight(token);
uint256 weightPerSecond = _getWeightPerSecond(fromDenorm, targetDenorm, fromTimestamp, targetTimestamp);
require(weightPerSecond <= _maxWeightPerSecond, "MAX_WEIGHT_PER_SECOND");
require(weightPerSecond >= _minWeightPerSecond, "MIN_WEIGHT_PER_SECOND");
_records[token].denorm = fromDenorm;
_dynamicWeights[token] = DynamicWeight({
fromTimestamp: fromTimestamp,
targetTimestamp: targetTimestamp,
targetDenorm: targetDenorm
});
uint256 denormSum = 0;
uint256 len = _tokens.length;
for (uint256 i = 0; i < len; i++) {
denormSum = badd(denormSum, _dynamicWeights[_tokens[i]].targetDenorm);
}
require(denormSum <= MAX_TOTAL_WEIGHT, "MAX_TARGET_TOTAL_WEIGHT");
emit SetDynamicWeight(token, fromDenorm, targetDenorm, fromTimestamp, targetTimestamp);
}
/**
* @notice Bind and setDynamicWeight at the same time.
* @param token Token for bind.
* @param balance Initial token balance.
* @param targetDenorm Target weight.
* @param fromTimestamp Start timestamp to change weight.
* @param targetTimestamp Target timestamp to change weight.
*/
function bind(
address token,
uint256 balance,
uint256 targetDenorm,
uint256 fromTimestamp,
uint256 targetTimestamp
)
external
_logs_ // _lock_ Bind does not lock because it jumps to `rebind` and `setDynamicWeight`, which does
{
super.bind(token, balance, MIN_WEIGHT);
setDynamicWeight(token, targetDenorm, fromTimestamp, targetTimestamp);
}
/**
* @dev Override parent unbind function.
* @param token Token for unbind.
*/
function unbind(address token) public override {
super.unbind(token);
_dynamicWeights[token] = DynamicWeight(0, 0, 0);
}
/**
* @dev Override parent bind function and disable.
*/
function bind(
address token,
uint256 balance,
uint256 denorm
) public override {
super.bind(token, balance, denorm);
}
/**
* @notice Override parent rebind function. Allowed only for calling from bind function.
* @param token Token for rebind.
* @param balance Balance for rebind.
* @param denorm Weight for rebind.
*/
function rebind(
address token,
uint256 balance,
uint256 denorm
) public override {
super.rebind(token, balance, denorm);
_dynamicWeights[token].fromTimestamp = 0;
_dynamicWeights[token].targetTimestamp = 0;
_dynamicWeights[token].targetDenorm = 0;
}
/*** View Functions ***/
function getDynamicWeightSettings(address token)
external
view
returns (
uint256 fromTimestamp,
uint256 targetTimestamp,
uint256 fromDenorm,
uint256 targetDenorm
)
{
DynamicWeight storage dw = _dynamicWeights[token];
return (dw.fromTimestamp, dw.targetTimestamp, _records[token].denorm, dw.targetDenorm);
}
function getWeightPerSecondBounds() external view returns (uint256 minWeightPerSecond, uint256 maxWeightPerSecond) {
return (_minWeightPerSecond, _maxWeightPerSecond);
}
/*** Internal Functions ***/
function _getDenormWeight(address token) internal view override returns (uint256) {
DynamicWeight memory dw = _dynamicWeights[token];
uint256 fromDenorm = _records[token].denorm;
if (dw.fromTimestamp == 0 || dw.targetDenorm == fromDenorm || block.timestamp <= dw.fromTimestamp) {
return fromDenorm;
}
if (block.timestamp >= dw.targetTimestamp) {
return dw.targetDenorm;
}
uint256 weightPerSecond = _getWeightPerSecond(fromDenorm, dw.targetDenorm, dw.fromTimestamp, dw.targetTimestamp);
uint256 deltaCurrentTime = bsub(block.timestamp, dw.fromTimestamp);
if (dw.targetDenorm > fromDenorm) {
return badd(fromDenorm, deltaCurrentTime * weightPerSecond);
} else {
return bsub(fromDenorm, deltaCurrentTime * weightPerSecond);
}
}
function _getWeightPerSecond(
uint256 fromDenorm,
uint256 targetDenorm,
uint256 fromTimestamp,
uint256 targetTimestamp
) internal pure returns (uint256) {
uint256 delta = targetDenorm > fromDenorm ? bsub(targetDenorm, fromDenorm) : bsub(fromDenorm, targetDenorm);
return div(delta, bsub(targetTimestamp, fromTimestamp));
}
function _getTotalWeight() internal view override returns (uint256) {
uint256 sum = 0;
uint256 len = _tokens.length;
for (uint256 i = 0; i < len; i++) {
sum = badd(sum, _getDenormWeight(_tokens[i]));
}
return sum;
}
function _addTotalWeight(uint256 _amount) internal virtual override {
// storage total weight don't change, it's calculated only by _getTotalWeight()
}
function _subTotalWeight(uint256 _amount) internal virtual override {
// storage total weight don't change, it's calculated only by _getTotalWeight()
}
}