TokenVesting.sol 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* solium-disable security/no-block-members */
  2. pragma solidity ^0.4.23;
  3. import "./ERC20Basic.sol";
  4. import "./SafeERC20.sol";
  5. import "../../ownership/Ownable.sol";
  6. import "../../math/SafeMath.sol";
  7. /**
  8. * @title TokenVesting
  9. * @dev A token holder contract that can release its token balance gradually like a
  10. * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the
  11. * owner.
  12. */
  13. contract TokenVesting is Ownable {
  14. using SafeMath for uint256;
  15. using SafeERC20 for ERC20Basic;
  16. event Released(uint256 amount);
  17. event Revoked();
  18. // beneficiary of tokens after they are released
  19. address public beneficiary;
  20. uint256 public cliff;
  21. uint256 public start;
  22. uint256 public duration;
  23. bool public revocable;
  24. mapping (address => uint256) public released;
  25. mapping (address => bool) public revoked;
  26. /**
  27. * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
  28. * _beneficiary, gradually in a linear fashion until _start + _duration. By then all
  29. * of the balance will have vested.
  30. * @param _beneficiary address of the beneficiary to whom vested tokens are transferred
  31. * @param _cliff duration in seconds of the cliff in which tokens will begin to vest
  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(ERC20Basic 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(ERC20Basic token) public onlyOwner {
  69. require(revocable);
  70. require(!revoked[token]);
  71. uint256 balance = token.balanceOf(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(ERC20Basic 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(ERC20Basic 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. }