TokenVesting.sol 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* solium-disable security/no-block-members */
  2. pragma solidity ^0.4.24;
  3. import "./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 Released(uint256 amount);
  16. event Revoked();
  17. // beneficiary of tokens after they are released
  18. address public beneficiary;
  19. uint256 public cliff;
  20. uint256 public start;
  21. uint256 public duration;
  22. bool public revocable;
  23. mapping (address => uint256) public released;
  24. mapping (address => bool) public 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 _cliff 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 _cliff,
  39. uint256 _duration,
  40. bool _revocable
  41. )
  42. public
  43. {
  44. require(_beneficiary != address(0));
  45. require(_cliff <= _duration);
  46. beneficiary = _beneficiary;
  47. revocable = _revocable;
  48. duration = _duration;
  49. cliff = _start.add(_cliff);
  50. start = _start;
  51. }
  52. /**
  53. * @notice Transfers vested tokens to beneficiary.
  54. * @param _token ERC20 token which is being vested
  55. */
  56. function release(IERC20 _token) public {
  57. uint256 unreleased = releasableAmount(_token);
  58. require(unreleased > 0);
  59. released[_token] = released[_token].add(unreleased);
  60. _token.safeTransfer(beneficiary, unreleased);
  61. emit Released(unreleased);
  62. }
  63. /**
  64. * @notice Allows the owner to revoke the vesting. Tokens already vested
  65. * remain in the contract, the rest are returned to the owner.
  66. * @param _token ERC20 token which is being vested
  67. */
  68. function revoke(IERC20 _token) public onlyOwner {
  69. require(revocable);
  70. require(!revoked[_token]);
  71. uint256 balance = _token.balanceOf(address(this));
  72. uint256 unreleased = releasableAmount(_token);
  73. uint256 refund = balance.sub(unreleased);
  74. revoked[_token] = true;
  75. _token.safeTransfer(owner, refund);
  76. emit Revoked();
  77. }
  78. /**
  79. * @dev Calculates the amount that has already vested but hasn't been released yet.
  80. * @param _token ERC20 token which is being vested
  81. */
  82. function releasableAmount(IERC20 _token) public view returns (uint256) {
  83. return vestedAmount(_token).sub(released[_token]);
  84. }
  85. /**
  86. * @dev Calculates the amount that has already vested.
  87. * @param _token ERC20 token which is being vested
  88. */
  89. function vestedAmount(IERC20 _token) public view returns (uint256) {
  90. uint256 currentBalance = _token.balanceOf(this);
  91. uint256 totalBalance = currentBalance.add(released[_token]);
  92. if (block.timestamp < cliff) {
  93. return 0;
  94. } else if (block.timestamp >= start.add(duration) || revoked[_token]) {
  95. return totalBalance;
  96. } else {
  97. return totalBalance.mul(block.timestamp.sub(start)).div(duration);
  98. }
  99. }
  100. }