文件 1 的 4:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 2 的 4:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 3 的 4:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_setOwner(_msgSender());
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
_setOwner(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_setOwner(newOwner);
}
function _setOwner(address newOwner) private {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 4 的 4:Vesting.sol
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Vesting is Ownable {
IERC20 public immutable cpool;
uint256 public immutable vestingBegin;
uint256 public immutable vestingEnd;
struct VestingParams {
uint256 amount;
uint256 vestingCliff;
uint256 lastUpdate;
uint256 claimed;
}
mapping(uint256 => VestingParams) public vestings;
mapping(address => uint256[]) public vestingIds;
uint256 public totalVest;
uint256 private _nextVestingId;
struct HoldParams {
address recipient;
uint256 amount;
uint256 unlocked;
uint256 vestingCliff;
}
constructor(IERC20 cpool_, uint256 vestingBegin_, uint256 vestingEnd_) Ownable() {
require(vestingEnd_ > vestingBegin_, "Vesting: vesting end should be greater than vesting begin");
cpool = cpool_;
vestingBegin = vestingBegin_;
vestingEnd = vestingEnd_;
}
function claim(address account) external {
uint256 totalAmount;
for (uint8 i = 0; i < vestingIds[account].length; i++) {
uint256 amount = getAvailableBalance(vestingIds[account][i]);
if (amount > 0) {
totalAmount += amount;
vestings[vestingIds[account][i]].claimed += amount;
vestings[vestingIds[account][i]].lastUpdate = block.timestamp;
}
}
require(cpool.transfer(account, totalAmount), "Vesting::claim: transfer error");
}
function holdTokens(HoldParams[] memory params) external onlyOwner {
uint256 totalAmount;
for (uint8 i = 0; i < params.length; i++) {
totalAmount += params[i].amount;
}
require(cpool.transferFrom(msg.sender, address(this), totalAmount), "Vesting::holdTokens: transfer failed");
totalVest += totalAmount;
for (uint8 i = 0; i < params.length; i++) {
_holdTokens(params[i]);
}
}
function getAvailableBalanceOf(address account) external view returns (uint256 amount) {
for (uint8 i = 0; i < vestingIds[account].length; i++) {
amount += getAvailableBalance(vestingIds[account][i]);
}
}
function getAvailableBalance(uint256 id) public view returns (uint256) {
VestingParams memory vestParams = vestings[id];
if (block.timestamp < vestParams.vestingCliff) {
return 0;
}
uint256 amount;
if (block.timestamp >= vestingEnd) {
amount = vestParams.amount - vestParams.claimed;
} else {
amount = vestParams.amount * (block.timestamp - vestParams.lastUpdate) / (vestingEnd - vestingBegin);
}
return amount;
}
function vestingCountOf(address account) external view returns (uint256) {
return vestingIds[account].length;
}
function _holdTokens(HoldParams memory params) private {
require(params.amount > 0, "Vesting::holdTokens: can not hold zero amount");
require(vestingEnd > params.vestingCliff, "Vesting::holdTokens: cliff is too late");
require(params.vestingCliff >= vestingBegin, "Vesting::holdTokens: cliff is too early");
require(params.unlocked <= params.amount, "Vesting::holdTokens: unlocked can not be greater than amount");
if (params.unlocked > 0) {
cpool.transfer(params.recipient, params.unlocked);
}
if (params.unlocked < params.amount) {
vestings[_nextVestingId] = VestingParams({
amount: params.amount - params.unlocked,
vestingCliff: params.vestingCliff,
lastUpdate: vestingBegin,
claimed: 0
});
vestingIds[params.recipient].push(_nextVestingId);
_nextVestingId++;
}
}
}
{
"compilationTarget": {
"contracts/Vesting.sol": "Vesting"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IERC20","name":"cpool_","type":"address"},{"internalType":"uint256","name":"vestingBegin_","type":"uint256"},{"internalType":"uint256","name":"vestingEnd_","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"cpool","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"id","type":"uint256"}],"name":"getAvailableBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"getAvailableBalanceOf","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"unlocked","type":"uint256"},{"internalType":"uint256","name":"vestingCliff","type":"uint256"}],"internalType":"struct Vesting.HoldParams[]","name":"params","type":"tuple[]"}],"name":"holdTokens","outputs":[],"stateMutability":"nonpayable","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":"totalVest","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"vestingBegin","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"vestingCountOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"vestingEnd","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"vestingIds","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"vestings","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"vestingCliff","type":"uint256"},{"internalType":"uint256","name":"lastUpdate","type":"uint256"},{"internalType":"uint256","name":"claimed","type":"uint256"}],"stateMutability":"view","type":"function"}]