Vesting.sol 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
  4. import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
  5. import "@openzeppelin/contracts/math/Math.sol";
  6. import "@openzeppelin/contracts/math/SafeMath.sol";
  7. import "./ENS.sol";
  8. /**
  9. * @title Vesting
  10. * @dev A token holder contract that can release its token balance gradually like a
  11. * typical vesting scheme, with a cliff and vesting period. Optionally revocable by the
  12. * owner.
  13. */
  14. contract Vesting is EnsResolve {
  15. using SafeERC20 for IERC20;
  16. using SafeMath for uint256;
  17. uint256 public constant SECONDS_PER_MONTH = 30 days;
  18. event Released(uint256 amount);
  19. // beneficiary of tokens after they are released
  20. address public immutable beneficiary;
  21. IERC20 public immutable token;
  22. uint256 public immutable cliffInMonths;
  23. uint256 public immutable startTimestamp;
  24. uint256 public immutable durationInMonths;
  25. uint256 public released;
  26. /**
  27. * @dev Creates a vesting contract that vests its balance of any ERC20 token to the
  28. * _beneficiary, monthly in a linear fashion until duration has passed. 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 _cliffInMonths duration in months of the cliff in which tokens will begin to vest
  32. * @param _durationInMonths duration in months of the period in which the tokens will vest
  33. */
  34. constructor(
  35. bytes32 _token,
  36. address _beneficiary,
  37. uint256 _startTimestamp,
  38. uint256 _cliffInMonths,
  39. uint256 _durationInMonths
  40. ) public {
  41. require(_beneficiary != address(0), "Beneficiary cannot be empty");
  42. require(_cliffInMonths <= _durationInMonths, "Cliff is greater than duration");
  43. token = IERC20(resolve(_token));
  44. beneficiary = _beneficiary;
  45. durationInMonths = _durationInMonths;
  46. cliffInMonths = _cliffInMonths;
  47. startTimestamp = _startTimestamp == 0 ? blockTimestamp() : _startTimestamp;
  48. }
  49. /**
  50. * @notice Transfers vested tokens to beneficiary.
  51. */
  52. function release() external {
  53. uint256 vested = vestedAmount();
  54. require(vested > 0, "No tokens to release");
  55. released = released.add(vested);
  56. token.safeTransfer(beneficiary, vested);
  57. emit Released(vested);
  58. }
  59. /**
  60. * @dev Calculates the amount that has already vested but hasn't been released yet.
  61. */
  62. function vestedAmount() public view returns (uint256) {
  63. if (blockTimestamp() < startTimestamp) {
  64. return 0;
  65. }
  66. uint256 elapsedTime = blockTimestamp().sub(startTimestamp);
  67. uint256 elapsedMonths = elapsedTime.div(SECONDS_PER_MONTH);
  68. if (elapsedMonths < cliffInMonths) {
  69. return 0;
  70. }
  71. // If over vesting duration, all tokens vested
  72. if (elapsedMonths >= durationInMonths) {
  73. return token.balanceOf(address(this));
  74. } else {
  75. uint256 currentBalance = token.balanceOf(address(this));
  76. uint256 totalBalance = currentBalance.add(released);
  77. uint256 vested = totalBalance.mul(elapsedMonths).div(durationInMonths);
  78. uint256 unreleased = vested.sub(released);
  79. // currentBalance can be 0 in case of vesting being revoked earlier.
  80. return Math.min(currentBalance, unreleased);
  81. }
  82. }
  83. function blockTimestamp() public view virtual returns (uint256) {
  84. return block.timestamp;
  85. }
  86. }