Skip to content

Commit

Permalink
orderbook: instanciate code fetcher once (#2912)
Browse files Browse the repository at this point in the history
# Description
<!--- Describe your changes to provide context for reviewers, including
why it is needed -->

Reduce the number of RPC calls by instantiating the code fetcher only
once.

# Changes
<!-- List of detailed changes (how the change is accomplished) -->

- [ ] Instanciate code_fetcher in run (orderbook)
- [ ] Pass it to PriceEstimatorFactory

Fixes #2429

---------

Co-authored-by: Felix Leupold <[email protected]>
  • Loading branch information
ybensacq and fleupold authored Aug 20, 2024
1 parent d66f677 commit 4f0f946
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use {
trace_call::TraceCallDetector,
},
baseline_solver::BaseTokens,
code_fetching::CachedCodeFetcher,
http_client::HttpClientFactory,
maintenance::{Maintaining, ServiceMaintenance},
metrics::LivenessChecking,
Expand Down Expand Up @@ -297,6 +298,8 @@ pub async fn run(args: Arguments) {
})));
let block_retriever = args.shared.current_block.retriever(web3.clone());

let code_fetcher = Arc::new(CachedCodeFetcher::new(Arc::new(web3.clone())));

let mut price_estimator_factory = PriceEstimatorFactory::new(
&args.price_estimation,
&args.shared,
Expand All @@ -321,6 +324,7 @@ pub async fn run(args: Arguments) {
http_factory: http_factory.clone(),
bad_token_detector: bad_token_detector.clone(),
tokens: token_info_fetcher.clone(),
code_fetcher: code_fetcher.clone(),
},
)
.expect("failed to initialize price estimator factory");
Expand Down
5 changes: 4 additions & 1 deletion crates/orderbook/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ pub async fn run(args: Arguments) {
web3: web3.clone(),
})));

let code_fetcher = Arc::new(CachedCodeFetcher::new(Arc::new(web3.clone())));

let mut price_estimator_factory = PriceEstimatorFactory::new(
&args.price_estimation,
&args.shared,
Expand All @@ -268,6 +270,7 @@ pub async fn run(args: Arguments) {
http_factory: http_factory.clone(),
bad_token_detector: bad_token_detector.clone(),
tokens: token_info_fetcher.clone(),
code_fetcher: code_fetcher.clone(),
},
)
.expect("failed to initialize price estimator factory");
Expand Down Expand Up @@ -351,7 +354,7 @@ pub async fn run(args: Arguments) {
signature_validator,
Arc::new(postgres.clone()),
args.max_limit_orders_per_user,
Arc::new(CachedCodeFetcher::new(Arc::new(web3.clone()))),
code_fetcher,
app_data_validator.clone(),
args.max_gas_per_order,
));
Expand Down
7 changes: 2 additions & 5 deletions crates/shared/src/price_estimation/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct Components {
pub http_factory: HttpClientFactory,
pub bad_token_detector: Arc<dyn BadTokenDetecting>,
pub tokens: Arc<dyn TokenInfoFetching>,
pub code_fetcher: Arc<CachedCodeFetcher>,
}

impl<'a> PriceEstimatorFactory<'a> {
Expand Down Expand Up @@ -107,14 +108,10 @@ impl<'a> PriceEstimatorFactory<'a> {
None => Arc::new(web3.clone()),
};

let code_fetcher =
ethrpc::instrumented::instrument_with_label(&network.web3, "codeFetching".into());
let code_fetcher = Arc::new(CachedCodeFetcher::new(Arc::new(code_fetcher)));

Some(Arc::new(TradeVerifier::new(
web3,
simulator,
code_fetcher,
components.code_fetcher.clone(),
network.block_stream.clone(),
network.settlement,
network.native_token,
Expand Down

0 comments on commit 4f0f946

Please sign in to comment.