-
Notifications
You must be signed in to change notification settings - Fork 152
Remove CoW AMM indexer from driver #3680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 45 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
8330018
Receive helper with owners
squadgazzz 0b02371
Migrate to cache
squadgazzz 34061d3
Adapt autopilot
squadgazzz 16364e0
Drop redundant config
squadgazzz 99e9859
Redundant e2e config
squadgazzz 79d6fd0
Adapt driver tests
squadgazzz ffe080a
Nit
squadgazzz c69e53d
Fix types
squadgazzz 2a51779
Formatting
squadgazzz 1c64b5b
Revert "Formatting"
squadgazzz 7d12cd7
Revert "Fix types"
squadgazzz 1990102
Fix tests
squadgazzz a2c3778
Revert "Revert "Fix types""
squadgazzz 8723b99
Formatting
squadgazzz 26b8ff6
Fix types
squadgazzz 6fa32d8
Fix types
squadgazzz 7170235
Redundant log
squadgazzz 7575e6a
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz be1b55f
Redundant import
squadgazzz f70d7cc
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz 5e72b46
Fix
squadgazzz c6608f3
Revert to the previous request
squadgazzz 0d1c9c1
Fetch factory address
squadgazzz c30c6ac
Adjust config
squadgazzz bd17344
Adjust e2e config
squadgazzz af7762f
Docs
squadgazzz 5c48746
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz 58792c8
Fix config
squadgazzz ffeb480
Fix parsing
squadgazzz edce8d3
Fix
squadgazzz 1b5afa8
Drop legacy support
squadgazzz f858ded
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz c380440
Fix
squadgazzz 5851c93
Fix e2e test and simplify config
squadgazzz 6527374
Better alloy binding
squadgazzz c709bea
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz e637711
Fix
squadgazzz 8a29a52
Trigger Build
squadgazzz 7d0fb6f
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz cfc3e13
PR comments
squadgazzz 2d6ef2d
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz ca5e8ee
Formatting
squadgazzz de59609
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz 1224cc6
Review comments
squadgazzz fd348dc
Temp SC instance
squadgazzz b2aa34c
Simplify the test
squadgazzz 4e1b3d7
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz 1a92785
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz b2469c8
Merge branch 'main' into remove-driver-cow-amm-indexer
squadgazzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "abi": [ | ||
| { | ||
| "inputs": [], | ||
| "name": "FACTORY", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "address", | ||
| "name": "", | ||
| "type": "address" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use { | ||
| crate::domain::eth, | ||
| contracts::CowAmmLegacyHelper, | ||
| cow_amm::Amm, | ||
| ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy}, | ||
| itertools::{ | ||
| Either::{Left, Right}, | ||
| Itertools, | ||
| }, | ||
| std::{ | ||
| collections::{HashMap, HashSet}, | ||
| sync::Arc, | ||
| }, | ||
| tokio::sync::RwLock, | ||
| }; | ||
|
|
||
| /// Cache for CoW AMM data to avoid using the registry dependency. | ||
| /// Maps AMM address to the corresponding Amm instance. | ||
| pub struct Cache { | ||
| inner: RwLock<HashMap<eth::Address, Arc<Amm>>>, | ||
jmg-duarte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| web3: ethrpc::Web3, | ||
| helper_by_factory: HashMap<eth::Address, CowAmmLegacyHelper>, | ||
| } | ||
|
|
||
| impl Cache { | ||
| pub fn new(web3: ethrpc::Web3, factory_mapping: HashMap<eth::Address, eth::Address>) -> Self { | ||
| let helper_by_factory = factory_mapping | ||
| .into_iter() | ||
| .map(|(factory, helper)| (factory, CowAmmLegacyHelper::at(&web3, helper.0))) | ||
| .collect(); | ||
| Self { | ||
| inner: RwLock::new(HashMap::new()), | ||
| web3, | ||
| helper_by_factory, | ||
| } | ||
| } | ||
|
|
||
| /// Gets or creates AMM instances for the given surplus capturing JIT order | ||
| /// owners. | ||
| pub async fn get_or_create_amms( | ||
| &self, | ||
| surplus_capturing_jit_order_owners: &HashSet<eth::Address>, | ||
| ) -> Vec<Arc<Amm>> { | ||
| let (mut cached_amms, missing_amms): (Vec<Arc<Amm>>, Vec<eth::Address>) = { | ||
| let cache = self.inner.read().await; | ||
| surplus_capturing_jit_order_owners | ||
| .iter() | ||
| .partition_map(|&address| match cache.get(&address) { | ||
| Some(amm) => Left(amm.clone()), | ||
| None => Right(address), | ||
| }) | ||
| }; | ||
|
|
||
| if missing_amms.is_empty() { | ||
| return cached_amms; | ||
| } | ||
|
|
||
| let fetch_futures = missing_amms.into_iter().map(|amm_address| async move { | ||
| let factory_address = self | ||
| .fetch_amm_factory_address(amm_address) | ||
| .await | ||
| .inspect_err(|err| { | ||
| tracing::warn!( | ||
| ?err, | ||
| amm_address = ?amm_address.0, | ||
| "failed to fetch CoW AMM factory address" | ||
| ); | ||
| }) | ||
| .ok()?; | ||
|
|
||
| let Some(helper) = self.helper_by_factory.get(&factory_address) else { | ||
| tracing::warn!( | ||
| factory_address = ?factory_address.0, | ||
| amm_address = ?amm_address.0, | ||
| "no helper contract configured for CoW AMM factory" | ||
| ); | ||
| return None; | ||
| }; | ||
|
|
||
| match Amm::new(amm_address.0, helper).await { | ||
| Ok(amm) => Some((amm_address, Arc::new(amm))), | ||
| Err(err) => { | ||
| let helper_address = helper.address().0; | ||
| tracing::warn!( | ||
| ?err, | ||
| amm_address = ?amm_address.0, | ||
| ?helper_address, | ||
| "failed to create CoW AMM instance" | ||
| ); | ||
| None | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| let fetched_results = futures::future::join_all(fetch_futures).await; | ||
|
|
||
| // Update cache with newly fetched AMMs | ||
| let newly_created_amms = { | ||
| let mut cache = self.inner.write().await; | ||
| fetched_results | ||
| .into_iter() | ||
| .flatten() | ||
| .map(|(amm_address, amm)| { | ||
| cache.insert(amm_address, amm.clone()); | ||
| amm | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| }; | ||
|
|
||
| // Combine cached and newly created AMMs | ||
| cached_amms.extend(newly_created_amms); | ||
| cached_amms | ||
| } | ||
|
|
||
| /// Fetches the factory address for the given AMM by calling the | ||
| /// `FACTORY` function. | ||
| async fn fetch_amm_factory_address( | ||
| &self, | ||
| amm_address: eth::Address, | ||
| ) -> anyhow::Result<eth::Address> { | ||
| let factory_getter = | ||
| contracts::alloy::cow_amm::CowAmmFactoryGetter::CowAmmFactoryGetter::new( | ||
| amm_address.0.into_alloy(), | ||
| &self.web3.alloy, | ||
| ); | ||
| Ok(factory_getter.FACTORY().call().await?.into_legacy().into()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod competition; | ||
| pub mod cow_amm; | ||
| pub mod eth; | ||
| pub mod liquidity; | ||
| pub mod mempools; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment starting at line 358 appears to be incomplete or cut off. The comment should be completed to explain what happens when reference prices are not provided.