TokenTimelock.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 {TokenVesting}.
  11. */
  12. contract TokenTimelock {
  13. using SafeERC20 for IERC20;
  14. // ERC20 basic token contract being held
  15. IERC20 private _token;
  16. // beneficiary of tokens after they are released
  17. address private _beneficiary;
  18. // timestamp when token release is enabled
  19. uint256 private _releaseTime;
  20. constructor (IERC20 token, address beneficiary, uint256 releaseTime) public {
  21. // solhint-disable-next-line not-rely-on-time
  22. require(releaseTime > block.timestamp, "TokenTimelock: release time is before current time");
  23. _token = token;
  24. _beneficiary = beneficiary;
  25. _releaseTime = releaseTime;
  26. }
  27. /**
  28. * @return the token being held.
  29. */
  30. function token() public view returns (IERC20) {
  31. return _token;
  32. }
  33. /**
  34. * @return the beneficiary of the tokens.
  35. */
  36. function beneficiary() public view returns (address) {
  37. return _beneficiary;
  38. }
  39. /**
  40. * @return the time when the tokens are released.
  41. */
  42. function releaseTime() public view returns (uint256) {
  43. return _releaseTime;
  44. }
  45. /**
  46. * @notice Transfers tokens held by timelock to beneficiary.
  47. */
  48. function release() public {
  49. // solhint-disable-next-line not-rely-on-time
  50. require(block.timestamp >= _releaseTime, "TokenTimelock: current time is before release time");
  51. uint256 amount = _token.balanceOf(address(this));
  52. require(amount > 0, "TokenTimelock: no tokens to release");
  53. _token.safeTransfer(_beneficiary, amount);
  54. }
  55. }