use reth_chain_state::CanonStateSubscriptions;
use reth_chainspec::ChainSpecProvider;
use reth_storage_api::BlockReaderIdExt;
use reth_tasks::TaskSpawner;
use crate::{
fee_history::fee_history_cache_new_blocks_task, EthConfig, EthStateCache, FeeHistoryCache,
GasPriceOracle,
};
#[derive(Debug, Clone)]
pub struct EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events> {
pub provider: Provider,
pub pool: Pool,
pub network: Network,
pub evm_config: EvmConfig,
pub config: EthConfig,
pub executor: Tasks,
pub events: Events,
pub cache: EthStateCache,
}
impl<Provider, Pool, EvmConfig, Network, Tasks, Events>
EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events>
where
Provider: BlockReaderIdExt + Clone,
{
pub fn new_fee_history_cache(&self) -> FeeHistoryCache
where
Provider: ChainSpecProvider + 'static,
Tasks: TaskSpawner,
Events: CanonStateSubscriptions,
{
FeeHistoryCacheBuilder::build(self)
}
pub fn new_gas_price_oracle(&self) -> GasPriceOracle<Provider> {
GasPriceOracleBuilder::build(self)
}
}
#[derive(Debug)]
pub struct GasPriceOracleBuilder;
impl GasPriceOracleBuilder {
pub fn build<Provider, Pool, EvmConfig, Network, Tasks, Events>(
ctx: &EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events>,
) -> GasPriceOracle<Provider>
where
Provider: BlockReaderIdExt + Clone,
{
GasPriceOracle::new(ctx.provider.clone(), ctx.config.gas_oracle, ctx.cache.clone())
}
}
#[derive(Debug)]
pub struct FeeHistoryCacheBuilder;
impl FeeHistoryCacheBuilder {
pub fn build<Provider, Pool, EvmConfig, Network, Tasks, Events>(
ctx: &EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events>,
) -> FeeHistoryCache
where
Provider: ChainSpecProvider + BlockReaderIdExt + Clone + 'static,
Tasks: TaskSpawner,
Events: CanonStateSubscriptions,
{
let fee_history_cache =
FeeHistoryCache::new(ctx.cache.clone(), ctx.config.fee_history_cache);
let new_canonical_blocks = ctx.events.canonical_state_stream();
let fhc = fee_history_cache.clone();
let provider = ctx.provider.clone();
ctx.executor.spawn_critical(
"cache canonical blocks for fee history task",
Box::pin(async move {
fee_history_cache_new_blocks_task(fhc, new_canonical_blocks, provider).await;
}),
);
fee_history_cache
}
}