TokenTimelockUpgradeable.sol 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/utils/TokenTimelock.sol)
  3. pragma solidity ^0.8.0;
  4. import "./SafeERC20Upgradeable.sol";
  5. import "../../../proxy/utils/Initializable.sol";
  6. /**
  7. * @dev A token holder contract that will allow a beneficiary to extract the
  8. * tokens after a given release time.
  9. *
  10. * Useful for simple vesting schedules like "advisors get all of their tokens
  11. * after 1 year".
  12. */
  13. contract TokenTimelockUpgradeable is Initializable {
  14. using SafeERC20Upgradeable for IERC20Upgradeable;
  15. // ERC20 basic token contract being held
  16. IERC20Upgradeable private _token;
  17. // beneficiary of tokens after they are released
  18. address private _beneficiary;
  19. // timestamp when token release is enabled
  20. uint256 private _releaseTime;
  21. /**
  22. * @dev Deploys a timelock instance that is able to hold the token specified, and will only release it to
  23. * `beneficiary_` when {release} is invoked after `releaseTime_`. The release time is specified as a Unix timestamp
  24. * (in seconds).
  25. */
  26. function __TokenTimelock_init(
  27. IERC20Upgradeable token_,
  28. address beneficiary_,
  29. uint256 releaseTime_
  30. ) internal onlyInitializing {
  31. __TokenTimelock_init_unchained(token_, beneficiary_, releaseTime_);
  32. }
  33. function __TokenTimelock_init_unchained(
  34. IERC20Upgradeable token_,
  35. address beneficiary_,
  36. uint256 releaseTime_
  37. ) internal onlyInitializing {
  38. require(releaseTime_ > block.timestamp, "TokenTimelock: release time is before current time");
  39. _token = token_;
  40. _beneficiary = beneficiary_;
  41. _releaseTime = releaseTime_;
  42. }
  43. /**
  44. * @dev Returns the token being held.
  45. */
  46. function token() public view virtual returns (IERC20Upgradeable) {
  47. return _token;
  48. }
  49. /**
  50. * @dev Returns the beneficiary that will receive the tokens.
  51. */
  52. function beneficiary() public view virtual returns (address) {
  53. return _beneficiary;
  54. }
  55. /**
  56. * @dev Returns the time when the tokens are released in seconds since Unix epoch (i.e. Unix timestamp).
  57. */
  58. function releaseTime() public view virtual returns (uint256) {
  59. return _releaseTime;
  60. }
  61. /**
  62. * @dev Transfers tokens held by the timelock to the beneficiary. Will only succeed if invoked after the release
  63. * time.
  64. */
  65. function release() public virtual {
  66. require(block.timestamp >= releaseTime(), "TokenTimelock: current time is before release time");
  67. uint256 amount = token().balanceOf(address(this));
  68. require(amount > 0, "TokenTimelock: no tokens to release");
  69. token().safeTransfer(beneficiary(), amount);
  70. }
  71. /**
  72. * This empty reserved space is put in place to allow future versions to add new
  73. * variables without shifting down storage in the inheritance chain.
  74. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  75. */
  76. uint256[50] private __gap;
  77. }