{
    "Function": "_lock",
    "File": "contracts/VE3DLocker.sol",
    "Parent Contracts": [
        "node_modules/@openzeppelin/contracts/access/Ownable.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol",
        "node_modules/@openzeppelin/contracts/security/ReentrancyGuard.sol"
    ],
    "High-Level Calls": [
        "BoringMath",
        "BoringMath",
        "BoringMath",
        "BoringMath",
        "BoringMath",
        "BoringMath112",
        "BoringMath",
        "BoringMath112",
        "BoringMath224"
    ],
    "Internal Calls": [
        "require(bool,string)",
        "require(bool,string)",
        "_checkpointEpoch"
    ],
    "Library Calls": [
        "div",
        "add",
        "mul",
        "to112",
        "add",
        "add",
        "add",
        "add",
        "add"
    ],
    "Low-Level Calls": [],
    "Code": "function _lock(\n        address _account,\n        uint256 _amount,\n        bool _isRelock\n    ) internal {\n        require(_amount > 0, \"Cannot stake 0\");\n        require(!isShutdown, \"shutdown\");\n\n        Balances storage bal = balances[_account];\n\n        //must try check pointing epoch first\n        _checkpointEpoch();\n\n        //add user balances\n        uint112 lockAmount = _amount.to112();\n        bal.locked = bal.locked.add(lockAmount);\n\n        //add to total supplies\n        lockedSupply = lockedSupply.add(lockAmount);\n\n        //add user lock records or add to current\n        uint256 lockEpoch = block.timestamp.div(rewardsDuration).mul(rewardsDuration);\n        //if a fresh lock, add on an extra duration period\n        if (!_isRelock) {\n            lockEpoch = lockEpoch.add(rewardsDuration);\n        }\n        uint256 unlockTime = lockEpoch.add(lockDuration);\n        uint256 idx = userLocks[_account].length;\n\n        //if the latest user lock is smaller than this lock, always just add new entry to the end of the list\n        if (idx == 0 || userLocks[_account][idx - 1].unlockTime < unlockTime) {\n            userLocks[_account].push(\n                LockedBalance({amount: lockAmount, unlockTime: uint32(unlockTime)})\n            );\n        } else {\n            //else add to a current lock\n\n            //if latest lock is further in the future, lower index\n            //this can only happen if relocking an expired lock after creating a new lock\n            if (userLocks[_account][idx - 1].unlockTime > unlockTime) {\n                idx--;\n            }\n\n            //if idx points to the epoch when same unlock time, update\n            //(this is always true with a normal lock but maybe not with relock)\n            if (userLocks[_account][idx - 1].unlockTime == unlockTime) {\n                LockedBalance storage userL = userLocks[_account][idx - 1];\n                userL.amount = userL.amount.add(lockAmount);\n            } else {\n                //can only enter here if a relock is made after a lock and there's no lock entry\n                //for the current epoch.\n                //ex a list of locks such as \"[...][older][current*][next]\" but without a \"current\" lock\n                //length - 1 is the next epoch\n                //length - 2 is a past epoch\n                //thus need to insert an entry for current epoch at the 2nd to last entry\n                //we will copy and insert the tail entry(next) and then overwrite length-2 entry\n\n                //reset idx\n                idx = userLocks[_account].length;\n\n                //get current last item\n                LockedBalance storage userL = userLocks[_account][idx - 1];\n\n                //add a copy to end of list\n                userLocks[_account].push(\n                    LockedBalance({amount: userL.amount, unlockTime: userL.unlockTime})\n                );\n\n                //insert current epoch lock entry by overwriting the entry at length-2\n                userL.amount = lockAmount;\n                userL.unlockTime = uint32(unlockTime);\n            }\n        }\n\n        //update epoch supply, epoch checkpointed above so safe to add to latest\n        uint256 eIndex = epochs.length - 1;\n        //if relock, epoch should be current and not next, thus need to decrease index to length-2\n        if (_isRelock) {\n            eIndex--;\n        }\n        Epoch storage e = epochs[eIndex];\n        e.supply = e.supply.add(uint224(lockAmount));\n\n        emit Staked(_account, lockEpoch, _amount, lockAmount);\n    }"
}