VestingWalletCliff.sol 1.9 KB

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