{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/test/BalancerRegistry.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract BalancerRegistry {\n    using SafeMath for uint256;\n    using EnumerableSet for EnumerableSet.AddressSet;\n\n    struct PoolPairInfo {\n        uint80 weight1;\n        uint80 weight2;\n        uint80 swapFee;\n        uint256 liq;\n    }\n\n    struct SortedPools {\n        EnumerableSet.AddressSet pools;\n        bytes32 indices;\n    }\n\n    event PoolTokenPairAdded(\n        address indexed pool,\n        address indexed token1,\n        address indexed token2\n    );\n\n    event IndicesUpdated(\n        address indexed token1,\n        address indexed token2,\n        bytes32 oldIndices,\n        bytes32 newIndices\n    );\n\n    uint private constant BONE = 10**18;\n    uint private constant MAX_SWAP_FEE = (3 * BONE) / 100;\n\n    mapping(bytes32 => SortedPools) private _pools;\n    mapping(address => mapping(bytes32 => PoolPairInfo)) private _infos;\n\n    IBFactory public bfactory;\n\n    constructor(address _bfactory) public {\n        bfactory = IBFactory(_bfactory);\n    }\n\n    function getPairInfo(address pool, address fromToken, address destToken)\n        external view returns(uint256 weight1, uint256 weight2, uint256 swapFee)\n    {\n        bytes32 key = _createKey(fromToken, destToken);\n        PoolPairInfo memory info = _infos[pool][key];\n        return (info.weight1, info.weight2, info.swapFee);\n    }\n\n    function getPoolsWithLimit(address fromToken, address destToken, uint256 offset, uint256 limit)\n        public view returns(address[] memory result)\n    {\n        bytes32 key = _createKey(fromToken, destToken);\n        result = new address[](Math.min(limit, _pools[key].pools.length() - offset));\n        for (uint i = 0; i < result.length; i++) {\n            result[i] = _pools[key].pools.at(offset + i);\n        }\n    }\n\n    function getBestPools(address fromToken, address destToken)\n        external view returns(address[] memory pools)\n    {\n        return getBestPoolsWithLimit(fromToken, destToken, 32);\n    }\n\n    function getBestPoolsWithLimit(address fromToken, address destToken, uint256 limit)\n        public view returns(address[] memory pools)\n    {\n        bytes32 key = _createKey(fromToken, destToken);\n        bytes32 indices = _pools[key].indices;\n        uint256 len = 0;\n        while (indices[len] > 0 && len < Math.min(limit, indices.length)) {\n            len++;\n        }\n\n        pools = new address[](len);\n        for (uint i = 0; i < len; i++) {\n            uint256 index = uint256(uint8(indices[i])).sub(1);\n            pools[i] = _pools[key].pools.at(index);\n        }\n    }\n\n    // Add and update registry\n\n    function addPoolPair(address pool, address token1, address token2) public returns(uint256 listed) {\n\n        require(bfactory.isBPool(pool), \"ERR_NOT_BPOOL\");\n\n        uint256 swapFee = IBPool(pool).getSwapFee();\n        require(swapFee <= MAX_SWAP_FEE, \"ERR_FEE_TOO_HIGH\");\n\n        bytes32 key = _createKey(token1, token2);\n        _pools[key].pools.add(pool);\n\n         if (token1 < token2) {\n            _infos[pool][key] = PoolPairInfo({\n                weight1: uint80(IBPool(pool).getDenormalizedWeight(token1)),\n                weight2: uint80(IBPool(pool).getDenormalizedWeight(token2)),\n                swapFee: uint80(swapFee),\n                liq: uint256(0)\n            });\n        } else {\n            _infos[pool][key] = PoolPairInfo({\n                weight1: uint80(IBPool(pool).getDenormalizedWeight(token2)),\n                weight2: uint80(IBPool(pool).getDenormalizedWeight(token1)),\n                swapFee: uint80(swapFee),\n                liq: uint256(0)\n            });\n        }\n\n        emit PoolTokenPairAdded(\n            pool,\n            token1,\n            token2\n        );\n\n        listed++;\n\n    }\n\n    function addPools(address[] calldata pools, address token1, address token2) external returns(uint256[] memory listed) {\n        listed = new uint256[](pools.length);\n        for (uint i = 0; i < pools.length; i++) {\n            listed[i] = addPoolPair(pools[i], token1, token2);\n        }\n    }\n\n    function sortPools(address[] calldata tokens, uint256 lengthLimit) external {\n        for (uint i = 0; i < tokens.length; i++) {\n            for (uint j = i + 1; j < tokens.length; j++) {\n                bytes32 key = _createKey(tokens[i], tokens[j]);\n                address[] memory pools = getPoolsWithLimit(tokens[i], tokens[j], 0, Math.min(256, lengthLimit));\n                uint256[] memory effectiveLiquidity = _getEffectiveLiquidityForPools(tokens[i], tokens[j], pools);\n\n                bytes32 indices = _buildSortIndices(effectiveLiquidity);\n\n                if (indices != _pools[key].indices) {\n                    emit IndicesUpdated(\n                        tokens[i] < tokens[j] ? tokens[i] : tokens[j],\n                        tokens[i] < tokens[j] ? tokens[j] : tokens[i],\n                        _pools[key].indices,\n                        indices\n                    );\n                    _pools[key].indices = indices;\n                }\n            }\n        }\n    }\n\n    function sortPoolsWithPurge(address[] calldata tokens, uint256 lengthLimit) external {\n        for (uint i = 0; i < tokens.length; i++) {\n            for (uint j = i + 1; j < tokens.length; j++) {\n                bytes32 key = _createKey(tokens[i], tokens[j]);\n                address[] memory pools = getPoolsWithLimit(tokens[i], tokens[j], 0, Math.min(256, lengthLimit));\n                uint256[] memory effectiveLiquidity = _getEffectiveLiquidityForPoolsPurge(tokens[i], tokens[j], pools);\n                bytes32 indices = _buildSortIndices(effectiveLiquidity);\n\n                if (indices != _pools[key].indices) {\n                    emit IndicesUpdated(\n                        tokens[i] < tokens[j] ? tokens[i] : tokens[j],\n                        tokens[i] < tokens[j] ? tokens[j] : tokens[i],\n                        _pools[key].indices,\n                        indices\n                    );\n                    _pools[key].indices = indices;\n                }\n            }\n        }\n    }\n\n    // Internal\n\n    function _createKey(address token1, address token2)\n        internal pure returns(bytes32)\n    {\n        return bytes32(\n            (uint256(uint128((token1 < token2) ? token1 : token2)) << 128) |\n            (uint256(uint128((token1 < token2) ? token2 : token1)))\n        );\n    }\n\n    function _getEffectiveLiquidityForPools(address token1, address token2, address[] memory pools)\n        internal view returns(uint256[] memory effectiveLiquidity)\n    {\n        effectiveLiquidity = new uint256[](pools.length);\n        for (uint i = 0; i < pools.length; i++) {\n            bytes32 key = _createKey(token1, token2);\n            PoolPairInfo memory info = _infos[pools[i]][key];\n            if (token1 < token2) {\n                // we define effective liquidity as b2 * w1 / (w1 + w2)\n                effectiveLiquidity[i] = bdiv(uint256(info.weight1),uint256(info.weight1).add(uint256(info.weight2)));\n                effectiveLiquidity[i] = effectiveLiquidity[i].mul(IBPool(pools[i]).getBalance(token2));\n            } else {\n                effectiveLiquidity[i] = bdiv(uint256(info.weight2),uint256(info.weight1).add(uint256(info.weight2)));\n                effectiveLiquidity[i] = effectiveLiquidity[i].mul(IBPool(pools[i]).getBalance(token2));\n            }\n        }\n    }\n\n    // Calculates total liquidity for all existing token pair pools\n    // Removes any that are below threshold\n    function _getEffectiveLiquidityForPoolsPurge(address token1, address token2, address[] memory pools)\n        public returns(uint256[] memory effectiveLiquidity)\n    {\n        uint256 totalLiq = 0;\n        bytes32 key = _createKey(token1, token2);\n\n        // Store each pools liquidity and sum total liquidity\n        for (uint i = 0; i < pools.length; i++) {\n            PoolPairInfo memory info = _infos[pools[i]][key];\n            if (token1 < token2) {\n                // we define effective liquidity as b2 * w1 / (w1 + w2)\n                _infos[pools[i]][key].liq = bdiv(uint256(info.weight1), uint256(info.weight1).add(uint256(info.weight2)));\n                _infos[pools[i]][key].liq = _infos[pools[i]][key].liq.mul(IBPool(pools[i]).getBalance(token2));\n                totalLiq = totalLiq.add(_infos[pools[i]][key].liq);\n            } else {\n                _infos[pools[i]][key].liq = bdiv(uint256(info.weight2), uint256(info.weight1).add(uint256(info.weight2)));\n                _infos[pools[i]][key].liq = _infos[pools[i]][key].liq.mul(IBPool(pools[i]).getBalance(token2));\n                totalLiq = totalLiq.add(_infos[pools[i]][key].liq);\n            }\n        }\n\n        uint256 threshold = bmul(totalLiq, ((10 * BONE) / 100));\n\n        // Delete any pools that aren't greater than threshold (10% of total)\n        for(uint i = 0;i < _pools[key].pools.length();i++){\n            if(_infos[_pools[key].pools.at(i)][key].liq < threshold){\n                _pools[key].pools.remove(_pools[key].pools.at(i));\n            }\n        }\n\n        effectiveLiquidity = new uint256[](_pools[key].pools.length());\n\n        // pool.remove reorders pools so need to use correct liq for index\n        for(uint i = 0;i < _pools[key].pools.length();i++){\n            effectiveLiquidity[i] = _infos[_pools[key].pools.at(i)][key].liq;\n        }\n    }\n\n    function bdiv(uint a, uint b)\n        internal pure\n        returns (uint)\n    {\n        require(b != 0, \"ERR_DIV_ZERO\");\n        uint c0 = a * BONE;\n        require(a == 0 || c0 / a == BONE, \"ERR_DIV_INTERNAL\"); // bdiv overflow\n        uint c1 = c0 + (b / 2);\n        require(c1 >= c0, \"ERR_DIV_INTERNAL\"); //  badd require\n        uint c2 = c1 / b;\n        return c2;\n    }\n\n    function bmul(uint a, uint b)\n        internal pure\n        returns (uint)\n    {\n        uint c0 = a * b;\n        require(a == 0 || c0 / a == b, \"ERR_MUL_OVERFLOW\");\n        uint c1 = c0 + (BONE / 2);\n        require(c1 >= c0, \"ERR_MUL_OVERFLOW\");\n        uint c2 = c1 / BONE;\n        return c2;\n    }\n\n    function _buildSortIndices(uint256[] memory effectiveLiquidity)\n        internal pure returns(bytes32)\n    {\n        uint256 result = 0;\n        uint256 prevEffectiveLiquidity = uint256(-1);\n        for (uint i = 0; i < Math.min(effectiveLiquidity.length, 32); i++) {\n            uint256 bestIndex = 0;\n            for (uint j = 0; j < effectiveLiquidity.length; j++) {\n                if ((effectiveLiquidity[j] > effectiveLiquidity[bestIndex] && effectiveLiquidity[j] < prevEffectiveLiquidity) || effectiveLiquidity[bestIndex] >= prevEffectiveLiquidity) {\n                    bestIndex = j;\n                }\n            }\n            prevEffectiveLiquidity = effectiveLiquidity[bestIndex];\n            result |= (bestIndex + 1) << (248 - i * 8);\n        }\n        return bytes32(result);\n    }\n}"
}