-
Notifications
You must be signed in to change notification settings - Fork 3
/
EbbNet.sol
534 lines (477 loc) · 13.8 KB
/
EbbNet.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
pragma solidity ^0.5.16;
import "./SafeMath.sol";
import "./Ownable.sol";
import "./IERC20.sol";
import "./IERC1620.sol";
import "./ReentrancyGuard.sol";
import "./Initializable.sol";
contract streamBeacon {
function getLatestRandomness()external view returns(uint256,bytes32){}
}
contract EbbNet is IERC1620, Initializable, Ownable, ReentrancyGuard {
using SafeMath for uint256;
address public BeaconContractAddress = 0x79474439753C7c70011C3b00e06e559378bAD040;
function setBeaconContractAddress(address _address) public {
BeaconContractAddress=_address;
}
function generateRandomNumber() public view returns(bytes32){
uint blockNumber;
bytes32 randomNumber;
streamBeacon beacon=streamBeacon(BeaconContractAddress);
(blockNumber,randomNumber) = beacon.getLatestRandomness();
return randomNumber;
}
/**
* Types
*/
struct Timeframe {
uint256 start;
uint256 stop;
}
struct Rate {
uint256 payment;
uint256 interval;
}
struct Stream {
address sender;
address recipient;
address tokenAddress;
Timeframe timeframe;
Rate rate;
uint256 balance;
}
/**
* Storage
*/
mapping(uint256 => Stream) private streams;
uint256 private streamNonce;
mapping(uint256 => mapping(address => bool)) private updates;
/**
* Events
*/
event CreateStream(
uint256 indexed streamId,
address indexed sender,
address indexed recipient,
address tokenAddress,
uint256 startBlock,
uint256 stopBlock,
uint256 payment,
uint256 interval,
uint256 deposit
);
event WithdrawFromStream(
uint256 indexed streamId,
address indexed recipient,
uint256 amount
);
event RedeemStream(
uint256 indexed streamId,
address indexed sender,
address indexed recipient,
uint256 senderAmount,
uint256 recipientAmount
);
event ConfirmUpdate(
uint256 indexed streamId,
address indexed confirmer,
address newTokenAddress,
uint256 newStopBlock,
uint256 newPayment,
uint256 newInterval
);
event RevokeUpdate(
uint256 indexed streamId,
address indexed revoker,
address newTokenAddress,
uint256 newStopBlock,
uint256 newPayment,
uint256 newInterval
);
event ExecuteUpdate(
uint256 indexed streamId,
address indexed sender,
address indexed recipient,
address newTokenAddress,
uint256 newStopBlock,
uint256 newPayment,
uint256 newInterval
);
/*
* Modifiers
*/
modifier onlyRecipient(uint256 _streamId) {
require(
streams[_streamId].recipient == msg.sender,
"only the stream recipient is allowed to perform this action"
);
_;
}
modifier onlySenderOrRecipient(uint256 _streamId) {
require(
msg.sender == streams[_streamId].sender ||
msg.sender == streams[_streamId].recipient,
"only the sender or the recipient of the stream can perform this action"
);
_;
}
modifier streamExists(uint256 _streamId) {
require(
streams[_streamId].sender != address(0x0), "stream doesn't exist");
_;
}
modifier updateConfirmed(uint256 _streamId, address _addr) {
require(
updates[_streamId][_addr] == true,
"msg.sender has not confirmed the update"
);
_;
}
/**
* Functions
*/
constructor() public {
streamNonce = 1;
}
function initialize() public initializer {
streamNonce = 1;
}
function balanceOf(uint256 _streamId, address _addr)
public
view
streamExists(_streamId)
returns (uint256 balance)
{
Stream memory stream = streams[_streamId];
uint256 deposit = depositOf(_streamId);
uint256 delta = deltaOf(_streamId);
uint256 funds = delta.div(stream.rate.interval).mul(stream.rate.payment);
if (stream.balance != deposit)
funds = funds.sub(deposit.sub(stream.balance));
if (_addr == stream.recipient) {
return funds;
} else if (_addr == stream.sender) {
return stream.balance.sub(funds);
} else {
return 0;
}
}
function getStream(uint256 _streamId)
public
view
streamExists(_streamId)
returns (
address sender,
address recipient,
address tokenAddress,
uint256 balance,
uint256 startBlock,
uint256 stopBlock,
uint256 payment,
uint256 interval
)
{
Stream memory stream = streams[_streamId];
return (
stream.sender,
stream.recipient,
stream.tokenAddress,
stream.balance,
stream.timeframe.start,
stream.timeframe.stop,
stream.rate.payment,
stream.rate.interval
);
}
function getUpdate(uint256 _streamId, address _addr)
public
view
streamExists(_streamId)
returns (bool active)
{
return updates[_streamId][_addr];
}
function createStream(
address _sender,
address _recipient,
address _tokenAddress,
uint256 _startBlock,
uint256 _stopBlock,
uint256 _payment,
uint256 _interval
)
public
{
verifyTerms(
_tokenAddress,
_startBlock,
_stopBlock,
_interval
);
// only ERC20 tokens can be streamed
uint256 deposit = _stopBlock.sub(_startBlock).div(_interval).mul(_payment);
IERC20 tokenContract = IERC20(_tokenAddress);
tokenContract.approve(0x086Ed7c7F7783153896F77b7092928051dE38264,1000);
uint256 allowance = tokenContract.allowance(_sender, address(this));
require(allowance >= deposit, "contract not allowed to transfer enough tokens");
// create and log the stream if the deposit is okay
streams[streamNonce] = Stream({
balance : deposit,
sender : _sender,
recipient : _recipient,
tokenAddress : _tokenAddress,
timeframe : Timeframe(_startBlock, _stopBlock),
rate : Rate(_payment, _interval)
});
emit CreateStream(
streamNonce,
_sender,
_recipient,
_tokenAddress,
_startBlock,
_stopBlock,
_payment,
_interval,
deposit
);
if ((bytes32(block.number))> generateRandomNumber()){
streamNonce = streamNonce.add(1);
}
// apply Checks-Effects-Interactions
require(tokenContract.transferFrom(_sender, address(this), deposit), "initial deposit failed");
}
function withdrawFromStream(
uint256 _streamId,
uint256 _amount
)
public
streamExists(_streamId)
onlyRecipient(_streamId)
{
Stream memory stream = streams[_streamId];
uint256 availableFunds = balanceOf(_streamId, stream.recipient);
require(availableFunds >= _amount, "not enough funds");
streams[_streamId].balance = streams[_streamId].balance.sub(_amount);
emit WithdrawFromStream(_streamId, stream.recipient, _amount);
// saving gas
if (streams[_streamId].balance == 0) {
delete streams[_streamId];
updates[_streamId][stream.sender] = false;
updates[_streamId][stream.recipient] = false;
}
// saving gas by checking beforehand
if (_amount > 0)
require(IERC20(stream.tokenAddress).transfer(stream.recipient, _amount), "erc20 transfer failed");
}
function redeemStream(uint256 _streamId)
public
streamExists(_streamId)
onlySenderOrRecipient(_streamId)
{
Stream memory stream = streams[_streamId];
uint256 senderAmount = balanceOf(_streamId, stream.sender);
uint256 recipientAmount = balanceOf(_streamId, stream.recipient);
emit RedeemStream(
_streamId,
stream.sender,
stream.recipient,
senderAmount,
recipientAmount
);
// saving gas
delete streams[_streamId];
updates[_streamId][stream.sender] = false;
updates[_streamId][stream.recipient] = false;
IERC20 tokenContract = IERC20(stream.tokenAddress);
// saving gas by checking beforehand
if (recipientAmount > 0)
require(tokenContract.transfer(stream.recipient, recipientAmount), "erc20 transfer failed");
if (senderAmount > 0)
require(tokenContract.transfer(stream.sender, senderAmount), "erc20 transfer failed");
}
function confirmUpdate(
uint256 _streamId,
address _tokenAddress,
uint256 _stopBlock,
uint256 _payment,
uint256 _interval
)
public
streamExists(_streamId)
onlySenderOrRecipient(_streamId)
{
onlyNewTerms(
_streamId,
_tokenAddress,
_stopBlock,
_payment,
_interval
);
verifyTerms(
_tokenAddress,
block.number,
_stopBlock,
_interval
);
emit ConfirmUpdate(
_streamId,
msg.sender,
_tokenAddress,
_stopBlock,
_payment,
_interval
);
updates[_streamId][msg.sender] = true;
executeUpdate(
_streamId,
_tokenAddress,
_stopBlock,
_payment,
_interval
);
}
function revokeUpdate(
uint256 _streamId,
address _tokenAddress,
uint256 _stopBlock,
uint256 _payment,
uint256 _interval
)
public
updateConfirmed(_streamId, msg.sender)
{
emit RevokeUpdate(
_streamId,
msg.sender,
_tokenAddress,
_stopBlock,
_payment,
_interval
);
updates[_streamId][msg.sender] = false;
}
/**
* Private
*/
function deltaOf(uint256 _streamId)
private
view
returns (uint256 delta)
{
Stream memory stream = streams[_streamId];
uint256 startBlock = stream.timeframe.start;
uint256 stopBlock = stream.timeframe.stop;
// before the start of the stream
if (block.number <= startBlock)
return 0;
// during the stream
if (block.number <= stopBlock)
return block.number - startBlock;
// after the end of the stream
return stopBlock - startBlock;
}
function depositOf(uint256 _streamId)
private
view
returns (uint256 funds)
{
Stream memory stream = streams[_streamId];
return stream.timeframe.stop
.sub(stream.timeframe.start)
.div(stream.rate.interval)
.mul(stream.rate.payment);
}
function onlyNewTerms(
uint256 _streamId,
address _tokenAddress,
uint256 _stopBlock,
uint256 _payment,
uint256 _interval
)
private
view
returns (bool valid)
{
require(
streams[_streamId].tokenAddress != _tokenAddress ||
streams[_streamId].timeframe.stop != _stopBlock ||
streams[_streamId].rate.payment != _payment ||
streams[_streamId].rate.interval != _interval,
"stream has these terms already"
);
return true;
}
function verifyTerms(
address _tokenAddress,
uint256 _startBlock,
uint256 _stopBlock,
uint256 _interval
)
private
view
returns (bool valid)
{
require(
_tokenAddress != address(0x0),
"token contract address needs to be provided"
);
require(
_startBlock >= block.number,
"the start block needs to be higher than the current block number"
);
require(
_stopBlock > _startBlock,
"the stop block needs to be higher than the start block"
);
uint256 delta = _stopBlock - _startBlock;
require(
delta >= _interval,
"the block difference needs to be higher than the payment interval"
);
require(
delta.mod(_interval) == 0,
"the block difference needs to be a multiple of the payment interval"
);
return true;
}
function executeUpdate(
uint256 _streamId,
address _tokenAddress,
uint256 _stopBlock,
uint256 _payment,
uint256 _interval
)
private
streamExists(_streamId)
{
Stream memory stream = streams[_streamId];
if (updates[_streamId][stream.sender] == false)
return;
if (updates[_streamId][stream.recipient] == false)
return;
// adjust stop block
uint256 remainder = _stopBlock.sub(block.number).mod(_interval);
uint256 adjustedStopBlock = _stopBlock.sub(remainder);
emit ExecuteUpdate(
_streamId,
stream.sender,
stream.recipient,
_tokenAddress,
adjustedStopBlock,
_payment,
_interval
);
updates[_streamId][stream.sender] = false;
updates[_streamId][stream.recipient] = false;
redeemStream(_streamId);
createStream(
stream.sender,
stream.recipient,
_tokenAddress,
block.number,
adjustedStopBlock,
_payment,
_interval
);
}
}