TokenTimelock.sol 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "./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(
  26. IERC20 token_,
  27. address beneficiary_,
  28. uint256 releaseTime_
  29. ) {
  30. require(releaseTime_ > block.timestamp, "TokenTimelock: release time is before current time");
  31. _token = token_;
  32. _beneficiary = beneficiary_;
  33. _releaseTime = releaseTime_;
  34. }
  35. /**
  36. * @dev Returns the token being held.
  37. */
  38. function token() public view virtual returns (IERC20) {
  39. return _token;
  40. }
  41. /**
  42. * @dev Returns the beneficiary that will receive the tokens.
  43. */
  44. function beneficiary() public view virtual returns (address) {
  45. return _beneficiary;
  46. }
  47. /**
  48. * @dev Returns the time when the tokens are released in seconds since Unix epoch (i.e. Unix timestamp).
  49. */
  50. function releaseTime() public view virtual returns (uint256) {
  51. return _releaseTime;
  52. }
  53. /**
  54. * @dev Transfers tokens held by the timelock to the beneficiary. Will only succeed if invoked after the release
  55. * time.
  56. */
  57. function release() public virtual {
  58. require(block.timestamp >= releaseTime(), "TokenTimelock: current time is before release time");
  59. uint256 amount = token().balanceOf(address(this));
  60. require(amount > 0, "TokenTimelock: no tokens to release");
  61. token().safeTransfer(beneficiary(), amount);
  62. }
  63. }