TokenVesting.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. require(_start >= now);
  36. beneficiary = _beneficiary;
  37. revocable = _revocable;
  38. duration = _duration;
  39. cliff = _start + _cliff;
  40. start = _start;
  41. }
  42. /**
  43. * @notice Transfers vested tokens to beneficiary.
  44. * @param token ERC20 token which is being vested
  45. */
  46. function release(ERC20Basic token) {
  47. uint256 vested = vestedAmount(token);
  48. require(vested > 0);
  49. token.transfer(beneficiary, vested);
  50. released[token] = released[token].add(vested);
  51. Released(vested);
  52. }
  53. /**
  54. * @notice Allows the owner to revoke the vesting. Tokens already vested remain in the contract.
  55. * @param token ERC20 token which is being vested
  56. */
  57. function revoke(ERC20Basic token) onlyOwner {
  58. require(revocable);
  59. uint256 balance = token.balanceOf(this);
  60. uint256 vested = vestedAmount(token);
  61. token.transfer(owner, balance - vested);
  62. Revoked();
  63. }
  64. /**
  65. * @dev Calculates the amount that has already vested but not released.
  66. * @param token ERC20 token which is being vested
  67. */
  68. function vestedAmount(ERC20Basic token) constant returns (uint256) {
  69. if (now < cliff) {
  70. return 0;
  71. } else if (now >= start + duration) {
  72. return token.balanceOf(this);
  73. } else {
  74. uint256 currentBalance = token.balanceOf(this);
  75. uint256 totalBalance = currentBalance.add(released[token]);
  76. uint256 vested = totalBalance.mul(now - start).div(duration);
  77. uint256 unreleased = vested.sub(released[token]);
  78. // currentBalance can be 0 in case of a revoke
  79. return Math.min256(currentBalance, unreleased);
  80. }
  81. }
  82. }