-
Notifications
You must be signed in to change notification settings - Fork 49
/
Ticket.sol
422 lines (348 loc) Β· 13.2 KB
/
Ticket.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./libraries/ExtendedSafeCastLib.sol";
import "./libraries/TwabLib.sol";
import "./interfaces/ITicket.sol";
import "./ControlledToken.sol";
/**
* @title PoolTogether V4 Ticket
* @author PoolTogether Inc Team
* @notice The Ticket extends the standard ERC20 and ControlledToken interfaces with time-weighted average balance functionality.
The average balance held by a user between two timestamps can be calculated, as well as the historic balance. The
historic total supply is available as well as the average total supply between two timestamps.
A user may "delegate" their balance; increasing another user's historic balance while retaining their tokens.
*/
contract Ticket is ControlledToken, ITicket {
using SafeERC20 for IERC20;
using ExtendedSafeCastLib for uint256;
// solhint-disable-next-line var-name-mixedcase
bytes32 private immutable _DELEGATE_TYPEHASH =
keccak256("Delegate(address user,address delegate,uint256 nonce,uint256 deadline)");
/// @notice Record of token holders TWABs for each account.
mapping(address => TwabLib.Account) internal userTwabs;
/// @notice Record of tickets total supply and ring buff parameters used for observation.
TwabLib.Account internal totalSupplyTwab;
/// @notice Mapping of delegates. Each address can delegate their ticket power to another.
mapping(address => address) internal delegates;
/* ============ Constructor ============ */
/**
* @notice Constructs Ticket with passed parameters.
* @param _name ERC20 ticket token name.
* @param _symbol ERC20 ticket token symbol.
* @param decimals_ ERC20 ticket token decimals.
* @param _controller ERC20 ticket controller address (ie: Prize Pool address).
*/
constructor(
string memory _name,
string memory _symbol,
uint8 decimals_,
address _controller
) ControlledToken(_name, _symbol, decimals_, _controller) {}
/* ============ External Functions ============ */
/// @inheritdoc ITicket
function getAccountDetails(address _user)
external
view
override
returns (TwabLib.AccountDetails memory)
{
return userTwabs[_user].details;
}
/// @inheritdoc ITicket
function getTwab(address _user, uint16 _index)
external
view
override
returns (ObservationLib.Observation memory)
{
return userTwabs[_user].twabs[_index];
}
/// @inheritdoc ITicket
function getBalanceAt(address _user, uint64 _target) external view override returns (uint256) {
TwabLib.Account storage account = userTwabs[_user];
return
TwabLib.getBalanceAt(
account.twabs,
account.details,
uint32(_target),
uint32(block.timestamp)
);
}
/// @inheritdoc ITicket
function getAverageBalancesBetween(
address _user,
uint64[] calldata _startTimes,
uint64[] calldata _endTimes
) external view override returns (uint256[] memory) {
return _getAverageBalancesBetween(userTwabs[_user], _startTimes, _endTimes);
}
/// @inheritdoc ITicket
function getAverageTotalSuppliesBetween(
uint64[] calldata _startTimes,
uint64[] calldata _endTimes
) external view override returns (uint256[] memory) {
return _getAverageBalancesBetween(totalSupplyTwab, _startTimes, _endTimes);
}
/// @inheritdoc ITicket
function getAverageBalanceBetween(
address _user,
uint64 _startTime,
uint64 _endTime
) external view override returns (uint256) {
TwabLib.Account storage account = userTwabs[_user];
return
TwabLib.getAverageBalanceBetween(
account.twabs,
account.details,
uint32(_startTime),
uint32(_endTime),
uint32(block.timestamp)
);
}
/// @inheritdoc ITicket
function getBalancesAt(address _user, uint64[] calldata _targets)
external
view
override
returns (uint256[] memory)
{
uint256 length = _targets.length;
uint256[] memory _balances = new uint256[](length);
TwabLib.Account storage twabContext = userTwabs[_user];
TwabLib.AccountDetails memory details = twabContext.details;
for (uint256 i = 0; i < length; i++) {
_balances[i] = TwabLib.getBalanceAt(
twabContext.twabs,
details,
uint32(_targets[i]),
uint32(block.timestamp)
);
}
return _balances;
}
/// @inheritdoc ITicket
function getTotalSupplyAt(uint64 _target) external view override returns (uint256) {
return
TwabLib.getBalanceAt(
totalSupplyTwab.twabs,
totalSupplyTwab.details,
uint32(_target),
uint32(block.timestamp)
);
}
/// @inheritdoc ITicket
function getTotalSuppliesAt(uint64[] calldata _targets)
external
view
override
returns (uint256[] memory)
{
uint256 length = _targets.length;
uint256[] memory totalSupplies = new uint256[](length);
TwabLib.AccountDetails memory details = totalSupplyTwab.details;
for (uint256 i = 0; i < length; i++) {
totalSupplies[i] = TwabLib.getBalanceAt(
totalSupplyTwab.twabs,
details,
uint32(_targets[i]),
uint32(block.timestamp)
);
}
return totalSupplies;
}
/// @inheritdoc ITicket
function delegateOf(address _user) external view override returns (address) {
return delegates[_user];
}
/// @inheritdoc ITicket
function controllerDelegateFor(address _user, address _to) external override onlyController {
_delegate(_user, _to);
}
/// @inheritdoc ITicket
function delegateWithSignature(
address _user,
address _newDelegate,
uint256 _deadline,
uint8 _v,
bytes32 _r,
bytes32 _s
) external virtual override {
require(block.timestamp <= _deadline, "Ticket/delegate-expired-deadline");
bytes32 structHash = keccak256(abi.encode(_DELEGATE_TYPEHASH, _user, _newDelegate, _useNonce(_user), _deadline));
bytes32 hash = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(hash, _v, _r, _s);
require(signer == _user, "Ticket/delegate-invalid-signature");
_delegate(_user, _newDelegate);
}
/// @inheritdoc ITicket
function delegate(address _to) external virtual override {
_delegate(msg.sender, _to);
}
/// @notice Delegates a users chance to another
/// @param _user The user whose balance should be delegated
/// @param _to The delegate
function _delegate(address _user, address _to) internal {
uint256 balance = balanceOf(_user);
address currentDelegate = delegates[_user];
if (currentDelegate == _to) {
return;
}
delegates[_user] = _to;
_transferTwab(currentDelegate, _to, balance);
emit Delegated(_user, _to);
}
/* ============ Internal Functions ============ */
/**
* @notice Retrieves the average balances held by a user for a given time frame.
* @param _account The user whose balance is checked.
* @param _startTimes The start time of the time frame.
* @param _endTimes The end time of the time frame.
* @return The average balance that the user held during the time frame.
*/
function _getAverageBalancesBetween(
TwabLib.Account storage _account,
uint64[] calldata _startTimes,
uint64[] calldata _endTimes
) internal view returns (uint256[] memory) {
uint256 startTimesLength = _startTimes.length;
require(startTimesLength == _endTimes.length, "Ticket/start-end-times-length-match");
TwabLib.AccountDetails memory accountDetails = _account.details;
uint256[] memory averageBalances = new uint256[](startTimesLength);
uint32 currentTimestamp = uint32(block.timestamp);
for (uint256 i = 0; i < startTimesLength; i++) {
averageBalances[i] = TwabLib.getAverageBalanceBetween(
_account.twabs,
accountDetails,
uint32(_startTimes[i]),
uint32(_endTimes[i]),
currentTimestamp
);
}
return averageBalances;
}
// @inheritdoc ERC20
function _beforeTokenTransfer(address _from, address _to, uint256 _amount) internal override {
if (_from == _to) {
return;
}
address _fromDelegate;
if (_from != address(0)) {
_fromDelegate = delegates[_from];
}
address _toDelegate;
if (_to != address(0)) {
_toDelegate = delegates[_to];
}
_transferTwab(_fromDelegate, _toDelegate, _amount);
}
/// @notice Transfers the given TWAB balance from one user to another
/// @param _from The user to transfer the balance from. May be zero in the event of a mint.
/// @param _to The user to transfer the balance to. May be zero in the event of a burn.
/// @param _amount The balance that is being transferred.
function _transferTwab(address _from, address _to, uint256 _amount) internal {
// If we are transferring tokens from a delegated account to an undelegated account
if (_from != address(0)) {
_decreaseUserTwab(_from, _amount);
if (_to == address(0)) {
_decreaseTotalSupplyTwab(_amount);
}
}
// If we are transferring tokens from an undelegated account to a delegated account
if (_to != address(0)) {
_increaseUserTwab(_to, _amount);
if (_from == address(0)) {
_increaseTotalSupplyTwab(_amount);
}
}
}
/**
* @notice Increase `_to` TWAB balance.
* @param _to Address of the delegate.
* @param _amount Amount of tokens to be added to `_to` TWAB balance.
*/
function _increaseUserTwab(
address _to,
uint256 _amount
) internal {
if (_amount == 0) {
return;
}
TwabLib.Account storage _account = userTwabs[_to];
(
TwabLib.AccountDetails memory accountDetails,
ObservationLib.Observation memory twab,
bool isNew
) = TwabLib.increaseBalance(_account, _amount.toUint208(), uint32(block.timestamp));
_account.details = accountDetails;
if (isNew) {
emit NewUserTwab(_to, twab);
}
}
/**
* @notice Decrease `_to` TWAB balance.
* @param _to Address of the delegate.
* @param _amount Amount of tokens to be added to `_to` TWAB balance.
*/
function _decreaseUserTwab(
address _to,
uint256 _amount
) internal {
if (_amount == 0) {
return;
}
TwabLib.Account storage _account = userTwabs[_to];
(
TwabLib.AccountDetails memory accountDetails,
ObservationLib.Observation memory twab,
bool isNew
) = TwabLib.decreaseBalance(
_account,
_amount.toUint208(),
"Ticket/twab-burn-lt-balance",
uint32(block.timestamp)
);
_account.details = accountDetails;
if (isNew) {
emit NewUserTwab(_to, twab);
}
}
/// @notice Decreases the total supply twab. Should be called anytime a balance moves from delegated to undelegated
/// @param _amount The amount to decrease the total by
function _decreaseTotalSupplyTwab(uint256 _amount) internal {
if (_amount == 0) {
return;
}
(
TwabLib.AccountDetails memory accountDetails,
ObservationLib.Observation memory tsTwab,
bool tsIsNew
) = TwabLib.decreaseBalance(
totalSupplyTwab,
_amount.toUint208(),
"Ticket/burn-amount-exceeds-total-supply-twab",
uint32(block.timestamp)
);
totalSupplyTwab.details = accountDetails;
if (tsIsNew) {
emit NewTotalSupplyTwab(tsTwab);
}
}
/// @notice Increases the total supply twab. Should be called anytime a balance moves from undelegated to delegated
/// @param _amount The amount to increase the total by
function _increaseTotalSupplyTwab(uint256 _amount) internal {
if (_amount == 0) {
return;
}
(
TwabLib.AccountDetails memory accountDetails,
ObservationLib.Observation memory _totalSupply,
bool tsIsNew
) = TwabLib.increaseBalance(totalSupplyTwab, _amount.toUint208(), uint32(block.timestamp));
totalSupplyTwab.details = accountDetails;
if (tsIsNew) {
emit NewTotalSupplyTwab(_totalSupply);
}
}
}