VestingWalletUpgradeable.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (finance/VestingWallet.sol)
  3. pragma solidity ^0.8.0;
  4. import "../token/ERC20/utils/SafeERC20Upgradeable.sol";
  5. import "../utils/AddressUpgradeable.sol";
  6. import "../utils/ContextUpgradeable.sol";
  7. import "../utils/math/MathUpgradeable.sol";
  8. import "../proxy/utils/Initializable.sol";
  9. /**
  10. * @title VestingWallet
  11. * @dev This contract handles the vesting of Eth and ERC20 tokens for a given beneficiary. Custody of multiple tokens
  12. * can be given to this contract, which will release the token to the beneficiary following a given vesting schedule.
  13. * The vesting schedule is customizable through the {vestedAmount} function.
  14. *
  15. * Any token transferred to this contract will follow the vesting schedule as if they were locked from the beginning.
  16. * Consequently, if the vesting has already started, any amount of tokens sent to this contract will (at least partly)
  17. * be immediately releasable.
  18. */
  19. contract VestingWalletUpgradeable is Initializable, ContextUpgradeable {
  20. event EtherReleased(uint256 amount);
  21. event ERC20Released(address indexed token, uint256 amount);
  22. uint256 private _released;
  23. mapping(address => uint256) private _erc20Released;
  24. address private _beneficiary;
  25. uint64 private _start;
  26. uint64 private _duration;
  27. /**
  28. * @dev Set the beneficiary, start timestamp and vesting duration of the vesting wallet.
  29. */
  30. function __VestingWallet_init(
  31. address beneficiaryAddress,
  32. uint64 startTimestamp,
  33. uint64 durationSeconds
  34. ) internal onlyInitializing {
  35. __VestingWallet_init_unchained(beneficiaryAddress, startTimestamp, durationSeconds);
  36. }
  37. function __VestingWallet_init_unchained(
  38. address beneficiaryAddress,
  39. uint64 startTimestamp,
  40. uint64 durationSeconds
  41. ) internal onlyInitializing {
  42. require(beneficiaryAddress != address(0), "VestingWallet: beneficiary is zero address");
  43. _beneficiary = beneficiaryAddress;
  44. _start = startTimestamp;
  45. _duration = durationSeconds;
  46. }
  47. /**
  48. * @dev The contract should be able to receive Eth.
  49. */
  50. receive() external payable virtual {}
  51. /**
  52. * @dev Getter for the beneficiary address.
  53. */
  54. function beneficiary() public view virtual returns (address) {
  55. return _beneficiary;
  56. }
  57. /**
  58. * @dev Getter for the start timestamp.
  59. */
  60. function start() public view virtual returns (uint256) {
  61. return _start;
  62. }
  63. /**
  64. * @dev Getter for the vesting duration.
  65. */
  66. function duration() public view virtual returns (uint256) {
  67. return _duration;
  68. }
  69. /**
  70. * @dev Amount of eth already released
  71. */
  72. function released() public view virtual returns (uint256) {
  73. return _released;
  74. }
  75. /**
  76. * @dev Amount of token already released
  77. */
  78. function released(address token) public view virtual returns (uint256) {
  79. return _erc20Released[token];
  80. }
  81. /**
  82. * @dev Release the native token (ether) that have already vested.
  83. *
  84. * Emits a {TokensReleased} event.
  85. */
  86. function release() public virtual {
  87. uint256 releasable = vestedAmount(uint64(block.timestamp)) - released();
  88. _released += releasable;
  89. emit EtherReleased(releasable);
  90. AddressUpgradeable.sendValue(payable(beneficiary()), releasable);
  91. }
  92. /**
  93. * @dev Release the tokens that have already vested.
  94. *
  95. * Emits a {TokensReleased} event.
  96. */
  97. function release(address token) public virtual {
  98. uint256 releasable = vestedAmount(token, uint64(block.timestamp)) - released(token);
  99. _erc20Released[token] += releasable;
  100. emit ERC20Released(token, releasable);
  101. SafeERC20Upgradeable.safeTransfer(IERC20Upgradeable(token), beneficiary(), releasable);
  102. }
  103. /**
  104. * @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
  105. */
  106. function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
  107. return _vestingSchedule(address(this).balance + released(), timestamp);
  108. }
  109. /**
  110. * @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
  111. */
  112. function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
  113. return _vestingSchedule(IERC20Upgradeable(token).balanceOf(address(this)) + released(token), timestamp);
  114. }
  115. /**
  116. * @dev Virtual implementation of the vesting formula. This returns the amout vested, as a function of time, for
  117. * an asset given its total historical allocation.
  118. */
  119. function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
  120. if (timestamp < start()) {
  121. return 0;
  122. } else if (timestamp > start() + duration()) {
  123. return totalAllocation;
  124. } else {
  125. return (totalAllocation * (timestamp - start())) / duration();
  126. }
  127. }
  128. /**
  129. * This empty reserved space is put in place to allow future versions to add new
  130. * variables without shifting down storage in the inheritance chain.
  131. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  132. */
  133. uint256[48] private __gap;
  134. }