TokenVesting.sol 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Revokable by the owner.
  10. */
  11. contract TokenVesting is Ownable {
  12. using SafeMath for uint256;
  13. // beneficiary of tokens after they are released
  14. address beneficiary;
  15. uint256 cliff;
  16. uint256 start;
  17. uint256 end;
  18. mapping (address => uint256) released;
  19. /**
  20. * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
  21. * _beneficiary, gradually in a linear fashion until _end. By then all of the balance
  22. * will have vested.
  23. * @param _beneficiary address of the beneficiary to whom vested tokens are transferred
  24. * @param _cliff timestamp of the moment when tokens will begin to vest
  25. * @param _end timestamp of the moment when all balance will have been vested
  26. */
  27. function TokenVesting(address _beneficiary, uint256 _cliff, uint256 _end) {
  28. beneficiary = _beneficiary;
  29. cliff = _cliff;
  30. end = _end;
  31. start = now;
  32. }
  33. /**
  34. * @notice Transfers vested tokens to beneficiary.
  35. * @param token ERC20 token which is being vested
  36. */
  37. function release(ERC20Basic token) {
  38. uint256 vested = vestedAmount(token);
  39. require(vested > 0);
  40. token.transfer(beneficiary, vested);
  41. released[token] = released[token].add(vested);
  42. }
  43. /**
  44. * @notice Allows the owner to revoke the vesting. Tokens already vested remain in the contract.
  45. * @param token ERC20 token which is being vested
  46. */
  47. function revoke(ERC20Basic token) onlyOwner {
  48. uint256 balance = token.balanceOf(this);
  49. uint256 vested = vestedAmount(token);
  50. token.transfer(owner, balance - vested);
  51. }
  52. /**
  53. * @dev Calculates the amount that has already vested.
  54. * @param token ERC20 token which is being vested
  55. */
  56. function vestedAmount(ERC20Basic token) constant returns (uint256) {
  57. if (now < cliff) {
  58. return 0;
  59. } else {
  60. uint256 currentBalance = token.balanceOf(this);
  61. uint256 totalBalance = currentBalance.add(released[token]);
  62. uint256 vested = totalBalance.mul(now - start).div(end - start);
  63. return Math.min256(currentBalance, vested);
  64. }
  65. }
  66. }