TokenVesting.sol 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. pragma solidity ^0.4.11;
  2. import './ERC20Basic.sol';
  3. import './SafeERC20.sol';
  4. import '../ownership/Ownable.sol';
  5. import '../math/Math.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. function TokenVesting(address _beneficiary, uint256 _start, uint256 _cliff, uint256 _duration, bool _revocable) {
  36. require(_beneficiary != address(0));
  37. require(_cliff <= _duration);
  38. beneficiary = _beneficiary;
  39. revocable = _revocable;
  40. duration = _duration;
  41. cliff = _start.add(_cliff);
  42. start = _start;
  43. }
  44. /**
  45. * @notice Transfers vested tokens to beneficiary.
  46. * @param token ERC20 token which is being vested
  47. */
  48. function release(ERC20Basic token) public {
  49. uint256 unreleased = releasableAmount(token);
  50. require(unreleased > 0);
  51. released[token] = released[token].add(unreleased);
  52. token.safeTransfer(beneficiary, unreleased);
  53. Released(unreleased);
  54. }
  55. /**
  56. * @notice Allows the owner to revoke the vesting. Tokens already vested
  57. * remain in the contract, the rest are returned to the owner.
  58. * @param token ERC20 token which is being vested
  59. */
  60. function revoke(ERC20Basic token) public onlyOwner {
  61. require(revocable);
  62. require(!revoked[token]);
  63. uint256 balance = token.balanceOf(this);
  64. uint256 unreleased = releasableAmount(token);
  65. uint256 refund = balance.sub(unreleased);
  66. revoked[token] = true;
  67. token.safeTransfer(owner, refund);
  68. Revoked();
  69. }
  70. /**
  71. * @dev Calculates the amount that has already vested but hasn't been released yet.
  72. * @param token ERC20 token which is being vested
  73. */
  74. function releasableAmount(ERC20Basic token) public constant returns (uint256) {
  75. return vestedAmount(token).sub(released[token]);
  76. }
  77. /**
  78. * @dev Calculates the amount that has already vested.
  79. * @param token ERC20 token which is being vested
  80. */
  81. function vestedAmount(ERC20Basic token) public constant returns (uint256) {
  82. uint256 currentBalance = token.balanceOf(this);
  83. uint256 totalBalance = currentBalance.add(released[token]);
  84. if (now < cliff) {
  85. return 0;
  86. } else if (now >= start.add(duration) || revoked[token]) {
  87. return totalBalance;
  88. } else {
  89. return totalBalance.mul(now.sub(start)).div(duration);
  90. }
  91. }
  92. }