-
Notifications
You must be signed in to change notification settings - Fork 2
/
SablierV2Payroll.sol
437 lines (352 loc) Β· 16 KB
/
SablierV2Payroll.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// SPDX-License-Identifier: LGPL-3.0
pragma solidity >=0.8.13;
import { ERC721 } from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import { IERC20 } from "@prb/contracts/token/erc20/IERC20.sol";
import { SafeERC20 } from "@prb/contracts/token/erc20/SafeERC20.sol";
import { UD60x18, toUD60x18 } from "@prb/math/UD60x18.sol";
import { DataTypes } from "./libraries/DataTypes.sol";
import { Errors } from "./libraries/Errors.sol";
import { Events } from "./libraries/Events.sol";
import { Validations } from "./libraries/Validations.sol";
import { ISablierV2Payroll } from "./interfaces/ISablierV2Payroll.sol";
contract SablierV2Payroll is
ERC721("Sablier V2 Payroll", "SAB-V2-ROLL"), // one dependency
ISablierV2Payroll // one dependency
{
using SafeERC20 for IERC20;
/*//////////////////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////////////////*/
/// @notice Checks that `msg.sender` is either the sender or the recipient of the stream either approved,
/// implicitly checks that the stream exists.
modifier authorizedForStream(uint256 streamId) {
if (msg.sender != getSender(streamId) && !isApprovedOrOwner(streamId))
revert Errors.SablierV2Payroll__Unauthorized(streamId, msg.sender);
_;
}
/// @notice Checks that `msg.sender` is the sender of the stream.
modifier onlySender(uint256 streamId) {
if (msg.sender != getSender(streamId)) revert Errors.SablierV2Payroll__Unauthorized(streamId, msg.sender);
_;
}
/// @dev Checks that `streamId` points to a stream that exists.
modifier streamExists(uint256 streamId) {
if (!isStream(streamId)) revert Errors.SablierV2Payroll__StreamNonExistent(streamId);
_;
}
/*//////////////////////////////////////////////////////////////////////////
PUBLIC STORAGE
//////////////////////////////////////////////////////////////////////////*/
/// @notice Counter for stream ids.
uint256 public override nextStreamId;
/*//////////////////////////////////////////////////////////////////////////
INTERNAL STORAGE
//////////////////////////////////////////////////////////////////////////*/
/// @dev Sablier V2 payroll streams mapped by unsigned integers.
mapping(uint256 => DataTypes.PayrollStream) internal _streams;
/*//////////////////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////////////////*/
constructor() {
nextStreamId = 1;
}
/*//////////////////////////////////////////////////////////////////////////
CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @inheritdoc ISablierV2Payroll
function getAmountPerSecond(uint256 streamId) public view returns (uint256 amountPerSecond) {
amountPerSecond = _streams[streamId].amountPerSecond;
}
/// @inheritdoc ISablierV2Payroll
function getBalance(uint256 streamId) public view returns (uint256 balance) {
balance = _streams[streamId].balance;
}
/// @inheritdoc ISablierV2Payroll
function getLastUpdate(uint256 streamId) public view returns (uint64 lastUpdate) {
lastUpdate = _streams[streamId].lastUpdate;
}
/// @inheritdoc ISablierV2Payroll
function getRecipient(uint256 streamId) public view returns (address recipient) {
recipient = _ownerOf(streamId);
}
/// @inheritdoc ISablierV2Payroll
function getReturnableAmount(uint256 streamId) public view returns (uint256 returnableAmount) {
// Invariant: balance >= withdrawableAmount
unchecked {
returnableAmount = getBalance(streamId) - getWithdrawableAmount(streamId);
}
}
/// @inheritdoc ISablierV2Payroll
function getSender(uint256 streamId) public view returns (address sender) {
sender = _streams[streamId].sender;
}
/// @inheritdoc ISablierV2Payroll
function getSenderBalance(uint256 streamId) external view returns (int256 senderBalance) {
int256 balance = int256(getBalance(streamId));
int256 streamedAmount = int256(getStreamedAmount(streamId));
senderBalance = balance - streamedAmount;
}
/// @inheritdoc ISablierV2Payroll
function getStream(uint256 streamId) external view returns (DataTypes.PayrollStream memory stream) {
stream = _streams[streamId];
}
/// @inheritdoc ISablierV2Payroll
function getStreamedAmount(uint256 streamId) public view returns (uint256 streamedAmount) {
// If the stream does not exist, return zero.
if (!isStream(streamId)) {
return 0;
}
// Invariant: lastUpdate <= block.timestamp;
uint256 currentTime = block.timestamp;
uint256 lastUpdate = uint256(getLastUpdate(streamId));
// Calculate how much has been streamed.
unchecked {
UD60x18 elapsedTime = toUD60x18(currentTime - lastUpdate);
UD60x18 amountPerSecond = UD60x18.wrap(getAmountPerSecond(streamId));
streamedAmount = UD60x18.unwrap(elapsedTime.mul(amountPerSecond));
}
}
/// @inheritdoc ISablierV2Payroll
function getWithdrawableAmount(uint256 streamId) public view returns (uint256 withdrawableAmount) {
uint256 balance = getBalance(streamId);
uint256 streamedAmount = getStreamedAmount(streamId);
if (balance == 0) {
return 0;
}
// If there has been streamed more than how much is available, return the stream balance.
if (streamedAmount >= balance) {
return balance;
} else {
withdrawableAmount = streamedAmount;
}
}
/// @inheritdoc ISablierV2Payroll
function isApprovedOrOwner(uint256 streamId) public view returns (bool approvedOrOwner) {
approvedOrOwner = ERC721._isApprovedOrOwner(msg.sender, streamId);
}
/// @inheritdoc ISablierV2Payroll
function isStream(uint256 streamId) public view returns (bool) {
return getSender(streamId) != address(0);
}
/// @inheritdoc ERC721
function tokenURI(uint256 streamId) public view override streamExists(streamId) returns (string memory) {
return "";
}
/*//////////////////////////////////////////////////////////////////////////
NON-CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @inheritdoc ISablierV2Payroll
function adjustStream(uint256 streamId, uint256 amountPerSecond)
external
streamExists(streamId)
onlySender(streamId)
{
// Checks: the amount per second is not zero.
if (amountPerSecond == 0) revert Errors.SablierV2Payroll__AmountPerSecondZero();
// Effects and Interactions: adjust the stream.
_adjustStream(streamId, amountPerSecond);
}
/// @inheritdoc ISablierV2Payroll
function cancel(uint256 streamId) external authorizedForStream(streamId) {
_cancel(streamId);
}
/// @inheritdoc ISablierV2Payroll
function create(
address recipient,
address sender,
uint256 amountPerSecond,
IERC20 token
) external returns (uint256 streamId) {
// Checks,Effects and Interactions: create the stream and mint the NFT.
streamId = _create(recipient, sender, amountPerSecond, token);
}
/// @inheritdoc ISablierV2Payroll
function createAndDeposit(
address recipient,
address sender,
uint256 amountPerSecond,
IERC20 token,
uint256 depositAmount
) external returns (uint256 streamId) {
// Checks,Effects and Interactions: create the stream and mint the NFT.
streamId = _create(recipient, sender, amountPerSecond, token);
// Effects and Interactions: deposit on the stream.
_deposit(streamId, depositAmount);
}
/// @inheritdoc ISablierV2Payroll
function deposit(uint256 streamId, uint256 amount) public {
// Effects and Interactions: deposit on the stream.
_deposit(streamId, amount);
}
/// @inheritdoc ISablierV2Payroll
function depositAll(uint256[] calldata streamIds, uint256[] calldata amounts) external {
uint256 streamIdsCount = streamIds.length;
uint256 amountsCount = amounts.length;
// Checks: count of `streamIds` matches count of `amounts`.
if (streamIdsCount != amountsCount)
revert Errors.SablierV2Payroll__DepositAllArraysNotEqual(streamIdsCount, amountsCount);
uint256 streamId;
uint256 amount;
for (uint256 i = 0; i < streamIdsCount; ) {
streamId = streamIds[i];
amount = amounts[i];
// Effects and Interactions: deposit on the stream.
_deposit(streamId, amount);
// Increment the for loop iterator.
unchecked {
i += 1;
}
}
}
/// @inheritdoc ISablierV2Payroll
function withdraw(uint256 streamId, uint256 amount) external authorizedForStream(streamId) {
// Effects and Interactions: withdraw from the stream.
_withdraw(streamId, getRecipient(streamId), amount, getWithdrawableAmount(streamId));
// Effects: update the stream time reference.
_updateTimeReference(streamId);
}
/// @inheritdoc ISablierV2Payroll
function withdrawSender(uint256 streamId, uint256 amount) external streamExists(streamId) onlySender(streamId) {
// Effects and Interactions: withdraw from the stream.
_withdraw(streamId, getSender(streamId), amount, getReturnableAmount(streamId));
}
/// @inheritdoc ISablierV2Payroll
function withdrawTo(
uint256 streamId,
address to,
uint256 amount
) external {
// Checks: the provided address to withdraw to is not zero.
if (to == address(0)) revert Errors.SablierV2Payroll__WithdrawZeroAddress();
// Checks: the `msg.sender` is either the recipient of the stream or is approved.
if (!isApprovedOrOwner(streamId)) revert Errors.SablierV2Payroll__Unauthorized(streamId, msg.sender);
// Effects and Interactions: withdraw from the stream.
_withdraw(streamId, to, amount, getWithdrawableAmount(streamId));
// Effects: update the stream time reference.
_updateTimeReference(streamId);
}
/*//////////////////////////////////////////////////////////////////////////
INTERNAL NON-CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @dev See the documentation for the public functions that call this internal function.
function _adjustStream(uint256 streamId, uint256 amountPerSecond) internal {
uint256 withdrawAmount = getWithdrawableAmount(streamId);
if (withdrawAmount > 0) {
// Effects and interactions: update the `balance` and perform the ERC-20 transfer.
_updateBalance(streamId, getRecipient(streamId), withdrawAmount);
}
// Effects: update the stream time reference.
_updateTimeReference(streamId);
// Emit an event.
emit Events.AdjustStream(streamId, withdrawAmount, getAmountPerSecond(streamId), amountPerSecond);
// Effects: change the amount per second.
_streams[streamId].amountPerSecond = amountPerSecond;
}
/// @dev See the documentation for the public functions that call this internal function.
function _cancel(uint256 streamId) internal {
address recipient = getRecipient(streamId);
uint256 returnAmount = getReturnableAmount(streamId);
uint256 withdrawAmount = getWithdrawableAmount(streamId);
// Load the stream in memory, we will need it below.
DataTypes.PayrollStream memory stream = _streams[streamId];
// Interactions: withdraw the tokens to the recipient, if any.
if (withdrawAmount > 0) {
stream.token.safeTransfer(recipient, withdrawAmount);
}
// Interactions: return the tokens to the sender, if any.
if (returnAmount > 0) {
stream.token.safeTransfer(getSender(streamId), returnAmount);
}
// Effects: delete the stream from storage.
delete _streams[streamId];
// Interactions: burn the NFT.
_burn(streamId);
// Emit an event.
emit Events.Cancel(streamId, recipient, withdrawAmount, returnAmount);
}
/// @dev See the documentation for the public functions that call this internal function.
function _create(
address recipient,
address sender,
uint256 amountPerSecond,
IERC20 token
) internal returns (uint256 streamId) {
// Validates the requirements for the `create` function.
Validations.payrollCreate(sender, recipient, amountPerSecond);
// Effects: create and store the stream.
streamId = nextStreamId;
_streams[streamId] = DataTypes.PayrollStream({
amountPerSecond: amountPerSecond,
balance: 0,
lastUpdate: uint64(block.timestamp),
sender: sender,
token: token
});
// Effects: mint the NFT to the recipient's address.
_mint(recipient, streamId);
// Effects: bump the next stream id.
// We're using unchecked arithmetic here because this cannot realistically overflow, ever.
unchecked {
nextStreamId = streamId + 1;
}
// Emit an event.
emit Events.CreatePayrollStream(
streamId,
msg.sender,
sender,
recipient,
amountPerSecond,
token,
uint64(block.timestamp)
);
}
/// @dev See the documentation for the public functions that call this internal function.
function _deposit(uint256 streamId, uint256 amount) internal streamExists(streamId) {
// Checks: the amount is not zero.
if (amount == 0) revert Errors.SablierV2Payroll__DepositAmountZero();
// Effects: update the stream balance.
unchecked {
_streams[streamId].balance += amount;
}
// Load the stream in memory, we will need it below.
DataTypes.PayrollStream memory stream = _streams[streamId];
// Interactions: perform the ERC-20 transfer.
stream.token.safeTransferFrom(msg.sender, address(this), amount);
// Emit an event.
emit Events.DepositPayrollStream(streamId, msg.sender, amount);
}
/// @dev Helper function for updating the `balance`.
function _updateBalance(
uint256 streamId,
address to,
uint256 amount
) internal {
// Effects: update the stream balance.
unchecked {
_streams[streamId].balance -= amount;
}
// Load the stream in memory, we will need it below.
DataTypes.PayrollStream memory stream = _streams[streamId];
// Interactions: perform the ERC-20 transfer.
stream.token.safeTransfer(to, amount);
}
/// @dev Helper function for updating the `lastUpdate`.
function _updateTimeReference(uint256 streamId) internal {
// Effects: set the `lastUpdate` to `block.timestamp`.
_streams[streamId].lastUpdate = uint64(block.timestamp);
}
/// @dev See the documentation for the public functions that call this internal function.
function _withdraw(
uint256 streamId,
address to,
uint256 amount,
uint256 availableAmount
) internal {
// Validates the requirements for the `amount` to withdraw.
Validations.withdrawAmount(amount, availableAmount);
// Effects and interactions: update the `balance` and perform the ERC-20 transfer.
_updateBalance(streamId, to, amount);
// Emit an event.
emit Events.Withdraw(streamId, to, amount);
}
}