TokenTimelock.sol 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. pragma solidity ^0.5.0;
  2. import "./SafeERC20.sol";
  3. /**
  4. * @dev A token holder contract that will allow a beneficiary to extract the
  5. * tokens after a given release time.
  6. *
  7. * Useful for simple vesting schedules like "advisors get all of their tokens
  8. * after 1 year".
  9. *
  10. * For a more complete vesting schedule, see
  11. * [`TokenVesting`](api/drafts#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 {
  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. }