|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {FHE, ebool, euint64} from "@fhevm/solidity/lib/FHE.sol"; |
| 5 | +import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; |
| 6 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 7 | +import {ReentrancyGuardTransient} from "@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol"; |
| 8 | +import {IConfidentialFungibleToken} from "./../interfaces/IConfidentialFungibleToken.sol"; |
| 9 | +import {TFHESafeMath} from "./../utils/TFHESafeMath.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @dev A vesting wallet is an ownable contract that can receive ConfidentialFungibleTokens, and release these |
| 13 | + * assets to the wallet owner, also referred to as "beneficiary", according to a vesting schedule. |
| 14 | + * |
| 15 | + * Any assets transferred to this contract will follow the vesting schedule as if they were locked from the beginning. |
| 16 | + * Consequently, if the vesting has already started, any amount of tokens sent to this contract will (at least partly) |
| 17 | + * be immediately releasable. |
| 18 | + * |
| 19 | + * By setting the duration to 0, one can configure this contract to behave like an asset timelock that holds tokens for |
| 20 | + * a beneficiary until a specified time. |
| 21 | + * |
| 22 | + * NOTE: Since the wallet is `Ownable`, and ownership can be transferred, it is possible to sell unvested tokens. |
| 23 | + * |
| 24 | + * NOTE: When using this contract with any token whose balance is adjusted automatically (i.e. a rebase token), make |
| 25 | + * sure to account the supply/balance adjustment in the vesting schedule to ensure the vested amount is as intended. |
| 26 | + * |
| 27 | + * WARNING: The aggregate value of a single token sent to the vesting wallet must not exceed `type(uint64).max`. Sending |
| 28 | + * in excess of this value will result in unexpected behavior and may result in loss of funds. |
| 29 | + */ |
| 30 | +abstract contract VestingWalletConfidential is OwnableUpgradeable, ReentrancyGuardTransient { |
| 31 | + /// @custom:storage-location erc7201:openzeppelin.storage.VestingWalletConfidential |
| 32 | + struct VestingWalletStorage { |
| 33 | + mapping(address token => euint64) _tokenReleased; |
| 34 | + uint64 _start; |
| 35 | + uint64 _duration; |
| 36 | + } |
| 37 | + |
| 38 | + // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.VestingWalletConfidential")) - 1)) & ~bytes32(uint256(0xff)) |
| 39 | + // solhint-disable-next-line const-name-snakecase |
| 40 | + bytes32 private constant VestingWalletStorageLocation = |
| 41 | + 0x78ce9ee9eb65fa0cf5bf10e861c3a95cb7c3c713c96ab1e5323a21e846796800; |
| 42 | + |
| 43 | + function _getVestingWalletStorage() private pure returns (VestingWalletStorage storage $) { |
| 44 | + assembly { |
| 45 | + $.slot := VestingWalletStorageLocation |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + event VestingWalletConfidentialTokenReleased(address indexed token, euint64 amount); |
| 50 | + |
| 51 | + error VestingWalletConfidentialInvalidDuration(); |
| 52 | + |
| 53 | + /** |
| 54 | + * @dev Initializes the vesting wallet for a given `beneficiary` with a start time of `startTimestamp` |
| 55 | + * and an end time of `startTimestamp + durationSeconds`. |
| 56 | + */ |
| 57 | + // solhint-disable-next-line func-name-mixedcase |
| 58 | + function __VestingWalletConfidential_init( |
| 59 | + address beneficiary, |
| 60 | + uint48 startTimestamp, |
| 61 | + uint48 durationSeconds |
| 62 | + ) internal onlyInitializing { |
| 63 | + __Ownable_init(beneficiary); |
| 64 | + VestingWalletStorage storage $ = _getVestingWalletStorage(); |
| 65 | + $._start = startTimestamp; |
| 66 | + $._duration = durationSeconds; |
| 67 | + } |
| 68 | + |
| 69 | + /// @dev Timestamp at which the vesting starts. |
| 70 | + function start() public view virtual returns (uint64) { |
| 71 | + return _getVestingWalletStorage()._start; |
| 72 | + } |
| 73 | + |
| 74 | + /// @dev Duration of the vesting in seconds. |
| 75 | + function duration() public view virtual returns (uint64) { |
| 76 | + return _getVestingWalletStorage()._duration; |
| 77 | + } |
| 78 | + |
| 79 | + /// @dev Timestamp at which the vesting ends. |
| 80 | + function end() public view virtual returns (uint64) { |
| 81 | + return start() + duration(); |
| 82 | + } |
| 83 | + |
| 84 | + /// @dev Amount of token already released |
| 85 | + function released(address token) public view virtual returns (euint64) { |
| 86 | + return _getVestingWalletStorage()._tokenReleased[token]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @dev Getter for the amount of releasable `token` tokens. `token` should be the address of an |
| 91 | + * {IConfidentialFungibleToken} contract. |
| 92 | + */ |
| 93 | + function releasable(address token) public virtual returns (euint64) { |
| 94 | + return FHE.sub(vestedAmount(token, uint64(block.timestamp)), released(token)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @dev Release the tokens that have already vested. |
| 99 | + * |
| 100 | + * Emits a {VestingWalletConfidentialTokenReleased} event. |
| 101 | + */ |
| 102 | + function release(address token) public virtual nonReentrant { |
| 103 | + euint64 amount = releasable(token); |
| 104 | + FHE.allowTransient(amount, token); |
| 105 | + euint64 amountSent = IConfidentialFungibleToken(token).confidentialTransfer(owner(), amount); |
| 106 | + |
| 107 | + euint64 newReleasedAmount = FHE.add(released(token), amountSent); |
| 108 | + FHE.allow(newReleasedAmount, owner()); |
| 109 | + FHE.allowThis(newReleasedAmount); |
| 110 | + _getVestingWalletStorage()._tokenReleased[token] = newReleasedAmount; |
| 111 | + emit VestingWalletConfidentialTokenReleased(token, amountSent); |
| 112 | + } |
| 113 | + |
| 114 | + /// @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve. |
| 115 | + function vestedAmount(address token, uint64 timestamp) public virtual returns (euint64) { |
| 116 | + return |
| 117 | + _vestingSchedule( |
| 118 | + // Will overflow if the aggregate value of a single token sent to the vesting wallet exceeds `type(uint64).max`. |
| 119 | + FHE.add(IConfidentialFungibleToken(token).confidentialBalanceOf(address(this)), released(token)), |
| 120 | + timestamp |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + /// @dev This returns the amount vested, as a function of time, for an asset given its total historical allocation. |
| 125 | + function _vestingSchedule(euint64 totalAllocation, uint64 timestamp) internal virtual returns (euint64) { |
| 126 | + if (timestamp < start()) { |
| 127 | + return euint64.wrap(0); |
| 128 | + } else if (timestamp >= end()) { |
| 129 | + return totalAllocation; |
| 130 | + } else { |
| 131 | + return FHE.div(FHE.mul(totalAllocation, (timestamp - start())), duration()); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments