VestingWallet.sol 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (finance/VestingWallet.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC20} from "../token/ERC20/IERC20.sol";
  5. import {SafeERC20} from "../token/ERC20/utils/SafeERC20.sol";
  6. import {Address} from "../utils/Address.sol";
  7. import {Context} from "../utils/Context.sol";
  8. import {Ownable} from "../access/Ownable.sol";
  9. /**
  10. * @dev A vesting wallet is an ownable contract that can receive native currency and ERC20 tokens, and release these
  11. * assets to the wallet owner, also referred to as "beneficiary", according to a vesting schedule.
  12. *
  13. * Any assets transferred to this contract will follow the vesting schedule as if they were locked from the beginning.
  14. * Consequently, if the vesting has already started, any amount of tokens sent to this contract will (at least partly)
  15. * be immediately releasable.
  16. *
  17. * By setting the duration to 0, one can configure this contract to behave like an asset timelock that hold tokens for
  18. * a beneficiary until a specified time.
  19. *
  20. * NOTE: Since the wallet is {Ownable}, and ownership can be transferred, it is possible to sell unvested tokens.
  21. * Preventing this in a smart contract is difficult, considering that: 1) a beneficiary address could be a
  22. * counterfactually deployed contract, 2) there is likely to be a migration path for EOAs to become contracts in the
  23. * near future.
  24. *
  25. * NOTE: When using this contract with any token whose balance is adjusted automatically (i.e. a rebase token), make
  26. * sure to account the supply/balance adjustment in the vesting schedule to ensure the vested amount is as intended.
  27. */
  28. contract VestingWallet is Context, Ownable {
  29. event EtherReleased(uint256 amount);
  30. event ERC20Released(address indexed token, uint256 amount);
  31. uint256 private _released;
  32. mapping(address token => uint256) private _erc20Released;
  33. uint64 private immutable _start;
  34. uint64 private immutable _duration;
  35. /**
  36. * @dev Sets the sender as the initial owner, the beneficiary as the pending owner, the start timestamp and the
  37. * vesting duration of the vesting wallet.
  38. */
  39. constructor(address beneficiary, uint64 startTimestamp, uint64 durationSeconds) payable Ownable(beneficiary) {
  40. _start = startTimestamp;
  41. _duration = durationSeconds;
  42. }
  43. /**
  44. * @dev The contract should be able to receive Eth.
  45. */
  46. receive() external payable virtual {}
  47. /**
  48. * @dev Getter for the start timestamp.
  49. */
  50. function start() public view virtual returns (uint256) {
  51. return _start;
  52. }
  53. /**
  54. * @dev Getter for the vesting duration.
  55. */
  56. function duration() public view virtual returns (uint256) {
  57. return _duration;
  58. }
  59. /**
  60. * @dev Getter for the end timestamp.
  61. */
  62. function end() public view virtual returns (uint256) {
  63. return start() + duration();
  64. }
  65. /**
  66. * @dev Amount of eth already released
  67. */
  68. function released() public view virtual returns (uint256) {
  69. return _released;
  70. }
  71. /**
  72. * @dev Amount of token already released
  73. */
  74. function released(address token) public view virtual returns (uint256) {
  75. return _erc20Released[token];
  76. }
  77. /**
  78. * @dev Getter for the amount of releasable eth.
  79. */
  80. function releasable() public view virtual returns (uint256) {
  81. return vestedAmount(uint64(block.timestamp)) - released();
  82. }
  83. /**
  84. * @dev Getter for the amount of releasable `token` tokens. `token` should be the address of an
  85. * IERC20 contract.
  86. */
  87. function releasable(address token) public view virtual returns (uint256) {
  88. return vestedAmount(token, uint64(block.timestamp)) - released(token);
  89. }
  90. /**
  91. * @dev Release the native token (ether) that have already vested.
  92. *
  93. * Emits a {EtherReleased} event.
  94. */
  95. function release() public virtual {
  96. uint256 amount = releasable();
  97. _released += amount;
  98. emit EtherReleased(amount);
  99. Address.sendValue(payable(owner()), amount);
  100. }
  101. /**
  102. * @dev Release the tokens that have already vested.
  103. *
  104. * Emits a {ERC20Released} event.
  105. */
  106. function release(address token) public virtual {
  107. uint256 amount = releasable(token);
  108. _erc20Released[token] += amount;
  109. emit ERC20Released(token, amount);
  110. SafeERC20.safeTransfer(IERC20(token), owner(), amount);
  111. }
  112. /**
  113. * @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
  114. */
  115. function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
  116. return _vestingSchedule(address(this).balance + released(), timestamp);
  117. }
  118. /**
  119. * @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
  120. */
  121. function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
  122. return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
  123. }
  124. /**
  125. * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
  126. * an asset given its total historical allocation.
  127. */
  128. function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
  129. if (timestamp < start()) {
  130. return 0;
  131. } else if (timestamp >= end()) {
  132. return totalAllocation;
  133. } else {
  134. return (totalAllocation * (timestamp - start())) / duration();
  135. }
  136. }
  137. }