Skip to content

Commit

Permalink
update constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
yutingzhao1991 committed Aug 1, 2024
1 parent f2c17af commit 162dc5c
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions demo-contract/contracts/wtfswap/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 162dc5c

Please sign in to comment.