From a3c964ecbf9929ac91c8cafdf4e84db2081aec3c Mon Sep 17 00:00:00 2001 From: AkemiHomura-maow <34213712+AkemiHomura-maow@users.noreply.github.com> Date: Mon, 2 Sep 2024 12:17:22 +0800 Subject: [PATCH] Allow only one CL pool for each token pair used for routing --- contracts/VeloOracle.sol | 52 ++++++++++------------------------------ 1 file changed, 12 insertions(+), 40 deletions(-) diff --git a/contracts/VeloOracle.sol b/contracts/VeloOracle.sol index a768959..902e297 100644 --- a/contracts/VeloOracle.sol +++ b/contracts/VeloOracle.sol @@ -27,7 +27,7 @@ contract VeloOracle is IVeloOracle { ICLFactory public immutable CLFactory; address owner; - mapping(address => mapping(address => ICLPool[])) public enabledCLPools; + mapping(address => mapping(address => ICLPool)) public enabledCLPools; /// @notice Maximum number of hops allowed for rate calculations uint8 maxHop = 10; @@ -90,56 +90,29 @@ contract VeloOracle is IVeloOracle { } /// @notice Permissioned function to enable routing through certain CL pools - function enableCLPairTickSpacing(CLPairParams[] calldata params) public { + function enableCLPairs(CLPairParams[] calldata params) public { require(msg.sender == owner); for (uint256 i; i < params.length; i++) { CLPairParams memory param = params[i]; address pair = CLFactory.getPool(param.tokenA, param.tokenB, param.tickSpacing); require(pair != address(0x0)); - enabledCLPools[param.tokenA][param.tokenB].push(ICLPool(pair)); - enabledCLPools[param.tokenB][param.tokenA].push(ICLPool(pair)); + enabledCLPools[param.tokenA][param.tokenB] = ICLPool(pair); + enabledCLPools[param.tokenB][param.tokenA] = ICLPool(pair); } } /// @notice Permissioned function to disable routing through certain CL pools - function disableCLPairTickSpacing(CLPairParams[] calldata params) public { + function disableCLPairs(CLPairParams[] calldata params) public { require(msg.sender == owner); - for (uint256 i; i < params.length; i++) { - CLPairParams memory param = params[i]; - address pair = CLFactory.getPool(param.tokenA, param.tokenB, param.tickSpacing); - require(pair != address(0x0)); - - // Remove the CLPool from the enabledCLPools mappings - removeCLPool(param.tokenA, param.tokenB, pair); - removeCLPool(param.tokenB, param.tokenA, pair); - } - } - - function removeCLPool(address tokenA, address tokenB, address pair) private { - // Find the index of the pair in the array for tokenA to tokenB - uint256 index = findCLPoolIndex(enabledCLPools[tokenA][tokenB], pair); - require(index < enabledCLPools[tokenA][tokenB].length, "Pair not found"); - // Remove the pair by swapping it with the last element and then popping the array - enabledCLPools[tokenA][tokenB][index] = enabledCLPools[tokenA][tokenB][enabledCLPools[tokenA][tokenB].length - 1]; - enabledCLPools[tokenA][tokenB].pop(); - - // Repeat for tokenB to tokenA - index = findCLPoolIndex(enabledCLPools[tokenB][tokenA], pair); - require(index < enabledCLPools[tokenB][tokenA].length, "Pair not found"); - enabledCLPools[tokenB][tokenA][index] = enabledCLPools[tokenB][tokenA][enabledCLPools[tokenB][tokenA].length - 1]; - enabledCLPools[tokenB][tokenA].pop(); - } + for (uint256 i; i < params.length; i++) { + CLPairParams memory param = params[i]; + delete enabledCLPools[param.tokenA][param.tokenA]; + delete enabledCLPools[param.tokenA][param.tokenB]; - function findCLPoolIndex(ICLPool[] storage pools, address pair) private view returns (uint256) { - for (uint256 i = 0; i < pools.length; i++) { - if (address(pools[i]) == pair) { - return i; - } } - // Return an out-of-bounds index if the pair is not found - return pools.length; } + /// @notice Internal function to get balance of two tokens /// @param from First token of the pair /// @param to Second token of the pair @@ -383,10 +356,9 @@ contract VeloOracle is IVeloOracle { { uint256 maxLiquidity; bool isSrcToken0 = srcToken < dstToken; - ICLPool[] memory pools = enabledCLPools[address(srcToken)][address(dstToken)]; - for (uint256 i; i < pools.length; i++) { - ICLPool pool = pools[i]; + ICLPool pool = enabledCLPools[address(srcToken)][address(dstToken)]; + if (address(pool) != address(0x0)){ uint256 liquidity = uint256(pool.liquidity()); if (liquidity > maxLiquidity) {