TokenVesting.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. pragma solidity ^0.4.11;
  2. import './ERC20Basic.sol';
  3. import '../ownership/Ownable.sol';
  4. import '../math/Math.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. event Released(uint256 amount);
  15. event Revoked();
  16. // beneficiary of tokens after they are released
  17. address beneficiary;
  18. uint256 cliff;
  19. uint256 start;
  20. uint256 duration;
  21. bool revocable;
  22. mapping (address => uint256) released;
  23. /**
  24. * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
  25. * _beneficiary, gradually in a linear fashion until _start + _duration. By then all
  26. * of the balance will have vested.
  27. * @param _beneficiary address of the beneficiary to whom vested tokens are transferred
  28. * @param _cliff duration in seconds of the cliff in which tokens will begin to vest
  29. * @param _duration duration in seconds of the period in which the tokens will vest
  30. * @param _revocable whether the vesting is revocable or not
  31. */
  32. function TokenVesting(address _beneficiary, uint256 _start, uint256 _cliff, uint256 _duration, bool _revocable) {
  33. require(_beneficiary != 0x0);
  34. require(_cliff <= _duration);
  35. beneficiary = _beneficiary;
  36. revocable = _revocable;
  37. duration = _duration;
  38. cliff = _start + _cliff;
  39. start = _start;
  40. }
  41. /**
  42. * @notice Transfers vested tokens to beneficiary.
  43. * @param token ERC20 token which is being vested
  44. */
  45. function release(ERC20Basic token) {
  46. uint256 vested = vestedAmount(token);
  47. require(vested > 0);
  48. token.transfer(beneficiary, vested);
  49. released[token] = released[token].add(vested);
  50. Released(vested);
  51. }
  52. /**
  53. * @notice Allows the owner to revoke the vesting. Tokens already vested remain in the contract.
  54. * @param token ERC20 token which is being vested
  55. */
  56. function revoke(ERC20Basic token) onlyOwner {
  57. require(revocable);
  58. uint256 balance = token.balanceOf(this);
  59. uint256 vested = vestedAmount(token);
  60. token.transfer(owner, balance - vested);
  61. Revoked();
  62. }
  63. /**
  64. * @dev Calculates the amount that has already vested but hasn't been released yet.
  65. * @param token ERC20 token which is being vested
  66. */
  67. function vestedAmount(ERC20Basic token) constant returns (uint256) {
  68. if (now < cliff) {
  69. return 0;
  70. } else if (now >= start + duration) {
  71. return token.balanceOf(this);
  72. } else {
  73. uint256 currentBalance = token.balanceOf(this);
  74. uint256 totalBalance = currentBalance.add(released[token]);
  75. uint256 vested = totalBalance.mul(now - start).div(duration);
  76. uint256 unreleased = vested.sub(released[token]);
  77. // currentBalance can be 0 in case of vesting being revoked earlier.
  78. return Math.min256(currentBalance, unreleased);
  79. }
  80. }
  81. }