{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/adapters/exchanges/BalancerAdapter.sol",
    "Parent Contracts": [
        "contracts/adapters/BaseAdapter.sol",
        "contracts/interfaces/IBaseAdapter.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract BalancerAdapter is BaseAdapter {\n    using SafeMath for uint256;\n    using SafeERC20 for IERC20;\n\n    struct Pool {\n        address pool;\n        uint    tokenBalanceIn;\n        uint    tokenWeightIn;\n        uint    tokenBalanceOut;\n        uint    tokenWeightOut;\n        uint    swapFee;\n        uint    effectiveLiquidity;\n    }\n\n    struct Swap {\n        address pool;\n        address tokenIn;\n        address tokenOut;\n        uint    swapAmount; // tokenInAmount / tokenOutAmount\n        uint    limitReturnAmount; // minAmountOut / maxAmountIn\n        uint    maxPrice;\n    }\n\n    RegistryInterface public immutable registry;\n    uint256 private constant BONE = 10**18;\n    uint256 private constant NPOOLS = 3;\n\n    constructor(address registry_, address weth_) public BaseAdapter(weth_) {\n        registry = RegistryInterface(registry_);\n    }\n\n    /*\n     * WARNING: This function can be called by anyone! Never approve this contract\n     * to transfer your tokens. It should only ever be called by a contract which\n     * approves an exact token amount and immediately swaps the tokens OR is used\n     * in a delegate call where this contract NEVER gets approved to transfer tokens.\n     */\n    function swap(\n        uint256 amount,\n        uint256 expected,\n        address tokenIn,\n        address tokenOut,\n        address from,\n        address to\n    ) public override {\n        require(tokenIn != tokenOut, \"Tokens cannot match\");\n        (Swap[] memory swaps, ) = _viewSplitExactIn(tokenIn, tokenOut, amount, NPOOLS, false);\n        _batchSwapExactIn(swaps, IERC20(tokenIn), IERC20(tokenOut), from, to, amount, expected);\n    }\n\n    function _batchSwapExactIn(\n        Swap[] memory swaps,\n        IERC20 tokenIn,\n        IERC20 tokenOut,\n        address from,\n        address to,\n        uint totalAmountIn,\n        uint minTotalAmountOut\n    )\n        internal\n        returns (uint totalAmountOut)\n    {\n        if (from != address(this))\n            tokenIn.safeTransferFrom(from, address(this), totalAmountIn);\n\n        for (uint i = 0; i < swaps.length; i++) {\n            Swap memory _swap = swaps[i];\n            IERC20 SwapTokenIn = IERC20(_swap.tokenIn);\n            PoolInterface pool = PoolInterface(_swap.pool);\n\n            if (SwapTokenIn.allowance(address(this), _swap.pool) != 0) {\n                SwapTokenIn.safeApprove(_swap.pool, 0);\n            }\n            SwapTokenIn.safeApprove(_swap.pool, _swap.swapAmount);\n\n            (uint tokenAmountOut,) = pool.swapExactAmountIn(\n                                        _swap.tokenIn,\n                                        _swap.swapAmount,\n                                        _swap.tokenOut,\n                                        _swap.limitReturnAmount,\n                                        _swap.maxPrice\n                                    );\n            totalAmountOut = tokenAmountOut.add(totalAmountOut);\n        }\n\n        require(totalAmountOut >= minTotalAmountOut, \"ERR_LIMIT_OUT\");\n\n        if (to != address(this))\n            tokenOut.safeTransfer(to, totalAmountOut);\n        if (from != address(this))\n            tokenIn.safeTransfer(from, tokenIn.balanceOf(address(this))); //Return unused funds\n    }\n\n    function _viewSplitExactIn(\n        address tokenIn,\n        address tokenOut,\n        uint swapAmount,\n        uint nPools,\n        bool spot\n    )\n        internal view\n        returns (Swap[] memory swaps, uint totalOutput)\n    {\n        address[] memory poolAddresses = registry.getBestPoolsWithLimit(tokenIn, tokenOut, nPools);\n\n        Pool[] memory pools = new Pool[](poolAddresses.length);\n        uint sumEffectiveLiquidity;\n        for (uint i = 0; i < poolAddresses.length; i++) {\n            pools[i] = _getPoolData(tokenIn, tokenOut, poolAddresses[i]);\n            sumEffectiveLiquidity = sumEffectiveLiquidity.add(pools[i].effectiveLiquidity);\n        }\n\n        uint[] memory bestInputAmounts = new uint[](pools.length);\n        uint totalInputAmount;\n        for (uint i = 0; i < pools.length; i++) {\n            bestInputAmounts[i] = swapAmount.mul(pools[i].effectiveLiquidity).div(sumEffectiveLiquidity);\n            totalInputAmount = totalInputAmount.add(bestInputAmounts[i]);\n        }\n\n        if (totalInputAmount < swapAmount) {\n            bestInputAmounts[0] = bestInputAmounts[0].add(swapAmount.sub(totalInputAmount));\n        } else {\n            bestInputAmounts[0] = bestInputAmounts[0].sub(totalInputAmount.sub(swapAmount));\n        }\n\n        swaps = new Swap[](pools.length);\n\n        for (uint i = 0; i < pools.length; i++) {\n            swaps[i] = Swap({\n                        pool: pools[i].pool,\n                        tokenIn: tokenIn,\n                        tokenOut: tokenOut,\n                        swapAmount: bestInputAmounts[i],\n                        limitReturnAmount: 0,\n                        maxPrice: uint(-1)\n                    });\n        }\n\n        totalOutput = _calcTotalOutExactIn(bestInputAmounts, pools, spot);\n\n        return (swaps, totalOutput);\n    }\n\n    function _getPoolData(\n        address tokenIn,\n        address tokenOut,\n        address poolAddress\n    )\n        internal view\n        returns (Pool memory)\n    {\n        PoolInterface pool = PoolInterface(poolAddress);\n        uint tokenBalanceIn = pool.getBalance(tokenIn);\n        uint tokenBalanceOut = pool.getBalance(tokenOut);\n        uint tokenWeightIn = pool.getDenormalizedWeight(tokenIn);\n        uint tokenWeightOut = pool.getDenormalizedWeight(tokenOut);\n        uint swapFee = pool.getSwapFee();\n\n        uint effectiveLiquidity = _calcEffectiveLiquidity(\n                                            tokenWeightIn,\n                                            tokenBalanceOut,\n                                            tokenWeightOut\n                                        );\n        Pool memory returnPool = Pool({\n            pool: poolAddress,\n            tokenBalanceIn: tokenBalanceIn,\n            tokenWeightIn: tokenWeightIn,\n            tokenBalanceOut: tokenBalanceOut,\n            tokenWeightOut: tokenWeightOut,\n            swapFee: swapFee,\n            effectiveLiquidity: effectiveLiquidity\n        });\n\n        return returnPool;\n    }\n\n    function _calcEffectiveLiquidity(\n        uint tokenWeightIn,\n        uint tokenBalanceOut,\n        uint tokenWeightOut\n    )\n        internal pure\n        returns (uint effectiveLiquidity)\n    {\n\n        // Bo * wi/(wi+wo)\n        effectiveLiquidity =\n            tokenWeightIn.mul(BONE).div(\n                tokenWeightOut.add(tokenWeightIn)\n            ).mul(tokenBalanceOut).div(BONE);\n\n        return effectiveLiquidity;\n    }\n\n    function _calcTotalOutExactIn(\n        uint[] memory bestInputAmounts,\n        Pool[] memory bestPools,\n        bool spot\n    )\n        internal pure\n        returns (uint totalOutput)\n    {\n        totalOutput = 0;\n        for (uint i = 0; i < bestInputAmounts.length; i++) {\n            uint output = PoolInterface(bestPools[i].pool).calcOutGivenIn(\n                                bestPools[i].tokenBalanceIn,\n                                bestPools[i].tokenWeightIn,\n                                bestPools[i].tokenBalanceOut,\n                                bestPools[i].tokenWeightOut,\n                                bestInputAmounts[i],\n                                spot ? 0 : bestPools[i].swapFee\n                            );\n\n            totalOutput = totalOutput.add(output);\n        }\n        return totalOutput;\n    }\n}"
}