-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpool_manager_lens.rs
More file actions
830 lines (748 loc) · 27.7 KB
/
Copy pathpool_manager_lens.rs
File metadata and controls
830 lines (748 loc) · 27.7 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! ## Pool Manager Lens
//! This module provides a lens for querying the Uniswap V4 pool manager. It is similar to
//! [`StateView`](https://github.com/Uniswap/v4-periphery/blob/main/src/lens/StateView.sol), but
//! does the slot calculation and ABI decoding in Rust instead of Solidity. It does not require
//! contract deployment and uses `extsload` to read the state under the hood.
use crate::prelude::{Error, IExtsload};
use alloy::{
eips::BlockId,
network::{Ethereum, Network},
providers::Provider,
uint,
};
use alloy_primitives::{
Address, B256, U160, U256,
aliases::{I24, U24},
keccak256,
};
use alloy_sol_types::SolValue;
use uniswap_v3_sdk::prelude::*;
const POOLS_SLOT: U256 = uint!(6_U256);
const FEE_GROWTH_GLOBAL0_OFFSET: U256 = uint!(1_U256);
const LIQUIDITY_OFFSET: U256 = uint!(3_U256);
const TICKS_OFFSET: U256 = uint!(4_U256);
const TICK_BITMAP_OFFSET: U256 = uint!(5_U256);
const POSITIONS_OFFSET: U256 = uint!(6_U256);
fn get_pool_state_slot(pool_id: B256) -> U256 {
U256::from_be_bytes(keccak256((pool_id, POOLS_SLOT).abi_encode()).0)
}
fn get_tick_bitmap_slot<I: TickIndex>(pool_id: B256, tick: I) -> U256 {
let state_slot = get_pool_state_slot(pool_id);
let tick_bitmap_mapping = state_slot + TICK_BITMAP_OFFSET;
U256::from_be_bytes(keccak256((tick.to_i24().as_i16(), tick_bitmap_mapping).abi_encode()).0)
}
fn get_tick_info_slot<I: TickIndex>(pool_id: B256, tick: I) -> U256 {
let state_slot = get_pool_state_slot(pool_id);
let ticks_mapping_slot = state_slot + TICKS_OFFSET;
U256::from_be_bytes(keccak256((tick.to_i24(), ticks_mapping_slot).abi_encode()).0)
}
fn get_position_info_slot(pool_id: B256, position_key: B256) -> U256 {
let state_slot = get_pool_state_slot(pool_id);
let position_mapping_slot = state_slot + POSITIONS_OFFSET;
U256::from_be_bytes(keccak256((position_key, position_mapping_slot).abi_encode()).0)
}
/// A lens for querying Uniswap V4 pool manager
#[derive(Clone, Debug)]
pub struct PoolManagerLens<P, N = Ethereum>
where
N: Network,
P: Provider<N>,
{
pub manager: IExtsload::IExtsloadInstance<P, N>,
_network: core::marker::PhantomData<N>,
}
impl<P, N> PoolManagerLens<P, N>
where
N: Network,
P: Provider<N>,
{
/// Creates a new `PoolManagerLens`
#[inline]
pub const fn new(manager: Address, provider: P) -> Self {
Self {
manager: IExtsload::new(manager, provider),
_network: core::marker::PhantomData,
}
}
/// Retrieves the Slot0 of a pool: sqrtPriceX96, tick, protocolFee, lpFee
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `sqrtPriceX96`: The square root of the price of the pool, in Q96 precision
/// * `tick`: The current tick of the pool
/// * `protocol_fee`: The protocol fee of the pool
/// * `lp_fee`: The swap fee of the pool
#[inline]
pub async fn get_slot0(
&self,
pool_id: B256,
block_id: Option<BlockId>,
) -> Result<(U160, I24, U24, U24), Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let state_slot = get_pool_state_slot(pool_id);
let data = self
.manager
.extsload_0(B256::from(state_slot))
.block(block_id)
.call()
.await?;
let sqrt_price_x96 = U160::from_be_slice(&data[12..32]);
let tick_bytes = unsafe { (data.as_ptr().add(9) as *const [u8; 3]).read_unaligned() };
let tick = I24::from_be_bytes(tick_bytes);
let protocol_fee_bytes =
unsafe { (data.as_ptr().add(6) as *const [u8; 3]).read_unaligned() };
let protocol_fee = U24::from_be_bytes(protocol_fee_bytes);
let lp_fee_bytes = unsafe { (data.as_ptr().add(3) as *const [u8; 3]).read_unaligned() };
let lp_fee = U24::from_be_bytes(lp_fee_bytes);
Ok((sqrt_price_x96, tick, protocol_fee, lp_fee))
}
/// Retrieves full tick information from a pool at a specific tick
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `tick`: The tick to retrieve information for
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `liquidity_gross`: The total position liquidity that references this tick
/// * `liquidity_net`: The amount of net liquidity added (subtracted) when tick is crossed from
/// left to right (right to left)
/// * `fee_growth_outside0_x128`: Fee growth per unit of liquidity on the other side of this
/// tick for token0
/// * `fee_growth_outside1_x128`: Fee growth per unit of liquidity on the other side of this
/// tick for token1
#[inline]
pub async fn get_tick_info<I: TickIndex>(
&self,
pool_id: B256,
tick: I,
block_id: Option<BlockId>,
) -> Result<(u128, i128, U256, U256), Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = get_tick_info_slot(pool_id, tick);
let data = self
.manager
.extsload_1(B256::from(slot), uint!(3_U256))
.block(block_id)
.call()
.await?;
let (liquidity_gross, liquidity_net) = decode_liquidity_gross_and_net(data[0]);
let fee_growth_outside0_x128 = U256::from_be_bytes(data[1].0);
let fee_growth_outside1_x128 = U256::from_be_bytes(data[2].0);
Ok((
liquidity_gross,
liquidity_net,
fee_growth_outside0_x128,
fee_growth_outside1_x128,
))
}
/// Retrieves the liquidity information of a pool at a specific tick
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `tick`: The tick to retrieve liquidity for
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `liquidity_gross`: The total position liquidity that references this tick
/// * `liquidity_net`: The amount of net liquidity added (subtracted) when tick is crossed from
/// left to right (right to left)
#[inline]
pub async fn get_tick_liquidity<I: TickIndex>(
&self,
pool_id: B256,
tick: I,
block_id: Option<BlockId>,
) -> Result<(u128, i128), Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = get_tick_info_slot(pool_id, tick);
let value = self
.manager
.extsload_0(B256::from(slot))
.block(block_id)
.call()
.await?;
Ok(decode_liquidity_gross_and_net(value))
}
/// Retrieves the fee growth outside a tick of a pool
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `tick`: The tick to retrieve fee growth for
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `fee_growth_outside0_x128`: Fee growth per unit of liquidity on the other side of this
/// tick for token0
/// * `fee_growth_outside1_x128`: Fee growth per unit of liquidity on the other side of this
/// tick for token1
#[inline]
pub async fn get_tick_fee_growth_outside<I: TickIndex>(
&self,
pool_id: B256,
tick: I,
block_id: Option<BlockId>,
) -> Result<(U256, U256), Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = B256::from(get_tick_info_slot(pool_id, tick) + uint!(1_U256));
let data = self
.manager
.extsload_1(slot, uint!(2_U256))
.block(block_id)
.call()
.await?;
let fee_growth_outside0_x128 = U256::from_be_bytes(data[0].0);
let fee_growth_outside1_x128 = U256::from_be_bytes(data[1].0);
Ok((fee_growth_outside0_x128, fee_growth_outside1_x128))
}
/// Retrieves the global fee growth of a pool
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `fee_growth_global0`: The global fee growth for token0
/// * `fee_growth_global1`: The global fee growth for token1
#[inline]
pub async fn get_fee_growth_globals(
&self,
pool_id: B256,
block_id: Option<BlockId>,
) -> Result<(U256, U256), Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let state_slot = get_pool_state_slot(pool_id);
let slot_fee_growth_global0 = B256::from(state_slot + FEE_GROWTH_GLOBAL0_OFFSET);
let data = self
.manager
.extsload_1(slot_fee_growth_global0, uint!(2_U256))
.block(block_id)
.call()
.await?;
let fee_growth_global0 = U256::from_be_bytes(data[0].0);
let fee_growth_global1 = U256::from_be_bytes(data[1].0);
Ok((fee_growth_global0, fee_growth_global1))
}
/// Retrieves the total liquidity of a pool
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `liquidity`: The liquidity of the pool
#[inline]
pub async fn get_liquidity(
&self,
pool_id: B256,
block_id: Option<BlockId>,
) -> Result<u128, Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = B256::from(get_pool_state_slot(pool_id) + LIQUIDITY_OFFSET);
let value = self.manager.extsload_0(slot).block(block_id).call().await?;
Ok(decode_liquidity(value))
}
/// Retrieves the tick bitmap of a pool at a specific tick
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `tick`: The tick to retrieve the bitmap for
/// * `block_id`: Optional block ID to query at
#[inline]
pub async fn get_tick_bitmap<I: TickIndex>(
&self,
pool_id: B256,
tick: I,
block_id: Option<BlockId>,
) -> Result<U256, Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = get_tick_bitmap_slot(pool_id, tick);
let word = self
.manager
.extsload_0(B256::from(slot))
.block(block_id)
.call()
.await?;
Ok(U256::from_be_bytes(word.0))
}
/// Retrieves the position information of a pool at a specific position key
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `position_key`: The key of the position
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `liquidity`: The liquidity of the position
/// * `fee_growth_inside0_last_x128`: The fee growth inside the position for token0
/// * `fee_growth_inside1_last_x128`: The fee growth inside the position for token1
#[inline]
pub async fn get_position_info(
&self,
pool_id: B256,
position_key: B256,
block_id: Option<BlockId>,
) -> Result<(u128, U256, U256), Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = get_position_info_slot(pool_id, position_key);
let data = self
.manager
.extsload_1(B256::from(slot), uint!(3_U256))
.block(block_id)
.call()
.await?;
let liquidity = decode_liquidity(data[0]);
let fee_growth_inside0_last_x128 = U256::from_be_bytes(data[1].0);
let fee_growth_inside1_last_x128 = U256::from_be_bytes(data[2].0);
Ok((
liquidity,
fee_growth_inside0_last_x128,
fee_growth_inside1_last_x128,
))
}
/// Retrieves just the liquidity of a position
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `position_key`: The key of the position
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `liquidity`: The liquidity of the position
#[inline]
pub async fn get_position_liquidity(
&self,
pool_id: B256,
position_key: B256,
block_id: Option<BlockId>,
) -> Result<u128, Error> {
let block_id = block_id.unwrap_or(BlockId::latest());
let slot = get_position_info_slot(pool_id, position_key);
let value = self
.manager
.extsload_0(B256::from(slot))
.block(block_id)
.call()
.await?;
Ok(decode_liquidity(value))
}
/// Calculates the fee growth inside a tick range of a pool
///
/// ## Arguments
///
/// * `pool_id`: The ID of the pool
/// * `tick_lower`: The lower tick of the range
/// * `tick_upper`: The upper tick of the range
/// * `block_id`: Optional block ID to query at
///
/// ## Returns
///
/// * `fee_growth_inside0_x128`: The fee growth inside the tick range for token0
/// * `fee_growth_inside1_x128`: The fee growth inside the tick range for token1
#[inline]
pub async fn get_fee_growth_inside<I: TickIndex>(
&self,
pool_id: B256,
tick_lower: I,
tick_upper: I,
block_id: Option<BlockId>,
) -> Result<(U256, U256), Error> {
let (fee_growth_global0_x128, fee_growth_global1_x128) =
self.get_fee_growth_globals(pool_id, block_id).await?;
let (lower_fee_growth_outside0_x128, lower_fee_growth_outside1_x128) = self
.get_tick_fee_growth_outside(pool_id, tick_lower, block_id)
.await?;
let (upper_fee_growth_outside0_x128, upper_fee_growth_outside1_x128) = self
.get_tick_fee_growth_outside(pool_id, tick_upper, block_id)
.await?;
let (_, tick_current, _, _) = self.get_slot0(pool_id, block_id).await?;
let (fee_growth_inside0_x128, fee_growth_inside1_x128) =
if tick_current < tick_lower.to_i24() {
(
lower_fee_growth_outside0_x128 - upper_fee_growth_outside0_x128,
lower_fee_growth_outside1_x128 - upper_fee_growth_outside1_x128,
)
} else if tick_current >= tick_upper.to_i24() {
(
upper_fee_growth_outside0_x128 - lower_fee_growth_outside0_x128,
upper_fee_growth_outside1_x128 - lower_fee_growth_outside1_x128,
)
} else {
(
fee_growth_global0_x128
- lower_fee_growth_outside0_x128
- upper_fee_growth_outside0_x128,
fee_growth_global1_x128
- lower_fee_growth_outside1_x128
- upper_fee_growth_outside1_x128,
)
};
Ok((fee_growth_inside0_x128, fee_growth_inside1_x128))
}
}
const fn decode_liquidity_gross_and_net(word: B256) -> (u128, i128) {
// In Solidity:
// liquidityNet := sar(128, value)
// liquidityGross := and(value, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
let liquidity_gross = decode_liquidity(word);
let liquidity_net = unsafe {
// Create a pointer to the start of the first half of the array
let net_ptr = word.0.as_ptr() as *const i128;
// Read the value in big-endian format
i128::from_be(net_ptr.read_unaligned())
};
(liquidity_gross, liquidity_net)
}
const fn decode_liquidity(word: B256) -> u128 {
unsafe {
// Create a pointer to the start of the second half of the array
let ptr = word.0.as_ptr().add(16) as *const u128;
// Read the value in big-endian format
u128::from_be(ptr.read_unaligned())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{prelude::*, tests::*};
use alloy::providers::RootProvider;
use once_cell::sync::Lazy;
const TICK_SPACING: i32 = 10;
static POOL_MANAGER: Lazy<PoolManagerLens<RootProvider>> =
Lazy::new(|| PoolManagerLens::new(*POOL_MANAGER_ADDRESS, PROVIDER.clone()));
#[tokio::test]
async fn test_get_slot0() {
let (sqrt_price_x96_lens, tick_lens, protocol_fee_lens, lp_fee_lens) = POOL_MANAGER
.get_slot0(*POOL_ID_ETH_USDC, BLOCK_ID)
.await
.unwrap();
let slot0_state_view = STATE_VIEW
.getSlot0(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
assert_ne!(sqrt_price_x96_lens, U160::ZERO);
assert_eq!(
sqrt_price_x96_lens, slot0_state_view.sqrtPriceX96,
"sqrtPriceX96 mismatch"
);
assert_eq!(tick_lens, slot0_state_view.tick, "tick mismatch");
assert_eq!(
protocol_fee_lens, slot0_state_view.protocolFee,
"protocolFee mismatch"
);
assert_eq!(lp_fee_lens, slot0_state_view.lpFee, "lpFee mismatch");
}
macro_rules! assert_tick_info_match {
($pool_id:expr, $tick:expr, $block_id:expr) => {
let (
liquidity_gross_lens,
liquidity_net_lens,
fee_growth_outside0_x128_lens,
fee_growth_outside1_x128_lens,
) = POOL_MANAGER
.get_tick_info($pool_id, $tick, $block_id)
.await
.unwrap();
let tick_info = STATE_VIEW
.getTickInfo($pool_id, $tick.to_i24())
.block($block_id.unwrap())
.call()
.await
.unwrap();
assert_ne!(liquidity_gross_lens, 0);
assert_eq!(
liquidity_gross_lens, tick_info.liquidityGross,
"liquidityGross"
);
assert_ne!(liquidity_net_lens, 0);
assert_eq!(liquidity_net_lens, tick_info.liquidityNet, "liquidityNet");
assert_eq!(
fee_growth_outside0_x128_lens, tick_info.feeGrowthOutside0X128,
"feeGrowthOutside0X128"
);
assert_eq!(
fee_growth_outside1_x128_lens, tick_info.feeGrowthOutside1X128,
"feeGrowthOutside1X128"
);
};
}
async fn nearest_populated_tick(tick: I24) -> i32 {
let word = tick.as_i32().compress(TICK_SPACING).position().0;
let bitmap = POOL_MANAGER
.get_tick_bitmap(*POOL_ID_ETH_USDC, word, BLOCK_ID)
.await
.unwrap();
let msb = most_significant_bit(bitmap);
((word << 8) + msb as i32) * TICK_SPACING
}
#[tokio::test]
async fn test_get_tick_info() {
let slot0 = STATE_VIEW
.getSlot0(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
let tick = nearest_populated_tick(slot0.tick).await;
assert_tick_info_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
let tick = nearest_usable_tick(MIN_TICK_I32, TICK_SPACING);
assert_tick_info_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
let tick = nearest_usable_tick(MAX_TICK_I32, TICK_SPACING);
assert_tick_info_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
}
macro_rules! assert_tick_liquidity_match {
($pool_id:expr, $tick:expr, $block_id:expr) => {
let (liquidity_gross_lens, liquidity_net_lens) = POOL_MANAGER
.get_tick_liquidity($pool_id, $tick, $block_id)
.await
.unwrap();
let tick_liquidity = STATE_VIEW
.getTickLiquidity($pool_id, $tick.to_i24())
.block($block_id.unwrap())
.call()
.await
.unwrap();
assert_ne!(liquidity_gross_lens, 0);
assert_eq!(
liquidity_gross_lens, tick_liquidity.liquidityGross,
"liquidityGross"
);
assert_ne!(liquidity_net_lens, 0);
assert_eq!(
liquidity_net_lens, tick_liquidity.liquidityNet,
"liquidityNet"
);
};
}
#[tokio::test]
async fn test_get_tick_liquidity() {
let slot0 = STATE_VIEW
.getSlot0(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
let tick = nearest_populated_tick(slot0.tick).await;
assert_tick_liquidity_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
let tick = nearest_usable_tick(MIN_TICK_I32, TICK_SPACING);
assert_tick_liquidity_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
let tick = nearest_usable_tick(MAX_TICK_I32, TICK_SPACING);
assert_tick_liquidity_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
}
macro_rules! assert_fee_growth_outside_match {
($pool_id:expr, $tick:expr, $block_id:expr) => {
let (fee_growth_outside0_x128_lens, fee_growth_outside1_x128_lens) = POOL_MANAGER
.get_tick_fee_growth_outside($pool_id, $tick, $block_id)
.await
.unwrap();
let fee_growth_outside = STATE_VIEW
.getTickFeeGrowthOutside($pool_id, $tick.to_i24())
.block($block_id.unwrap())
.call()
.await
.unwrap();
assert_eq!(
fee_growth_outside0_x128_lens, fee_growth_outside.feeGrowthOutside0X128,
"feeGrowthOutside0X128"
);
assert_eq!(
fee_growth_outside1_x128_lens, fee_growth_outside.feeGrowthOutside1X128,
"feeGrowthOutside1X128"
);
};
}
#[tokio::test]
async fn test_get_tick_fee_growth_outside() {
let slot0 = STATE_VIEW
.getSlot0(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
let tick = nearest_populated_tick(slot0.tick).await;
assert_fee_growth_outside_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
let tick = nearest_usable_tick(MIN_TICK_I32, TICK_SPACING);
assert_fee_growth_outside_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
let tick = nearest_usable_tick(MAX_TICK_I32, TICK_SPACING);
assert_fee_growth_outside_match!(*POOL_ID_ETH_USDC, tick, BLOCK_ID);
}
#[tokio::test]
async fn test_get_fee_growth_globals() {
let (fee_growth_global0_lens, fee_growth_global1_lens) = POOL_MANAGER
.get_fee_growth_globals(*POOL_ID_ETH_USDC, BLOCK_ID)
.await
.unwrap();
let fee_growth_globals = STATE_VIEW
.getFeeGrowthGlobals(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
assert_eq!(
fee_growth_global0_lens, fee_growth_globals.feeGrowthGlobal0,
"feeGrowthGlobal0"
);
assert_eq!(
fee_growth_global1_lens, fee_growth_globals.feeGrowthGlobal1,
"feeGrowthGlobal1"
);
}
#[tokio::test]
async fn test_get_liquidity() {
let liquidity_lens = POOL_MANAGER
.get_liquidity(*POOL_ID_ETH_USDC, BLOCK_ID)
.await
.unwrap();
let liquidity = STATE_VIEW
.getLiquidity(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
assert_eq!(liquidity_lens, liquidity);
}
macro_rules! assert_tick_bitmap_match {
($pool_id:expr, $pos:expr, $block_id:expr) => {
let bitmap_lens = POOL_MANAGER
.get_tick_bitmap($pool_id, $pos, $block_id)
.await
.unwrap();
let bitmap_state_view = STATE_VIEW
.getTickBitmap($pool_id, $pos as i16)
.block($block_id.unwrap())
.call()
.await
.unwrap();
assert_ne!(bitmap_lens, U256::ZERO);
assert_eq!(bitmap_lens, bitmap_state_view);
};
}
#[tokio::test]
async fn test_get_tick_bitmap() {
let slot0 = STATE_VIEW
.getSlot0(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
let word = slot0.tick.as_i32().compress(TICK_SPACING).position().0;
for pos in word - 2..=word + 2 {
assert_tick_bitmap_match!(*POOL_ID_ETH_USDC, pos, BLOCK_ID);
}
let word = MIN_TICK_I32.compress(TICK_SPACING).position().0;
assert_tick_bitmap_match!(*POOL_ID_ETH_USDC, word, BLOCK_ID);
let word = MAX_TICK_I32.compress(TICK_SPACING).position().0;
assert_tick_bitmap_match!(*POOL_ID_ETH_USDC, word, BLOCK_ID);
}
async fn get_position_keys() -> Vec<B256> {
get_position_keys_in_blocks(
*POOL_MANAGER.manager.address(),
*POOL_ID_ETH_USDC,
BLOCK_ID.unwrap().as_u64().unwrap() - 499,
BLOCK_ID.unwrap().as_u64().unwrap(),
&*PROVIDER,
)
.await
.unwrap()
}
#[tokio::test]
async fn test_get_position_info() {
let position_keys = get_position_keys().await;
assert!(!position_keys.is_empty());
for position_key in position_keys {
let (
liquidity_lens,
fee_growth_inside0_last_x128_lens,
fee_growth_inside1_last_x128_lens,
) = POOL_MANAGER
.get_position_info(*POOL_ID_ETH_USDC, position_key, BLOCK_ID)
.await
.unwrap();
let position_info = STATE_VIEW
.getPositionInfo_1(*POOL_ID_ETH_USDC, position_key)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
assert_eq!(liquidity_lens, position_info.liquidity);
assert_eq!(
fee_growth_inside0_last_x128_lens, position_info.feeGrowthInside0LastX128,
"feeGrowthInside0LastX128"
);
assert_eq!(
fee_growth_inside1_last_x128_lens, position_info.feeGrowthInside1LastX128,
"feeGrowthInside1LastX128"
);
}
}
#[tokio::test]
async fn test_get_position_liquidity() {
let position_keys = get_position_keys().await;
assert!(!position_keys.is_empty());
for position_key in position_keys {
let liquidity_lens = POOL_MANAGER
.get_position_liquidity(*POOL_ID_ETH_USDC, position_key, BLOCK_ID)
.await
.unwrap();
let liquidity = STATE_VIEW
.getPositionLiquidity(*POOL_ID_ETH_USDC, position_key)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
assert_eq!(liquidity_lens, liquidity);
}
}
#[tokio::test]
async fn test_get_fee_growth_inside() {
let slot0 = STATE_VIEW
.getSlot0(*POOL_ID_ETH_USDC)
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
let tick = nearest_populated_tick(slot0.tick).await;
let tick_lower = tick - TICK_SPACING;
let tick_upper = tick + TICK_SPACING;
let (fee_growth_inside0_lens, fee_growth_inside1_lens) = POOL_MANAGER
.get_fee_growth_inside(*POOL_ID_ETH_USDC, tick_lower, tick_upper, BLOCK_ID)
.await
.unwrap();
let fee_growth_inside = STATE_VIEW
.getFeeGrowthInside(*POOL_ID_ETH_USDC, tick_lower.to_i24(), tick_upper.to_i24())
.block(BLOCK_ID.unwrap())
.call()
.await
.unwrap();
assert_eq!(
fee_growth_inside0_lens, fee_growth_inside.feeGrowthInside0X128,
"feeGrowthInside0X128"
);
assert_eq!(
fee_growth_inside1_lens, fee_growth_inside.feeGrowthInside1X128,
"feeGrowthInside1X128"
);
}
}