{
    "Function": "_mintInternal",
    "File": "contracts/yieldspace/Pool.sol",
    "Parent Contracts": [
        "contracts/utils/access/Ownable.sol",
        "contracts/utils/token/ERC20Permit.sol",
        "contracts/interfaces/yieldspace/IPool.sol",
        "contracts/interfaces/external/IERC2612.sol",
        "contracts/utils/token/ERC20.sol",
        "contracts/interfaces/external/IERC20Metadata.sol",
        "contracts/interfaces/external/IERC20.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function _mintInternal(address to, bool calculateFromBase, uint256 fyTokenToBuy, uint256 minTokensMinted)\n        internal\n        returns (uint256, uint256, uint256)\n    {\n        // Gather data\n        uint256 supply = _totalSupply;\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        uint256 _realFYTokenCached = _fyTokenCached - supply;    // The fyToken cache includes the virtual fyToken, equal to the supply\n\n        // Calculate trade\n        uint256 tokensMinted;\n        uint256 baseIn;\n        uint256 baseReturned;\n        uint256 fyTokenIn;\n\n        if (supply == 0) {\n            require (calculateFromBase && fyTokenToBuy == 0, \"Pool: Initialize only from base\");\n            baseIn = base.balanceOf(address(this)) - _baseCached;\n            tokensMinted = baseIn;   // If supply == 0 we are initializing the pool and tokensMinted == baseIn; fyTokenIn == 0\n        } else {\n            // There is an optional virtual trade before the mint\n            uint256 baseToSell;\n            if (fyTokenToBuy > 0) {     // calculateFromBase == true and fyTokenToBuy > 0 can't happen in this implementation. To implement a virtual trade and calculateFromBase the trade would need to be a BaseToBuy parameter.\n                baseToSell = _buyFYTokenPreview(\n                    fyTokenToBuy.u128(),\n                    _baseCached,\n                    _fyTokenCached\n                ); \n            }\n\n            if (calculateFromBase) {   // We use all the available base tokens, surplus is in fyTokens\n                baseIn = base.balanceOf(address(this)) - _baseCached;\n                tokensMinted = (supply * baseIn) / _baseCached;\n                fyTokenIn = (_realFYTokenCached * tokensMinted) / supply;\n                require(_realFYTokenCached + fyTokenIn <= fyToken.balanceOf(address(this)), \"Pool: Not enough fyToken in\");\n            } else {                   // We use all the available fyTokens, plus a virtual trade if it happened, surplus is in base tokens\n                fyTokenIn = fyToken.balanceOf(address(this)) - _realFYTokenCached;\n                tokensMinted = (supply * (fyTokenToBuy + fyTokenIn)) / (_realFYTokenCached - fyTokenToBuy);\n                baseIn = baseToSell + ((_baseCached + baseToSell) * tokensMinted) / supply;\n                uint256 _baseBalance = base.balanceOf(address(this));\n                require(_baseBalance - _baseCached >= baseIn, \"Pool: Not enough base token in\");\n                \n                // If we did a trade means we came in through `mintWithBase`, and want to return the base token surplus\n                if (fyTokenToBuy > 0) baseReturned = (_baseBalance - _baseCached) - baseIn;\n            }\n        }\n\n        // Slippage\n        require (tokensMinted >= minTokensMinted, \"Pool: Not enough tokens minted\");\n\n        // Update TWAR\n        _update(\n            (_baseCached + baseIn).u128(),\n            (_fyTokenCached + fyTokenIn + tokensMinted).u128(), // Account for the \"virtual\" fyToken from the new minted LP tokens\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Execute mint\n        _mint(to, tokensMinted);\n\n        // Return any unused base if we did a trade, meaning slippage was involved.\n        if (supply > 0 && fyTokenToBuy > 0) base.safeTransfer(to, baseReturned);\n\n        emit Liquidity(maturity, msg.sender, to, -(baseIn.i256()), -(fyTokenIn.i256()), tokensMinted.i256());\n        return (baseIn, fyTokenIn, tokensMinted);\n    }"
}