/// @dev IDs of pools assigned by this contract //_poolIds记录所有交易池的地址和编号的对应关系 mapping(address => uint80) private _poolIds;
/// @dev Pool keys by pool ID, to save on SSTOREs for position data //_poolIdToPoolKey记录交易池编号和PoolKey的对应关系。PoolKey中包含了token0,token1,fee mapping(uint80 => PoolAddress.PoolKey) private _poolIdToPoolKey;
/// @dev The token ID position data mapping(uint256 => Position) private _positions;
/// @dev The ID of the next token that will be minted. Skips 0 //position的编号 uint176 private _nextId = 1; /// @dev The ID of the next pool that is used for the first time. Skips 0 //每一个Pool的唯一编号 uint80 private _nextPoolId = 1;
/// @dev The address of the token descriptor contract, which handles generating token URIs for position tokens address private immutable _tokenDescriptor;
/// @notice Returns the pool address for a given pair of tokens and a fee, or address 0 if it does not exist /// @dev tokenA and tokenB may be passed in either token0/token1 or token1/token0 order /// @param tokenA The contract address of either token0 or token1 /// @param tokenB The contract address of the other token /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip /// @return pool The pool address function getPool( address tokenA, address tokenB, uint24 fee ) external view returns (address pool);
在UniswapV3Factory中的getPool
1 2 3 4 5
contract UniswapV3Factory is IUniswapV3Factory, UniswapV3PoolDeployer, NoDelegateCall { ... mapping(address => mapping(address => mapping(uint24 => address))) public override getPool; ... }
/// @dev Deploys a pool with the given parameters by transiently setting the parameters storage slot and then /// clearing it after deploying the pool. /// @param factory The contract address of the Uniswap V3 factory /// @param token0 The first token of the pool by address sort order /// @param token1 The second token of the pool by address sort order /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip /// @param tickSpacing The spacing between usable ticks function deploy( address factory, address token0, address token1, uint24 fee, int24 tickSpacing ) internal returns (address pool) { parameters = Parameters({factory: factory, token0: token0, token1: token1, fee: fee, tickSpacing: tickSpacing}); pool = address(new UniswapV3Pool{salt: keccak256(abi.encode(token0, token1, fee))}()); delete parameters; }
/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee library PoolAddress { bytes32 internal constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
/// @notice The identifying key of the pool struct PoolKey { address token0; address token1; uint24 fee; }
...
/// @notice Deterministically computes the pool address given the factory and PoolKey /// @param factory The Uniswap V3 factory contract address /// @param key The PoolKey /// @return pool The contract address of the V3 pool function computeAddress(address factory, PoolKey memory key) internal pure returns (address pool) { require(key.token0 < key.token1); pool = address( uint256( keccak256( abi.encodePacked( hex'ff', factory, keccak256(abi.encode(key.token0, key.token1, key.fee)), POOL_INIT_CODE_HASH ) ) ) ); } }
struct Slot0 { // the current price uint160 sqrtPriceX96; // the current tick int24 tick; // the most-recently updated index of the observations array uint16 observationIndex; // the current maximum number of observations that are being stored uint16 observationCardinality; // the next maximum number of observations to store, triggered in observations.write uint16 observationCardinalityNext; // the current protocol fee as a percentage of the swap fee taken on withdrawal // represented as an integer denominator (1/x)% uint8 feeProtocol; // whether the pool is locked bool unlocked; } /// @inheritdoc IUniswapV3PoolState Slot0 public override slot0;
/// @inheritdoc IUniswapV3PoolActions /// @dev not locked because it initializes unlocked //这里的sqrtPriceX96为sqrt(amountToken1/amountToken0)Q64.96精度的定点数值 function initialize(uint160 sqrtPriceX96) external override { //检查池子价格是否未初始化 require(slot0.sqrtPriceX96 == 0, 'AI');