TokenVesting.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. pragma solidity ^0.5.0;
  2. import "../token/ERC20/SafeERC20.sol";
  3. import "../ownership/Ownable.sol";
  4. import "../math/SafeMath.sol";
  5. /**
  6. * @title TokenVesting
  7. * @dev A token holder contract that can release its token balance gradually like a
  8. * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the
  9. * owner.
  10. */
  11. contract TokenVesting is Ownable {
  12. // The vesting schedule is time-based (i.e. using block timestamps as opposed to e.g. block numbers), and is
  13. // therefore sensitive to timestamp manipulation (which is something miners can do, to a certain degree). Therefore,
  14. // it is recommended to avoid using short time durations (less than a minute). Typical vesting schemes, with a
  15. // cliff period of a year and a duration of four years, are safe to use.
  16. // solhint-disable not-rely-on-time
  17. using SafeMath for uint256;
  18. using SafeERC20 for IERC20;
  19. event TokensReleased(address token, uint256 amount);
  20. event TokenVestingRevoked(address token);
  21. // beneficiary of tokens after they are released
  22. address private _beneficiary;
  23. // Durations and timestamps are expressed in UNIX time, the same units as block.timestamp.
  24. uint256 private _cliff;
  25. uint256 private _start;
  26. uint256 private _duration;
  27. bool private _revocable;
  28. mapping (address => uint256) private _released;
  29. mapping (address => bool) private _revoked;
  30. /**
  31. * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
  32. * beneficiary, gradually in a linear fashion until start + duration. By then all
  33. * of the balance will have vested.
  34. * @param beneficiary address of the beneficiary to whom vested tokens are transferred
  35. * @param cliffDuration duration in seconds of the cliff in which tokens will begin to vest
  36. * @param start the time (as Unix time) at which point vesting starts
  37. * @param duration duration in seconds of the period in which the tokens will vest
  38. * @param revocable whether the vesting is revocable or not
  39. */
  40. constructor (address beneficiary, uint256 start, uint256 cliffDuration, uint256 duration, bool revocable) public {
  41. require(beneficiary != address(0), "TokenVesting: beneficiary is the zero address");
  42. // solhint-disable-next-line max-line-length
  43. require(cliffDuration <= duration, "TokenVesting: cliff is longer than duration");
  44. require(duration > 0, "TokenVesting: duration is 0");
  45. // solhint-disable-next-line max-line-length
  46. require(start.add(duration) > block.timestamp, "TokenVesting: final time is before current time");
  47. _beneficiary = beneficiary;
  48. _revocable = revocable;
  49. _duration = duration;
  50. _cliff = start.add(cliffDuration);
  51. _start = start;
  52. }
  53. /**
  54. * @return the beneficiary of the tokens.
  55. */
  56. function beneficiary() public view returns (address) {
  57. return _beneficiary;
  58. }
  59. /**
  60. * @return the cliff time of the token vesting.
  61. */
  62. function cliff() public view returns (uint256) {
  63. return _cliff;
  64. }
  65. /**
  66. * @return the start time of the token vesting.
  67. */
  68. function start() public view returns (uint256) {
  69. return _start;
  70. }
  71. /**
  72. * @return the duration of the token vesting.
  73. */
  74. function duration() public view returns (uint256) {
  75. return _duration;
  76. }
  77. /**
  78. * @return true if the vesting is revocable.
  79. */
  80. function revocable() public view returns (bool) {
  81. return _revocable;
  82. }
  83. /**
  84. * @return the amount of the token released.
  85. */
  86. function released(address token) public view returns (uint256) {
  87. return _released[token];
  88. }
  89. /**
  90. * @return true if the token is revoked.
  91. */
  92. function revoked(address token) public view returns (bool) {
  93. return _revoked[token];
  94. }
  95. /**
  96. * @notice Transfers vested tokens to beneficiary.
  97. * @param token ERC20 token which is being vested
  98. */
  99. function release(IERC20 token) public {
  100. uint256 unreleased = _releasableAmount(token);
  101. require(unreleased > 0, "TokenVesting: no tokens are due");
  102. _released[address(token)] = _released[address(token)].add(unreleased);
  103. token.safeTransfer(_beneficiary, unreleased);
  104. emit TokensReleased(address(token), unreleased);
  105. }
  106. /**
  107. * @notice Allows the owner to revoke the vesting. Tokens already vested
  108. * remain in the contract, the rest are returned to the owner.
  109. * @param token ERC20 token which is being vested
  110. */
  111. function revoke(IERC20 token) public onlyOwner {
  112. require(_revocable, "TokenVesting: cannot revoke");
  113. require(!_revoked[address(token)], "TokenVesting: token already revoked");
  114. uint256 balance = token.balanceOf(address(this));
  115. uint256 unreleased = _releasableAmount(token);
  116. uint256 refund = balance.sub(unreleased);
  117. _revoked[address(token)] = true;
  118. token.safeTransfer(owner(), refund);
  119. emit TokenVestingRevoked(address(token));
  120. }
  121. /**
  122. * @dev Calculates the amount that has already vested but hasn't been released yet.
  123. * @param token ERC20 token which is being vested
  124. */
  125. function _releasableAmount(IERC20 token) private view returns (uint256) {
  126. return _vestedAmount(token).sub(_released[address(token)]);
  127. }
  128. /**
  129. * @dev Calculates the amount that has already vested.
  130. * @param token ERC20 token which is being vested
  131. */
  132. function _vestedAmount(IERC20 token) private view returns (uint256) {
  133. uint256 currentBalance = token.balanceOf(address(this));
  134. uint256 totalBalance = currentBalance.add(_released[address(token)]);
  135. if (block.timestamp < _cliff) {
  136. return 0;
  137. } else if (block.timestamp >= _start.add(_duration) || _revoked[address(token)]) {
  138. return totalBalance;
  139. } else {
  140. return totalBalance.mul(block.timestamp.sub(_start)).div(_duration);
  141. }
  142. }
  143. }