Skip to content

Commit

Permalink
add gas estimate return
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 9, 2024
1 parent d3b0261 commit fdb6244
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149027
149138
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154511
154622
2 changes: 1 addition & 1 deletion .forge-snapshots/Quoter_exactOutputSingle_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
83641
83747
2 changes: 1 addition & 1 deletion .forge-snapshots/Quoter_exactOutputSingle_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88449
88555
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128559
128751
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153480
153672
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87503
87695
2 changes: 1 addition & 1 deletion .forge-snapshots/Quoter_quoteExactInput_twoHops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212546
212738
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148306
148498
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178443
178635
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148374
148566
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125073
125265
2 changes: 1 addition & 1 deletion .forge-snapshots/Quoter_quoteExactOutput_twoHops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
252910
253102
8 changes: 4 additions & 4 deletions src/interfaces/IQuoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface IQuoter {
/// @return sqrtPriceX96After The sqrt price of the pool after the swap
function quoteExactInputSingle(QuoteExactSingleParams memory params)
external
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After);
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint256 gasEstimate);

/// @notice Returns the delta amounts along the swap path for a given exact input swap
/// @param params the params for the quote, encoded as 'QuoteExactParams'
Expand All @@ -59,7 +59,7 @@ interface IQuoter {
/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each pool in the path
function quoteExactInput(QuoteExactParams memory params)
external
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList);
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, uint256 gasEstimate);

/// @notice Returns the delta amounts for a given exact output swap of a single pool
/// @param params The params for the quote, encoded as `QuoteExactSingleParams`
Expand All @@ -72,7 +72,7 @@ interface IQuoter {
/// @return sqrtPriceX96After The sqrt price of the pool after the swap
function quoteExactOutputSingle(QuoteExactSingleParams memory params)
external
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After);
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint256 gasEstimate);

/// @notice Returns the delta amounts along the swap path for a given exact output swap
/// @param params the params for the quote, encoded as 'QuoteExactParams'
Expand All @@ -83,5 +83,5 @@ interface IQuoter {
/// @return sqrtPriceX96AfterList List of the sqrt price after the swap for each pool in the path
function quoteExactOutput(QuoteExactParams memory params)
external
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList);
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, uint256 gasEstimate);
}
34 changes: 22 additions & 12 deletions src/lens/Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,53 @@ contract Quoter is IQuoter, SafeCallback {
/// @inheritdoc IQuoter
function quoteExactInputSingle(QuoteExactSingleParams memory params)
public
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After)
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
try poolManager.unlock(abi.encodeCall(this._quoteExactInputSingle, (params))) {}
catch (bytes memory reason) {
return _handleRevertSingle(reason);
gasEstimate = gasBefore - gasleft();
return _handleRevertSingle(reason, gasEstimate);
}
}

/// @inheritdoc IQuoter
function quoteExactInput(QuoteExactParams memory params)
external
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList)
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
try poolManager.unlock(abi.encodeCall(this._quoteExactInput, (params))) {}
catch (bytes memory reason) {
return _handleRevert(reason);
gasEstimate = gasBefore - gasleft();
return _handleRevert(reason, gasEstimate);
}
}

/// @inheritdoc IQuoter
function quoteExactOutputSingle(QuoteExactSingleParams memory params)
public
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After)
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
try poolManager.unlock(abi.encodeCall(this._quoteExactOutputSingle, (params))) {}
catch (bytes memory reason) {
gasEstimate = gasBefore - gasleft();
if (params.sqrtPriceLimitX96 == 0) delete amountOutCached;
return _handleRevertSingle(reason);
return _handleRevertSingle(reason, gasEstimate);
}
}

/// @inheritdoc IQuoter
function quoteExactOutput(QuoteExactParams memory params)
public
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList)
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
try poolManager.unlock(abi.encodeCall(this._quoteExactOutput, (params))) {}
catch (bytes memory reason) {
return _handleRevert(reason);
gasEstimate = gasBefore - gasleft();
return _handleRevert(reason, gasEstimate);
}
}

Expand All @@ -111,23 +119,25 @@ contract Quoter is IQuoter, SafeCallback {
}

/// @dev parse revert bytes from a single-pool quote
function _handleRevertSingle(bytes memory reason)
function _handleRevertSingle(bytes memory reason, uint256 gasEstimate)
private
pure
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After)
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint256)
{
reason = validateRevertReason(reason);
(deltaAmounts, sqrtPriceX96After) = abi.decode(reason, (int128[], uint160));
return (deltaAmounts, sqrtPriceX96After, gasEstimate);
}

/// @dev parse revert bytes from a potentially multi-hop quote and return the delta amounts, and sqrtPriceX96After
function _handleRevert(bytes memory reason)
function _handleRevert(bytes memory reason, uint256 gasEstimate)
private
pure
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList)
returns (int128[] memory deltaAmounts, uint160[] memory sqrtPriceX96AfterList, uint256)
{
reason = validateRevertReason(reason);
(deltaAmounts, sqrtPriceX96AfterList) = abi.decode(reason, (int128[], uint160[]));
return (deltaAmounts, sqrtPriceX96AfterList, gasEstimate);
}

/// @dev quote an ExactInput swap along a path of tokens, then revert with the result
Expand Down
Loading

0 comments on commit fdb6244

Please sign in to comment.