// File: @openzeppelin/contracts/token/ERC20/IERC20.sol
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
// File: @openzeppelin/contracts/interfaces/IERC20.sol
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC20.sol)
pragma solidity ^0.8.20;
// File: @openzeppelin/contracts/utils/Context.sol
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// File: @openzeppelin/contracts/access/Ownable.sol
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// File: vesting.sol
pragma solidity ^0.8.0;
contract VRVesting is Ownable {
uint48 public constant HUNDRED_PERCENT = 1e6;
struct User {
uint120 amount;
uint16 vestingRoundId;
uint120 claimed;
}
struct VestingRound {
uint48 linearVestingStart;
uint48 linearVestingEnd;
uint48[] unlockTimestamps;
uint48[] unlockPercentages;
}
IERC20 public token;
uint public vestingRoundCount;
mapping(address => User) public users;
mapping(uint => VestingRound) vestingRounds;
event Claim(address user, uint amount);
event AddVestingRound(uint id, uint48 linearVestingStart, uint48 linearVestingEnd, uint48[] unlockTimestamps, uint48[] unlockPercentages);
event AddWallets(address[] wallets, uint120[] amounts, uint16[] vestingRoundIds);
event DeleteWallets(address[] wallets);
constructor(IERC20 _token) Ownable(msg.sender) {
token = _token;
}
// ======================= OWNER FUNCTIONS ======================= //
function addVestingRound(
uint48 linearVestingStart,
uint48 linearVestingEnd,
uint48[] calldata unlockTimestamps,
uint48[] calldata unlockPercentages
) external onlyOwner {
if (linearVestingStart > 0) {
require(unlockTimestamps.length == 0, "unlockTimestamps not empty");
require(unlockPercentages.length == 0, "unlockPercentages not empty");
require(linearVestingEnd > linearVestingStart, "end > start timestamp");
} else {
require(linearVestingEnd == 0, "linearVestingEnd not zero");
require(unlockTimestamps.length == unlockPercentages.length, "different array lengths");
for (uint i = 1; i < unlockTimestamps.length; i++) {
require(unlockTimestamps[i] > unlockTimestamps[i - 1], "invalid unlockTimestamps");
}
uint48 unlockPercentagesSum = 0;
for (uint i = 0; i < unlockPercentages.length; i++) {
unlockPercentagesSum += unlockPercentages[i];
}
require(unlockPercentagesSum == HUNDRED_PERCENT, "unlockPercentages sum not 100%");
}
vestingRounds[vestingRoundCount++] = VestingRound(
linearVestingStart,
linearVestingEnd,
unlockTimestamps,
unlockPercentages
);
emit AddVestingRound(vestingRoundCount - 1, linearVestingStart, linearVestingEnd, unlockTimestamps, unlockPercentages);
}
function addWallets(
address[] calldata wallets,
uint120[] calldata amounts,
uint16[] calldata vestingRoundIds,
bool applyDecimals
) external onlyOwner {
require(wallets.length == amounts.length, "different array lengths");
require(amounts.length == vestingRoundIds.length, "different array lengths");
for (uint i; i < wallets.length; i++) {
require(users[wallets[i]].amount == 0, "whitelisted already");
require(vestingRoundIds[i] < vestingRoundCount, "invalid vestingRoundId");
uint120 amount = amounts[i];
if (applyDecimals) {
amount *= 1e18;
}
users[wallets[i]] = User(
amount,
vestingRoundIds[i],
0
);
}
emit AddWallets(wallets, amounts, vestingRoundIds);
}
function deleteWallets(
address[] calldata wallets
) external onlyOwner {
for (uint i; i < wallets.length; i++) {
delete users[wallets[i]];
}
emit DeleteWallets(wallets);
}
// ======================= EXTERNAL FUNCTIONS ======================= //
function claim() external {
User storage user = users[msg.sender];
uint48 vestingStart = getVestingStart(user.vestingRoundId);
require(user.amount > 0, "not whitelisted");
require(block.timestamp > vestingStart, "vesting not started");
require(user.claimed < user.amount, "claimed all");
uint claimable = getClaimable(msg.sender);
user.claimed += uint120(claimable);
token.transfer(msg.sender, claimable);
emit Claim(msg.sender, claimable);
}
// ======================= VIEW FUNCTIONS ======================= //
function getVestingStart(uint vestingRoundId) public view returns (uint48) {
VestingRound storage vestingRound = vestingRounds[vestingRoundId];
if (vestingRound.linearVestingStart > 0) {
return vestingRound.linearVestingStart;
} else {
return vestingRound.unlockTimestamps[0];
}
}
function getClaimable(
address wallet
) public view returns (uint) {
User storage user = users[wallet];
VestingRound storage vestingRound = vestingRounds[user.vestingRoundId];
if (vestingRound.linearVestingStart > 0) {
if (block.timestamp > vestingRound.linearVestingEnd) {
return user.amount - user.claimed;
} else {
return uint(user.amount) * (block.timestamp - vestingRound.linearVestingStart) / (vestingRound.linearVestingEnd - vestingRound.linearVestingStart) - user.claimed;
}
} else {
uint48 cumulativePercentage = 0;
for (uint i; i < vestingRound.unlockTimestamps.length; i++) {
if (vestingRound.unlockTimestamps[i] > block.timestamp) {
break;
}
cumulativePercentage += vestingRound.unlockPercentages[i];
}
return uint(user.amount) * cumulativePercentage / HUNDRED_PERCENT - user.claimed;
}
}
function getVestingRound(
uint vestingRoundId
) external view returns (VestingRound memory vestingRound) {
return vestingRounds[vestingRoundId];
}
}
{
"compilationTarget": {
"VRVesting.sol": "VRVesting"
},
"evmVersion": "shanghai",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IERC20","name":"_token","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":false,"internalType":"uint48","name":"linearVestingStart","type":"uint48"},{"indexed":false,"internalType":"uint48","name":"linearVestingEnd","type":"uint48"},{"indexed":false,"internalType":"uint48[]","name":"unlockTimestamps","type":"uint48[]"},{"indexed":false,"internalType":"uint48[]","name":"unlockPercentages","type":"uint48[]"}],"name":"AddVestingRound","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address[]","name":"wallets","type":"address[]"},{"indexed":false,"internalType":"uint120[]","name":"amounts","type":"uint120[]"},{"indexed":false,"internalType":"uint16[]","name":"vestingRoundIds","type":"uint16[]"}],"name":"AddWallets","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Claim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address[]","name":"wallets","type":"address[]"}],"name":"DeleteWallets","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"HUNDRED_PERCENT","outputs":[{"internalType":"uint48","name":"","type":"uint48"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint48","name":"linearVestingStart","type":"uint48"},{"internalType":"uint48","name":"linearVestingEnd","type":"uint48"},{"internalType":"uint48[]","name":"unlockTimestamps","type":"uint48[]"},{"internalType":"uint48[]","name":"unlockPercentages","type":"uint48[]"}],"name":"addVestingRound","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"wallets","type":"address[]"},{"internalType":"uint120[]","name":"amounts","type":"uint120[]"},{"internalType":"uint16[]","name":"vestingRoundIds","type":"uint16[]"},{"internalType":"bool","name":"applyDecimals","type":"bool"}],"name":"addWallets","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"wallets","type":"address[]"}],"name":"deleteWallets","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"}],"name":"getClaimable","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"vestingRoundId","type":"uint256"}],"name":"getVestingRound","outputs":[{"components":[{"internalType":"uint48","name":"linearVestingStart","type":"uint48"},{"internalType":"uint48","name":"linearVestingEnd","type":"uint48"},{"internalType":"uint48[]","name":"unlockTimestamps","type":"uint48[]"},{"internalType":"uint48[]","name":"unlockPercentages","type":"uint48[]"}],"internalType":"struct VRVesting.VestingRound","name":"vestingRound","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"vestingRoundId","type":"uint256"}],"name":"getVestingStart","outputs":[{"internalType":"uint48","name":"","type":"uint48"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"users","outputs":[{"internalType":"uint120","name":"amount","type":"uint120"},{"internalType":"uint16","name":"vestingRoundId","type":"uint16"},{"internalType":"uint120","name":"claimed","type":"uint120"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"vestingRoundCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]