This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
mod.rs
488 lines (417 loc) · 16.4 KB
/
mod.rs
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
use std::sync::{Arc, Mutex};
use ethers::{
providers::Middleware,
types::{BlockNumber, Filter, Log, ValueOrArray, H160, H256, U64},
};
use indicatif::ProgressBar;
use crate::{
abi, batch_requests,
errors::CFMMError,
pool::{Pool, UniswapV2Pool, UniswapV3Pool},
throttle::RequestThrottle,
};
use serde::{Deserialize, Serialize};
use self::{uniswap_v2::UniswapV2Dex, uniswap_v3::UniswapV3Dex};
pub mod uniswap_v2;
pub mod uniswap_v3;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash)]
pub enum Dex {
UniswapV2(UniswapV2Dex),
UniswapV3(UniswapV3Dex),
}
impl Dex {
pub fn new(
factory_address: H160,
dex_variant: DexVariant,
creation_block: u64,
fee: Option<u64>,
) -> Dex {
let fee = if let Some(fee) = fee { fee } else { 300 };
match dex_variant {
DexVariant::UniswapV2 => Dex::UniswapV2(UniswapV2Dex::new(
factory_address,
BlockNumber::Number(creation_block.into()),
fee,
)),
DexVariant::UniswapV3 => Dex::UniswapV3(UniswapV3Dex::new(
factory_address,
BlockNumber::Number(creation_block.into()),
)),
}
}
pub fn factory_address(&self) -> H160 {
match self {
Dex::UniswapV2(uniswap_v2_dex) => uniswap_v2_dex.factory_address,
Dex::UniswapV3(uniswap_v3_dex) => uniswap_v3_dex.factory_address,
}
}
pub fn creation_block(&self) -> BlockNumber {
match self {
Dex::UniswapV2(uniswap_v2_dex) => uniswap_v2_dex.creation_block,
Dex::UniswapV3(uniswap_v3_dex) => uniswap_v3_dex.creation_block,
}
}
pub fn pool_created_event_signature(&self) -> H256 {
match self {
Dex::UniswapV2(uniswap_v2_dex) => uniswap_v2_dex.pool_created_event_signature(),
Dex::UniswapV3(uniswap_v3_dex) => uniswap_v3_dex.pool_created_event_signature(),
}
}
pub async fn new_pool_from_event_log<M: Middleware>(
&self,
log: Log,
middleware: Arc<M>,
) -> Result<Pool, CFMMError<M>> {
Pool::new_from_event_log(log, middleware).await
}
pub async fn get_all_pools<M: 'static + Middleware>(
&self,
request_throttle: Arc<Mutex<RequestThrottle>>,
step: usize,
progress_bar: ProgressBar,
middleware: Arc<M>,
) -> Result<Vec<Pool>, CFMMError<M>> {
match self {
Dex::UniswapV2(uniswap_v2_dex) => {
uniswap_v2_dex
.get_all_pairs_via_batched_calls(middleware, request_throttle, progress_bar)
.await
}
Dex::UniswapV3(_) => {
let current_block = middleware
.get_block_number()
.await
.map_err(CFMMError::MiddlewareError)?;
self.get_all_pools_from_logs(
current_block.into(),
step,
request_throttle,
progress_bar,
middleware,
)
.await
}
}
}
//Gets all pool data and sync reserves
pub async fn get_all_pool_data<M: Middleware>(
&self,
pools: &mut [Pool],
request_throttle: Arc<Mutex<RequestThrottle>>,
progress_bar: ProgressBar,
middleware: Arc<M>,
) -> Result<(), CFMMError<M>> {
match self {
Dex::UniswapV2(_) => {
let step = 127; //Max batch size for call
for pools in pools.chunks_mut(step) {
request_throttle
.lock()
.expect("Error when acquiring request throttle mutex lock")
.increment_or_sleep(1);
batch_requests::uniswap_v2::get_pool_data_batch_request(
pools,
middleware.clone(),
)
.await?;
progress_bar.inc(step as u64);
}
}
Dex::UniswapV3(_) => {
let step = 76; //Max batch size for call
for pools in pools.chunks_mut(step) {
request_throttle
.lock()
.expect("Error when acquiring request throttle mutex lock")
.increment_or_sleep(1);
batch_requests::uniswap_v3::get_pool_data_batch_request(
pools,
middleware.clone(),
)
.await?;
progress_bar.inc(step as u64);
}
}
}
//For each pair in the pairs vec, get the pool data
Ok(())
}
pub fn new_empty_pool_from_event<M: Middleware>(&self, log: Log) -> Result<Pool, CFMMError<M>> {
match self {
Dex::UniswapV2(uniswap_v2_dex) => uniswap_v2_dex.new_empty_pool_from_event(log),
Dex::UniswapV3(uniswap_v3_dex) => uniswap_v3_dex.new_empty_pool_from_event(log),
}
}
//TODO: rename this to be specific to what it needs to do
//This should get the pool with the best liquidity from the dex variant.
//If univ2, there will only be one pool, if univ3 there will be multiple
pub async fn get_pool_with_best_liquidity<M: Middleware>(
&self,
token_a: H160,
token_b: H160,
middleware: Arc<M>,
) -> Result<Option<Pool>, CFMMError<M>> {
match self {
Dex::UniswapV2(uniswap_v2_dex) => {
let uniswap_v2_factory =
abi::IUniswapV2Factory::new(uniswap_v2_dex.factory_address, middleware.clone());
let pair_address = uniswap_v2_factory.get_pair(token_a, token_b).call().await?;
if pair_address.is_zero() {
Ok(None)
} else {
Ok(Some(Pool::UniswapV2(
UniswapV2Pool::new_from_address(pair_address, middleware).await?,
)))
}
}
Dex::UniswapV3(uniswap_v3_dex) => {
let uniswap_v3_factory =
abi::IUniswapV3Factory::new(uniswap_v3_dex.factory_address, middleware.clone());
let mut best_liquidity = 0;
let mut best_pool_address = H160::zero();
for fee in [100, 300, 500, 1000] {
let pool_address = match uniswap_v3_factory
.get_pool(token_a, token_b, fee)
.call()
.await
{
Ok(address) => {
if !address.is_zero() {
address
} else {
continue;
}
}
Err(_) => {
//TODO: return descriptive errors if there is an issue with the contract or if the pair does not exist
continue;
}
};
let uniswap_v3_pool =
abi::IUniswapV3Pool::new(pool_address, middleware.clone());
let liquidity = uniswap_v3_pool.liquidity().call().await?;
if best_liquidity < liquidity {
best_liquidity = liquidity;
best_pool_address = pool_address;
}
}
if best_pool_address.is_zero() {
Ok(None)
} else {
Ok(Some(Pool::UniswapV3(
UniswapV3Pool::new_from_address(best_pool_address, middleware).await?,
)))
}
}
}
}
//If univ2, there will only be one pool, if univ3 there will be multiple
pub async fn get_all_pools_for_pair<M: Middleware>(
&self,
token_a: H160,
token_b: H160,
middleware: Arc<M>,
) -> Result<Option<Vec<Pool>>, CFMMError<M>> {
match self {
Dex::UniswapV2(uniswap_v2_dex) => {
let uniswap_v2_factory =
abi::IUniswapV2Factory::new(uniswap_v2_dex.factory_address, middleware.clone());
let pair_address = uniswap_v2_factory.get_pair(token_a, token_b).call().await?;
if pair_address.is_zero() {
Ok(None)
} else {
Ok(Some(vec![Pool::UniswapV2(
UniswapV2Pool::new_from_address(pair_address, middleware).await?,
)]))
}
}
Dex::UniswapV3(uniswap_v3_dex) => {
let uniswap_v3_factory =
abi::IUniswapV3Factory::new(uniswap_v3_dex.factory_address, middleware.clone());
let mut pools = vec![];
for fee in [100, 300, 500, 1000] {
match uniswap_v3_factory
.get_pool(token_a, token_b, fee)
.call()
.await
{
Ok(address) => {
if !address.is_zero() {
pools.push(Pool::UniswapV3(
UniswapV3Pool::new_from_address(address, middleware.clone())
.await?,
))
}
}
Err(_) => {
//TODO: return descriptive errors if there is an issue with the contract or if the pair does not exist
continue;
}
};
}
if pools.is_empty() {
Ok(None)
} else {
Ok(Some(pools))
}
}
}
}
//Function to get all pair created events for a given Dex factory address and sync pool data
pub async fn get_all_pools_from_logs<M: 'static + Middleware>(
self,
current_block: BlockNumber,
step: usize,
request_throttle: Arc<Mutex<RequestThrottle>>,
progress_bar: ProgressBar,
middleware: Arc<M>,
) -> Result<Vec<Pool>, CFMMError<M>> {
//Unwrap can be used here because the creation block was verified within `Dex::new()`
let from_block = self
.creation_block()
.as_number()
.expect("Error converting creation block as number")
.as_u64();
let current_block = current_block
.as_number()
.expect("Error converting current block as number")
.as_u64();
let mut aggregated_pairs: Vec<Pool> = vec![];
//Initialize the progress bar message
progress_bar.set_length(current_block - from_block);
//For each block within the range, get all pairs asynchronously
for from_block in (from_block..=current_block).step_by(step) {
let request_throttle = request_throttle.clone();
let provider = middleware.clone();
let progress_bar = progress_bar.clone();
//Get pair created event logs within the block range
let to_block = from_block + step as u64;
//Update the throttle
request_throttle
.lock()
.expect("Error when acquiring request throttle mutex lock")
.increment_or_sleep(1);
let logs = provider
.get_logs(
&Filter::new()
.topic0(ValueOrArray::Value(self.pool_created_event_signature()))
.address(self.factory_address())
.from_block(BlockNumber::Number(U64([from_block])))
.to_block(BlockNumber::Number(U64([to_block]))),
)
.await
.map_err(CFMMError::MiddlewareError)?;
//For each pair created log, create a new Pair type and add it to the pairs vec
for log in logs {
let pool = self.new_empty_pool_from_event(log)?;
aggregated_pairs.push(pool);
}
//Increment the progress bar by the step
progress_bar.inc(step as u64);
}
Ok(aggregated_pairs)
}
//Function to get all pair created events for a given Dex factory address and sync pool data
pub async fn get_all_pools_from_logs_within_range<M: 'static + Middleware>(
self,
from_block: BlockNumber,
to_block: BlockNumber,
step: usize,
request_throttle: Arc<Mutex<RequestThrottle>>,
progress_bar: ProgressBar,
middleware: Arc<M>,
) -> Result<Vec<Pool>, CFMMError<M>> {
//Unwrap can be used here because the creation block was verified within `Dex::new()`
let from_block = from_block
.as_number()
.expect("Error converting creation block as number")
.as_u64();
let to_block = to_block
.as_number()
.expect("Error converting current block as number")
.as_u64();
let mut aggregated_pairs: Vec<Pool> = vec![];
//Initialize the progress bar message
progress_bar.set_length(to_block - from_block);
//For each block within the range, get all pairs asynchronously
for from_block in (from_block..=to_block).step_by(step) {
let request_throttle = request_throttle.clone();
let provider = middleware.clone();
let progress_bar = progress_bar.clone();
//Get pair created event logs within the block range
let to_block = from_block + step as u64;
//Update the throttle
request_throttle
.lock()
.expect("Error when acquiring request throttle mutex lock")
.increment_or_sleep(1);
let logs = provider
.get_logs(
&Filter::new()
.topic0(ValueOrArray::Value(self.pool_created_event_signature()))
.address(self.factory_address())
.from_block(BlockNumber::Number(U64([from_block])))
.to_block(BlockNumber::Number(U64([to_block]))),
)
.await
.map_err(CFMMError::MiddlewareError)?;
//For each pair created log, create a new Pair type and add it to the pairs vec
for log in logs {
let pool = self.new_empty_pool_from_event(log)?;
aggregated_pairs.push(pool);
}
//Increment the progress bar by the step
progress_bar.inc(step as u64);
}
Ok(aggregated_pairs)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
pub enum DexVariant {
UniswapV2,
UniswapV3,
}
impl DexVariant {
pub fn pool_created_event_signature(&self) -> H256 {
match self {
DexVariant::UniswapV2 => uniswap_v2::PAIR_CREATED_EVENT_SIGNATURE,
DexVariant::UniswapV3 => uniswap_v3::POOL_CREATED_EVENT_SIGNATURE,
}
}
}
#[cfg(test)]
mod tests {
use std::{env, str::FromStr, sync::Arc};
use ethers::{
providers::{Http, Provider},
types::H160,
};
use super::{Dex, DexVariant};
#[test]
fn test_factory_address() {}
#[test]
fn test_get_pool_with_best_liquidity() {}
#[tokio::test]
async fn test_get_all_pools_for_pair() {
//Univ3 on ethereum
let univ3_pool = Dex::new(
H160::from_str("0x1F98431c8aD98523631AE4a59f267346ea31F984").unwrap(),
DexVariant::UniswapV3,
12369621,
None,
);
let usdc = H160::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap();
let weth = H160::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap();
let provider = Arc::new(
Provider::<Http>::try_from(
env::var("ETHEREUM_MAINNET_ENDPOINT").expect("Could not initialize provider"),
)
.unwrap(),
);
let pools = univ3_pool
.get_all_pools_for_pair(usdc, weth, provider)
.await
.expect("Could not get all pools for pair");
println!("Pools: {pools:?}");
}
}