Skip to content

Commit

Permalink
Almost done with BalancerFetcher impl?
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinquaXD committed Aug 18, 2023
1 parent 8bd77ac commit 2c7086c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
47 changes: 39 additions & 8 deletions crates/driver/src/boundary/balances.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
use {
crate::{
domain::{competition::order::SellTokenBalance, eth},
infra::Ethereum,
infra::{self, Ethereum},
},
anyhow::Result,
anyhow::{Context, Result},
model::{interaction::InteractionData, order::SellTokenSource},
shared::account_balances::{BalanceFetching, Query as LegacyQuery, SimulationBalanceFetcher},
shared::{
account_balances::{
BalanceFetching,
CachingBalanceFetcher,
Query as LegacyQuery,
SimulationBalanceFetcher,
},
code_simulation::{CodeSimulating, SimulationError},
ethrpc::extensions::StateOverrides,
},
std::sync::Arc,
web3::types::CallRequest,
};

#[derive(Clone)]
pub struct Fetcher(Arc<SimulationBalanceFetcher>);
pub struct Fetcher(Arc<CachingBalanceFetcher>);

impl Fetcher {
pub async fn new(eth: &Ethereum) -> Self {
pub async fn new(eth: &Ethereum, simulator: infra::Simulator) -> Self {
let vault = eth
.contracts()
.settlement()
Expand All @@ -22,12 +32,17 @@ impl Fetcher {
.await
.expect("can't fetch balancer vault from settlement contract");

Self(Arc::new(SimulationBalanceFetcher::new(
Arc::new(super::web3(eth)),
let fetcher = Arc::new(SimulationBalanceFetcher::new(
Arc::new(simulator),
eth.contracts().settlement().address(),
vault,
None,
)))
));

let fetcher = Arc::new(CachingBalanceFetcher::new(fetcher));
// TODO spawn background task
// fetcher.spawn_background_task(block_stream);
Self(fetcher)
}

pub async fn get(&self, query: Query) -> Result<eth::TokenAmount> {
Expand Down Expand Up @@ -64,3 +79,19 @@ pub struct Query {
pub source: SellTokenBalance,
pub pre_interactions: Vec<eth::Interaction>,
}

#[async_trait::async_trait]
impl CodeSimulating for infra::Simulator {
async fn simulate(
&self,
_call: CallRequest,
_overrides: StateOverrides,
) -> Result<Vec<u8>, SimulationError> {
// TODO move ethrpc into a separate crate and use it to implement
// [`CodeSimulating`] for [`infra::Simulator`].
self.simulate_with_overrides()
.await
.context("miep")
.map_err(Into::into)
}
}
2 changes: 1 addition & 1 deletion crates/driver/src/infra/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Api {
.layer(tower_http::trace::TraceLayer::new_for_http()),
);

let balances = balances::Fetcher::new(&self.eth).await;
let balances = balances::Fetcher::new(&self.eth, self.simulator.clone()).await;

// Add the metrics endpoint.
app = routes::metrics(app);
Expand Down
4 changes: 4 additions & 0 deletions crates/driver/src/infra/simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ impl Simulator {
Inner::Ethereum(ethereum) => ethereum.estimate_gas(tx).await?,
})
}

pub async fn simulate_with_overrides(&self) -> Result<Vec<u8>, Error> {
todo!()
}
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 2c7086c

Please sign in to comment.