Skip to content

Commit

Permalink
Allow only one CL pool for each token pair used for routing
Browse files Browse the repository at this point in the history
  • Loading branch information
AkemiHomura-maow committed Sep 2, 2024
1 parent 269ee2f commit a3c964e
Showing 1 changed file with 12 additions and 40 deletions.
52 changes: 12 additions & 40 deletions contracts/VeloOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a3c964e

Please sign in to comment.