VestingWalletCliff.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. abstract contract VestingWalletCliff is VestingWallet {
  9. using SafeCast for *;
  10. uint64 private immutable _cliff;
  11. /// @dev The specified cliff duration is larger than the vesting duration.
  12. error InvalidCliffDuration(uint64 cliffSeconds, uint64 durationSeconds);
  13. /**
  14. * @dev Set the start timestamp of the vesting wallet cliff.
  15. */
  16. constructor(uint64 cliffSeconds) {
  17. if (cliffSeconds > duration()) {
  18. revert InvalidCliffDuration(cliffSeconds, duration().toUint64());
  19. }
  20. _cliff = start().toUint64() + cliffSeconds;
  21. }
  22. /**
  23. * @dev Getter for the cliff timestamp.
  24. */
  25. function cliff() public view virtual returns (uint256) {
  26. return _cliff;
  27. }
  28. /**
  29. * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
  30. * an asset given its total historical allocation. Returns 0 if the {cliff} timestamp is not met.
  31. *
  32. * IMPORTANT: The cliff not only makes the schedule return 0, but it also ignores every possible side
  33. * effect from calling the inherited implementation (i.e. `super._vestingSchedule`). Carefully consider
  34. * this caveat if the overridden implementation of this function has any (e.g. writing to memory or reverting).
  35. */
  36. function _vestingSchedule(
  37. uint256 totalAllocation,
  38. uint64 timestamp
  39. ) internal view virtual override returns (uint256) {
  40. return timestamp < cliff() ? 0 : super._vestingSchedule(totalAllocation, timestamp);
  41. }
  42. }