VestingWalletCliff.sol 2.0 KB

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