TokenVesting.sol 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. pragma solidity ^0.5.7;
  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));
  42. require(cliffDuration <= duration);
  43. require(duration > 0);
  44. require(start.add(duration) > block.timestamp);
  45. _beneficiary = beneficiary;
  46. _revocable = revocable;
  47. _duration = duration;
  48. _cliff = start.add(cliffDuration);
  49. _start = start;
  50. }
  51. /**
  52. * @return the beneficiary of the tokens.
  53. */
  54. function beneficiary() public view returns (address) {
  55. return _beneficiary;
  56. }
  57. /**
  58. * @return the cliff time of the token vesting.
  59. */
  60. function cliff() public view returns (uint256) {
  61. return _cliff;
  62. }
  63. /**
  64. * @return the start time of the token vesting.
  65. */
  66. function start() public view returns (uint256) {
  67. return _start;
  68. }
  69. /**
  70. * @return the duration of the token vesting.
  71. */
  72. function duration() public view returns (uint256) {
  73. return _duration;
  74. }
  75. /**
  76. * @return true if the vesting is revocable.
  77. */
  78. function revocable() public view returns (bool) {
  79. return _revocable;
  80. }
  81. /**
  82. * @return the amount of the token released.
  83. */
  84. function released(address token) public view returns (uint256) {
  85. return _released[token];
  86. }
  87. /**
  88. * @return true if the token is revoked.
  89. */
  90. function revoked(address token) public view returns (bool) {
  91. return _revoked[token];
  92. }
  93. /**
  94. * @notice Transfers vested tokens to beneficiary.
  95. * @param token ERC20 token which is being vested
  96. */
  97. function release(IERC20 token) public {
  98. uint256 unreleased = _releasableAmount(token);
  99. require(unreleased > 0);
  100. _released[address(token)] = _released[address(token)].add(unreleased);
  101. token.safeTransfer(_beneficiary, unreleased);
  102. emit TokensReleased(address(token), unreleased);
  103. }
  104. /**
  105. * @notice Allows the owner to revoke the vesting. Tokens already vested
  106. * remain in the contract, the rest are returned to the owner.
  107. * @param token ERC20 token which is being vested
  108. */
  109. function revoke(IERC20 token) public onlyOwner {
  110. require(_revocable);
  111. require(!_revoked[address(token)]);
  112. uint256 balance = token.balanceOf(address(this));
  113. uint256 unreleased = _releasableAmount(token);
  114. uint256 refund = balance.sub(unreleased);
  115. _revoked[address(token)] = true;
  116. token.safeTransfer(owner(), refund);
  117. emit TokenVestingRevoked(address(token));
  118. }
  119. /**
  120. * @dev Calculates the amount that has already vested but hasn't been released yet.
  121. * @param token ERC20 token which is being vested
  122. */
  123. function _releasableAmount(IERC20 token) private view returns (uint256) {
  124. return _vestedAmount(token).sub(_released[address(token)]);
  125. }
  126. /**
  127. * @dev Calculates the amount that has already vested.
  128. * @param token ERC20 token which is being vested
  129. */
  130. function _vestedAmount(IERC20 token) private view returns (uint256) {
  131. uint256 currentBalance = token.balanceOf(address(this));
  132. uint256 totalBalance = currentBalance.add(_released[address(token)]);
  133. if (block.timestamp < _cliff) {
  134. return 0;
  135. } else if (block.timestamp >= _start.add(_duration) || _revoked[address(token)]) {
  136. return totalBalance;
  137. } else {
  138. return totalBalance.mul(block.timestamp.sub(_start)).div(_duration);
  139. }
  140. }
  141. }