Skip to content

Add msgSender() to V4Quoter #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions snapshots/QuoterTest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"Quoter_exactInputSingle_oneForZero_multiplePositions": "145902",
"Quoter_exactInputSingle_zeroForOne_multiplePositions": "152117",
"Quoter_exactOutputSingle_oneForZero": "79267",
"Quoter_exactOutputSingle_zeroForOne": "84512",
"Quoter_quoteExactInput_oneHop_1TickLoaded": "122728",
"Quoter_quoteExactInput_oneHop_initializedAfter": "147363",
"Quoter_quoteExactInput_oneHop_startingInitialized": "80638",
"Quoter_quoteExactInput_twoHops": "204942",
"Quoter_quoteExactOutput_oneHop_1TickLoaded": "122224",
"Quoter_quoteExactOutput_oneHop_2TicksLoaded": "152879",
"Quoter_quoteExactOutput_oneHop_initializedAfter": "122251",
"Quoter_quoteExactOutput_oneHop_startingInitialized": "98545",
"Quoter_quoteExactOutput_twoHops": "204670"
"Quoter_exactInputSingle_oneForZero_multiplePositions": "146134",
"Quoter_exactInputSingle_zeroForOne_multiplePositions": "152349",
"Quoter_exactOutputSingle_oneForZero": "79477",
"Quoter_exactOutputSingle_zeroForOne": "84722",
"Quoter_quoteExactInput_oneHop_1TickLoaded": "122938",
"Quoter_quoteExactInput_oneHop_initializedAfter": "147573",
"Quoter_quoteExactInput_oneHop_startingInitialized": "80848",
"Quoter_quoteExactInput_twoHops": "205152",
"Quoter_quoteExactOutput_oneHop_1TickLoaded": "122434",
"Quoter_quoteExactOutput_oneHop_2TicksLoaded": "153089",
"Quoter_quoteExactOutput_oneHop_initializedAfter": "122461",
"Quoter_quoteExactOutput_oneHop_startingInitialized": "98755",
"Quoter_quoteExactOutput_twoHops": "204880"
}
3 changes: 2 additions & 1 deletion src/base/BaseActionsRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {SafeCallback} from "./SafeCallback.sol";
import {CalldataDecoder} from "../libraries/CalldataDecoder.sol";
import {ActionConstants} from "../libraries/ActionConstants.sol";
import {IMsgSender} from "../interfaces/IMsgSender.sol";

/// @notice Abstract contract for performing a combination of actions on Uniswap v4.
/// @dev Suggested uint256 action values are defined in Actions.sol, however any definition can be used
abstract contract BaseActionsRouter is SafeCallback {
abstract contract BaseActionsRouter is IMsgSender, SafeCallback {
using CalldataDecoder for bytes;

/// @notice emitted when different numbers of parameters and actions are provided
Expand Down
13 changes: 13 additions & 0 deletions src/interfaces/IMsgSender.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/// @title IMsgSender
/// @notice Interface for contracts that expose the original caller
interface IMsgSender {
/// @notice Returns the address of the original caller (msg.sender)
/// @dev Uniswap v4 periphery contracts implement a callback pattern which lose
/// the original msg.sender caller context. This view function provides a way for
/// integrating contracts (e.g. hooks) to access the original caller address.
/// @return The address of the original caller
function msgSender() external view returns (address);
}
3 changes: 2 additions & 1 deletion src/interfaces/IV4Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {PathKey} from "../libraries/PathKey.sol";
import {IImmutableState} from "./IImmutableState.sol";
import {IMsgSender} from "./IMsgSender.sol";

/// @title IV4Quoter
/// @notice Interface for the V4Quoter contract
interface IV4Quoter is IImmutableState {
interface IV4Quoter is IImmutableState, IMsgSender {
struct QuoteExactSingleParams {
PoolKey poolKey;
bool zeroForOne;
Expand Down
18 changes: 18 additions & 0 deletions src/lens/V4Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {IV4Quoter} from "../interfaces/IV4Quoter.sol";
import {PathKey} from "../libraries/PathKey.sol";
import {QuoterRevert} from "../libraries/QuoterRevert.sol";
import {BaseV4Quoter} from "../base/BaseV4Quoter.sol";
import {Locker} from "../libraries/Locker.sol";
import {IMsgSender} from "../interfaces/IMsgSender.sol";

/// @title V4Quoter
/// @notice Supports quoting the delta amounts for exact input or exact output swaps.
Expand All @@ -20,9 +22,16 @@ contract V4Quoter is IV4Quoter, BaseV4Quoter {

constructor(IPoolManager _poolManager) BaseV4Quoter(_poolManager) {}

modifier setMsgSender() {
Locker.set(msg.sender);
_; // execute the function
Locker.set(address(0)); // reset the locker
}

/// @inheritdoc IV4Quoter
function quoteExactInputSingle(QuoteExactSingleParams memory params)
external
setMsgSender
returns (uint256 amountOut, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
Expand All @@ -37,6 +46,7 @@ contract V4Quoter is IV4Quoter, BaseV4Quoter {
/// @inheritdoc IV4Quoter
function quoteExactInput(QuoteExactParams memory params)
external
setMsgSender
returns (uint256 amountOut, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
Expand All @@ -51,6 +61,7 @@ contract V4Quoter is IV4Quoter, BaseV4Quoter {
/// @inheritdoc IV4Quoter
function quoteExactOutputSingle(QuoteExactSingleParams memory params)
external
setMsgSender
returns (uint256 amountIn, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
Expand All @@ -65,6 +76,7 @@ contract V4Quoter is IV4Quoter, BaseV4Quoter {
/// @inheritdoc IV4Quoter
function quoteExactOutput(QuoteExactParams memory params)
external
setMsgSender
returns (uint256 amountIn, uint256 gasEstimate)
{
uint256 gasBefore = gasleft();
Expand Down Expand Up @@ -138,4 +150,10 @@ contract V4Quoter is IV4Quoter, BaseV4Quoter {
uint256 amountIn = params.zeroForOne ? uint128(-swapDelta.amount0()) : uint128(-swapDelta.amount1());
amountIn.revertQuote();
}

/// @inheritdoc IMsgSender
function msgSender() external view returns (address) {
// despite using the Locker library, V4Quoter does not have a reentrancy lock
return Locker.get();
}
}
Loading