Skip to content

Commit d74f7d9

Browse files
committed
Add study notes for 2025-08-19
1 parent d899c5a commit d74f7d9

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Hickerzeed.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,71 @@ timezone: UTC+8
1515
## Notes
1616

1717
<!-- Content_START -->
18+
# 2025-08-19
19+
20+
初始化交易池
21+
初始化交易池调用的是 UniswapV3Factory 合约的 initialize,参数为当前价格 sqrtPriceX96,含义上面已经介绍过了。
22+
代码如下:
23+
/// @inheritdoc IUniswapV3PoolActions
24+
/// @dev not locked because it initializes unlocked
25+
function initialize(uint160 sqrtPriceX96) external override {
26+
require(slot0.sqrtPriceX96 == 0, 'AI');
27+
28+
int24 tick = TickMath.getTickAtSqrtRatio(sqrtPriceX96);
29+
30+
(uint16 cardinality, uint16 cardinalityNext) = observations.initialize(_blockTimestamp());
31+
32+
slot0 = Slot0({
33+
sqrtPriceX96: sqrtPriceX96,
34+
tick: tick,
35+
observationIndex: 0,
36+
observationCardinality: cardinality,
37+
observationCardinalityNext: cardinalityNext,
38+
feeProtocol: 0,
39+
unlocked: true
40+
});
41+
42+
emit Initialize(sqrtPriceX96, tick);
43+
}
44+
45+
首先从 sqrtPriceX96 换算出 tick 的值。
46+
int24 tick = TickMath.getTickAtSqrtRatio(sqrtPriceX96);
47+
48+
然后初始化预言机,cardinality 表示当前预言机的观测点数组容量, cardinalityNext 表示预言机扩容后的观测点数组容量,这里不详细解释。
49+
(uint16 cardinality, uint16 cardinalityNext) = observations.initialize(_blockTimestamp());
50+
51+
最后初始化 slot0 变量,用于记录交易池的全局状态,这里主要就是记录价格和预言机的状态。
52+
slot0 = Slot0({
53+
sqrtPriceX96: sqrtPriceX96,
54+
tick: tick,
55+
observationIndex: 0,
56+
observationCardinality: cardinality,
57+
observationCardinalityNext: cardinalityNext,
58+
feeProtocol: 0,
59+
unlocked: true
60+
});
61+
62+
Slot0结构如下,源码中已经有了详细的注释。
63+
struct Slot0 {
64+
// the current price
65+
uint160 sqrtPriceX96;
66+
// the current tick
67+
int24 tick;
68+
// the most-recently updated index of the observations array
69+
uint16 observationIndex;
70+
// the current maximum number of observations that are being stored
71+
uint16 observationCardinality;
72+
// the next maximum number of observations to store, triggered in observations.write
73+
uint16 observationCardinalityNext;
74+
// the current protocol fee as a percentage of the swap fee taken on withdrawal
75+
// represented as an integer denominator (1/x)%
76+
uint8 feeProtocol;
77+
// whether the pool is locked
78+
bool unlocked;
79+
}
80+
81+
至此完成了交易池合约的初始化。
82+
1883
# 2025-08-18
1984

2085
部署交易池

0 commit comments

Comments
 (0)