-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCollector.sol
339 lines (292 loc) · 11.5 KB
/
Collector.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {AccessControlUpgradeable} from 'openzeppelin-contracts-upgradeable/contracts/access/AccessControlUpgradeable.sol';
import {ReentrancyGuardUpgradeable} from 'openzeppelin-contracts-upgradeable/contracts/utils/ReentrancyGuardUpgradeable.sol';
import {IERC20} from 'openzeppelin-contracts/contracts/token/ERC20/IERC20.sol';
import {SafeERC20} from 'openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol';
import {Address} from 'openzeppelin-contracts/contracts/utils/Address.sol';
import {ICollector} from './ICollector.sol';
/**
* @title Collector
* @notice Stores ERC20 tokens of an ecosystem reserve and allows to dispose of them via approval
* or transfer dynamics or streaming capabilities.
* Modification of Sablier https://github.com/sablierhq/sablier/blob/develop/packages/protocol/contracts/Sablier.sol
* Original can be found also deployed on https://etherscan.io/address/0xCD18eAa163733Da39c232722cBC4E8940b1D8888
* Modifications:
* - Sablier "pulls" the funds from the creator of the stream at creation. In the Aave case, we already have the funds.
* - Anybody can create streams on Sablier. Here, only the funds admin (Aave governance via controller) can
* - Adapted codebase to Solidity 0.8.11, mainly removing SafeMath and CarefulMath to use native safe math
* - Same as with creation, on Sablier the `sender` and `recipient` can cancel a stream. Here, only fund admin and recipient
* @author BGD Labs
**/
contract Collector is AccessControlUpgradeable, ReentrancyGuardUpgradeable, ICollector {
using SafeERC20 for IERC20;
using Address for address payable;
/*** Storage Properties ***/
/// @inheritdoc ICollector
address public constant ETH_MOCK_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @inheritdoc ICollector
bytes32 public constant FUNDS_ADMIN_ROLE = 'FUNDS_ADMIN';
// Reserved storage space to account for deprecated inherited storage
// 0 was lastInitializedRevision
// 1-50 were the ____gap
// 51 was the reentrancy guard _status
// 52 was the _fundsAdmin
// On some networks the layout was shifted by 1 due to `initializing` being on slot 1
// The upgrade proposal would in this case manually shift the storage layout to properly align the networks
uint256[53] private ______gap;
/**
* @notice Counter for new stream ids.
*/
uint256 private _nextStreamId;
/**
* @notice The stream objects identifiable by their unsigned integer ids.
*/
mapping(uint256 => Stream) private _streams;
/*** Modifiers ***/
/**
* @dev Throws if the caller does not have the FUNDS_ADMIN role
*/
modifier onlyFundsAdmin() {
if (_onlyFundsAdmin() == false) {
revert OnlyFundsAdmin();
}
_;
}
/**
* @dev Throws if the caller is not the funds admin of the recipient of the stream.
* @param streamId The id of the stream to query.
*/
modifier onlyAdminOrRecipient(uint256 streamId) {
if (_onlyFundsAdmin() == false && msg.sender != _streams[streamId].recipient) {
revert OnlyFundsAdminOrRecipient();
}
_;
}
/**
* @dev Throws if the provided id does not point to a valid stream.
*/
modifier streamExists(uint256 streamId) {
if (!_streams[streamId].isEntity) revert StreamDoesNotExist();
_;
}
constructor() {
_disableInitializers();
}
/*** Contract Logic Starts Here */
/** @notice Initializes the contracts
* @param nextStreamId StreamId to set, applied if greater than 0
* @param admin The default admin managing the FundsAdmins
**/
function initialize(uint256 nextStreamId, address admin) external virtual initializer {
__AccessControl_init();
__ReentrancyGuard_init();
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_grantRole(FUNDS_ADMIN_ROLE, admin);
if (nextStreamId != 0) {
_nextStreamId = nextStreamId;
}
}
/*** View Functions ***/
/// @inheritdoc ICollector
function isFundsAdmin(address admin) external view returns (bool) {
return hasRole(FUNDS_ADMIN_ROLE, admin);
}
/// @inheritdoc ICollector
function getNextStreamId() external view returns (uint256) {
return _nextStreamId;
}
/// @inheritdoc ICollector
function getStream(
uint256 streamId
)
external
view
streamExists(streamId)
returns (
address sender,
address recipient,
uint256 deposit,
address tokenAddress,
uint256 startTime,
uint256 stopTime,
uint256 remainingBalance,
uint256 ratePerSecond
)
{
sender = _streams[streamId].sender;
recipient = _streams[streamId].recipient;
deposit = _streams[streamId].deposit;
tokenAddress = _streams[streamId].tokenAddress;
startTime = _streams[streamId].startTime;
stopTime = _streams[streamId].stopTime;
remainingBalance = _streams[streamId].remainingBalance;
ratePerSecond = _streams[streamId].ratePerSecond;
}
/**
* @notice Returns either the delta in seconds between `block.timestamp` and `startTime` or
* between `stopTime` and `startTime, whichever is smaller. If `block.timestamp` is before
* `startTime`, it returns 0.
* @dev Throws if the id does not point to a valid stream.
* @param streamId The id of the stream for which to query the delta.
* @notice Returns the time delta in seconds.
*/
function deltaOf(uint256 streamId) public view streamExists(streamId) returns (uint256 delta) {
Stream memory stream = _streams[streamId];
if (block.timestamp <= stream.startTime) return 0;
if (block.timestamp < stream.stopTime) return block.timestamp - stream.startTime;
return stream.stopTime - stream.startTime;
}
struct BalanceOfLocalVars {
uint256 recipientBalance;
uint256 withdrawalAmount;
uint256 senderBalance;
}
/// @inheritdoc ICollector
function balanceOf(
uint256 streamId,
address who
) public view streamExists(streamId) returns (uint256 balance) {
Stream memory stream = _streams[streamId];
BalanceOfLocalVars memory vars;
uint256 delta = deltaOf(streamId);
vars.recipientBalance = delta * stream.ratePerSecond;
/*
* If the stream `balance` does not equal `deposit`, it means there have been withdrawals.
* We have to subtract the total amount withdrawn from the amount of money that has been
* streamed until now.
*/
if (stream.deposit > stream.remainingBalance) {
vars.withdrawalAmount = stream.deposit - stream.remainingBalance;
vars.recipientBalance = vars.recipientBalance - vars.withdrawalAmount;
}
if (who == stream.recipient) return vars.recipientBalance;
if (who == stream.sender) {
vars.senderBalance = stream.remainingBalance - vars.recipientBalance;
return vars.senderBalance;
}
return 0;
}
/*** Public Effects & Interactions Functions ***/
/// @inheritdoc ICollector
function approve(IERC20 token, address recipient, uint256 amount) external onlyFundsAdmin {
token.forceApprove(recipient, amount);
}
/// @inheritdoc ICollector
function transfer(IERC20 token, address recipient, uint256 amount) external onlyFundsAdmin {
if (recipient == address(0)) revert InvalidZeroAddress();
if (address(token) == ETH_MOCK_ADDRESS) {
payable(recipient).sendValue(amount);
} else {
token.safeTransfer(recipient, amount);
}
}
function _onlyFundsAdmin() internal view returns (bool) {
return hasRole(FUNDS_ADMIN_ROLE, msg.sender);
}
struct CreateStreamLocalVars {
uint256 duration;
uint256 ratePerSecond;
}
/// @inheritdoc ICollector
/**
* @dev Throws if the recipient is the zero address, the contract itself or the caller.
* Throws if the deposit is 0.
* Throws if the start time is before `block.timestamp`.
* Throws if the stop time is before the start time.
* Throws if the duration calculation has a math error.
* Throws if the deposit is smaller than the duration.
* Throws if the deposit is not a multiple of the duration.
* Throws if the rate calculation has a math error.
* Throws if the next stream id calculation has a math error.
* Throws if the contract is not allowed to transfer enough tokens.
* Throws if there is a token transfer failure.
*/
function createStream(
address recipient,
uint256 deposit,
address tokenAddress,
uint256 startTime,
uint256 stopTime
) external onlyFundsAdmin returns (uint256) {
if (recipient == address(0)) revert InvalidZeroAddress();
if (recipient == address(this)) revert InvalidRecipient();
if (recipient == msg.sender) revert InvalidRecipient();
if (deposit == 0) revert InvalidZeroAmount();
if (startTime < block.timestamp) revert InvalidStartTime();
if (stopTime <= startTime) revert InvalidStopTime();
CreateStreamLocalVars memory vars;
vars.duration = stopTime - startTime;
/* Without this, the rate per second would be zero. */
if (deposit < vars.duration) revert DepositSmallerTimeDelta();
/* This condition avoids dealing with remainders */
if (deposit % vars.duration > 0) revert DepositNotMultipleTimeDelta();
vars.ratePerSecond = deposit / vars.duration;
/* Create and store the stream object. */
uint256 streamId = _nextStreamId;
_streams[streamId] = Stream({
remainingBalance: deposit,
deposit: deposit,
isEntity: true,
ratePerSecond: vars.ratePerSecond,
recipient: recipient,
sender: address(this),
startTime: startTime,
stopTime: stopTime,
tokenAddress: tokenAddress
});
/* Increment the next stream id. */
_nextStreamId++;
emit CreateStream(
streamId,
address(this),
recipient,
deposit,
tokenAddress,
startTime,
stopTime
);
return streamId;
}
/// @inheritdoc ICollector
/**
* @dev Throws if the id does not point to a valid stream.
* Throws if the caller is not the funds admin or the recipient of the stream.
* Throws if the amount exceeds the available balance.
* Throws if there is a token transfer failure.
*/
function withdrawFromStream(
uint256 streamId,
uint256 amount
) external nonReentrant streamExists(streamId) onlyAdminOrRecipient(streamId) returns (bool) {
if (amount == 0) revert InvalidZeroAmount();
Stream memory stream = _streams[streamId];
uint256 balance = balanceOf(streamId, stream.recipient);
if (balance < amount) revert BalanceExceeded();
_streams[streamId].remainingBalance = stream.remainingBalance - amount;
if (_streams[streamId].remainingBalance == 0) delete _streams[streamId];
IERC20(stream.tokenAddress).safeTransfer(stream.recipient, amount);
emit WithdrawFromStream(streamId, stream.recipient, amount);
return true;
}
/// @inheritdoc ICollector
/**
* @dev Throws if the id does not point to a valid stream.
* Throws if the caller is not the funds admin or the recipient of the stream.
* Throws if there is a token transfer failure.
*/
function cancelStream(
uint256 streamId
) external nonReentrant streamExists(streamId) onlyAdminOrRecipient(streamId) returns (bool) {
Stream memory stream = _streams[streamId];
uint256 senderBalance = balanceOf(streamId, stream.sender);
uint256 recipientBalance = balanceOf(streamId, stream.recipient);
delete _streams[streamId];
IERC20 token = IERC20(stream.tokenAddress);
if (recipientBalance > 0) token.safeTransfer(stream.recipient, recipientBalance);
emit CancelStream(streamId, stream.sender, stream.recipient, senderBalance, recipientBalance);
return true;
}
/// @dev needed in order to receive ETH from the Aave v1 ecosystem reserve
receive() external payable {}
}