VestingWalletCliff.sol 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {SafeCast} from "../utils/math/SafeCast.sol";
  4. import {VestingWallet} from "./VestingWallet.sol";
  5. /**
  6. * @dev Extension of {VestingWallet} that adds a cliff to the vesting schedule.
  7. *
  8. * _Available since v5.1._
  9. */
  10. abstract contract VestingWalletCliff is VestingWallet {
  11. using SafeCast for *;
  12. uint64 private immutable _cliff;
  13. /// @dev The specified cliff duration is larger than the vesting duration.
  14. error InvalidCliffDuration(uint64 cliffSeconds, uint64 durationSeconds);
  15. /**
  16. * @dev Set the duration of the cliff, in seconds. The cliff starts vesting schedule (see {VestingWallet}'s
  17. * constructor) and ends `cliffSeconds` later.
  18. */
  19. constructor(uint64 cliffSeconds) {
  20. if (cliffSeconds > duration()) {
  21. revert InvalidCliffDuration(cliffSeconds, duration().toUint64());
  22. }
  23. _cliff = start().toUint64() + cliffSeconds;
  24. }
  25. /**
  26. * @dev Getter for the cliff timestamp.
  27. */
  28. function cliff() public view virtual returns (uint256) {
  29. return _cliff;
  30. }
  31. /**
  32. * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
  33. * an asset given its total historical allocation. Returns 0 if the {cliff} timestamp is not met.
  34. *
  35. * IMPORTANT: The cliff not only makes the schedule return 0, but it also ignores every possible side
  36. * effect from calling the inherited implementation (i.e. `super._vestingSchedule`). Carefully consider
  37. * this caveat if the overridden implementation of this function has any (e.g. writing to memory or reverting).
  38. */
  39. function _vestingSchedule(
  40. uint256 totalAllocation,
  41. uint64 timestamp
  42. ) internal view virtual override returns (uint256) {
  43. return timestamp < cliff() ? 0 : super._vestingSchedule(totalAllocation, timestamp);
  44. }
  45. }