diff --git a/contracts/PriceFetcher.sol b/contracts/PriceFetcher.sol index 8551cad..4116289 100644 --- a/contracts/PriceFetcher.sol +++ b/contracts/PriceFetcher.sol @@ -26,15 +26,15 @@ contract PriceFetcher { } } - function add_tokens(bool is_connector, address[] calldata _tokens) public{ + function add_tokens(bool is_connector, address[] calldata _tokens) public { _only_owner(); IERC20Metadata[] storage arr = is_connector ? connectors : source_tokens; for (uint256 i = 0; i < _tokens.length; i++) { - arr.push( IERC20Metadata(_tokens[i]) ); + arr.push(IERC20Metadata(_tokens[i])); } } - function remove_tokens(bool is_connector, uint256[] calldata _indices) public{ + function remove_tokens(bool is_connector, uint256[] calldata _indices) public { _only_owner(); IERC20Metadata[] storage arr = is_connector ? connectors : source_tokens; for (uint256 i = 0; i < _indices.length; i++) { @@ -43,12 +43,16 @@ contract PriceFetcher { } } - function change_USDC(address _USDC) public{ + function change_USDC(address _USDC) public { _only_owner(); USDC = IERC20Metadata(_USDC); } - function _construct_oracle_args(uint256 _src_len, uint256 _src_offset) internal view returns (IERC20Metadata[] memory oracle_args){ + function _construct_oracle_args(uint256 _src_len, uint256 _src_offset) + internal + view + returns (IERC20Metadata[] memory oracle_args) + { oracle_args = new IERC20Metadata[]( _src_len + connectors.length + 1); for (uint256 i = _src_offset; i < _src_offset + _src_len; i++) { oracle_args[i - _src_offset] = source_tokens[i]; @@ -72,7 +76,7 @@ contract PriceFetcher { USDC = IERC20Metadata(_USDC); } - function _only_owner() internal view{ + function _only_owner() internal view { require(msg.sender == owner); } @@ -111,15 +115,15 @@ contract PriceFetcher { /// @notice Fetches prices for a list of tokens. /// @dev Can only be called by the owner. Emits a PriceFetched event for each token. /// @param _src_len The number of source tokens to fetch prices for. - /// @param _src_offset The number of source tokens to skip. - function fetchPrices(uint _src_len, uint _src_offset) public { + /// @param _src_offset The number of source tokens to skip. + function fetchPrices(uint256 _src_len, uint256 _src_offset) public { require(msg.sender == owner || callers[msg.sender]); - uint256[] memory prices = oracle.getManyRatesWithConnectors(uint8(_src_len), _construct_oracle_args(_src_len, _src_offset)); + uint256[] memory prices = + oracle.getManyRatesWithConnectors(uint8(_src_len), _construct_oracle_args(_src_len, _src_offset)); - for (uint i = _src_offset; i < _src_offset + _src_len; i++) { - emit PriceFetched( address(source_tokens[i]), prices[i - _src_offset]); + for (uint256 i = _src_offset; i < _src_offset + _src_len; i++) { + emit PriceFetched(address(source_tokens[i]), prices[i - _src_offset]); } } - } diff --git a/contracts/VeloOracle.sol b/contracts/VeloOracle.sol index 4fcfa78..8ca3e2e 100644 --- a/contracts/VeloOracle.sol +++ b/contracts/VeloOracle.sol @@ -78,7 +78,7 @@ contract VeloOracle is IVeloOracle { } /// @notice Struct to hold variables needed to identify CL pools - struct CLPairParams{ + struct CLPairParams { address tokenA; address tokenB; int24 tickSpacing; @@ -90,14 +90,14 @@ contract VeloOracle is IVeloOracle { } /// @notice Permissioned function to enable routing through certain CL pools - function enableCLPairTickSpacing(CLPairParams[] calldata params) public{ + function enableCLPairTickSpacing(CLPairParams[] calldata params) public { require(msg.sender == owner); - for (uint256 i; i < params.length; i++){ + 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].push(ICLPool(pair)); + enabledCLPools[param.tokenB][param.tokenA].push(ICLPool(pair)); } } @@ -143,7 +143,6 @@ contract VeloOracle is IVeloOracle { out.mod = true; (out.bal0, out.bal1, out.isStable) = (maxPair1, maxPair2, isMaxStable); } - } /** @@ -281,7 +280,7 @@ contract VeloOracle is IVeloOracle { uint256 newOut = 0; // newOut in t1 - try IVeloPair(currentPair).getAmountOut((10**t0_dec), address(t0)) returns (uint256 result) { + try IVeloPair(currentPair).getAmountOut((10 ** t0_dec), address(t0)) returns (uint256 result) { newOut = result; } catch { return 0; @@ -341,29 +340,27 @@ contract VeloOracle is IVeloOracle { function _getVirtualBalances(IERC20Metadata srcToken, IERC20Metadata dstToken) internal view - returns (uint256 srcVirtualBalance, uint256 dstVirtualBalance){ + returns (uint256 srcVirtualBalance, uint256 dstVirtualBalance) + { + uint256 maxLiquidity; + bool isSrcToken0 = srcToken < dstToken; + ICLPool[] memory pools = enabledCLPools[address(srcToken)][address(dstToken)]; - 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]; + uint256 liquidity = uint256(pool.liquidity()); - for(uint256 i; i < pools.length; i++){ - ICLPool pool = pools[i]; - uint256 liquidity = uint256(pool.liquidity()); + if (liquidity > maxLiquidity) { + (uint160 sqrtPriceX96,,,,,) = pool.slot0(); - if (liquidity > maxLiquidity){ - (uint160 sqrtPriceX96, , , , ,) = pool.slot0(); + (srcVirtualBalance, dstVirtualBalance) = isSrcToken0 + ? ((liquidity << 96) / sqrtPriceX96, (liquidity * (sqrtPriceX96 >> 32)) >> 64) + : ((liquidity * (sqrtPriceX96 >> 32)) >> 64, (liquidity << 96) / sqrtPriceX96); - (srcVirtualBalance, dstVirtualBalance) = - isSrcToken0 ? - ( (liquidity << 96) / sqrtPriceX96 , (liquidity * (sqrtPriceX96 >> 32 ) ) >> 64 ) - : ( (liquidity * (sqrtPriceX96 >> 32 ) ) >> 64 , (liquidity << 96) / sqrtPriceX96 ); - - maxLiquidity = liquidity; - } + maxLiquidity = liquidity; } - } + } /// @notice Internal function to fetch the pair from tokens using correct order /// @param tokenA First input token @@ -387,4 +384,3 @@ contract VeloOracle is IVeloOracle { return a < b ? a : b; } } -