|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import {console} from "forge-std/console.sol"; |
| 5 | + |
| 6 | +import {Zora} from "../src/zora/Zora.sol"; |
| 7 | +import {IZora} from "../src/zora/IZora.sol"; |
| 8 | +import {Test} from "forge-std/Test.sol"; |
| 9 | +import {IImmutableCreate2Factory} from "../src/deployment/IImmutableCreate2Factory.sol"; |
| 10 | +import {Create2} from "@openzeppelin/contracts/utils/Create2.sol"; |
| 11 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 12 | +import {DeploymentBase} from "../src/deployment/DeploymentBase.sol"; |
| 13 | + |
| 14 | +/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee |
| 15 | +library PoolAddress { |
| 16 | + bytes32 internal constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54; |
| 17 | + |
| 18 | + /// @notice The identifying key of the pool |
| 19 | + struct PoolKey { |
| 20 | + address token0; |
| 21 | + address token1; |
| 22 | + uint24 fee; |
| 23 | + } |
| 24 | + |
| 25 | + /// @notice Returns PoolKey: the ordered tokens with the matched fee levels |
| 26 | + /// @param tokenA The first token of a pool, unsorted |
| 27 | + /// @param tokenB The second token of a pool, unsorted |
| 28 | + /// @param fee The fee level of the pool |
| 29 | + /// @return Poolkey The pool details with ordered token0 and token1 assignments |
| 30 | + function getPoolKey(address tokenA, address tokenB, uint24 fee) internal pure returns (PoolKey memory) { |
| 31 | + if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA); |
| 32 | + return PoolKey({token0: tokenA, token1: tokenB, fee: fee}); |
| 33 | + } |
| 34 | + |
| 35 | + /// @notice Deterministically computes the pool address given the factory and PoolKey |
| 36 | + /// @param factory The Uniswap V3 factory contract address |
| 37 | + /// @param key The PoolKey |
| 38 | + /// @return pool The contract address of the V3 pool |
| 39 | + function computeAddress(address factory, PoolKey memory key) internal pure returns (address pool) { |
| 40 | + require(key.token0 < key.token1); |
| 41 | + pool = address( |
| 42 | + uint160(uint256(keccak256(abi.encodePacked(hex"ff", factory, keccak256(abi.encode(key.token0, key.token1, key.fee)), POOL_INIT_CODE_HASH)))) |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +interface IUniswapV3Factory { |
| 48 | + function createPool(address tokenA, address tokenB, uint24 fee) external returns (address pool); |
| 49 | +} |
| 50 | + |
| 51 | +contract PoolAddressesTest is Test { |
| 52 | + function testPoolAddresses() public { |
| 53 | + vm.createSelectFork("base"); |
| 54 | + |
| 55 | + address factory = 0x33128a8fC17869897dcE68Ed026d694621f6FDfD; |
| 56 | + |
| 57 | + address zora = 0x1111111111166b7FE7bd91427724B487980aFc69; |
| 58 | + address weth = 0x4200000000000000000000000000000000000006; |
| 59 | + address usdc = 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913; |
| 60 | + |
| 61 | + // 1000 = 0.1% |
| 62 | + // 10000 = 1% |
| 63 | + uint24 fee = 10000; |
| 64 | + |
| 65 | + PoolAddress.PoolKey memory zoraWeth = PoolAddress.PoolKey({token0: zora, token1: weth, fee: fee}); |
| 66 | + address expectedWethPool = PoolAddress.computeAddress(factory, zoraWeth); |
| 67 | + PoolAddress.PoolKey memory zoraUsdc = PoolAddress.PoolKey({token0: zora, token1: usdc, fee: fee}); |
| 68 | + address expectedUsdcPool = PoolAddress.computeAddress(factory, zoraUsdc); |
| 69 | + |
| 70 | + address actualWethPool = IUniswapV3Factory(factory).createPool(zora, weth, fee); |
| 71 | + address actualUsdcPool = IUniswapV3Factory(factory).createPool(zora, usdc, fee); |
| 72 | + |
| 73 | + assertEq(actualWethPool, expectedWethPool); |
| 74 | + assertEq(actualUsdcPool, expectedUsdcPool); |
| 75 | + |
| 76 | + console.log("zoraWethPool", expectedWethPool); |
| 77 | + console.log("zoraUsdcPool", expectedUsdcPool); |
| 78 | + } |
| 79 | +} |
0 commit comments