-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpool.rs
More file actions
312 lines (286 loc) · 9.75 KB
/
pool.rs
File metadata and controls
312 lines (286 loc) · 9.75 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
//! ## Pool Extension
//! This module provides functions to create a V4 [`Pool`] struct from pool parameters by fetching
//! on-chain data including pool state and token metadata.
use crate::{entities::pool::Pool, prelude::*};
use alloc::string::{String, ToString};
use alloy::{eips::BlockId, network::Network, providers::Provider};
use alloy_primitives::{Address, ChainId, aliases::U24};
use uniswap_sdk_core::{
prelude::{Currency, Ether, Token},
token,
};
use uniswap_v3_sdk::{
entities::TickIndex, extensions::lens::bindings::ierc20metadata::IERC20Metadata,
};
impl Pool {
/// Get a V4 [`Pool`] struct from pool parameters
///
/// Fetches pool state and token metadata in parallel using `tokio::join!`.
/// When using [`CallBatchLayer`](https://docs.rs/alloy-provider/latest/alloy_provider/layers/struct.CallBatchLayer.html),
/// parallel calls are automatically batched (only for latest block queries).
///
/// ## Arguments
///
/// * `chain_id`: The chain id
/// * `manager`: The pool manager address
/// * `currency_a`: Address of one currency in the pool (Address::ZERO for native ETH)
/// * `currency_b`: Address of the other currency in the pool (Address::ZERO for native ETH)
/// * `fee`: Fee tier of the pool
/// * `tick_spacing`: Tick spacing of the pool
/// * `hooks`: Hook contract address
/// * `provider`: The alloy provider
/// * `block_id`: Optional block number to query
#[inline]
#[allow(clippy::too_many_arguments)]
pub async fn from_pool_key<P, N, I>(
chain_id: ChainId,
manager: Address,
currency_a: Address,
currency_b: Address,
fee: U24,
tick_spacing: I,
hooks: Address,
provider: P,
block_id: Option<BlockId>,
) -> Result<Self, Error>
where
P: Provider<N>,
N: Network,
I: TickIndex,
{
let block_id = block_id.unwrap_or(BlockId::latest());
// Create temporary currencies for pool key calculation (without metadata)
let temp_currency_a = create_currency(chain_id, currency_a, None);
let temp_currency_b = create_currency(chain_id, currency_b, None);
let pool_id =
Self::get_pool_id(&temp_currency_a, &temp_currency_b, fee, tick_spacing, hooks)?;
let lens = PoolManagerLens::new(manager, &provider);
let (slot0, liquidity, token_a_data, token_b_data) = tokio::join!(
lens.get_slot0(pool_id, Some(block_id)),
lens.get_liquidity(pool_id, Some(block_id)),
async {
if currency_a.is_zero() {
Ok(None)
} else {
fetch_token_metadata::<N, _>(currency_a, &provider, block_id)
.await
.map(Some)
}
},
async {
if currency_b.is_zero() {
Ok(None)
} else {
fetch_token_metadata::<N, _>(currency_b, &provider, block_id)
.await
.map(Some)
}
}
);
let (sqrt_price_x96, _, _, _) = slot0?;
let liquidity = liquidity?;
if sqrt_price_x96.is_zero() {
return Err(Error::PoolNotInitialized);
}
Self::new(
create_currency(chain_id, currency_a, token_a_data?),
create_currency(chain_id, currency_b, token_b_data?),
fee,
tick_spacing.to_i24().as_i32(),
hooks,
sqrt_price_x96,
liquidity,
)
}
}
impl<P, N, I> Pool<SimpleTickDataProvider<P, N, I>>
where
P: Provider<N>,
N: Network,
I: TickIndex,
{
/// Get a V4 [`Pool`] struct with tick data provider from pool parameters
///
/// ## Arguments
///
/// * `chain_id`: The chain id
/// * `manager`: The pool manager address
/// * `currency_a`: Address of one currency in the pool (Address::ZERO for native ETH)
/// * `currency_b`: Address of the other currency in the pool (Address::ZERO for native ETH)
/// * `fee`: Fee tier of the pool
/// * `tick_spacing`: Tick spacing of the pool
/// * `hooks`: Hook contract address
/// * `provider`: The alloy provider
/// * `block_id`: Optional block number to query
///
/// ## Returns
///
/// A [`Pool`] struct with tick data provider
#[inline]
#[allow(clippy::too_many_arguments)]
pub async fn from_pool_key_with_tick_data_provider(
chain_id: ChainId,
manager: Address,
currency_a: Address,
currency_b: Address,
fee: U24,
tick_spacing: I,
hooks: Address,
provider: P,
block_id: Option<BlockId>,
) -> Result<Self, Error> {
let pool = Pool::from_pool_key(
chain_id,
manager,
currency_a,
currency_b,
fee,
tick_spacing,
hooks,
&provider,
block_id,
)
.await?;
Self::new_with_tick_data_provider(
pool.currency0,
pool.currency1,
pool.fee,
tick_spacing,
pool.hooks,
pool.sqrt_price_x96,
pool.liquidity,
SimpleTickDataProvider::new(manager, pool.pool_id, provider, block_id),
)
}
}
/// Creates a Currency from an address and optional metadata
fn create_currency(
chain_id: ChainId,
address: Address,
metadata: Option<(u8, String, String)>,
) -> Currency {
if address.is_zero() {
Currency::NativeCurrency(Ether::on_chain(chain_id))
} else if let Some((decimals, name, symbol)) = metadata {
Currency::Token(token!(chain_id, address, decimals, symbol, name))
} else {
// Placeholder for pool ID calculation when metadata not yet fetched
Currency::Token(token!(chain_id, address, 18))
}
}
/// Fetches ERC20 token metadata (decimals, name, symbol) in parallel
async fn fetch_token_metadata<N, P>(
address: Address,
provider: P,
block_id: BlockId,
) -> Result<(u8, String, String), Error>
where
N: Network,
P: Provider<N>,
{
let contract = IERC20Metadata::new(address, provider);
let decimals = contract.decimals().block(block_id);
let name = contract.name().block(block_id);
let symbol = contract.symbol().block(block_id);
let (decimals, name, symbol) = tokio::join!(decimals.call(), name.call(), symbol.call());
Ok((decimals?, name?, symbol?))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::*;
use alloy::providers::{ProviderBuilder, layers::CallBatchLayer};
use uniswap_v3_sdk::{constants::FeeAmount, entities::TickDataProvider};
const FEE: FeeAmount = FeeAmount::LOW;
const TICK_SPACING: i32 = 10;
#[tokio::test]
async fn test_from_pool_key() {
// Use CallBatchLayer to demonstrate recommended setup
// Note: batching only works for latest block queries
let provider = ProviderBuilder::new()
.layer(CallBatchLayer::new())
.connect_http(RPC_URL.clone());
// Use the ETH-USDC pool with fee 500 (0.05%) and tick spacing 10
let pool = Pool::from_pool_key(
1,
*POOL_MANAGER_ADDRESS,
Address::ZERO, // ETH
USDC.address,
FEE.into(),
TICK_SPACING,
Address::ZERO, // No hooks
provider,
BLOCK_ID,
)
.await
.unwrap();
// Verify pool was created successfully
assert!(
!pool.sqrt_price_x96.is_zero(),
"sqrt_price_x96 should be non-zero"
);
assert_ne!(
pool.liquidity, 0,
"liquidity should be non-zero for active pool"
);
// Verify currency0 is ETH (native currency)
assert!(
matches!(pool.currency0, Currency::NativeCurrency(_)),
"currency0 should be native ETH"
);
// Verify currency1 is USDC token with correct metadata
if let Currency::Token(token) = &pool.currency1 {
assert_eq!(token.decimals, 6, "USDC should have 6 decimals");
assert_eq!(
token.symbol.as_deref(),
Some("USDC"),
"USDC symbol should match"
);
} else {
panic!("currency1 should be a Token");
}
// Verify pool parameters
assert_eq!(
pool.fee,
<U24 as From<FeeAmount>>::from(FEE),
"fee should be 500"
);
assert_eq!(pool.tick_spacing, TICK_SPACING, "tick_spacing should be 10");
assert_eq!(pool.hooks, Address::ZERO, "hooks should be zero address");
}
#[tokio::test]
async fn test_from_pool_key_with_tick_data_provider() {
let provider = ProviderBuilder::new()
.layer(CallBatchLayer::new())
.connect_http(RPC_URL.clone());
let pool = Pool::from_pool_key_with_tick_data_provider(
1,
*POOL_MANAGER_ADDRESS,
Address::ZERO, // ETH
USDC.address,
FEE.into(),
TICK_SPACING,
Address::ZERO, // No hooks
provider,
BLOCK_ID,
)
.await
.unwrap();
// Verify pool was created successfully
assert!(
!pool.sqrt_price_x96.is_zero(),
"sqrt_price_x96 should be non-zero"
);
assert_ne!(
pool.liquidity, 0,
"liquidity should be non-zero for active pool"
);
// Verify tick data provider is working by fetching a tick
let tick = pool
.tick_data_provider
.get_tick(pool.tick_current)
.await
.unwrap();
assert_eq!(tick.index, pool.tick_current);
}
}