File size: 12,198 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
  "language": "Solidity",
  "sources": {
    "src/Vesting.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r\npragma solidity ^0.8.13;\r\n\r\nimport {IERC20} from \"lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol\";\r\nimport {IVesting} from \"src/interfaces/IVesting.sol\";\r\nimport {IVestingFactory} from \"src/interfaces/IVestingFactory.sol\";\r\n\r\n/*//////////////////////////////////////////////////////////////\r\n                        CUSTOM ERROR\r\n//////////////////////////////////////////////////////////////*/\r\n\r\nerror NotInitialised();\r\nerror Initialised();\r\nerror NoAccess();\r\nerror ZeroAddress();\r\nerror NoVestingData();\r\nerror StartLessThanNow();\r\nerror ZeroAmount();\r\nerror ZeroClaimAmount();\r\nerror AlreadyClaimed();\r\nerror AlreadyCancelled();\r\nerror Uncancellable();\r\nerror SameRecipient();\r\n\r\n/*//////////////////////////////////////////////////////////////\r\n                          CONTRACT\r\n//////////////////////////////////////////////////////////////*/\r\n\r\n/// @title Vesting Contract\r\ncontract Vesting is IVesting {\r\n    // The recipient of the tokens\r\n    address public recipient;\r\n\r\n    uint40 public start;\r\n    uint40 public duration;\r\n    uint256 public amount;\r\n\r\n    // Total amount of tokens which are claimed\r\n    uint256 public totalClaimedAmount;\r\n\r\n    // Flag for whether the vesting is cancellable or not\r\n    bool public isCancellable;\r\n    // Flag for whether the vesting is cancelled or not\r\n    bool public cancelled;\r\n    // Flag to check if its initialised\r\n    bool private initialised;\r\n\r\n    IVestingFactory public factory;\r\n\r\n    /*//////////////////////////////////////////////////////////////\r\n                            CONSTRUCTOR\r\n    //////////////////////////////////////////////////////////////*/\r\n\r\n    function initialise(address _recipient, uint40 _start, uint40 _duration, uint256 _amount, bool _isCancellable)\r\n        external\r\n        override\r\n    {\r\n        if (initialised) revert Initialised();\r\n        initialised = true;\r\n\r\n        recipient = _recipient;\r\n        start = _start;\r\n        duration = _duration;\r\n        amount = _amount;\r\n        isCancellable = _isCancellable;\r\n        factory = IVestingFactory(msg.sender);\r\n    }\r\n\r\n    /*//////////////////////////////////////////////////////////////\r\n                            MODIFIERS\r\n    //////////////////////////////////////////////////////////////*/\r\n\r\n    modifier onlyTreasury() {\r\n        if (msg.sender != factory.treasury()) revert NoAccess();\r\n        _;\r\n    }\r\n\r\n    modifier onlyOwner() {\r\n        bool isOwner;\r\n        if (isCancellable == false) {\r\n            // If the vest is uncancellable, only the recipient can call the function\r\n            isOwner = (msg.sender == recipient);\r\n        } else {\r\n            // If the vest is cancellable, only the treasury or the recipient can call the function\r\n            isOwner = (msg.sender == recipient) || (msg.sender == factory.treasury());\r\n        }\r\n        if (!isOwner) revert NoAccess();\r\n        _;\r\n    }\r\n\r\n    modifier onlyInit() {\r\n        if (!initialised) revert NotInitialised();\r\n        _;\r\n    }\r\n\r\n    /*//////////////////////////////////////////////////////////////\r\n                            VIEW FUNCTIONS\r\n    //////////////////////////////////////////////////////////////*/\r\n\r\n    /// @notice Calculates the amount of tokens which have been accrued to date.\r\n    /// @notice This does not take into account the amount which has been claimed already.\r\n    /// @return uint256 The amount of tokens which have been accrued to date.\r\n    function getAccruedTokens() public view returns (uint256) {\r\n        if (block.timestamp >= start + duration) {\r\n            return amount;\r\n        } else if (block.timestamp < start) {\r\n            // Allows us to set up vests in advance\r\n            return 0;\r\n        } else {\r\n            return (block.timestamp - start) * (amount / duration);\r\n        }\r\n    }\r\n\r\n    /// @notice Calculates the amount of tokens which can be claimed.\r\n    /// @return uint256 The amount of tokens which can be claimed.\r\n    function getClaimableTokens() public view returns (uint256) {\r\n        if (cancelled) {\r\n            return 0;\r\n        }\r\n\r\n        uint256 accruedTokens = getAccruedTokens();\r\n\r\n        // Calculate the amount of tokens which can be claimed\r\n        uint256 tokensToClaim = accruedTokens - totalClaimedAmount;\r\n\r\n        return tokensToClaim;\r\n    }\r\n\r\n    /// @notice Gets the vesting details of the contract.\r\n    function getVestingDetails() public view returns (uint40, uint40, uint256, uint256, bool) {\r\n        return (start, duration, amount, totalClaimedAmount, isCancellable);\r\n    }\r\n\r\n    /*//////////////////////////////////////////////////////////////\r\n                          EXTERNAL FUNCTIONS\r\n    //////////////////////////////////////////////////////////////*/\r\n\r\n    /// @notice Changes the recipient of vested tokens.\r\n    /// @notice The old recipient will not be able to claim the tokens, the new recipient will be able to claim all unclaimed tokens accrued to date.\r\n    /// @dev Can be called by the treasury or the recipient depending on whether the vest is cancellable or not.\r\n    /// @param _newRecipient Address of the new recipient recieving the vested tokens.\r\n    function changeRecipient(address _newRecipient) external onlyOwner onlyInit {\r\n        if (_newRecipient == address(0)) revert ZeroAddress();\r\n        if (_newRecipient == recipient) revert SameRecipient();\r\n        if (start == 0) revert NoVestingData();\r\n        if (cancelled) revert AlreadyCancelled();\r\n        factory.changeRecipient(recipient, _newRecipient);\r\n        recipient = _newRecipient;\r\n    }\r\n\r\n    /// @notice Cancels the vest and transfers the accrued amount to the recipient.\r\n    /// @dev Can only be called by the treasury.\r\n    function cancelVest() external onlyTreasury onlyInit {\r\n        if (start < 1) revert NoVestingData();\r\n        if (isCancellable == false) revert Uncancellable();\r\n        if (cancelled) revert AlreadyCancelled();\r\n\r\n        uint256 claimAmount = getClaimableTokens();\r\n\r\n        if (claimAmount > 0) {\r\n            totalClaimedAmount += claimAmount;\r\n            factory.token().transfer(recipient, claimAmount);\r\n        }\r\n\r\n        cancelled = true;\r\n\r\n        // Transfer the remainder of the tokens to the treasury\r\n        factory.token().transfer(factory.treasury(), amount - totalClaimedAmount);\r\n    }\r\n\r\n    /// @notice A function allowing the recipient to claim the vested tokens.\r\n    /// @notice The function returns unclaimed tokens to the treasury.\r\n    /// @dev This function can be called by anyone.\r\n    function claim() public override onlyInit {\r\n        if (totalClaimedAmount >= amount) revert AlreadyClaimed();\r\n        if (cancelled) revert AlreadyCancelled();\r\n\r\n        uint256 claimAmount = getClaimableTokens();\r\n        if (claimAmount < 1) revert ZeroClaimAmount();\r\n\r\n        totalClaimedAmount += claimAmount;\r\n        factory.token().transfer(recipient, claimAmount);\r\n    }\r\n}\r\n"
    },
    "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol": {
      "content": "// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)\r\n\r\npragma solidity ^0.8.0;\r\n\r\n/**\r\n * @dev Interface of the ERC20 standard as defined in the EIP.\r\n */\r\ninterface IERC20 {\r\n    /**\r\n     * @dev Emitted when `value` tokens are moved from one account (`from`) to\r\n     * another (`to`).\r\n     *\r\n     * Note that `value` may be zero.\r\n     */\r\n    event Transfer(address indexed from, address indexed to, uint256 value);\r\n\r\n    /**\r\n     * @dev Emitted when the allowance of a `spender` for an `owner` is set by\r\n     * a call to {approve}. `value` is the new allowance.\r\n     */\r\n    event Approval(address indexed owner, address indexed spender, uint256 value);\r\n\r\n    /**\r\n     * @dev Returns the amount of tokens in existence.\r\n     */\r\n    function totalSupply() external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Returns the amount of tokens owned by `account`.\r\n     */\r\n    function balanceOf(address account) external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Moves `amount` tokens from the caller's account to `to`.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * Emits a {Transfer} event.\r\n     */\r\n    function transfer(address to, uint256 amount) external returns (bool);\r\n\r\n    /**\r\n     * @dev Returns the remaining number of tokens that `spender` will be\r\n     * allowed to spend on behalf of `owner` through {transferFrom}. This is\r\n     * zero by default.\r\n     *\r\n     * This value changes when {approve} or {transferFrom} are called.\r\n     */\r\n    function allowance(address owner, address spender) external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * IMPORTANT: Beware that changing an allowance with this method brings the risk\r\n     * that someone may use both the old and the new allowance by unfortunate\r\n     * transaction ordering. One possible solution to mitigate this race\r\n     * condition is to first reduce the spender's allowance to 0 and set the\r\n     * desired value afterwards:\r\n     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\r\n     *\r\n     * Emits an {Approval} event.\r\n     */\r\n    function approve(address spender, uint256 amount) external returns (bool);\r\n\r\n    /**\r\n     * @dev Moves `amount` tokens from `from` to `to` using the\r\n     * allowance mechanism. `amount` is then deducted from the caller's\r\n     * allowance.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * Emits a {Transfer} event.\r\n     */\r\n    function transferFrom(\r\n        address from,\r\n        address to,\r\n        uint256 amount\r\n    ) external returns (bool);\r\n}\r\n"
    },
    "src/interfaces/IVesting.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r\npragma solidity ^0.8.0;\r\n\r\ninterface IVesting {\r\n    function cancelled() external returns (bool);\r\n\r\n    function totalClaimedAmount() external returns (uint256);\r\n    \r\n    function amount() external returns (uint256);\r\n\r\n    function initialise(address _recipient, uint40 _start, uint40 _duration, uint256 _amount, bool _isCancellable)\r\n        external;\r\n\r\n    function claim() external;\r\n}\r\n"
    },
    "src/interfaces/IVestingFactory.sol": {
      "content": "// SPDX-License-Identifier: BSD-3-Clause\r\npragma solidity ^0.8.0;\r\n\r\nimport {IERC20} from \"lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol\";\r\n\r\ninterface IVestingFactory {\r\n    function treasury() external returns (address);\r\n\r\n    function token() external returns (IERC20);\r\n\r\n    function changeRecipient(address _oldRecipient, address _newRecipient) external;\r\n}\r\n"
    }
  },
  "settings": {
    "remappings": [
      "ds-test/=lib/forge-std/lib/ds-test/src/",
      "forge-std/=lib/forge-std/src/",
      "openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/"
    ],
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "metadata": {
      "bytecodeHash": "ipfs"
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "evmVersion": "london",
    "libraries": {}
  }
}