VestingWalletCliff.sol 2.0 KB

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