|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Based on VestingWallet from OpenZeppelin Contracts (last updated v4.8.0) (finance/VestingWallet.sol) |
| 3 | +pragma solidity 0.8.17; |
| 4 | + |
| 5 | +import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 6 | +import "@openzeppelin/contracts/utils/Address.sol"; |
| 7 | +import "@openzeppelin/contracts/utils/Context.sol"; |
| 8 | +import "@openzeppelin/contracts/utils/math/Math.sol"; |
| 9 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 10 | + |
| 11 | +import "./interfaces/IMultiVestingWallet.sol"; |
| 12 | +import "./EmergencyGuard.sol"; |
| 13 | + |
| 14 | +import "hardhat/console.sol"; |
| 15 | + |
| 16 | +contract MultiVestingWallet is |
| 17 | + IMultiVestingWallet, |
| 18 | + Context, |
| 19 | + Ownable, |
| 20 | + EmergencyGuard |
| 21 | +{ |
| 22 | + uint64 private immutable _start; |
| 23 | + uint64 private immutable _duration; |
| 24 | + |
| 25 | + // Total initial ETH amount |
| 26 | + uint256 private _totalInitialETH; |
| 27 | + |
| 28 | + // Mapping from token to total initial token amount |
| 29 | + mapping(address => uint256) private _totalInitialToken; |
| 30 | + |
| 31 | + // Total released token amount |
| 32 | + uint256 private _totalReleasedETH; |
| 33 | + |
| 34 | + // Mapping from ttoken to total released token amount |
| 35 | + mapping(address => uint256) private _totalReleasedToken; |
| 36 | + |
| 37 | + // Mapping from beneficiary address to initial ETH |
| 38 | + mapping(address => uint256) private _userInitialETH; |
| 39 | + |
| 40 | + // Mapping from beneficiary address to release ETH |
| 41 | + mapping(address => uint256) private _userReleaseETH; |
| 42 | + |
| 43 | + // Mapping from beneficiary address to token address to initial token |
| 44 | + mapping(address => mapping(address => uint256)) private _userInitialToken; |
| 45 | + |
| 46 | + // Mapping from beneficiary address to token address to release token |
| 47 | + mapping(address => mapping(address => uint256)) private _userReleasedToken; |
| 48 | + |
| 49 | + /** |
| 50 | + * @dev Set the start timestamp and vesting duration of the vesting wallet. |
| 51 | + */ |
| 52 | + constructor(uint64 startTimestamp, uint64 durationSeconds) payable { |
| 53 | + _start = startTimestamp; |
| 54 | + _duration = durationSeconds; |
| 55 | + } |
| 56 | + |
| 57 | + receive() external payable virtual {} |
| 58 | + |
| 59 | + function addBeneficiaries( |
| 60 | + address[] calldata beneficiaries, |
| 61 | + uint256[] calldata amounts |
| 62 | + ) external virtual onlyOwner { |
| 63 | + require( |
| 64 | + beneficiaries.length == amounts.length, |
| 65 | + "MultiVestingWallet: mismatching beneficiaries / amounts pair" |
| 66 | + ); |
| 67 | + |
| 68 | + for (uint256 i = 0; i < beneficiaries.length; i++) { |
| 69 | + addBeneficiary(beneficiaries[i], amounts[i]); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + function addBeneficiaries( |
| 74 | + address[] calldata beneficiaries, |
| 75 | + address token, |
| 76 | + uint256[] calldata amounts |
| 77 | + ) external virtual onlyOwner { |
| 78 | + require( |
| 79 | + beneficiaries.length == amounts.length, |
| 80 | + "MultiVestingWallet: mismatching beneficiaries / amounts pair" |
| 81 | + ); |
| 82 | + |
| 83 | + for (uint256 i = 0; i < beneficiaries.length; i++) { |
| 84 | + addBeneficiary(beneficiaries[i], token, amounts[i]); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + function addBeneficiary( |
| 89 | + address beneficiary, |
| 90 | + uint256 amount |
| 91 | + ) public virtual onlyOwner { |
| 92 | + require( |
| 93 | + address(this).balance + _totalReleasedETH - _totalInitialETH >= |
| 94 | + amount, |
| 95 | + "MultiVestingWallet: ETH amount exceeds balance" |
| 96 | + ); |
| 97 | + |
| 98 | + _userInitialETH[beneficiary] = amount; |
| 99 | + _totalInitialETH += amount; |
| 100 | + } |
| 101 | + |
| 102 | + function addBeneficiary( |
| 103 | + address beneficiary, |
| 104 | + address token, |
| 105 | + uint256 amount |
| 106 | + ) public virtual onlyOwner { |
| 107 | + require( |
| 108 | + IERC20(token).balanceOf(address(this)) + |
| 109 | + _totalReleasedToken[token] - |
| 110 | + _totalInitialToken[token] >= |
| 111 | + amount, |
| 112 | + "MultiVestingWallet: Token amount exceeds balance" |
| 113 | + ); |
| 114 | + |
| 115 | + _userInitialToken[beneficiary][token] = amount; |
| 116 | + _totalInitialToken[token] += amount; |
| 117 | + } |
| 118 | + |
| 119 | + function start() public view virtual returns (uint256) { |
| 120 | + return _start; |
| 121 | + } |
| 122 | + |
| 123 | + function duration() public view virtual returns (uint256) { |
| 124 | + return _duration; |
| 125 | + } |
| 126 | + |
| 127 | + function initial( |
| 128 | + address beneficiary |
| 129 | + ) public view virtual returns (uint256) { |
| 130 | + return _userInitialETH[beneficiary]; |
| 131 | + } |
| 132 | + |
| 133 | + function initial( |
| 134 | + address beneficiary, |
| 135 | + address token |
| 136 | + ) public view virtual returns (uint256) { |
| 137 | + return _userInitialToken[beneficiary][token]; |
| 138 | + } |
| 139 | + |
| 140 | + function released( |
| 141 | + address beneficiary |
| 142 | + ) public view virtual returns (uint256) { |
| 143 | + return _userReleaseETH[beneficiary]; |
| 144 | + } |
| 145 | + |
| 146 | + function released( |
| 147 | + address beneficiary, |
| 148 | + address token |
| 149 | + ) public view virtual returns (uint256) { |
| 150 | + return _userReleasedToken[beneficiary][token]; |
| 151 | + } |
| 152 | + |
| 153 | + function releasable( |
| 154 | + address beneficiary |
| 155 | + ) public view virtual returns (uint256) { |
| 156 | + return |
| 157 | + vestedAmount(beneficiary, uint64(block.timestamp)) - |
| 158 | + released(beneficiary); |
| 159 | + } |
| 160 | + |
| 161 | + function releasable( |
| 162 | + address beneficiary, |
| 163 | + address token |
| 164 | + ) public view virtual returns (uint256) { |
| 165 | + return |
| 166 | + vestedAmount(beneficiary, token, uint64(block.timestamp)) - |
| 167 | + released(beneficiary, token); |
| 168 | + } |
| 169 | + |
| 170 | + function release(address beneficiary) public virtual { |
| 171 | + uint256 amount = releasable(beneficiary); |
| 172 | + _userReleaseETH[beneficiary] += amount; |
| 173 | + _totalReleasedETH += amount; |
| 174 | + emit EtherReleased(beneficiary, amount); |
| 175 | + Address.sendValue(payable(beneficiary), amount); |
| 176 | + } |
| 177 | + |
| 178 | + function release(address beneficiary, address token) public virtual { |
| 179 | + uint256 amount = releasable(beneficiary, token); |
| 180 | + _userReleasedToken[beneficiary][token] += amount; |
| 181 | + _totalReleasedToken[token] += amount; |
| 182 | + emit ERC20Released(beneficiary, token, amount); |
| 183 | + SafeERC20.safeTransfer(IERC20(token), beneficiary, amount); |
| 184 | + } |
| 185 | + |
| 186 | + function vestedAmount( |
| 187 | + address beneficiary, |
| 188 | + uint64 timestamp |
| 189 | + ) public view virtual returns (uint256) { |
| 190 | + return _vestingSchedule(_userInitialETH[beneficiary], timestamp); |
| 191 | + } |
| 192 | + |
| 193 | + function vestedAmount( |
| 194 | + address beneficiary, |
| 195 | + address token, |
| 196 | + uint64 timestamp |
| 197 | + ) public view virtual returns (uint256) { |
| 198 | + return |
| 199 | + _vestingSchedule(_userInitialToken[beneficiary][token], timestamp); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for |
| 204 | + * an asset given its total historical allocation. |
| 205 | + */ |
| 206 | + function _vestingSchedule( |
| 207 | + uint256 totalAllocation, |
| 208 | + uint64 timestamp |
| 209 | + ) internal view virtual returns (uint256) { |
| 210 | + if (timestamp < start()) { |
| 211 | + return 0; |
| 212 | + } else if (timestamp > start() + duration()) { |
| 213 | + return totalAllocation; |
| 214 | + } else { |
| 215 | + return (totalAllocation * (timestamp - start())) / duration(); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + function emergencyWithdraw(uint256 amount) external override onlyOwner { |
| 220 | + super._emergencyWithdraw(amount); |
| 221 | + } |
| 222 | + |
| 223 | + function emergencyWithdrawToken( |
| 224 | + address token, |
| 225 | + uint256 amount |
| 226 | + ) external override onlyOwner { |
| 227 | + super._emergencyWithdrawToken(token, amount); |
| 228 | + } |
| 229 | +} |
0 commit comments