TokenVesting.sol 4.7 KB

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