Skip to content

Commit

Permalink
refactor: more readable names
Browse files Browse the repository at this point in the history
  • Loading branch information
danielattilasimon committed Feb 9, 2025
1 parent 628c4a2 commit f8f3b3e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
9 changes: 7 additions & 2 deletions contracts/src/Zappers/Interfaces/IExchangeHelpersV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
pragma solidity ^0.8.0;

interface IExchangeHelpersV2 {
function getDy(uint256 _dx, bool _collToBold, address _collToken) external returns (uint256 dy);
function getDx(uint256 _dy, bool _collToBold, address _collToken) external returns (uint256 dx);
function quoteExactInput(uint256 _inputAmount, bool _collToBold, address _collToken)
external
returns (uint256 outputAmount);

function quoteExactOutput(uint256 _outputAmount, bool _collToBold, address _collToken)
external
returns (uint256 inputAmount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ contract HybridCurveUniV3ExchangeHelpersV2 is IExchangeHelpersV2 {
uniV3Quoter = _uniV3Quoter;
}

function getDy(uint256 _dx, bool _collToBold, address _collToken) external returns (uint256 dy) {
function quoteExactInput(uint256 _inputAmount, bool _collToBold, address _collToken)
external
returns (uint256 outputAmount)
{
if (_collToBold) {
// (Coll ->) WETH -> USDC?
bytes memory path;
Expand All @@ -56,13 +59,13 @@ contract HybridCurveUniV3ExchangeHelpersV2 is IExchangeHelpersV2 {
path = abi.encodePacked(_collToken, feeWethColl, WETH, feeUsdcWeth, USDC);
}

(uint256 uniDy,,,) = uniV3Quoter.quoteExactInput(path, _dx);
(uint256 intermediateAmount,,,) = uniV3Quoter.quoteExactInput(path, _inputAmount);

// USDC -> BOLD?
dy = curvePool.get_dy(USDC_INDEX, BOLD_TOKEN_INDEX, uniDy);
outputAmount = curvePool.get_dy(USDC_INDEX, BOLD_TOKEN_INDEX, intermediateAmount);
} else {
// BOLD -> USDC?
uint256 curveDy = curvePool.get_dy(BOLD_TOKEN_INDEX, USDC_INDEX, _dx);
uint256 intermediateAmount = curvePool.get_dy(BOLD_TOKEN_INDEX, USDC_INDEX, _inputAmount);

// USDC -> WETH (-> Coll)?
bytes memory path;
Expand All @@ -72,14 +75,17 @@ contract HybridCurveUniV3ExchangeHelpersV2 is IExchangeHelpersV2 {
path = abi.encodePacked(USDC, feeUsdcWeth, WETH, feeWethColl, _collToken);
}

(dy,,,) = uniV3Quoter.quoteExactInput(path, curveDy);
(outputAmount,,,) = uniV3Quoter.quoteExactInput(path, intermediateAmount);
}
}

function getDx(uint256 _dy, bool _collToBold, address _collToken) external returns (uint256 dx) {
function quoteExactOutput(uint256 _outputAmount, bool _collToBold, address _collToken)
external
returns (uint256 inputAmount)
{
if (_collToBold) {
// USDC? -> BOLD
uint256 curveDx = curvePool.get_dx(USDC_INDEX, BOLD_TOKEN_INDEX, _dy);
uint256 intermediateAmount = curvePool.get_dx(USDC_INDEX, BOLD_TOKEN_INDEX, _outputAmount);

// Uniswap expects path to be reversed when quoting exact output
// USDC <- WETH (<- Coll)?
Expand All @@ -90,7 +96,7 @@ contract HybridCurveUniV3ExchangeHelpersV2 is IExchangeHelpersV2 {
path = abi.encodePacked(USDC, feeUsdcWeth, WETH, feeWethColl, _collToken);
}

(dx,,,) = uniV3Quoter.quoteExactOutput(path, curveDx);
(inputAmount,,,) = uniV3Quoter.quoteExactOutput(path, intermediateAmount);
} else {
// Uniswap expects path to be reversed when quoting exact output
// (Coll <-) WETH <- USDC?
Expand All @@ -101,10 +107,10 @@ contract HybridCurveUniV3ExchangeHelpersV2 is IExchangeHelpersV2 {
path = abi.encodePacked(_collToken, feeWethColl, WETH, feeUsdcWeth, USDC);
}

(uint256 uniDx,,,) = uniV3Quoter.quoteExactOutput(path, _dy);
(uint256 intermediateAmount,,,) = uniV3Quoter.quoteExactOutput(path, _outputAmount);

// BOLD? -> USDC
dx = curvePool.get_dx(BOLD_TOKEN_INDEX, USDC_INDEX, uniDx);
inputAmount = curvePool.get_dx(BOLD_TOKEN_INDEX, USDC_INDEX, intermediateAmount);
}
}
}
26 changes: 16 additions & 10 deletions contracts/test/ExchangeHelpers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ contract ExchangeHelpersTest is Test, UseDeployment {
dyExpected = bound(dyExpected, 0.001 ether, 100 ether);
}

uint256 dx = exchangeHelpersV2_getDx(dyExpected, collToBold, collToken);
uint256 dx = exchangeHelpersV2_quoteExactOutput(dyExpected, collToBold, collToken);
uint256 balance0 = IERC20(outputToken).balanceOf(address(this));
deal(inputToken, address(this), dx);
IERC20(inputToken).approve(address(exchange[collToken]), dx);
Expand All @@ -192,7 +192,7 @@ contract ExchangeHelpersTest is Test, UseDeployment {
dx = bound(dx, 1 ether, 100_000 ether);
}

uint256 dyExpected = exchangeHelpersV2_getDy(dx, collToBold, collToken);
uint256 dyExpected = exchangeHelpersV2_quoteExactInput(dx, collToBold, collToken);
uint256 balance0 = IERC20(outputToken).balanceOf(address(this));
deal(inputToken, address(this), dx);
IERC20(inputToken).approve(address(exchange[collToken]), dx);
Expand Down Expand Up @@ -235,24 +235,30 @@ contract ExchangeHelpersTest is Test, UseDeployment {
}
}

function exchangeHelpersV2_getDx_throw(uint256 dy, bool collToBold, address collToken) external {
revert QuoteResult(exchangeHelpersV2.getDx(dy, collToBold, collToken));
function exchangeHelpersV2_quoteExactOutput_throw(uint256 dy, bool collToBold, address collToken) external {
revert QuoteResult(exchangeHelpersV2.quoteExactOutput(dy, collToBold, collToken));
}

function exchangeHelpersV2_getDx(uint256 dy, bool collToBold, address collToken) internal returns (uint256) {
try this.exchangeHelpersV2_getDx_throw(dy, collToBold, collToken) {
function exchangeHelpersV2_quoteExactOutput(uint256 dy, bool collToBold, address collToken)
internal
returns (uint256)
{
try this.exchangeHelpersV2_quoteExactOutput_throw(dy, collToBold, collToken) {
revert("Should have reverted");
} catch (bytes memory revertData) {
return _decodeQuoteResult(revertData);
}
}

function exchangeHelpersV2_getDy_throw(uint256 dx, bool collToBold, address collToken) external {
revert QuoteResult(exchangeHelpersV2.getDy(dx, collToBold, collToken));
function exchangeHelpersV2_quoteExactInput_throw(uint256 dx, bool collToBold, address collToken) external {
revert QuoteResult(exchangeHelpersV2.quoteExactInput(dx, collToBold, collToken));
}

function exchangeHelpersV2_getDy(uint256 dx, bool collToBold, address collToken) internal returns (uint256) {
try this.exchangeHelpersV2_getDy_throw(dx, collToBold, collToken) {
function exchangeHelpersV2_quoteExactInput(uint256 dx, bool collToBold, address collToken)
internal
returns (uint256)
{
try this.exchangeHelpersV2_quoteExactInput_throw(dx, collToBold, collToken) {
revert("Should have reverted");
} catch (bytes memory revertData) {
return _decodeQuoteResult(revertData);
Expand Down

0 comments on commit f8f3b3e

Please sign in to comment.