TokenTimelock.sol 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/utils/TokenTimelock.sol)
  3. pragma solidity ^0.8.0;
  4. import "./SafeERC20.sol";
  5. /**
  6. * @dev A token holder contract that will allow a beneficiary to extract the
  7. * tokens after a given release time.
  8. *
  9. * Useful for simple vesting schedules like "advisors get all of their tokens
  10. * after 1 year".
  11. */
  12. contract TokenTimelock {
  13. using SafeERC20 for IERC20;
  14. // ERC20 basic token contract being held
  15. IERC20 private immutable _token;
  16. // beneficiary of tokens after they are released
  17. address private immutable _beneficiary;
  18. // timestamp when token release is enabled
  19. uint256 private immutable _releaseTime;
  20. /**
  21. * @dev Deploys a timelock instance that is able to hold the token specified, and will only release it to
  22. * `beneficiary_` when {release} is invoked after `releaseTime_`. The release time is specified as a Unix timestamp
  23. * (in seconds).
  24. */
  25. constructor(IERC20 token_, address beneficiary_, uint256 releaseTime_) {
  26. require(releaseTime_ > block.timestamp, "TokenTimelock: release time is before current time");
  27. _token = token_;
  28. _beneficiary = beneficiary_;
  29. _releaseTime = releaseTime_;
  30. }
  31. /**
  32. * @dev Returns the token being held.
  33. */
  34. function token() public view virtual returns (IERC20) {
  35. return _token;
  36. }
  37. /**
  38. * @dev Returns the beneficiary that will receive the tokens.
  39. */
  40. function beneficiary() public view virtual returns (address) {
  41. return _beneficiary;
  42. }
  43. /**
  44. * @dev Returns the time when the tokens are released in seconds since Unix epoch (i.e. Unix timestamp).
  45. */
  46. function releaseTime() public view virtual returns (uint256) {
  47. return _releaseTime;
  48. }
  49. /**
  50. * @dev Transfers tokens held by the timelock to the beneficiary. Will only succeed if invoked after the release
  51. * time.
  52. */
  53. function release() public virtual {
  54. require(block.timestamp >= releaseTime(), "TokenTimelock: current time is before release time");
  55. uint256 amount = token().balanceOf(address(this));
  56. require(amount > 0, "TokenTimelock: no tokens to release");
  57. token().safeTransfer(beneficiary(), amount);
  58. }
  59. }