-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStreamFactory.sol
322 lines (298 loc) · 13.2 KB
/
StreamFactory.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;
import { IStream } from "./IStream.sol";
import { IERC20 } from "openzeppelin-contracts/interfaces/IERC20.sol";
import { SafeERC20 } from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import { LibClone } from "solady/utils/LibClone.sol";
/**
* @title Stream Factory
* @notice Creates minimal clones of `Stream`.
* The cloning approach enables delayed funding of streams, which is useful for payers who acquire tokens for streams
* asynchronously, e.g. by using https://github.com/nounsDAO/token-buyer.
* Each stream in its own contract is better than multiple streams in one contract, in the case of delayed funding
* because it avoid the problem of recipients competing for the same funds.
* @dev Uses `LibClone` which creates clones with immutable arguments written into the clone's code section; this
* approach provides significant gas savings.
*/
contract StreamFactory {
using LibClone for address;
using SafeERC20 for IERC20;
/**
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
* ERRORS
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
error PayerIsAddressZero();
error RecipientIsAddressZero();
error TokenAmountIsZero();
error DurationMustBePositive();
error UnexpectedStreamAddress();
error StopTimeNotInTheFuture();
/**
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
* EVENTS
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @dev msgSender is part of the event to enable event indexing with which account performed this action.
event StreamCreated(
address indexed msgSender,
address indexed payer,
address indexed recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime,
address streamAddress
);
/**
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
* IMMUTABLES
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// @notice The address of the Stream implementation to use when creating Stream clones.
address public immutable streamImplementation;
/**
* @param _streamImplementation the address of the Stream implementation to use when creating Stream clones.
*/
constructor(address _streamImplementation) {
streamImplementation = _streamImplementation;
}
/**
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
* EXTERNAL TXS
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/**
* @notice Create a new stream contract instance.
* The payer is assumed to be `msg.sender`.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the stream start timestamp in seconds.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
*/
function createStream(
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime
) external returns (address) {
return createStream(
msg.sender, recipient, tokenAmount, tokenAddress, startTime, stopTime, 0
);
}
/**
* @notice Create a new stream contract instance, and fully fund it.
* The payer is assumed to be `msg.sender`.
* `msg.sender` must approve this contract to spend at least `tokenAmount`, otherwise the transaction
* will revert.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
* @return stream the address of the new stream contract.
*/
function createAndFundStream(
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime
) external returns (address stream) {
stream =
createStream(msg.sender, recipient, tokenAmount, tokenAddress, startTime, stopTime, 0);
IERC20(tokenAddress).safeTransferFrom(msg.sender, stream, tokenAmount);
}
/**
* @notice Create a new stream contract instance.
* @param payer the account responsible for funding the stream.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
* @return stream the address of the new stream contract.
*/
function createStream(
address payer,
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime
) external returns (address) {
return createStream(payer, recipient, tokenAmount, tokenAddress, startTime, stopTime, 0);
}
/**
* @notice Create a new stream contract instance, and verify the new stream address matches expectations from
* using `predictStreamAddress`.
* The payer is assumed to be `msg.sender`.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
* @param nonce the nonce for this stream creation.
* @param predictedStreamAddress the expected stream address the user got from calling the predict function.
* @return stream the address of the new stream contract.
*/
function createStream(
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime,
uint8 nonce,
address predictedStreamAddress
) external returns (address stream) {
stream = createStream(
msg.sender, recipient, tokenAmount, tokenAddress, startTime, stopTime, nonce
);
if (stream != predictedStreamAddress) revert UnexpectedStreamAddress();
}
/**
* @notice Create a new stream contract instance.
* This version allows you to specify an additional `nonce` in case payer wants to create multiple streams
* with the same parameters. In all other versions nonce is zero.
* @dev The added nonce helps payer avoid stream contract address collisions among streams where all other
* parameters are identical.
* @param payer the account responsible for funding the stream.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
* @param nonce the nonce for this stream creation.
* @return stream the address of the new stream contract.
*/
function createStream(
address payer,
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime,
uint8 nonce
) public returns (address stream) {
// These input checks are here rather than in Stream because these parameters are written
// using clone-with-immutable-args, meaning they are already set when Stream is created and can't be
// verified there. The main benefit of this approach is significant gas savings.
if (payer == address(0)) revert PayerIsAddressZero();
if (recipient == address(0)) revert RecipientIsAddressZero();
if (tokenAmount == 0) revert TokenAmountIsZero();
if (stopTime <= startTime) revert DurationMustBePositive();
if (stopTime <= block.timestamp) revert StopTimeNotInTheFuture();
stream = streamImplementation.cloneDeterministic(
encodeData(payer, recipient, tokenAmount, tokenAddress, startTime, stopTime),
salt(
msg.sender, payer, recipient, tokenAmount, tokenAddress, startTime, stopTime, nonce
)
);
IStream(stream).initialize();
emit StreamCreated(
msg.sender, payer, recipient, tokenAmount, tokenAddress, startTime, stopTime, stream
);
}
/**
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
* VIEW FUNCTIONS
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/**
* @notice Get the expected contract address of a stream created with the provided parameters.
* @param msgSender the expected `msg.sender` to create the stream.
* @param payer the account responsible for funding the stream.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
*/
function predictStreamAddress(
address msgSender,
address payer,
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime
) external view returns (address) {
return predictStreamAddress(
msgSender, payer, recipient, tokenAmount, tokenAddress, startTime, stopTime, 0
);
}
/**
* @notice Get the expected contract address of a stream created with the provided parameters.
* Use this version when creating streams with a non-zero `nonce`. Should only be used on the rare occasion
* when a payer wants to create multiple streams with identical parameters.
* @param msgSender the expected `msg.sender` to create the stream.
* @param payer the account responsible for funding the stream.
* @param recipient the recipient of the stream.
* @param tokenAmount the total token amount payer is streaming to recipient.
* @param tokenAddress the contract address of the payment token.
* @param startTime the unix timestamp for when the stream starts.
* @param stopTime the unix timestamp for when the stream ends.
* @param nonce the nonce for this stream creation.
*/
function predictStreamAddress(
address msgSender,
address payer,
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime,
uint8 nonce
) public view returns (address) {
return streamImplementation.predictDeterministicAddress(
encodeData(payer, recipient, tokenAmount, tokenAddress, startTime, stopTime),
salt(msgSender, payer, recipient, tokenAmount, tokenAddress, startTime, stopTime, nonce),
address(this)
);
}
/**
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
* INTERNAL FUNCTIONS
* ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/**
* @dev Encodes Stream's immutable arguments, as expected by LibClone, and in the order `Stream` uses to read
* their values. Any change here should result in a change in how `Stream` reads these arguments.
*/
function encodeData(
address payer,
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime
) internal view returns (bytes memory) {
return abi.encodePacked(
address(this), payer, recipient, tokenAmount, tokenAddress, startTime, stopTime
);
}
/**
* @dev Generates the salt for `cloneDeterministic` and `predictDeterministicAddress`; salt is the unique input
* per Stream that results in each Stream instance having its unique address.
* For more info look into `LibClone` and how the `create2` opcode work.
*/
function salt(
address msgSender,
address payer,
address recipient,
uint256 tokenAmount,
address tokenAddress,
uint256 startTime,
uint256 stopTime,
uint8 nonce
) internal pure returns (bytes32) {
return keccak256(
abi.encodePacked(
msgSender, payer, recipient, tokenAmount, tokenAddress, startTime, stopTime, nonce
)
);
}
}