TokenVesting.sol 3.4 KB

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