From 162dc5cb01906f2ee990d82d55144ebb2e28c48b Mon Sep 17 00:00:00 2001 From: "tingzhao.ytz" Date: Thu, 1 Aug 2024 20:57:50 +0800 Subject: [PATCH] update constructor --- demo-contract/contracts/wtfswap/Pool.sol | 43 +++++++++++++----------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/demo-contract/contracts/wtfswap/Pool.sol b/demo-contract/contracts/wtfswap/Pool.sol index fa6eb2b..491338c 100644 --- a/demo-contract/contracts/wtfswap/Pool.sol +++ b/demo-contract/contracts/wtfswap/Pool.sol @@ -6,28 +6,33 @@ import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "./interfaces/IFactory.sol"; contract Pool is IPool, ERC20 { - function factory() external view override returns (address) {} - - function token0() external view override returns (address) {} - - function token1() external view override returns (address) {} - - function tickLower() external view override returns (int24) {} - - function tickUpper() external view override returns (int24) {} - - function fee() external view override returns (uint24) {} - - function sqrtPriceX96() external view override returns (uint160) {} - - function tick() external view override returns (int24) {} - - function liquidity() external view override returns (uint128) {} + /// @inheritdoc IPool + address public immutable override factory; + /// @inheritdoc IPool + address public immutable override token0; + /// @inheritdoc IPool + address public immutable override token1; + /// @inheritdoc IPool + uint24 public immutable override fee; + /// @inheritdoc IPool + int24 public immutable override tickLower; + /// @inheritdoc IPool + int24 public immutable override tickUpper; + + /// @inheritdoc IPool + uint160 public override sqrtPriceX96; + /// @inheritdoc IPool + int24 public override tick; + /// @inheritdoc IPool + uint128 public override liquidity; constructor() { ERC20("Wtfswap", "WTF-SWAP"); - // 调用 IFactory 的 parameters 获取参数 - (factory, token0, token1, tickLower, tickUpper, fee) = IFactory( + // constructor 中初始化 immutable 的常量 + // Factory 创建 Pool 时会通 new Pool{salt: salt}() 的方式创建 Pool 合约,通过 salt 指定 Pool 的地址,这样其他地方也可以推算出 Pool 的地址 + // 参数通过读取 Factory 合约的 parameters 获取 + // 不通过构造函数传入,因为 CREATE2 会根据 initcode 计算出新地址(new_address = hash(0xFF, sender, salt, bytecode)),带上参数就不能计算出稳定的地址了 + (factory, token0, token1, fee, tickLower, tickUpper) = IFactory( msg.sender ).parameters(); }