{
    "Function": "swap",
    "File": "contracts/UpsideProtocol.sol",
    "Parent Contracts": [
        "node_modules/@openzeppelin/contracts/access/Ownable.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol"
    ],
    "High-Level Calls": [
        "SafeERC20",
        "SafeERC20",
        "SafeERC20",
        "SafeERC20"
    ],
    "Internal Calls": [
        "revert MetaCoinNonExistent()",
        "revert InsufficientLiquidity()",
        "revert InsufficientOutput()",
        "processSwapFee"
    ],
    "Library Calls": [
        "safeTransferFrom",
        "safeTransferFrom",
        "safeTransfer",
        "safeTransfer"
    ],
    "Low-Level Calls": [],
    "Code": "function swap(\n        address _metaCoinAddress,\n        bool _isBuy,\n        uint256 _tokenAmount,\n        uint256 _minimumOut,\n        address _recipient\n    ) external returns (uint256 amountOut) {\n        MetaCoinInfo storage metaCoinInfo = metaCoinInfoMap[_metaCoinAddress];\n\n        if (metaCoinInfo.deployer == address(0)) {\n            revert MetaCoinNonExistent();\n        }\n\n        if (_isBuy) {\n            IERC20Metadata(liquidityTokenAddress).safeTransferFrom(msg.sender, address(this), _tokenAmount);\n        } else {\n            IERC20Metadata(_metaCoinAddress).safeTransferFrom(msg.sender, address(this), _tokenAmount);\n        }\n\n        uint256 amountInAfterFee = processSwapFee(_metaCoinAddress, _isBuy, _tokenAmount, metaCoinInfo.deployer);\n\n        uint256 newLiquidityTokenReserves;\n        uint256 newMetaCoinReserves;\n        if (_isBuy) {\n            newLiquidityTokenReserves = metaCoinInfo.liquidityTokenReserves + amountInAfterFee;\n            amountOut = (metaCoinInfo.metaCoinReserves * amountInAfterFee) / newLiquidityTokenReserves;\n            newMetaCoinReserves = metaCoinInfo.metaCoinReserves - amountOut;\n        } else {\n            newMetaCoinReserves = metaCoinInfo.metaCoinReserves + amountInAfterFee;\n            amountOut = (metaCoinInfo.liquidityTokenReserves * amountInAfterFee) / newMetaCoinReserves;\n            newLiquidityTokenReserves = metaCoinInfo.liquidityTokenReserves - amountOut;\n\n            if (newLiquidityTokenReserves < INITIAL_LIQUIDITY_RESERVES) revert InsufficientLiquidity();\n        }\n        if (_minimumOut > amountOut) revert InsufficientOutput();\n\n        metaCoinInfo.liquidityTokenReserves = newLiquidityTokenReserves;\n        metaCoinInfo.metaCoinReserves = newMetaCoinReserves;\n\n        emit Trade(_metaCoinAddress, _isBuy, msg.sender, _tokenAmount, amountInAfterFee, amountOut, _recipient);\n\n        if (_isBuy) {\n            IERC20Metadata(_metaCoinAddress).safeTransfer(_recipient, amountOut);\n        } else {\n            IERC20Metadata(liquidityTokenAddress).safeTransfer(_recipient, amountOut);\n        }\n    }"
}