TokenTimelock.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "./SafeERC20.sol";
  4. /**
  5. * @dev A token holder contract that will allow a beneficiary to extract the
  6. * tokens after a given release time.
  7. *
  8. * Useful for simple vesting schedules like "advisors get all of their tokens
  9. * after 1 year".
  10. *
  11. * For a more complete vesting schedule, see {TokenVesting}.
  12. */
  13. contract TokenTimelock {
  14. using SafeERC20 for IERC20;
  15. // ERC20 basic token contract being held
  16. IERC20 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. constructor (IERC20 token, address beneficiary, uint256 releaseTime) public {
  22. // solhint-disable-next-line not-rely-on-time
  23. require(releaseTime > block.timestamp, "TokenTimelock: release time is before current time");
  24. _token = token;
  25. _beneficiary = beneficiary;
  26. _releaseTime = releaseTime;
  27. }
  28. /**
  29. * @return the token being held.
  30. */
  31. function token() public view returns (IERC20) {
  32. return _token;
  33. }
  34. /**
  35. * @return the beneficiary of the tokens.
  36. */
  37. function beneficiary() public view returns (address) {
  38. return _beneficiary;
  39. }
  40. /**
  41. * @return the time when the tokens are released.
  42. */
  43. function releaseTime() public view returns (uint256) {
  44. return _releaseTime;
  45. }
  46. /**
  47. * @notice Transfers tokens held by timelock to beneficiary.
  48. */
  49. function release() public virtual {
  50. // solhint-disable-next-line not-rely-on-time
  51. require(block.timestamp >= _releaseTime, "TokenTimelock: current time is before release time");
  52. uint256 amount = _token.balanceOf(address(this));
  53. require(amount > 0, "TokenTimelock: no tokens to release");
  54. _token.safeTransfer(_beneficiary, amount);
  55. }
  56. }