-
Notifications
You must be signed in to change notification settings - Fork 48
/
SablierV2Lockup.sol
302 lines (252 loc) · 12 KB
/
SablierV2Lockup.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.19;
import { ERC721 } from "@openzeppelin/token/ERC721/ERC721.sol";
import { IERC721Metadata } from "@openzeppelin/token/ERC721/extensions/IERC721Metadata.sol";
import { ISablierV2Comptroller } from "../interfaces/ISablierV2Comptroller.sol";
import { ISablierV2Lockup } from "../interfaces/ISablierV2Lockup.sol";
import { ISablierV2NFTDescriptor } from "../interfaces/ISablierV2NFTDescriptor.sol";
import { Errors } from "../libraries/Errors.sol";
import { Lockup } from "../types/DataTypes.sol";
import { SablierV2Base } from "./SablierV2Base.sol";
/// @title SablierV2Lockup
/// @notice See the documentation in {ISablierV2Lockup}.
abstract contract SablierV2Lockup is
SablierV2Base, // 4 inherited components
ISablierV2Lockup, // 4 inherited components
ERC721 // 6 inherited components
{
/*//////////////////////////////////////////////////////////////////////////
INTERNAL STORAGE
//////////////////////////////////////////////////////////////////////////*/
/// @dev Contract that generates the non-fungible token URI.
ISablierV2NFTDescriptor internal _nftDescriptor;
/*//////////////////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////////////////*/
/// @param initialAdmin The address of the initial contract admin.
/// @param initialComptroller The address of the initial comptroller.
/// @param initialNFTDescriptor The address of the initial NFT descriptor.
constructor(
address initialAdmin,
ISablierV2Comptroller initialComptroller,
ISablierV2NFTDescriptor initialNFTDescriptor
)
SablierV2Base(initialAdmin, initialComptroller)
{
_nftDescriptor = initialNFTDescriptor;
}
/*//////////////////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////////////////*/
/// @dev Checks that `streamId` does not reference a cold stream.
modifier notCold(uint256 streamId) {
Lockup.Status status = statusOf(streamId);
if (status == Lockup.Status.SETTLED || status == Lockup.Status.CANCELED || status == Lockup.Status.DEPLETED) {
revert Errors.SablierV2Lockup_StreamCold(streamId);
}
_;
}
/// @dev Checks that `streamId` does not reference a null stream.
modifier notNull(uint256 streamId) {
if (!isStream(streamId)) {
revert Errors.SablierV2Lockup_Null(streamId);
}
_;
}
/// @notice Checks that `msg.sender` is either the stream's sender or the stream's recipient (i.e. the NFT owner).
modifier onlySenderOrRecipient(uint256 streamId) {
if (!_isCallerStreamSender(streamId) && msg.sender != _ownerOf(streamId)) {
revert Errors.SablierV2Lockup_Unauthorized(streamId, msg.sender);
}
_;
}
/*//////////////////////////////////////////////////////////////////////////
USER-FACING CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @inheritdoc ISablierV2Lockup
function getRecipient(uint256 streamId) external view override returns (address recipient) {
// Checks: the stream NFT exists.
_requireMinted({ tokenId: streamId });
// The NFT owner is the stream's recipient.
recipient = _ownerOf(streamId);
}
/// @inheritdoc ISablierV2Lockup
function isStream(uint256 streamId) public view virtual override returns (bool result);
/// @inheritdoc ISablierV2Lockup
function statusOf(uint256 streamId) public view virtual override returns (Lockup.Status status);
/// @inheritdoc ERC721
function tokenURI(uint256 streamId) public view override(IERC721Metadata, ERC721) returns (string memory uri) {
// Checks: the stream NFT exists.
_requireMinted({ tokenId: streamId });
// Generate the URI describing the stream NFT.
uri = _nftDescriptor.tokenURI(this, streamId);
}
/// @inheritdoc ISablierV2Lockup
function withdrawableAmountOf(uint256 streamId)
public
view
override
notNull(streamId)
returns (uint128 withdrawableAmount)
{
withdrawableAmount = _withdrawableAmountOf(streamId);
}
/*//////////////////////////////////////////////////////////////////////////
USER-FACING NON-CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @inheritdoc ISablierV2Lockup
function burn(uint256 streamId) external override noDelegateCall notNull(streamId) {
// Checks: the stream is depleted.
if (statusOf(streamId) != Lockup.Status.DEPLETED) {
revert Errors.SablierV2Lockup_StreamNotDepleted(streamId);
}
// Checks:
// 1. NFT exists (see {IERC721.getApproved}).
// 2. `msg.sender` is either the owner of the NFT or an approved third party.
if (!_isCallerStreamRecipientOrApproved(streamId)) {
revert Errors.SablierV2Lockup_Unauthorized(streamId, msg.sender);
}
// Effects: burn the NFT.
_burn({ tokenId: streamId });
}
/// @inheritdoc ISablierV2Lockup
function cancel(uint256 streamId)
public
override
noDelegateCall
notNull(streamId)
notCold(streamId)
onlySenderOrRecipient(streamId)
{
_cancel(streamId);
}
/// @inheritdoc ISablierV2Lockup
function cancelMultiple(uint256[] calldata streamIds) external override noDelegateCall {
// Iterate over the provided array of stream ids and cancel each stream.
uint256 count = streamIds.length;
for (uint256 i = 0; i < count;) {
// Effects and Interactions: cancel the stream.
cancel(streamIds[i]);
// Increment the loop iterator.
unchecked {
i += 1;
}
}
}
/// @inheritdoc ISablierV2Lockup
function renounce(uint256 streamId) external override noDelegateCall notNull(streamId) notCold(streamId) {
// Checks: `msg.sender` is the stream's sender.
if (!_isCallerStreamSender(streamId)) {
revert Errors.SablierV2Lockup_Unauthorized(streamId, msg.sender);
}
// Effects: renounce the stream.
_renounce(streamId);
}
/// @inheritdoc ISablierV2Lockup
function setNFTDescriptor(ISablierV2NFTDescriptor newNFTDescriptor) external override onlyAdmin {
// Effects: set the NFT descriptor.
ISablierV2NFTDescriptor oldNftDescriptor = _nftDescriptor;
_nftDescriptor = newNFTDescriptor;
// Log the change of the NFT descriptor.
emit ISablierV2Lockup.SetNFTDescriptor({
admin: msg.sender,
oldNFTDescriptor: oldNftDescriptor,
newNFTDescriptor: newNFTDescriptor
});
}
/// @inheritdoc ISablierV2Lockup
function withdraw(uint256 streamId, address to, uint128 amount) public override noDelegateCall {
// Checks: the withdrawal address is not zero.
if (to == address(0)) {
revert Errors.SablierV2Lockup_WithdrawToZeroAddress();
}
// Checks, Effects, and Interactions: check the parameters and make the withdrawal.
_checkParamsAndWithdraw(streamId, to, amount);
}
/// @inheritdoc ISablierV2Lockup
function withdrawMax(uint256 streamId, address to) external override {
withdraw(streamId, to, _withdrawableAmountOf(streamId));
}
/// @inheritdoc ISablierV2Lockup
function withdrawMultiple(
uint256[] calldata streamIds,
address to,
uint128[] calldata amounts
)
external
override
noDelegateCall
{
// Checks: the withdrawal address is not zero.
if (to == address(0)) {
revert Errors.SablierV2Lockup_WithdrawToZeroAddress();
}
// Checks: there is an equal number of `streamIds` and `amounts`.
uint256 streamIdsCount = streamIds.length;
uint256 amountsCount = amounts.length;
if (streamIdsCount != amountsCount) {
revert Errors.SablierV2Lockup_WithdrawArrayCountsNotEqual(streamIdsCount, amountsCount);
}
// Iterate over the provided array of stream ids and withdraw from each stream.
uint256 streamId;
for (uint256 i = 0; i < streamIdsCount;) {
streamId = streamIds[i];
// Checks, Effects, and Interactions: check the parameters and make the withdrawal.
_checkParamsAndWithdraw(streamId, to, amounts[i]);
// Increment the loop iterator.
unchecked {
i += 1;
}
}
}
/*//////////////////////////////////////////////////////////////////////////
INTERNAL CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @notice Checks whether `msg.sender` is the stream's recipient or an approved third party.
/// @param streamId The stream id for the query.
function _isCallerStreamRecipientOrApproved(uint256 streamId) internal view returns (bool result) {
address recipient = _ownerOf(streamId);
result = (
msg.sender == recipient || isApprovedForAll({ owner: recipient, operator: msg.sender })
|| getApproved(streamId) == msg.sender
);
}
/// @notice Checks whether `msg.sender` is the stream's sender.
/// @param streamId The stream id for the query.
function _isCallerStreamSender(uint256 streamId) internal view virtual returns (bool result);
/// @dev See the documentation for the user-facing functions that call this internal function.
function _withdrawableAmountOf(uint256 streamId) internal view virtual returns (uint128 withdrawableAmount);
/*//////////////////////////////////////////////////////////////////////////
INTERNAL NON-CONSTANT FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
/// @dev See the documentation for the user-facing functions that call this internal function.
function _cancel(uint256 tokenId) internal virtual;
/// @dev Common logic between {withdraw} and {withdrawMultiple}.
function _checkParamsAndWithdraw(uint256 streamId, address to, uint128 amount) internal notNull(streamId) {
// Checks: the stream is neither pending nor depleted.
Lockup.Status status = statusOf(streamId);
if (status == Lockup.Status.PENDING) {
revert Errors.SablierV2Lockup_StreamPending(streamId);
} else if (status == Lockup.Status.DEPLETED) {
revert Errors.SablierV2Lockup_StreamDepleted(streamId);
}
// Checks: `msg.sender` is the stream's sender, the stream's recipient, or an approved third party.
if (!_isCallerStreamSender(streamId) && !_isCallerStreamRecipientOrApproved(streamId)) {
revert Errors.SablierV2Lockup_Unauthorized(streamId, msg.sender);
}
// Checks: if `msg.sender` is the stream's sender, the withdrawal address must be the recipient.
if (_isCallerStreamSender(streamId) && to != _ownerOf(streamId)) {
revert Errors.SablierV2Lockup_InvalidSenderWithdrawal(streamId, msg.sender, to);
}
// Checks: the withdraw amount is not zero.
if (amount == 0) {
revert Errors.SablierV2Lockup_WithdrawAmountZero(streamId);
}
// Checks, Effects and Interactions: make the withdrawal.
_withdraw(streamId, to, amount);
}
/// @dev See the documentation for the user-facing functions that call this internal function.
function _renounce(uint256 streamId) internal virtual;
/// @dev See the documentation for the user-facing functions that call this internal function.
function _withdraw(uint256 streamId, address to, uint128 amount) internal virtual;
}