-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathVestingSchedulerV2.sol
More file actions
710 lines (627 loc) · 25.7 KB
/
VestingSchedulerV2.sol
File metadata and controls
710 lines (627 loc) · 25.7 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// SPDX-License-Identifier: AGPLv3
// solhint-disable not-rely-on-time
pragma solidity ^0.8.0;
import {
ISuperfluid, ISuperToken, SuperAppDefinitions
} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperfluid.sol";
import { SuperAppBase } from "@superfluid-finance/ethereum-contracts/contracts/apps/SuperAppBase.sol";
import { SuperTokenV1Library } from "@superfluid-finance/ethereum-contracts/contracts/apps/SuperTokenV1Library.sol";
import { IVestingSchedulerV2 } from "./interface/IVestingSchedulerV2.sol";
import { SafeMath } from "@openzeppelin/contracts/utils/math/SafeMath.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
contract VestingSchedulerV2 is IVestingSchedulerV2, SuperAppBase {
using SuperTokenV1Library for ISuperToken;
ISuperfluid public immutable HOST;
mapping(bytes32 => VestingSchedule) public vestingSchedules; // id = keccak(supertoken, sender, receiver)
uint32 public constant MIN_VESTING_DURATION = 7 days;
uint32 public constant START_DATE_VALID_AFTER = 3 days;
uint32 public constant END_DATE_VALID_BEFORE = 1 days;
struct ScheduleAggregate {
ISuperToken superToken;
address sender;
address receiver;
bytes32 id;
VestingSchedule schedule;
}
constructor(ISuperfluid host) {
// Superfluid SuperApp registration. This is a dumb SuperApp, only for front-end tx batch calls.
uint256 configWord = SuperAppDefinitions.APP_LEVEL_FINAL |
SuperAppDefinitions.BEFORE_AGREEMENT_CREATED_NOOP |
SuperAppDefinitions.AFTER_AGREEMENT_CREATED_NOOP |
SuperAppDefinitions.BEFORE_AGREEMENT_UPDATED_NOOP |
SuperAppDefinitions.AFTER_AGREEMENT_UPDATED_NOOP |
SuperAppDefinitions.BEFORE_AGREEMENT_TERMINATED_NOOP |
SuperAppDefinitions.AFTER_AGREEMENT_TERMINATED_NOOP;
host.registerApp(configWord);
HOST = host;
}
/// @dev IVestingScheduler.createVestingSchedule implementation.
function createVestingSchedule(
ISuperToken superToken,
address receiver,
uint32 startDate,
uint32 cliffDate,
int96 flowRate,
uint256 cliffAmount,
uint32 endDate,
uint32 claimValidityDate,
bytes memory ctx
) external returns (bytes memory newCtx) {
newCtx = ctx;
address sender = _getSender(ctx);
_validateAndCreateVestingSchedule(
ScheduleCreationParams({
superToken: superToken,
sender: sender,
receiver: receiver,
startDate: _normalizeStartDate(startDate),
claimValidityDate: claimValidityDate,
cliffDate: cliffDate,
flowRate: flowRate,
cliffAmount: cliffAmount,
endDate: endDate,
remainderAmount: 0
})
);
}
/// @dev IVestingScheduler.createVestingSchedule implementation.
function createVestingSchedule(
ISuperToken superToken,
address receiver,
uint32 startDate,
uint32 cliffDate,
int96 flowRate,
uint256 cliffAmount,
uint32 endDate,
uint32 claimValidityDate
) external {
_validateAndCreateVestingSchedule(
ScheduleCreationParams({
superToken: superToken,
sender: msg.sender,
receiver: receiver,
startDate: _normalizeStartDate(startDate),
claimValidityDate: claimValidityDate,
cliffDate: cliffDate,
flowRate: flowRate,
cliffAmount: cliffAmount,
endDate: endDate,
remainderAmount: 0
})
);
}
/// @dev IVestingScheduler.createVestingSchedule implementation.
/// @dev Note: VestingScheduler (V1) compatible function
function createVestingSchedule(
ISuperToken superToken,
address receiver,
uint32 startDate,
uint32 cliffDate,
int96 flowRate,
uint256 cliffAmount,
uint32 endDate,
bytes memory ctx
) external returns (bytes memory newCtx) {
newCtx = ctx;
address sender = _getSender(ctx);
_validateAndCreateVestingSchedule(
ScheduleCreationParams({
superToken: superToken,
sender: sender,
receiver: receiver,
startDate: _normalizeStartDate(startDate),
claimValidityDate: 0,
cliffDate: cliffDate,
flowRate: flowRate,
cliffAmount: cliffAmount,
endDate: endDate,
remainderAmount: 0
})
);
}
/// @dev IVestingScheduler.createVestingScheduleFromAmountAndDuration implementation.
/// @dev Note: creating from amount and duration is the preferred way
function createVestingScheduleFromAmountAndDuration(
ISuperToken superToken,
address receiver,
uint256 totalAmount,
uint32 totalDuration,
uint32 startDate,
uint32 cliffPeriod,
uint32 claimPeriod,
bytes memory ctx
) external returns (bytes memory newCtx) {
newCtx = ctx;
address sender = _getSender(ctx);
_validateAndCreateVestingSchedule(
mapCreateVestingScheduleParams(
superToken,
sender,
receiver,
totalAmount,
totalDuration,
_normalizeStartDate(startDate),
cliffPeriod,
claimPeriod
)
);
}
/// @dev IVestingScheduler.createVestingScheduleFromAmountAndDuration implementation.
function createVestingScheduleFromAmountAndDuration(
ISuperToken superToken,
address receiver,
uint256 totalAmount,
uint32 totalDuration,
uint32 startDate,
uint32 cliffPeriod,
uint32 claimPeriod
) external {
_validateAndCreateVestingSchedule(
mapCreateVestingScheduleParams(
superToken,
msg.sender,
receiver,
totalAmount,
totalDuration,
_normalizeStartDate(startDate),
cliffPeriod,
claimPeriod
)
);
}
/// @dev IVestingScheduler.mapCreateVestingScheduleParams implementation.
function mapCreateVestingScheduleParams(
ISuperToken superToken,
address sender,
address receiver,
uint256 totalAmount,
uint32 totalDuration,
uint32 startDate,
uint32 cliffPeriod,
uint32 claimPeriod
) public pure override returns (ScheduleCreationParams memory params) {
uint32 claimValidityDate = claimPeriod != 0
? startDate + claimPeriod
: 0;
uint32 endDate = startDate + totalDuration;
int96 flowRate = SafeCast.toInt96(
SafeCast.toInt256(totalAmount / totalDuration)
);
uint96 remainderAmount = SafeCast.toUint96(
totalAmount - (SafeCast.toUint256(flowRate) * totalDuration)
);
if (cliffPeriod == 0) {
params = ScheduleCreationParams({
superToken: superToken,
sender: sender,
receiver: receiver,
startDate: startDate,
claimValidityDate: claimValidityDate,
cliffDate: 0,
flowRate: flowRate,
cliffAmount: 0,
endDate: endDate,
remainderAmount: remainderAmount
});
} else {
uint256 cliffAmount = SafeMath.mul(
cliffPeriod,
SafeCast.toUint256(flowRate)
);
params = ScheduleCreationParams({
superToken: superToken,
sender: sender,
receiver: receiver,
startDate: startDate,
claimValidityDate: claimValidityDate,
cliffDate: startDate + cliffPeriod,
flowRate: flowRate,
cliffAmount: cliffAmount,
endDate: endDate,
remainderAmount: remainderAmount
});
}
}
function _validateAndCreateVestingSchedule(
ScheduleCreationParams memory params
) private {
// Note: Vesting Scheduler V2 doesn't allow start date to be in the past.
// V1 did but didn't allow cliff and flow to be in the past though.
if (params.startDate < block.timestamp) revert TimeWindowInvalid();
if (params.endDate <= END_DATE_VALID_BEFORE) revert TimeWindowInvalid();
if (params.receiver == address(0) || params.receiver == params.sender) revert AccountInvalid();
if (address(params.superToken) == address(0)) revert ZeroAddress();
if (params.flowRate <= 0) revert FlowRateInvalid();
if (params.cliffDate != 0 && params.startDate > params.cliffDate) revert TimeWindowInvalid();
if (params.cliffDate == 0 && params.cliffAmount != 0) revert CliffInvalid();
uint32 cliffAndFlowDate = params.cliffDate == 0 ? params.startDate : params.cliffDate;
// Note: Vesting Scheduler V2 allows cliff and flow to be in the schedule creation block, V1 didn't.
if (cliffAndFlowDate < block.timestamp ||
cliffAndFlowDate >= params.endDate ||
cliffAndFlowDate + START_DATE_VALID_AFTER >= params.endDate - END_DATE_VALID_BEFORE ||
params.endDate - cliffAndFlowDate < MIN_VESTING_DURATION
) revert TimeWindowInvalid();
// Note : claimable schedule created with a claim validity date equal to 0 is considered regular schedule
if (params.claimValidityDate != 0 && params.claimValidityDate < cliffAndFlowDate)
revert TimeWindowInvalid();
bytes32 id = _getId(address(params.superToken), params.sender, params.receiver);
if (vestingSchedules[id].endDate != 0) revert ScheduleAlreadyExists();
vestingSchedules[id] = VestingSchedule({
cliffAndFlowDate: cliffAndFlowDate,
endDate: params.endDate,
flowRate: params.flowRate,
cliffAmount: params.cliffAmount,
remainderAmount: params.remainderAmount,
claimValidityDate: params.claimValidityDate
});
emit VestingScheduleCreated(
params.superToken,
params.sender,
params.receiver,
params.startDate,
params.cliffDate,
params.flowRate,
params.endDate,
params.cliffAmount,
params.claimValidityDate,
params.remainderAmount
);
}
/// @dev IVestingScheduler.createAndExecuteVestingScheduleFromAmountAndDuration.
function createAndExecuteVestingScheduleFromAmountAndDuration(
ISuperToken superToken,
address receiver,
uint256 totalAmount,
uint32 totalDuration,
bytes memory ctx
) external returns (bytes memory newCtx) {
newCtx = _validateAndCreateAndExecuteVestingScheduleFromAmountAndDuration(
superToken,
receiver,
totalAmount,
totalDuration,
ctx
);
}
/// @dev IVestingScheduler.createAndExecuteVestingScheduleFromAmountAndDuration.
function createAndExecuteVestingScheduleFromAmountAndDuration(
ISuperToken superToken,
address receiver,
uint256 totalAmount,
uint32 totalDuration
) external {
_validateAndCreateAndExecuteVestingScheduleFromAmountAndDuration(
superToken,
receiver,
totalAmount,
totalDuration,
bytes("")
);
}
/// @dev IVestingScheduler.createAndExecuteVestingScheduleFromAmountAndDuration.
function _validateAndCreateAndExecuteVestingScheduleFromAmountAndDuration(
ISuperToken superToken,
address receiver,
uint256 totalAmount,
uint32 totalDuration,
bytes memory ctx
) private returns (bytes memory newCtx) {
newCtx = ctx;
address sender = _getSender(ctx);
_validateAndCreateVestingSchedule(
mapCreateVestingScheduleParams(
superToken,
sender,
receiver,
totalAmount,
totalDuration,
_normalizeStartDate(0),
0, // cliffPeriod
0 // claimValidityDate
)
);
ScheduleAggregate memory agg = _getVestingScheduleAggregate(superToken, sender, receiver);
_validateBeforeCliffAndFlow(agg.schedule, /* disableClaimCheck: */ false);
assert(_executeCliffAndFlow(agg));
}
/// @dev IVestingScheduler.updateVestingSchedule implementation.
function updateVestingSchedule(
ISuperToken superToken,
address receiver,
uint32 endDate,
bytes memory ctx
) external returns (bytes memory newCtx) {
newCtx = ctx;
address sender = _getSender(ctx);
ScheduleAggregate memory agg = _getVestingScheduleAggregate(superToken, sender, receiver);
VestingSchedule memory schedule = agg.schedule;
if (endDate <= block.timestamp) revert TimeWindowInvalid();
// Note: Claimable schedules that have not been claimed cannot be updated
// Only allow an update if 1. vesting exists 2. executeCliffAndFlow() has been called
if (schedule.cliffAndFlowDate != 0 || schedule.endDate == 0) revert ScheduleNotFlowing();
vestingSchedules[agg.id].endDate = endDate;
// Note: Nullify the remainder amount when complexity of updates is introduced.
vestingSchedules[agg.id].remainderAmount = 0;
emit VestingScheduleUpdated(
superToken,
sender,
receiver,
schedule.endDate,
endDate,
0 // remainderAmount
);
}
/// @dev IVestingScheduler.deleteVestingSchedule implementation.
function deleteVestingSchedule(
ISuperToken superToken,
address receiver,
bytes memory ctx
) external returns (bytes memory newCtx) {
newCtx = ctx;
address sender = _getSender(ctx);
ScheduleAggregate memory agg = _getVestingScheduleAggregate(superToken, sender, receiver);
VestingSchedule memory schedule = agg.schedule;
if (schedule.endDate != 0) {
delete vestingSchedules[agg.id];
emit VestingScheduleDeleted(superToken, sender, receiver);
} else {
revert ScheduleDoesNotExist();
}
}
/// @dev IVestingScheduler.executeCliffAndFlow implementation.
function executeCliffAndFlow(
ISuperToken superToken,
address sender,
address receiver
) external returns (bool success) {
ScheduleAggregate memory agg = _getVestingScheduleAggregate(superToken, sender, receiver);
VestingSchedule memory schedule = agg.schedule;
if (schedule.claimValidityDate != 0) {
_validateAndClaim(agg);
_validateBeforeCliffAndFlow(schedule, /* disableClaimCheck: */ true);
if (block.timestamp >= _gteDateToExecuteEndVesting(schedule)) {
_validateBeforeEndVesting(schedule, /* disableClaimCheck: */ true);
success = _executeVestingAsSingleTransfer(agg);
} else {
success = _executeCliffAndFlow(agg);
}
} else {
_validateBeforeCliffAndFlow(schedule, /* disableClaimCheck: */ false);
success = _executeCliffAndFlow(agg);
}
}
function _validateBeforeCliffAndFlow(
VestingSchedule memory schedule,
bool disableClaimCheck
) private view {
if (schedule.cliffAndFlowDate == 0)
revert AlreadyExecuted();
if (!disableClaimCheck && schedule.claimValidityDate != 0)
revert ScheduleNotClaimed();
// Ensure that the claiming date is after the cliff/flow date and before the claim validity date
if (schedule.cliffAndFlowDate > block.timestamp ||
_lteDateToExecuteCliffAndFlow(schedule) < block.timestamp)
revert TimeWindowInvalid();
}
function _lteDateToExecuteCliffAndFlow(
VestingSchedule memory schedule
) private pure returns (uint32) {
if (schedule.cliffAndFlowDate == 0)
revert AlreadyExecuted();
if (schedule.claimValidityDate != 0) {
return schedule.claimValidityDate;
} else {
return schedule.cliffAndFlowDate + START_DATE_VALID_AFTER;
}
}
function _validateAndClaim(
ScheduleAggregate memory agg
) private {
VestingSchedule memory schedule = agg.schedule;
// Ensure that the caller is the sender or the receiver if the vesting schedule requires claiming.
if (msg.sender != agg.sender && msg.sender != agg.receiver)
revert CannotClaimScheduleOnBehalf();
if (schedule.claimValidityDate < block.timestamp)
revert TimeWindowInvalid();
delete vestingSchedules[agg.id].claimValidityDate;
emit VestingClaimed(agg.superToken, agg.sender, agg.receiver, msg.sender);
}
/// @dev IVestingScheduler.executeCliffAndFlow implementation.
function _executeCliffAndFlow(
ScheduleAggregate memory agg
) private returns (bool success) {
VestingSchedule memory schedule = agg.schedule;
// Invalidate configuration straight away -- avoid any chance of re-execution or re-entry.
delete vestingSchedules[agg.id].cliffAndFlowDate;
delete vestingSchedules[agg.id].cliffAmount;
// Compensate for the fact that flow will almost always be executed slightly later than scheduled.
uint256 flowDelayCompensation =
(block.timestamp - schedule.cliffAndFlowDate) * uint96(schedule.flowRate);
// If there's cliff or compensation then transfer that amount.
if (schedule.cliffAmount != 0 || flowDelayCompensation != 0) {
// Note: Super Tokens revert, not return false, i.e. we expect always true here.
assert(
agg.superToken.transferFrom(
agg.sender, agg.receiver, schedule.cliffAmount + flowDelayCompensation));
}
// Create a flow according to the vesting schedule configuration.
agg.superToken.createFlowFrom(agg.sender, agg.receiver, schedule.flowRate);
emit VestingCliffAndFlowExecuted(
agg.superToken,
agg.sender,
agg.receiver,
schedule.cliffAndFlowDate,
schedule.flowRate,
schedule.cliffAmount,
flowDelayCompensation
);
return true;
}
function _executeVestingAsSingleTransfer(
ScheduleAggregate memory agg
) private returns (bool success) {
VestingSchedule memory schedule = agg.schedule;
delete vestingSchedules[agg.id];
uint256 totalVestedAmount = _getTotalVestedAmount(schedule);
// Note: Super Tokens revert, not return false, i.e. we expect always true here.
assert(agg.superToken.transferFrom(agg.sender, agg.receiver, totalVestedAmount));
emit VestingCliffAndFlowExecuted(
agg.superToken,
agg.sender,
agg.receiver,
schedule.cliffAndFlowDate,
0, // flow rate
schedule.cliffAmount,
totalVestedAmount - schedule.cliffAmount // flow delay compensation
);
emit VestingEndExecuted(
agg.superToken,
agg.sender,
agg.receiver,
schedule.endDate,
0, // Early end compensation
false // Did end fail
);
return true;
}
function _getTotalVestedAmount(
VestingSchedule memory schedule
) private pure returns (uint256) {
return
schedule.cliffAmount +
schedule.remainderAmount +
(schedule.endDate - schedule.cliffAndFlowDate) * SafeCast.toUint256(schedule.flowRate);
}
/// @dev IVestingScheduler.executeEndVesting implementation.
function executeEndVesting(
ISuperToken superToken,
address sender,
address receiver
) external returns (bool success) {
ScheduleAggregate memory agg = _getVestingScheduleAggregate(superToken, sender, receiver);
VestingSchedule memory schedule = agg.schedule;
_validateBeforeEndVesting(schedule, /* disableClaimCheck: */ false);
// Invalidate configuration straight away -- avoid any chance of re-execution or re-entry.
delete vestingSchedules[agg.id];
// If vesting is not running, we can't do anything, just emit failing event.
if (_isFlowOngoing(superToken, sender, receiver)) {
// delete first the stream and unlock deposit amount.
superToken.deleteFlowFrom(sender, receiver);
uint256 earlyEndCompensation = schedule.endDate >= block.timestamp
? (schedule.endDate - block.timestamp) * uint96(schedule.flowRate) + schedule.remainderAmount
: 0;
// Note: we consider the compensation as failed if the stream is still ongoing after the end date.
bool didCompensationFail = schedule.endDate < block.timestamp;
if (earlyEndCompensation != 0) {
// Note: Super Tokens revert, not return false, i.e. we expect always true here.
assert(superToken.transferFrom(sender, receiver, earlyEndCompensation));
}
emit VestingEndExecuted(
superToken,
sender,
receiver,
schedule.endDate,
earlyEndCompensation,
didCompensationFail
);
} else {
emit VestingEndFailed(
superToken,
sender,
receiver,
schedule.endDate
);
}
return true;
}
function _validateBeforeEndVesting(
VestingSchedule memory schedule,
bool disableClaimCheck
) private view {
if (schedule.endDate == 0)
revert AlreadyExecuted();
if (!disableClaimCheck && schedule.claimValidityDate != 0)
revert ScheduleNotClaimed();
if (_gteDateToExecuteEndVesting(schedule) > block.timestamp)
revert TimeWindowInvalid();
}
function _gteDateToExecuteEndVesting(
VestingSchedule memory schedule
) private pure returns (uint32) {
if (schedule.endDate == 0)
revert AlreadyExecuted();
return schedule.endDate - END_DATE_VALID_BEFORE;
}
/// @dev IVestingScheduler.getVestingSchedule implementation.
function getVestingSchedule(
address superToken, address sender, address receiver
) external view returns (VestingSchedule memory) {
return vestingSchedules[_getId(address(superToken), sender, receiver)];
}
function _getVestingScheduleAggregate(
ISuperToken superToken, address sender, address receiver
) private view returns (ScheduleAggregate memory) {
bytes32 id = _getId(address(superToken), sender, receiver);
return ScheduleAggregate({
superToken: superToken,
sender: sender,
receiver: receiver,
id: id,
schedule: vestingSchedules[id]
});
}
function _normalizeStartDate(uint32 startDate) private view returns (uint32) {
// Default to current block timestamp if no start date is provided.
if (startDate == 0) {
return uint32(block.timestamp);
}
return startDate;
}
/// @dev IVestingScheduler.getMaximumNeededTokenAllowance implementation.
function getMaximumNeededTokenAllowance(
VestingSchedule memory schedule
) external pure override returns (uint256) {
uint256 maxFlowDelayCompensationAmount =
schedule.cliffAndFlowDate == 0
? 0
: START_DATE_VALID_AFTER * SafeCast.toUint256(schedule.flowRate);
uint256 maxEarlyEndCompensationAmount =
schedule.endDate == 0
? 0
: END_DATE_VALID_BEFORE * SafeCast.toUint256(schedule.flowRate);
if (schedule.claimValidityDate == 0) {
return
schedule.cliffAmount +
schedule.remainderAmount +
maxFlowDelayCompensationAmount +
maxEarlyEndCompensationAmount;
} else if (schedule.claimValidityDate >= _gteDateToExecuteEndVesting(schedule)) {
return _getTotalVestedAmount(schedule);
} else {
return schedule.cliffAmount +
schedule.remainderAmount +
(schedule.claimValidityDate - schedule.cliffAndFlowDate) * SafeCast.toUint256(schedule.flowRate) +
maxEarlyEndCompensationAmount;
}
}
/// @dev get sender of transaction from Superfluid Context or transaction itself.
function _getSender(bytes memory ctx) private view returns (address sender) {
if (ctx.length != 0) {
if (msg.sender != address(HOST)) revert HostInvalid();
sender = HOST.decodeCtx(ctx).msgSender;
} else {
sender = msg.sender;
}
// This is an invariant and should never happen.
assert(sender != address(0));
}
/// @dev get flowRate of stream
function _isFlowOngoing(ISuperToken superToken, address sender, address receiver) private view returns (bool) {
return superToken.getFlowRate(sender, receiver) != 0;
}
function _getId(
address superToken, address sender, address receiver
) private pure returns (bytes32) {
return keccak256(abi.encodePacked(superToken, sender, receiver));
}
}