VestingWallet.sol 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  9. * @title VestingWallet
  10. * @dev This contract handles the vesting of Eth and ERC20 tokens for a given beneficiary. Custody of multiple tokens
  11. * can be given to this contract, which will release the token to the beneficiary following a given vesting schedule.
  12. * The vesting schedule is customizable through the {vestedAmount} function.
  13. *
  14. * Any token transferred to this contract will follow the vesting schedule as if they were locked from the beginning.
  15. * Consequently, if the vesting has already started, any amount of tokens sent to this contract will (at least partly)
  16. * be immediately releasable.
  17. *
  18. * By setting the duration to 0, one can configure this contract to behave like an asset timelock that hold tokens for
  19. * a beneficiary until a specified time.
  20. *
  21. * NOTE: When using this contract with any token whose balance is adjusted automatically (i.e. a rebase token), make sure
  22. * to account the supply/balance adjustment in the vesting schedule to ensure the vested amount is as intended.
  23. */
  24. contract VestingWallet is Context {
  25. event EtherReleased(uint256 amount);
  26. event ERC20Released(address indexed token, uint256 amount);
  27. /**
  28. * @dev The `beneficiary` is not a valid account.
  29. */
  30. error VestingWalletInvalidBeneficiary(address beneficiary);
  31. uint256 private _released;
  32. mapping(address => uint256) private _erc20Released;
  33. address private immutable _beneficiary;
  34. uint64 private immutable _start;
  35. uint64 private immutable _duration;
  36. /**
  37. * @dev Set the beneficiary, start timestamp and vesting duration of the vesting wallet.
  38. */
  39. constructor(address beneficiaryAddress, uint64 startTimestamp, uint64 durationSeconds) payable {
  40. if (beneficiaryAddress == address(0)) {
  41. revert VestingWalletInvalidBeneficiary(address(0));
  42. }
  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 Getter for the end timestamp.
  71. */
  72. function end() public view virtual returns (uint256) {
  73. return start() + duration();
  74. }
  75. /**
  76. * @dev Amount of eth already released
  77. */
  78. function released() public view virtual returns (uint256) {
  79. return _released;
  80. }
  81. /**
  82. * @dev Amount of token already released
  83. */
  84. function released(address token) public view virtual returns (uint256) {
  85. return _erc20Released[token];
  86. }
  87. /**
  88. * @dev Getter for the amount of releasable eth.
  89. */
  90. function releasable() public view virtual returns (uint256) {
  91. return vestedAmount(uint64(block.timestamp)) - released();
  92. }
  93. /**
  94. * @dev Getter for the amount of releasable `token` tokens. `token` should be the address of an
  95. * IERC20 contract.
  96. */
  97. function releasable(address token) public view virtual returns (uint256) {
  98. return vestedAmount(token, uint64(block.timestamp)) - released(token);
  99. }
  100. /**
  101. * @dev Release the native token (ether) that have already vested.
  102. *
  103. * Emits a {EtherReleased} event.
  104. */
  105. function release() public virtual {
  106. uint256 amount = releasable();
  107. _released += amount;
  108. emit EtherReleased(amount);
  109. Address.sendValue(payable(beneficiary()), amount);
  110. }
  111. /**
  112. * @dev Release the tokens that have already vested.
  113. *
  114. * Emits a {ERC20Released} event.
  115. */
  116. function release(address token) public virtual {
  117. uint256 amount = releasable(token);
  118. _erc20Released[token] += amount;
  119. emit ERC20Released(token, amount);
  120. SafeERC20.safeTransfer(IERC20(token), beneficiary(), amount);
  121. }
  122. /**
  123. * @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
  124. */
  125. function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
  126. return _vestingSchedule(address(this).balance + released(), timestamp);
  127. }
  128. /**
  129. * @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
  130. */
  131. function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
  132. return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
  133. }
  134. /**
  135. * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
  136. * an asset given its total historical allocation.
  137. */
  138. function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
  139. if (timestamp < start()) {
  140. return 0;
  141. } else if (timestamp >= end()) {
  142. return totalAllocation;
  143. } else {
  144. return (totalAllocation * (timestamp - start())) / duration();
  145. }
  146. }
  147. }