From 75e81779cb6c3dd94b472ebbf9aad3cdcd4b4503 Mon Sep 17 00:00:00 2001 From: Hilliam T Date: Wed, 4 Sep 2024 23:02:52 +0100 Subject: [PATCH 1/3] v2 and v3 have factory address field for tracking --- src/amm/uniswap_v2/factory.rs | 3 ++- src/amm/uniswap_v2/mod.rs | 14 +++++++++++++- src/amm/uniswap_v3/mod.rs | 9 ++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/amm/uniswap_v2/factory.rs b/src/amm/uniswap_v2/factory.rs index 1acf9248..150b2863 100644 --- a/src/amm/uniswap_v2/factory.rs +++ b/src/amm/uniswap_v2/factory.rs @@ -127,7 +127,7 @@ impl AutomatedMarketMakerFactory for UniswapV2Factory { { let pair_created_event = IUniswapV2Factory::PairCreated::decode_log(log.as_ref(), true)?; Ok(AMM::UniswapV2Pool( - UniswapV2Pool::new_from_address(pair_created_event.pair, self.fee, provider).await?, + UniswapV2Pool::new_from_address(pair_created_event.pair, log.address(), self.fee, provider).await?, )) } @@ -136,6 +136,7 @@ impl AutomatedMarketMakerFactory for UniswapV2Factory { Ok(AMM::UniswapV2Pool(UniswapV2Pool { address: pair_created_event.pair, + factory_address: log.address(), token_a: pair_created_event.token0, token_b: pair_created_event.token1, token_a_decimals: 0, diff --git a/src/amm/uniswap_v2/mod.rs b/src/amm/uniswap_v2/mod.rs index a6ed26e1..97442bc1 100644 --- a/src/amm/uniswap_v2/mod.rs +++ b/src/amm/uniswap_v2/mod.rs @@ -39,6 +39,7 @@ sol! { #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct UniswapV2Pool { pub address: Address, + pub factory_address: Address, pub token_a: Address, pub token_a_decimals: u8, pub token_b: Address, @@ -187,6 +188,7 @@ impl UniswapV2Pool { #[allow(clippy::too_many_arguments)] pub fn new( address: Address, + factory_address: Address, token_a: Address, token_a_decimals: u8, token_b: Address, @@ -197,6 +199,7 @@ impl UniswapV2Pool { ) -> UniswapV2Pool { UniswapV2Pool { address, + factory_address, token_a, token_a_decimals, token_b, @@ -210,6 +213,7 @@ impl UniswapV2Pool { /// Creates a new instance of the pool from the pair address, and syncs the pool data. pub async fn new_from_address( pair_address: Address, + factory_address: Address, fee: u32, provider: Arc

, ) -> Result @@ -220,6 +224,7 @@ impl UniswapV2Pool { { let mut pool = UniswapV2Pool { address: pair_address, + factory_address, token_a: Address::ZERO, token_a_decimals: 0, token_b: Address::ZERO, @@ -256,7 +261,7 @@ impl UniswapV2Pool { if event_signature == IUniswapV2Factory::PairCreated::SIGNATURE_HASH { let pair_created_event = factory::IUniswapV2Factory::PairCreated::decode_log(log.as_ref(), true)?; - UniswapV2Pool::new_from_address(pair_created_event.pair, fee, provider).await + UniswapV2Pool::new_from_address(pair_created_event.pair, log.address(), fee, provider).await } else { Err(EventLogError::InvalidEventSignature)? } @@ -274,6 +279,7 @@ impl UniswapV2Pool { Ok(UniswapV2Pool { address: pair_created_event.pair, + factory_address: log.address(), token_a: pair_created_event.token0, token_b: pair_created_event.token1, token_a_decimals: 0, @@ -583,6 +589,7 @@ mod tests { let pool = UniswapV2Pool::new_from_address( address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"), + address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"), 300, provider.clone(), ) @@ -593,6 +600,10 @@ mod tests { pool.address, address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc") ); + assert_eq!( + pool.factory_address, + address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f") + ); assert_eq!( pool.token_a, address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48") @@ -640,6 +651,7 @@ mod tests { let token_b = address!("8f18dc399594b451eda8c5da02d0563c0b2d0f16"); let x = UniswapV2Pool { address: address!("652a7b75c229850714d4a11e856052aac3e9b065"), + factory_address: address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"), token_a, token_a_decimals: 18, token_b, diff --git a/src/amm/uniswap_v3/mod.rs b/src/amm/uniswap_v3/mod.rs index 6b13da87..1ca3ac4a 100644 --- a/src/amm/uniswap_v3/mod.rs +++ b/src/amm/uniswap_v3/mod.rs @@ -51,6 +51,7 @@ sol! { #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct UniswapV3Pool { pub address: Address, + pub factory_address: Address, pub token_a: Address, pub token_a_decimals: u8, pub token_b: Address, @@ -454,6 +455,7 @@ impl UniswapV3Pool { #[allow(clippy::too_many_arguments)] pub fn new( address: Address, + factory_address: Address, token_a: Address, token_a_decimals: u8, token_b: Address, @@ -468,6 +470,7 @@ impl UniswapV3Pool { ) -> UniswapV3Pool { UniswapV3Pool { address, + factory_address, token_a, token_a_decimals, token_b, @@ -487,6 +490,7 @@ impl UniswapV3Pool { /// This function will populate all pool data. pub async fn new_from_address( pair_address: Address, + factory_address: Address, creation_block: u64, provider: Arc

, ) -> Result @@ -497,6 +501,7 @@ impl UniswapV3Pool { { let mut pool = UniswapV3Pool { address: pair_address, + factory_address, token_a: Address::ZERO, token_a_decimals: 0, token_b: Address::ZERO, @@ -543,7 +548,7 @@ impl UniswapV3Pool { let pool_created_event = IUniswapV3Factory::PoolCreated::decode_log(&log.inner, true)?; - UniswapV3Pool::new_from_address(pool_created_event.pool, block_number, provider) + UniswapV3Pool::new_from_address(pool_created_event.pool, log.address(), block_number, provider) .await } else { Err(EventLogError::LogBlockNumberNotFound)? @@ -564,6 +569,7 @@ impl UniswapV3Pool { Ok(UniswapV3Pool { address: pool_created_event.pool, + factory_address: log.address(), token_a: pool_created_event.token0, token_b: pool_created_event.token1, token_a_decimals: 0, @@ -1859,6 +1865,7 @@ mod test { let pool = UniswapV3Pool::new_from_address( address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"), + address!("1F98431c8aD98523631AE4a59f267346ea31F984"), 12369620, provider.clone(), ) From 3bf9a22ebd71923d6c6c4febccb2dbc45dcf99ff Mon Sep 17 00:00:00 2001 From: Hilliam T Date: Sat, 14 Sep 2024 09:13:19 +0800 Subject: [PATCH 2/3] update with optional factory_address --- examples/filter-value.rs | 2 +- src/amm/uniswap_v2/mod.rs | 17 +++++++++-------- src/amm/uniswap_v3/factory.rs | 3 ++- src/amm/uniswap_v3/mod.rs | 13 +++++++------ 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/filter-value.rs b/examples/filter-value.rs index c945777c..b0ff2bd9 100644 --- a/examples/filter-value.rs +++ b/examples/filter-value.rs @@ -50,7 +50,7 @@ async fn main() -> eyre::Result<()> { let usdc = address!("3c499c542cEF5E3811e1192ce70d8cC03d5c3359"); let usd_weth_pair_address = address!("cd353F79d9FADe311fC3119B841e1f456b54e858"); let usd_weth_pool = AMM::UniswapV2Pool( - UniswapV2Pool::new_from_address(usd_weth_pair_address, 300, provider.clone()).await?, + UniswapV2Pool::new_from_address(usd_weth_pair_address, None, 300, provider.clone()).await?, ); let weth_value_in_token_to_weth_pool_threshold = U256::from(100000000000000000_u128); // 10 weth diff --git a/src/amm/uniswap_v2/mod.rs b/src/amm/uniswap_v2/mod.rs index 97442bc1..b284b01a 100644 --- a/src/amm/uniswap_v2/mod.rs +++ b/src/amm/uniswap_v2/mod.rs @@ -39,7 +39,8 @@ sol! { #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct UniswapV2Pool { pub address: Address, - pub factory_address: Address, + #[serde(skip_serializing_if = "Option::is_none")] + pub factory_address: Option

, pub token_a: Address, pub token_a_decimals: u8, pub token_b: Address, @@ -199,7 +200,7 @@ impl UniswapV2Pool { ) -> UniswapV2Pool { UniswapV2Pool { address, - factory_address, + factory_address: Some(factory_address), token_a, token_a_decimals, token_b, @@ -213,7 +214,7 @@ impl UniswapV2Pool { /// Creates a new instance of the pool from the pair address, and syncs the pool data. pub async fn new_from_address( pair_address: Address, - factory_address: Address, + factory_address: Option
, fee: u32, provider: Arc

, ) -> Result @@ -261,7 +262,7 @@ impl UniswapV2Pool { if event_signature == IUniswapV2Factory::PairCreated::SIGNATURE_HASH { let pair_created_event = factory::IUniswapV2Factory::PairCreated::decode_log(log.as_ref(), true)?; - UniswapV2Pool::new_from_address(pair_created_event.pair, log.address(), fee, provider).await + UniswapV2Pool::new_from_address(pair_created_event.pair, Some(log.address()), fee, provider).await } else { Err(EventLogError::InvalidEventSignature)? } @@ -279,7 +280,7 @@ impl UniswapV2Pool { Ok(UniswapV2Pool { address: pair_created_event.pair, - factory_address: log.address(), + factory_address: Some(log.address()), token_a: pair_created_event.token0, token_b: pair_created_event.token1, token_a_decimals: 0, @@ -589,7 +590,7 @@ mod tests { let pool = UniswapV2Pool::new_from_address( address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"), - address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"), + Some(address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")), 300, provider.clone(), ) @@ -602,7 +603,7 @@ mod tests { ); assert_eq!( pool.factory_address, - address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f") + Some(address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")) ); assert_eq!( pool.token_a, @@ -651,7 +652,7 @@ mod tests { let token_b = address!("8f18dc399594b451eda8c5da02d0563c0b2d0f16"); let x = UniswapV2Pool { address: address!("652a7b75c229850714d4a11e856052aac3e9b065"), - factory_address: address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"), + factory_address: Some(address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")), token_a, token_a_decimals: 18, token_b, diff --git a/src/amm/uniswap_v3/factory.rs b/src/amm/uniswap_v3/factory.rs index 55094ae5..95d3ba8c 100644 --- a/src/amm/uniswap_v3/factory.rs +++ b/src/amm/uniswap_v3/factory.rs @@ -65,7 +65,7 @@ impl AutomatedMarketMakerFactory for UniswapV3Factory { if let Some(block_number) = log.block_number { let pool_created_filter = IUniswapV3Factory::PoolCreated::decode_log(&log.inner, true)?; Ok(AMM::UniswapV3Pool( - UniswapV3Pool::new_from_address(pool_created_filter.pool, block_number, provider) + UniswapV3Pool::new_from_address(pool_created_filter.pool, Some(log.address()), block_number, provider) .await?, )) } else { @@ -126,6 +126,7 @@ impl AutomatedMarketMakerFactory for UniswapV3Factory { Ok(AMM::UniswapV3Pool(UniswapV3Pool { address: pool_created_event.pool, + factory_address: Some(log.address()), token_a: pool_created_event.token0, token_b: pool_created_event.token1, token_a_decimals: 0, diff --git a/src/amm/uniswap_v3/mod.rs b/src/amm/uniswap_v3/mod.rs index 1ca3ac4a..6c34f6af 100644 --- a/src/amm/uniswap_v3/mod.rs +++ b/src/amm/uniswap_v3/mod.rs @@ -51,7 +51,8 @@ sol! { #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct UniswapV3Pool { pub address: Address, - pub factory_address: Address, + #[serde(skip_serializing_if = "Option::is_none")] + pub factory_address: Option

, pub token_a: Address, pub token_a_decimals: u8, pub token_b: Address, @@ -455,7 +456,7 @@ impl UniswapV3Pool { #[allow(clippy::too_many_arguments)] pub fn new( address: Address, - factory_address: Address, + factory_address: Option
, token_a: Address, token_a_decimals: u8, token_b: Address, @@ -490,7 +491,7 @@ impl UniswapV3Pool { /// This function will populate all pool data. pub async fn new_from_address( pair_address: Address, - factory_address: Address, + factory_address: Option
, creation_block: u64, provider: Arc

, ) -> Result @@ -548,7 +549,7 @@ impl UniswapV3Pool { let pool_created_event = IUniswapV3Factory::PoolCreated::decode_log(&log.inner, true)?; - UniswapV3Pool::new_from_address(pool_created_event.pool, log.address(), block_number, provider) + UniswapV3Pool::new_from_address(pool_created_event.pool, Some(log.address()), block_number, provider) .await } else { Err(EventLogError::LogBlockNumberNotFound)? @@ -569,7 +570,7 @@ impl UniswapV3Pool { Ok(UniswapV3Pool { address: pool_created_event.pool, - factory_address: log.address(), + factory_address: Some(log.address()), token_a: pool_created_event.token0, token_b: pool_created_event.token1, token_a_decimals: 0, @@ -1865,7 +1866,7 @@ mod test { let pool = UniswapV3Pool::new_from_address( address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"), - address!("1F98431c8aD98523631AE4a59f267346ea31F984"), + Some(address!("1F98431c8aD98523631AE4a59f267346ea31F984")), 12369620, provider.clone(), ) From 18be10b1c3d7c18db07989bb84b197d5057eb642 Mon Sep 17 00:00:00 2001 From: Hilliam T Date: Sat, 14 Sep 2024 10:09:12 +0800 Subject: [PATCH 3/3] fix other v2 references with missing factory_address --- benches/uniswapv2_simulate.rs | 1 + examples/simulate-swap.rs | 2 +- examples/swap-calldata.rs | 2 +- src/amm/uniswap_v2/factory.rs | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/benches/uniswapv2_simulate.rs b/benches/uniswapv2_simulate.rs index 9e96c7f5..b7f6a9b2 100644 --- a/benches/uniswapv2_simulate.rs +++ b/benches/uniswapv2_simulate.rs @@ -15,6 +15,7 @@ fn criterion_benchmark(c: &mut Criterion) { let mut pool = UniswapV2Pool { address: address!("ddF8390cEd9fAD414b1ca1DD4Fe14F881C2Cfa70"), + factory_address: None, token_a, token_a_decimals: 18, token_b: address!("fc0d6cf33e38bce7ca7d89c0e292274031b7157a"), diff --git a/examples/simulate-swap.rs b/examples/simulate-swap.rs index 0d3778fd..133cb370 100644 --- a/examples/simulate-swap.rs +++ b/examples/simulate-swap.rs @@ -16,7 +16,7 @@ async fn main() -> eyre::Result<()> { // Initialize the pool let pool_address = address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"); // WETH/USDC - let pool = UniswapV2Pool::new_from_address(pool_address, 300, provider.clone()).await?; + let pool = UniswapV2Pool::new_from_address(pool_address, None, 300, provider.clone()).await?; // Simulate a swap let token_in = address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"); diff --git a/examples/swap-calldata.rs b/examples/swap-calldata.rs index c5b3f215..12bc44f4 100644 --- a/examples/swap-calldata.rs +++ b/examples/swap-calldata.rs @@ -16,7 +16,7 @@ async fn main() -> eyre::Result<()> { // Initialize the pool let pool_address = address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"); - let pool = UniswapV2Pool::new_from_address(pool_address, 300, provider.clone()).await?; + let pool = UniswapV2Pool::new_from_address(pool_address, None, 300, provider.clone()).await?; // Generate the swap calldata let to_address = address!("DecafC0ffee15BadDecafC0ffee15BadDecafC0f"); diff --git a/src/amm/uniswap_v2/factory.rs b/src/amm/uniswap_v2/factory.rs index 150b2863..1753e7d1 100644 --- a/src/amm/uniswap_v2/factory.rs +++ b/src/amm/uniswap_v2/factory.rs @@ -127,7 +127,7 @@ impl AutomatedMarketMakerFactory for UniswapV2Factory { { let pair_created_event = IUniswapV2Factory::PairCreated::decode_log(log.as_ref(), true)?; Ok(AMM::UniswapV2Pool( - UniswapV2Pool::new_from_address(pair_created_event.pair, log.address(), self.fee, provider).await?, + UniswapV2Pool::new_from_address(pair_created_event.pair, Some(log.address()), self.fee, provider).await?, )) } @@ -136,7 +136,7 @@ impl AutomatedMarketMakerFactory for UniswapV2Factory { Ok(AMM::UniswapV2Pool(UniswapV2Pool { address: pair_created_event.pair, - factory_address: log.address(), + factory_address: Some(log.address()), token_a: pair_created_event.token0, token_b: pair_created_event.token1, token_a_decimals: 0,