VestingWallet.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.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 ERC-20 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. * NOTE: Chains with support for native ERC20s may allow the vesting wallet to withdraw the underlying asset as both an
  29. * ERC20 and as native currency. For example, if chain C supports token A and the wallet gets deposited 100 A, then
  30. * at 50% of the vesting period, the beneficiary can withdraw 50 A as ERC20 and 25 A as native currency (totaling 75 A).
  31. * Consider disabling one of the withdrawal methods.
  32. */
  33. contract VestingWallet is Context, Ownable {
  34. event EtherReleased(uint256 amount);
  35. event ERC20Released(address indexed token, uint256 amount);
  36. uint256 private _released;
  37. mapping(address token => uint256) private _erc20Released;
  38. uint64 private immutable _start;
  39. uint64 private immutable _duration;
  40. /**
  41. * @dev Sets the beneficiary (owner), the start timestamp and the vesting duration (in seconds) of the vesting
  42. * wallet.
  43. */
  44. constructor(address beneficiary, uint64 startTimestamp, uint64 durationSeconds) payable Ownable(beneficiary) {
  45. _start = startTimestamp;
  46. _duration = durationSeconds;
  47. }
  48. /**
  49. * @dev The contract should be able to receive Eth.
  50. */
  51. receive() external payable virtual {}
  52. /**
  53. * @dev Getter for the start timestamp.
  54. */
  55. function start() public view virtual returns (uint256) {
  56. return _start;
  57. }
  58. /**
  59. * @dev Getter for the vesting duration.
  60. */
  61. function duration() public view virtual returns (uint256) {
  62. return _duration;
  63. }
  64. /**
  65. * @dev Getter for the end timestamp.
  66. */
  67. function end() public view virtual returns (uint256) {
  68. return start() + duration();
  69. }
  70. /**
  71. * @dev Amount of eth already released
  72. */
  73. function released() public view virtual returns (uint256) {
  74. return _released;
  75. }
  76. /**
  77. * @dev Amount of token already released
  78. */
  79. function released(address token) public view virtual returns (uint256) {
  80. return _erc20Released[token];
  81. }
  82. /**
  83. * @dev Getter for the amount of releasable eth.
  84. */
  85. function releasable() public view virtual returns (uint256) {
  86. return vestedAmount(uint64(block.timestamp)) - released();
  87. }
  88. /**
  89. * @dev Getter for the amount of releasable `token` tokens. `token` should be the address of an
  90. * {IERC20} contract.
  91. */
  92. function releasable(address token) public view virtual returns (uint256) {
  93. return vestedAmount(token, uint64(block.timestamp)) - released(token);
  94. }
  95. /**
  96. * @dev Release the native token (ether) that have already vested.
  97. *
  98. * Emits a {EtherReleased} event.
  99. */
  100. function release() public virtual {
  101. uint256 amount = releasable();
  102. _released += amount;
  103. emit EtherReleased(amount);
  104. Address.sendValue(payable(owner()), amount);
  105. }
  106. /**
  107. * @dev Release the tokens that have already vested.
  108. *
  109. * Emits a {ERC20Released} event.
  110. */
  111. function release(address token) public virtual {
  112. uint256 amount = releasable(token);
  113. _erc20Released[token] += amount;
  114. emit ERC20Released(token, amount);
  115. SafeERC20.safeTransfer(IERC20(token), owner(), amount);
  116. }
  117. /**
  118. * @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
  119. */
  120. function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
  121. return _vestingSchedule(address(this).balance + released(), timestamp);
  122. }
  123. /**
  124. * @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
  125. */
  126. function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
  127. return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
  128. }
  129. /**
  130. * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
  131. * an asset given its total historical allocation.
  132. */
  133. function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
  134. if (timestamp < start()) {
  135. return 0;
  136. } else if (timestamp >= end()) {
  137. return totalAllocation;
  138. } else {
  139. return (totalAllocation * (timestamp - start())) / duration();
  140. }
  141. }
  142. }