// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
}
contract Vesting {
IERC20 private _token = IERC20(0x173771c0fa751C56579536A85b2DACb562CDE80B);
address private _deployer;
uint256 private _creation;
uint256 private _unlocked = 0;
uint256 private _unlocks = 24;
constructor() {
_deployer = msg.sender;
_creation = block.timestamp;
}
function unlock() external {
// Only the contract deployer is authorized to unlock
require(msg.sender == _deployer, 'not authorized');
// Get the current token balance
uint256 balance = _token.balanceOf(address(this));
// The contract token balance needs to be positive
require(balance > 0, 'empty balance');
// Get the difference in seconds between current timestamp and creation time
uint256 difference = block.timestamp - _creation;
// Calculate months that have passed since creation (1 month = 60 seconds * 60 minutes * 24 hours * 30 days)
uint256 months = difference / 60 / 60 / 24 / 30;
// Cap months at max. number of unlocks (the vesting ends when months == _unlocks)
if (months > _unlocks) months = _unlocks;
// Calculate the initial amount of locked tokens
// This is equal to the current token balance of the contract plus tokens that have been unlocked
uint256 tokens = balance + _unlocked;
// Calculate the max. unlockable amount based on the number of passed months
// For example, if months = 6 and _unlocks = 24, this becomes initially locked tokens * 0.25
uint256 unlockable = tokens * months / _unlocks;
// Calculate the withdrawable amount, based on previously unlocked tokens
uint256 withdrawable = unlockable - _unlocked;
// Update the unlocked amount
_unlocked += withdrawable;
// Transfer the withdrawable amount
_token.transfer(_deployer, withdrawable);
}
}
{
"compilationTarget": {
"Vesting.sol": "Vesting"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"unlock","outputs":[],"stateMutability":"nonpayable","type":"function"}]