123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v4.9.0) (finance/VestingWallet.sol)
- pragma solidity ^0.8.20;
- import {IERC20} from "../token/ERC20/IERC20.sol";
- import {SafeERC20} from "../token/ERC20/utils/SafeERC20.sol";
- import {Address} from "../utils/Address.sol";
- import {Context} from "../utils/Context.sol";
- /**
- * @title VestingWallet
- * @dev This contract handles the vesting of Eth and ERC20 tokens for a given beneficiary. Custody of multiple tokens
- * can be given to this contract, which will release the token to the beneficiary following a given vesting schedule.
- * The vesting schedule is customizable through the {vestedAmount} function.
- *
- * Any token transferred to this contract will follow the vesting schedule as if they were locked from the beginning.
- * Consequently, if the vesting has already started, any amount of tokens sent to this contract will (at least partly)
- * be immediately releasable.
- *
- * By setting the duration to 0, one can configure this contract to behave like an asset timelock that hold tokens for
- * a beneficiary until a specified time.
- */
- contract VestingWallet is Context {
- event EtherReleased(uint256 amount);
- event ERC20Released(address indexed token, uint256 amount);
- /**
- * @dev The `beneficiary` is not a valid account.
- */
- error VestingWalletInvalidBeneficiary(address beneficiary);
- uint256 private _released;
- mapping(address => uint256) private _erc20Released;
- address private immutable _beneficiary;
- uint64 private immutable _start;
- uint64 private immutable _duration;
- /**
- * @dev Set the beneficiary, start timestamp and vesting duration of the vesting wallet.
- */
- constructor(address beneficiaryAddress, uint64 startTimestamp, uint64 durationSeconds) payable {
- if (beneficiaryAddress == address(0)) {
- revert VestingWalletInvalidBeneficiary(address(0));
- }
- _beneficiary = beneficiaryAddress;
- _start = startTimestamp;
- _duration = durationSeconds;
- }
- /**
- * @dev The contract should be able to receive Eth.
- */
- receive() external payable virtual {}
- /**
- * @dev Getter for the beneficiary address.
- */
- function beneficiary() public view virtual returns (address) {
- return _beneficiary;
- }
- /**
- * @dev Getter for the start timestamp.
- */
- function start() public view virtual returns (uint256) {
- return _start;
- }
- /**
- * @dev Getter for the vesting duration.
- */
- function duration() public view virtual returns (uint256) {
- return _duration;
- }
- /**
- * @dev Getter for the end timestamp.
- */
- function end() public view virtual returns (uint256) {
- return start() + duration();
- }
- /**
- * @dev Amount of eth already released
- */
- function released() public view virtual returns (uint256) {
- return _released;
- }
- /**
- * @dev Amount of token already released
- */
- function released(address token) public view virtual returns (uint256) {
- return _erc20Released[token];
- }
- /**
- * @dev Getter for the amount of releasable eth.
- */
- function releasable() public view virtual returns (uint256) {
- return vestedAmount(uint64(block.timestamp)) - released();
- }
- /**
- * @dev Getter for the amount of releasable `token` tokens. `token` should be the address of an
- * IERC20 contract.
- */
- function releasable(address token) public view virtual returns (uint256) {
- return vestedAmount(token, uint64(block.timestamp)) - released(token);
- }
- /**
- * @dev Release the native token (ether) that have already vested.
- *
- * Emits a {EtherReleased} event.
- */
- function release() public virtual {
- uint256 amount = releasable();
- _released += amount;
- emit EtherReleased(amount);
- Address.sendValue(payable(beneficiary()), amount);
- }
- /**
- * @dev Release the tokens that have already vested.
- *
- * Emits a {ERC20Released} event.
- */
- function release(address token) public virtual {
- uint256 amount = releasable(token);
- _erc20Released[token] += amount;
- emit ERC20Released(token, amount);
- SafeERC20.safeTransfer(IERC20(token), beneficiary(), amount);
- }
- /**
- * @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
- */
- function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
- return _vestingSchedule(address(this).balance + released(), timestamp);
- }
- /**
- * @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
- */
- function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
- return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
- }
- /**
- * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
- * an asset given its total historical allocation.
- */
- function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
- if (timestamp < start()) {
- return 0;
- } else if (timestamp > end()) {
- return totalAllocation;
- } else {
- return (totalAllocation * (timestamp - start())) / duration();
- }
- }
- }
|