forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRewardEscrow.sol
331 lines (273 loc) · 10.1 KB
/
RewardEscrow.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
/*
-----------------------------------------------------------------
FILE INFORMATION
-----------------------------------------------------------------
file: RewardEscrow.sol
version: 1.0
author: Jackson Chan
Clinton Ennis
date: 2019-03-01
-----------------------------------------------------------------
MODULE DESCRIPTION
-----------------------------------------------------------------
Escrows the SNX rewards from the inflationary supply awarded to
users for staking their SNX and maintaining the c-ratio target.
SNX rewards are escrowed for 1 year from the claim date and users
can call vest in 12 months time.
-----------------------------------------------------------------
*/
pragma solidity 0.4.25;
import "./SafeDecimalMath.sol";
import "./Owned.sol";
import "./interfaces/IFeePool.sol";
import "./interfaces/ISynthetix.sol";
/**
* @title A contract to hold escrowed SNX and free them at given schedules.
*/
contract RewardEscrow is Owned {
using SafeMath for uint;
/* The corresponding Synthetix contract. */
ISynthetix public synthetix;
IFeePool public feePool;
/* Lists of (timestamp, quantity) pairs per account, sorted in ascending time order.
* These are the times at which each given quantity of SNX vests. */
mapping(address => uint[2][]) public vestingSchedules;
/* An account's total escrowed synthetix balance to save recomputing this for fee extraction purposes. */
mapping(address => uint) public totalEscrowedAccountBalance;
/* An account's total vested reward synthetix. */
mapping(address => uint) public totalVestedAccountBalance;
/* The total remaining escrowed balance, for verifying the actual synthetix balance of this contract against. */
uint public totalEscrowedBalance;
uint constant TIME_INDEX = 0;
uint constant QUANTITY_INDEX = 1;
/* Limit vesting entries to disallow unbounded iteration over vesting schedules.
* There are 5 years of the supply schedule */
uint constant public MAX_VESTING_ENTRIES = 52*5;
/* ========== CONSTRUCTOR ========== */
constructor(address _owner, ISynthetix _synthetix, IFeePool _feePool)
Owned(_owner)
public
{
synthetix = _synthetix;
feePool = _feePool;
}
/* ========== SETTERS ========== */
/**
* @notice set the synthetix contract address as we need to transfer SNX when the user vests
*/
function setSynthetix(ISynthetix _synthetix)
external
onlyOwner
{
synthetix = _synthetix;
emit SynthetixUpdated(_synthetix);
}
/**
* @notice set the FeePool contract as it is the only authority to be able to call
* appendVestingEntry with the onlyFeePool modifer
*/
function setFeePool(IFeePool _feePool)
external
onlyOwner
{
feePool = _feePool;
emit FeePoolUpdated(_feePool);
}
/* ========== VIEW FUNCTIONS ========== */
/**
* @notice A simple alias to totalEscrowedAccountBalance: provides ERC20 balance integration.
*/
function balanceOf(address account)
public
view
returns (uint)
{
return totalEscrowedAccountBalance[account];
}
/**
* @notice The number of vesting dates in an account's schedule.
*/
function numVestingEntries(address account)
public
view
returns (uint)
{
return vestingSchedules[account].length;
}
/**
* @notice Get a particular schedule entry for an account.
* @return A pair of uints: (timestamp, synthetix quantity).
*/
function getVestingScheduleEntry(address account, uint index)
public
view
returns (uint[2])
{
return vestingSchedules[account][index];
}
/**
* @notice Get the time at which a given schedule entry will vest.
*/
function getVestingTime(address account, uint index)
public
view
returns (uint)
{
return getVestingScheduleEntry(account,index)[TIME_INDEX];
}
/**
* @notice Get the quantity of SNX associated with a given schedule entry.
*/
function getVestingQuantity(address account, uint index)
public
view
returns (uint)
{
return getVestingScheduleEntry(account,index)[QUANTITY_INDEX];
}
/**
* @notice Obtain the index of the next schedule entry that will vest for a given user.
*/
function getNextVestingIndex(address account)
public
view
returns (uint)
{
uint len = numVestingEntries(account);
for (uint i = 0; i < len; i++) {
if (getVestingTime(account, i) != 0) {
return i;
}
}
return len;
}
/**
* @notice Obtain the next schedule entry that will vest for a given user.
* @return A pair of uints: (timestamp, synthetix quantity). */
function getNextVestingEntry(address account)
public
view
returns (uint[2])
{
uint index = getNextVestingIndex(account);
if (index == numVestingEntries(account)) {
return [uint(0), 0];
}
return getVestingScheduleEntry(account, index);
}
/**
* @notice Obtain the time at which the next schedule entry will vest for a given user.
*/
function getNextVestingTime(address account)
external
view
returns (uint)
{
return getNextVestingEntry(account)[TIME_INDEX];
}
/**
* @notice Obtain the quantity which the next schedule entry will vest for a given user.
*/
function getNextVestingQuantity(address account)
external
view
returns (uint)
{
return getNextVestingEntry(account)[QUANTITY_INDEX];
}
/**
* @notice return the full vesting schedule entries vest for a given user.
* @dev For DApps to display the vesting schedule for the
* inflationary supply over 5 years. Solidity cant return variable length arrays
* so this is returning pairs of data. Vesting Time at [0] and quantity at [1] and so on
*/
function checkAccountSchedule(address account)
public
view
returns (uint[520])
{
uint[520] memory _result;
uint schedules = numVestingEntries(account);
for (uint i = 0; i < schedules; i++) {
uint[2] memory pair = getVestingScheduleEntry(account, i);
_result[i*2] = pair[0];
_result[i*2 + 1] = pair[1];
}
return _result;
}
/* ========== MUTATIVE FUNCTIONS ========== */
/**
* @notice Add a new vesting entry at a given time and quantity to an account's schedule.
* @dev A call to this should accompany a previous successful call to synthetix.transfer(rewardEscrow, amount),
* to ensure that when the funds are withdrawn, there is enough balance.
* Note; although this function could technically be used to produce unbounded
* arrays, it's only withinn the 4 year period of the weekly inflation schedule.
* @param account The account to append a new vesting entry to.
* @param quantity The quantity of SNX that will be escrowed.
*/
function appendVestingEntry(address account, uint quantity)
public
onlyFeePool
{
/* No empty or already-passed vesting entries allowed. */
require(quantity != 0, "Quantity cannot be zero");
/* There must be enough balance in the contract to provide for the vesting entry. */
totalEscrowedBalance = totalEscrowedBalance.add(quantity);
require(totalEscrowedBalance <= synthetix.balanceOf(this), "Must be enough balance in the contract to provide for the vesting entry");
/* Disallow arbitrarily long vesting schedules in light of the gas limit. */
uint scheduleLength = vestingSchedules[account].length;
require(scheduleLength <= MAX_VESTING_ENTRIES, "Vesting schedule is too long");
/* Escrow the tokens for 1 year. */
uint time = now + 52 weeks;
if (scheduleLength == 0) {
totalEscrowedAccountBalance[account] = quantity;
} else {
/* Disallow adding new vested SNX earlier than the last one.
* Since entries are only appended, this means that no vesting date can be repeated. */
require(getVestingTime(account, scheduleLength - 1) < time, "Cannot add new vested entries earlier than the last one");
totalEscrowedAccountBalance[account] = totalEscrowedAccountBalance[account].add(quantity);
}
vestingSchedules[account].push([time, quantity]);
emit VestingEntryCreated(account, now, quantity);
}
/**
* @notice Allow a user to withdraw any SNX in their schedule that have vested.
*/
function vest()
external
{
uint numEntries = numVestingEntries(msg.sender);
uint total;
for (uint i = 0; i < numEntries; i++) {
uint time = getVestingTime(msg.sender, i);
/* The list is sorted; when we reach the first future time, bail out. */
if (time > now) {
break;
}
uint qty = getVestingQuantity(msg.sender, i);
if (qty == 0) {
continue;
}
vestingSchedules[msg.sender][i] = [0, 0];
total = total.add(qty);
}
if (total != 0) {
totalEscrowedBalance = totalEscrowedBalance.sub(total);
totalEscrowedAccountBalance[msg.sender] = totalEscrowedAccountBalance[msg.sender].sub(total);
totalVestedAccountBalance[msg.sender] = totalVestedAccountBalance[msg.sender].add(total);
synthetix.transfer(msg.sender, total);
emit Vested(msg.sender, now, total);
}
}
/* ========== MODIFIERS ========== */
modifier onlyFeePool() {
bool isFeePool = msg.sender == address(feePool);
require(isFeePool, "Only the FeePool contracts can perform this action");
_;
}
/* ========== EVENTS ========== */
event SynthetixUpdated(address newSynthetix);
event FeePoolUpdated(address newFeePool);
event Vested(address indexed beneficiary, uint time, uint value);
event VestingEntryCreated(address indexed beneficiary, uint time, uint value);
}