1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use ;
use Result;
/// Decode slot0 data from raw bytes
///
/// Slot0 is the first field in the State struct (at state_slot + 0):
/// ```
/// struct State {
/// Slot0 slot0; // This field - contains price and tick information
/// ...
/// }
/// ```
///
/// Slot0 contains packed data:
/// - sqrt_price_x96 (U160): Current sqrt price in Q96 format, bytes 12-32
/// - tick (I24): Current tick value, bytes 9-12 (3 bytes, signed)
/// - protocol_fee (U24): Protocol fee rate, bytes 6-9 (3 bytes)
/// - lp_fee (U24): Liquidity provider fee rate, bytes 3-6 (3 bytes)
///
/// ## Arguments
/// * `data` - Raw 32-byte slot data from storage
///
/// ## Returns
/// A tuple of (sqrt_price_x96, tick, protocol_fee, lp_fee)
/// Decode tick_lower and tick_upper from packed V4 position info
///
/// This decodes position information from State.positions mapping:
/// ```
/// struct State {
/// ...
/// mapping(bytes32 => Position.State) positions; // At state_slot + POSITIONS_OFFSET
/// }
/// ```
///
/// V4 positions store tick_lower and tick_upper in a packed U256 format (32
/// bytes). This function extracts both values from the packed representation:
/// - tick_lower: bits 232-255 (3 bytes, signed 24-bit integer)
/// - tick_upper: bits 208-231 (3 bytes, signed 24-bit integer)
///
/// The packing format allows both tick values to fit in a single storage slot.
///
/// ## Arguments
/// * `position_info` - Packed position info as U256 containing both tick_lower
/// and tick_upper
///
/// ## Returns
/// A tuple of (tick_lower, tick_upper) as signed 24-bit integers
/// Decode liquidity gross and net from a B256 word
///
/// This decodes data from the TickInfo struct stored in the State.ticks
/// mapping. Each tick's TickInfo contains:
/// ```
/// struct TickInfo {
/// uint128 liquidityGross; // Total liquidity referencing this tick
/// int128 liquidityNet; // Net liquidity change when crossing this tick
/// ...
/// }
/// ```
///
/// The liquidity data is packed in a single B256 (32 bytes):
/// - liquidity_gross: bits 128-255 (last 16 bytes, u128) - bytes 16-31
/// - liquidity_net: bits 0-127 (first 16 bytes, i128, signed) - bytes 0-15
///
/// This is the first slot of TickInfo data retrieved from State.ticks[tick].
///
/// ## Arguments
/// * `word` - B256 containing packed liquidity_gross and liquidity_net
///
/// ## Returns
/// A tuple of (liquidity_gross, liquidity_net)
pub
/// Decode tick info from raw B256 data
///
/// This decodes the TickInfo struct from State.ticks mapping:
/// ```
/// struct State {
/// ...
/// mapping(int24 => TickInfo) ticks; // At state_slot + TICKS_OFFSET
/// ...
/// }
///
/// struct TickInfo {
/// uint128 liquidityGross; // data[0] bytes 16-31
/// int128 liquidityNet; // data[0] bytes 0-15
/// uint256 feeGrowthOutside0X128; // data[1]
/// uint256 feeGrowthOutside1X128; // data[2]
/// ...
/// }
/// ```
///
/// The tick info consists of 3 B256 (32-byte) values retrieved via extsload:
/// - data[0]: Packed liquidity_gross (u128) and liquidity_net (i128)
/// - data[1]: fee_growth_outside0_x128 (U256) - fee growth per unit of
/// liquidity outside this tick for token0
/// - data[2]: fee_growth_outside1_x128 (U256) - fee growth per unit of
/// liquidity outside this tick for token1
///
/// ## Arguments
/// * `data` - Array or slice of 3 B256 values containing tick info data from
/// State.ticks[tick]
///
/// ## Returns
/// A tuple of (liquidity_gross, liquidity_net, fee_growth_outside0_x128,
/// fee_growth_outside1_x128)
/// Decode pool liquidity from raw B256 storage data
///
/// This decodes the liquidity field from the State struct:
/// ```
/// struct State {
/// ...
/// uint128 liquidity; // At state_slot + LIQUIDITY_OFFSET (state_slot + 3)
/// ...
/// }
/// ```
///
/// In V4's storage layout, the liquidity field is stored as a uint128 in the
/// second half of a 32-byte storage slot (bytes 16-31). The first half (bytes
/// 0-15) may contain padding or other data.
///
/// The data retrieved from `get_liquidity_slot()` via `extsload_0` returns a
/// B256, and this function extracts the uint128 liquidity value from bytes
/// 16-31.
///
/// ## Arguments
/// * `data` - B256 storage data from State.liquidity slot
///
/// ## Returns
/// The liquidity value as u128