{
    "Function": "slitherConstructorVariables",
    "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": [
        "keccak256(bytes)"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract Pool is IPool, ERC20Permit, Ownable {\n    using SafeCast256 for uint256;\n    using SafeCast128 for uint128;\n    using MinimalTransferHelper for IERC20;\n\n    event Trade(uint32 maturity, address indexed from, address indexed to, int256 bases, int256 fyTokens);\n    event Liquidity(uint32 maturity, address indexed from, address indexed to, int256 bases, int256 fyTokens, int256 poolTokens);\n    event Sync(uint112 baseCached, uint112 fyTokenCached, uint256 cumulativeBalancesRatio);\n    event ParameterSet(bytes32 parameter, int128 k);\n\n    int128 private k1 = int128(uint128(uint256((1 << 64))) / 315576000); // 1 / Seconds in 10 years, in 64.64\n    int128 private g1 = int128(uint128(uint256((950 << 64))) / 1000); // To be used when selling base to the pool. All constants are `ufixed`, to divide them they must be converted to uint256\n    int128 private k2 = int128(uint128(uint256((1 << 64))) / 315576000); // k is stored twice to be able to recover with 1 SLOAD alongside both g1 and g2\n    int128 private g2 = int128(uint128(uint256((1000 << 64))) / 950); // To be used when selling fyToken to the pool. All constants are `ufixed`, to divide them they must be converted to uint256\n    uint32 public immutable override maturity;\n\n    IERC20 public immutable override base;\n    IFYToken public immutable override fyToken;\n\n    uint112 private baseCached;              // uses single storage slot, accessible via getCache\n    uint112 private fyTokenCached;           // uses single storage slot, accessible via getCache\n    uint32  private blockTimestampLast;      // uses single storage slot, accessible via getCache\n\n    uint256 public cumulativeBalancesRatio;  // Fixed point factor with 27 decimals (ray)\n\n    constructor()\n        ERC20Permit(\n            string(abi.encodePacked(\"Yield \", SafeERC20Namer.tokenName(IPoolFactory(msg.sender).nextFYToken()), \" LP Token\")),\n            string(abi.encodePacked(SafeERC20Namer.tokenSymbol(IPoolFactory(msg.sender).nextFYToken()), \"LP\")),\n            SafeERC20Namer.tokenDecimals(IPoolFactory(msg.sender).nextBase())\n        )\n    {\n        IFYToken _fyToken = IFYToken(IPoolFactory(msg.sender).nextFYToken());\n        fyToken = _fyToken;\n        base = IERC20(IPoolFactory(msg.sender).nextBase());\n\n        uint256 _maturity = _fyToken.maturity();\n        require (_maturity <= type(uint32).max, \"Pool: Maturity too far in the future\");\n        maturity = uint32(_maturity);\n    }\n\n    /// @dev Trading can only be done before maturity\n    modifier beforeMaturity() {\n        require(\n            block.timestamp < maturity,\n            \"Pool: Too late\"\n        );\n        _;\n    }\n\n    // ---- Administration ----\n\n    /// @dev Set the k, g1 or g2 parameters\n    function setParameter(bytes32 parameter, int128 value)\n        external onlyOwner\n    {\n        if (parameter == \"k\") k1 = k2 = value;\n        else if (parameter == \"g1\") g1 = value;\n        else if (parameter == \"g2\") g2 = value;\n        else revert(\"Pool: Unrecognized parameter\");\n        emit ParameterSet(parameter, value);\n    }\n\n    /// @dev Get k\n    function getK()\n        external view\n        returns (int128)\n    {\n        assert(k1 == k2);\n        return k1;\n    }\n\n    /// @dev Get g1\n    function getG1()\n        external view\n        returns (int128)\n    {\n        return g1;\n    }\n\n    /// @dev Get g2\n    function getG2()\n        external view\n        returns (int128)\n    {\n        return g2;\n    }\n\n    // ---- Balances management ----\n\n    /// @dev Updates the cache to match the actual balances.\n    function sync() external {\n        _update(getBaseBalance(), getFYTokenBalance(), baseCached, fyTokenCached);\n    }\n\n    /// @dev Returns the cached balances & last updated timestamp.\n    /// @return Cached base token balance.\n    /// @return Cached virtual FY token balance.\n    /// @return Timestamp that balances were last cached.\n    function getCache()\n        external view\n        returns (uint112, uint112, uint32)\n    {\n        return (baseCached, fyTokenCached, blockTimestampLast);\n    }\n\n    /// @dev Returns the \"virtual\" fyToken balance, which is the real balance plus the pool token supply.\n    function getFYTokenBalance()\n        public view override\n        returns(uint112)\n    {\n        return (fyToken.balanceOf(address(this)) + _totalSupply).u112();\n    }\n\n    /// @dev Returns the base balance\n    function getBaseBalance()\n        public view override\n        returns(uint112)\n    {\n        return base.balanceOf(address(this)).u112();\n    }\n\n    /// @dev Retrieve any base tokens not accounted for in the cache\n    function retrieveBase(address to)\n        external override\n        returns(uint128 retrieved)\n    {\n        retrieved = getBaseBalance() - baseCached; // Cache can never be above balances\n        base.safeTransfer(to, retrieved);\n        // Now the current balances match the cache, so no need to update the TWAR\n    }\n\n    /// @dev Retrieve any fyTokens not accounted for in the cache\n    function retrieveFYToken(address to)\n        external override\n        returns(uint128 retrieved)\n    {\n        retrieved = getFYTokenBalance() - fyTokenCached; // Cache can never be above balances\n        IERC20(address(fyToken)).safeTransfer(to, retrieved);\n        // Now the balances match the cache, so no need to update the TWAR\n    }\n\n    /// @dev Update cache and, on the first call per block, ratio accumulators\n    function _update(uint128 baseBalance, uint128 fyBalance, uint112 _baseCached, uint112 _fyTokenCached) private {\n        uint32 blockTimestamp = uint32(block.timestamp);\n        uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired\n        if (timeElapsed > 0 && _baseCached != 0 && _fyTokenCached != 0) {\n            // We multiply by 1e27 here so that r = t * y/x is a fixed point factor with 27 decimals \n            uint256 scaledFYTokenCached = uint256(_fyTokenCached) * 1e27;\n            cumulativeBalancesRatio += scaledFYTokenCached  * timeElapsed / _baseCached;\n        }\n        baseCached = baseBalance.u112();\n        fyTokenCached = fyBalance.u112();\n        blockTimestampLast = blockTimestamp;\n        emit Sync(baseCached, fyTokenCached, cumulativeBalancesRatio);\n    }\n\n    // ---- Liquidity ----\n\n    /// @dev Mint liquidity tokens in exchange for adding base and fyToken\n    /// The amount of liquidity tokens to mint is calculated from the amount of unaccounted for base tokens in this contract.\n    /// A proportional amount of fyTokens needs to be present in this contract, also unaccounted for.\n    /// @param to Wallet receiving the minted liquidity tokens.\n    /// @param calculateFromBase Calculate the amount of tokens to mint from the base tokens available, leaving a fyToken surplus.\n    /// @param minTokensMinted Minimum amount of liquidity tokens received.\n    /// @return The amount of liquidity tokens minted.\n    function mint(address to, bool calculateFromBase, uint256 minTokensMinted)\n        external override\n        returns (uint256, uint256, uint256)\n    {\n        return _mintInternal(to, calculateFromBase, 0, minTokensMinted);\n    }\n\n    /// @dev Mint liquidity tokens in exchange for adding only base\n    /// The amount of liquidity tokens is calculated from the amount of fyToken to buy from the pool.\n    /// The base tokens need to be present in this contract, unaccounted for.\n    /// @param to Wallet receiving the minted liquidity tokens.\n    /// @param fyTokenToBuy Amount of `fyToken` being bought in the Pool, from this we calculate how much base it will be taken in.\n    /// @param minTokensMinted Minimum amount of liquidity tokens received.\n    /// @return The amount of liquidity tokens minted.\n    function mintWithBase(address to, uint256 fyTokenToBuy, uint256 minTokensMinted)\n        external override\n        returns (uint256, uint256, uint256)\n    {\n        return _mintInternal(to, false, fyTokenToBuy, minTokensMinted);\n    }\n\n    /// @dev Mint liquidity tokens in exchange for adding only base, if fyTokenToBuy > 0.\n    /// If fyTokenToBuy == 0, mint liquidity tokens for both basea and fyToken.\n    /// @param to Wallet receiving the minted liquidity tokens.\n    /// @param calculateFromBase Calculate the amount of tokens to mint from the base tokens available, leaving a fyToken surplus.\n    /// @param fyTokenToBuy Amount of `fyToken` being bought in the Pool, from this we calculate how much base it will be taken in.\n    /// @param minTokensMinted Minimum amount of liquidity tokens received.\n    /// @return The amount of liquidity tokens minted.\n    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    }\n\n    /// @dev Burn liquidity tokens in exchange for base and fyToken.\n    /// The liquidity tokens need to be in this contract.\n    /// @param to Wallet receiving the base and fyToken.\n    /// @return The amount of tokens burned and returned (tokensBurned, bases, fyTokens).\n    function burn(address to, uint256 minBaseOut, uint256 minFYTokenOut)\n        external override\n        returns (uint256, uint256, uint256)\n    {\n        return _burnInternal(to, false, minBaseOut, minFYTokenOut);\n    }\n\n    /// @dev Burn liquidity tokens in exchange for base.\n    /// The liquidity provider needs to have called `pool.approve`.\n    /// @param to Wallet receiving the base and fyToken.\n    /// @return tokensBurned The amount of lp tokens burned.\n    /// @return baseOut The amount of base tokens returned.\n    function burnForBase(address to, uint256 minBaseOut)\n        external override\n        returns (uint256 tokensBurned, uint256 baseOut)\n    {\n        (tokensBurned, baseOut, ) = _burnInternal(to, true, minBaseOut, 0);\n    }\n\n\n    /// @dev Burn liquidity tokens in exchange for base.\n    /// The liquidity provider needs to have called `pool.approve`.\n    /// @param to Wallet receiving the base and fyToken.\n    /// @param tradeToBase Whether the resulting fyToken should be traded for base tokens.\n    /// @return tokensBurned The amount of pool tokens burned.\n    /// @return tokenOut The amount of base tokens returned.\n    /// @return fyTokenOut The amount of fyTokens returned.\n    function _burnInternal(address to, bool tradeToBase, uint256 minBaseOut, uint256 minFYTokenOut)\n        internal\n        returns (uint256 tokensBurned, uint256 tokenOut, uint256 fyTokenOut)\n    {\n        \n        tokensBurned = _balanceOf[address(this)];\n        uint256 supply = _totalSupply;\n        uint256 fyTokenBalance = fyToken.balanceOf(address(this));          // use the real balance rather than the virtual one\n        uint256 baseBalance = base.balanceOf(address(this));\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n\n        // Calculate trade\n        tokenOut = (tokensBurned * baseBalance) / supply;\n        fyTokenOut = (tokensBurned * fyTokenBalance) / supply;\n\n        if (tradeToBase) {\n            (int128 _k, int128 _g2) = (k2, g2);\n            tokenOut += YieldMath.baseOutForFYTokenIn(                      // This is a virtual sell\n                _baseCached - tokenOut.u128(),                              // Cache, minus virtual burn\n                _fyTokenCached - fyTokenOut.u128(),                         // Cache, minus virtual burn\n                fyTokenOut.u128(),                                          // Sell the virtual fyToken obtained\n                maturity - uint32(block.timestamp),                         // This can't be called after maturity\n                _k,\n                _g2\n            );\n            fyTokenOut = 0;\n        }\n\n        // Slippage\n        require (tokenOut >= minBaseOut, \"Pool: Not enough base tokens obtained\");\n        require (fyTokenOut >= minFYTokenOut, \"Pool: Not enough fyToken obtained\");\n\n        // Update TWAR\n        _update(\n            (baseBalance - tokenOut).u128(),\n            (fyTokenBalance - fyTokenOut + supply - tokensBurned).u128(),\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Transfer assets\n        _burn(address(this), tokensBurned);\n        base.safeTransfer(to, tokenOut);\n        if (fyTokenOut > 0) IERC20(address(fyToken)).safeTransfer(to, fyTokenOut);\n\n        emit Liquidity(maturity, msg.sender, to, tokenOut.i256(), fyTokenOut.i256(), -(tokensBurned.i256()));\n    }\n\n    // ---- Trading ----\n\n    /// @dev Sell base for fyToken.\n    /// The trader needs to have transferred the amount of base to sell to the pool before in the same transaction.\n    /// @param to Wallet receiving the fyToken being bought\n    /// @param min Minimm accepted amount of fyToken\n    /// @return Amount of fyToken that will be deposited on `to` wallet\n    function sellBase(address to, uint128 min)\n        external override\n        returns(uint128)\n    {\n        // Calculate trade\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        uint112 _baseBalance = getBaseBalance();\n        uint112 _fyTokenBalance = getFYTokenBalance();\n        uint128 baseIn = _baseBalance - _baseCached;\n        uint128 fyTokenOut = _sellBasePreview(\n            baseIn,\n            _baseCached,\n            _fyTokenBalance\n        );\n\n        // Slippage check\n        require(\n            fyTokenOut >= min,\n            \"Pool: Not enough fyToken obtained\"\n        );\n\n        // Update TWAR\n        _update(\n            _baseBalance,\n            _fyTokenBalance - fyTokenOut,\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Transfer assets\n        IERC20(address(fyToken)).safeTransfer(to, fyTokenOut);\n\n        emit Trade(maturity, msg.sender, to, -(baseIn.i128()), fyTokenOut.i128());\n        return fyTokenOut;\n    }\n\n    /// @dev Returns how much fyToken would be obtained by selling `baseIn` base\n    /// @param baseIn Amount of base hypothetically sold.\n    /// @return Amount of fyToken hypothetically bought.\n    function sellBasePreview(uint128 baseIn)\n        external view override\n        returns(uint128)\n    {\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        return _sellBasePreview(baseIn, _baseCached, _fyTokenCached);\n    }\n\n    /// @dev Returns how much fyToken would be obtained by selling `baseIn` base\n    function _sellBasePreview(\n        uint128 baseIn,\n        uint112 baseBalance,\n        uint112 fyTokenBalance\n    )\n        private view\n        beforeMaturity\n        returns(uint128)\n    {\n        (int128 _k, int128 _g1) = (k1, g1);\n        uint128 fyTokenOut = YieldMath.fyTokenOutForBaseIn(\n            baseBalance,\n            fyTokenBalance,\n            baseIn,\n            maturity - uint32(block.timestamp),             // This can't be called after maturity\n            _k,\n            _g1\n        );\n\n        require(\n            fyTokenBalance - fyTokenOut >= baseBalance + baseIn,\n            \"Pool: fyToken balance too low\"\n        );\n\n        return fyTokenOut;\n    }\n\n    /// @dev Buy base for fyToken\n    /// The trader needs to have called `fyToken.approve`\n    /// @param to Wallet receiving the base being bought\n    /// @param tokenOut Amount of base being bought that will be deposited in `to` wallet\n    /// @param max Maximum amount of fyToken that will be paid for the trade\n    /// @return Amount of fyToken that will be taken from caller\n    function buyBase(address to, uint128 tokenOut, uint128 max)\n        external override\n        returns(uint128)\n    {\n        // Calculate trade\n        uint128 fyTokenBalance = getFYTokenBalance();\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        uint128 fyTokenIn = _buyBasePreview(\n            tokenOut,\n            _baseCached,\n            _fyTokenCached\n        );\n        require(\n            fyTokenBalance - _fyTokenCached >= fyTokenIn,\n            \"Pool: Not enough fyToken in\"\n        );\n\n        // Slippage check\n        require(\n            fyTokenIn <= max,\n            \"Pool: Too much fyToken in\"\n        );\n\n        // Update TWAR\n        _update(\n            _baseCached - tokenOut,\n            _fyTokenCached + fyTokenIn,\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Transfer assets\n        base.safeTransfer(to, tokenOut);\n\n        emit Trade(maturity, msg.sender, to, tokenOut.i128(), -(fyTokenIn.i128()));\n        return fyTokenIn;\n    }\n\n    /// @dev Returns how much fyToken would be required to buy `tokenOut` base.\n    /// @param tokenOut Amount of base hypothetically desired.\n    /// @return Amount of fyToken hypothetically required.\n    function buyBasePreview(uint128 tokenOut)\n        external view override\n        returns(uint128)\n    {\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        return _buyBasePreview(tokenOut, _baseCached, _fyTokenCached);\n    }\n\n    /// @dev Returns how much fyToken would be required to buy `tokenOut` base.\n    function _buyBasePreview(\n        uint128 tokenOut,\n        uint112 baseBalance,\n        uint112 fyTokenBalance\n    )\n        private view\n        beforeMaturity\n        returns(uint128)\n    {\n        (int128 _k, int128 _g2) = (k2, g2);\n        return YieldMath.fyTokenInForBaseOut(\n            baseBalance,\n            fyTokenBalance,\n            tokenOut,\n            maturity - uint32(block.timestamp),             // This can't be called after maturity\n            _k,\n            _g2\n        );\n    }\n\n    /// @dev Sell fyToken for base\n    /// The trader needs to have transferred the amount of fyToken to sell to the pool before in the same transaction.\n    /// @param to Wallet receiving the base being bought\n    /// @param min Minimm accepted amount of base\n    /// @return Amount of base that will be deposited on `to` wallet\n    function sellFYToken(address to, uint128 min)\n        external override\n        returns(uint128)\n    {\n        // Calculate trade\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        uint112 _fyTokenBalance = getFYTokenBalance();\n        uint112 _baseBalance = getBaseBalance();\n        uint128 fyTokenIn = _fyTokenBalance - _fyTokenCached;\n        uint128 baseOut = _sellFYTokenPreview(\n            fyTokenIn,\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Slippage check\n        require(\n            baseOut >= min,\n            \"Pool: Not enough base obtained\"\n        );\n\n        // Update TWAR\n        _update(\n            _baseBalance - baseOut,\n            _fyTokenBalance,\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Transfer assets\n        base.safeTransfer(to, baseOut);\n\n        emit Trade(maturity, msg.sender, to, baseOut.i128(), -(fyTokenIn.i128()));\n        return baseOut;\n    }\n\n    /// @dev Returns how much base would be obtained by selling `fyTokenIn` fyToken.\n    /// @param fyTokenIn Amount of fyToken hypothetically sold.\n    /// @return Amount of base hypothetically bought.\n    function sellFYTokenPreview(uint128 fyTokenIn)\n        external view override\n        returns(uint128)\n    {\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        return _sellFYTokenPreview(fyTokenIn, _baseCached, _fyTokenCached);\n    }\n\n    /// @dev Returns how much base would be obtained by selling `fyTokenIn` fyToken.\n    function _sellFYTokenPreview(\n        uint128 fyTokenIn,\n        uint112 baseBalance,\n        uint112 fyTokenBalance\n    )\n        private view\n        beforeMaturity\n        returns(uint128)\n    {\n        (int128 _k, int128 _g2) = (k2, g2);\n        return YieldMath.baseOutForFYTokenIn(\n            baseBalance,\n            fyTokenBalance,\n            fyTokenIn,\n            maturity - uint32(block.timestamp),             // This can't be called after maturity\n            _k,\n            _g2\n        );\n    }\n\n    /// @dev Buy fyToken for base\n    /// The trader needs to have called `base.approve`\n    /// @param to Wallet receiving the fyToken being bought\n    /// @param fyTokenOut Amount of fyToken being bought that will be deposited in `to` wallet\n    /// @param max Maximum amount of base token that will be paid for the trade\n    /// @return Amount of base that will be taken from caller's wallet\n    function buyFYToken(address to, uint128 fyTokenOut, uint128 max)\n        external override\n        returns(uint128)\n    {\n        // Calculate trade\n        uint128 baseBalance = getBaseBalance();\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        uint128 baseIn = _buyFYTokenPreview(\n            fyTokenOut,\n            _baseCached,\n            _fyTokenCached\n        );\n        require(\n            baseBalance - _baseCached >= baseIn,\n            \"Pool: Not enough base token in\"\n        );\n\n        // Slippage check\n        require(\n            baseIn <= max,\n            \"Pool: Too much base token in\"\n        );\n\n        // Update TWAR\n        _update(\n            _baseCached + baseIn,\n            _fyTokenCached - fyTokenOut,\n            _baseCached,\n            _fyTokenCached\n        );\n\n        // Transfer assets\n        IERC20(address(fyToken)).safeTransfer(to, fyTokenOut);\n\n        emit Trade(maturity, msg.sender, to, -(baseIn.i128()), fyTokenOut.i128());\n        return baseIn;\n    }\n\n    /// @dev Returns how much base would be required to buy `fyTokenOut` fyToken.\n    /// @param fyTokenOut Amount of fyToken hypothetically desired.\n    /// @return Amount of base hypothetically required.\n    function buyFYTokenPreview(uint128 fyTokenOut)\n        external view override\n        returns(uint128)\n    {\n        (uint112 _baseCached, uint112 _fyTokenCached) =\n            (baseCached, fyTokenCached);\n        return _buyFYTokenPreview(fyTokenOut, _baseCached, _fyTokenCached);\n    }\n\n    /// @dev Returns how much base would be required to buy `fyTokenOut` fyToken.\n    function _buyFYTokenPreview(\n        uint128 fyTokenOut,\n        uint128 baseBalance,\n        uint128 fyTokenBalance\n    )\n        private view\n        beforeMaturity\n        returns(uint128)\n    {\n        (int128 _k, int128 _g1) = (k1, g1);\n        uint128 baseIn = YieldMath.baseInForFYTokenOut(\n            baseBalance,\n            fyTokenBalance,\n            fyTokenOut,\n            maturity - uint32(block.timestamp),             // This can't be called after maturity\n            _k,\n            _g1\n        );\n\n        require(\n            fyTokenBalance - fyTokenOut >= baseBalance + baseIn,\n            \"Pool: fyToken balance too low\"\n        );\n\n        return baseIn;\n    }\n}"
}