Skip to content

Commit

Permalink
Merge pull request #9 from core-coin/crate/provider
Browse files Browse the repository at this point in the history
fix provider crate
  • Loading branch information
error2215 authored May 28, 2024
2 parents ed85055 + 79e1615 commit 8ca6bcd
Show file tree
Hide file tree
Showing 28 changed files with 647 additions and 634 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Crates used in Core or that are used by other crates
- [x] json-rpc - in work (Misha)
- [x] network - in work (Misha)
- [x] node-bindings - in work (Misha)
- [ ] provider - in work (Misha)
- [x] provider - in work (Misha)
- [ ] pubsub - in work (Misha)
- [x] rpc-client - in work (Misha)
- [ ] rpc-types - in work (Misha)
Expand Down
12 changes: 6 additions & 6 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{CallDecoder, Error, EthCall, Result};
use alloy_dyn_abi::{DynSolValue, JsonAbiExt};
use alloy_json_abi::Function;
use alloy_network::{Ethereum, Network, ReceiptResponse, TransactionBuilder};
use alloy_network::{Core, Network, ReceiptResponse, TransactionBuilder};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_provider::{PendingTransactionBuilder, Provider};
use alloy_rpc_types::{state::StateOverride, AccessList, BlobTransactionSidecar, BlockId};
Expand All @@ -15,13 +15,13 @@ use std::{

/// [`CallBuilder`] using a [`SolCall`] type as the call decoder.
// NOTE: please avoid changing this type due to its use in the `sol!` macro.
pub type SolCallBuilder<T, P, C, N = Ethereum> = CallBuilder<T, P, PhantomData<C>, N>;
pub type SolCallBuilder<T, P, C, N = Core> = CallBuilder<T, P, PhantomData<C>, N>;

/// [`CallBuilder`] using a [`Function`] as the call decoder.
pub type DynCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, Function, N>;
pub type DynCallBuilder<T, P, N = Core> = CallBuilder<T, P, Function, N>;

/// [`CallBuilder`] that does not have a call decoder.
pub type RawCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, (), N>;
pub type RawCallBuilder<T, P, N = Core> = CallBuilder<T, P, (), N>;

/// A builder for sending a transaction via `eth_sendTransaction`, or calling a contract via
/// `eth_call`.
Expand Down Expand Up @@ -120,7 +120,7 @@ pub type RawCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, (), N>;
/// [sol]: alloy_sol_types::sol
#[derive(Clone)]
#[must_use = "call builders do nothing unless you `.call`, `.send`, or `.await` them"]
pub struct CallBuilder<T, P, D, N: Network = Ethereum> {
pub struct CallBuilder<T, P, D, N: Network = Core> {
request: N::TransactionRequest,
block: BlockId,
state: Option<StateOverride>,
Expand Down Expand Up @@ -538,7 +538,7 @@ impl<T, P, D: CallDecoder, N: Network> std::fmt::Debug for CallBuilder<T, P, D,
#[allow(unused_imports)]
mod tests {
use super::*;
use alloy_network::Ethereum;
use alloy_network::Core;
use alloy_node_bindings::{Anvil, AnvilInstance};
use alloy_primitives::{address, b256, bytes, hex, utils::parse_units, B256};
use alloy_provider::{
Expand Down
12 changes: 6 additions & 6 deletions crates/contract/src/eth_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
N: Network,
D: CallDecoder,
{
inner: alloy_provider::EthCall<'req, 'state, T, N>,
inner: alloy_provider::XcbCall<'req, 'state, T, N>,

decoder: &'coder D,
}
Expand All @@ -42,7 +42,7 @@ where
{
/// Create a new [`EthCall`].
pub const fn new(
inner: alloy_provider::EthCall<'req, 'state, T, N>,
inner: alloy_provider::XcbCall<'req, 'state, T, N>,
decoder: &'coder D,
) -> Self {
Self { inner, decoder }
Expand All @@ -55,7 +55,7 @@ where
N: Network,
{
/// Create a new [`EthCall`].
pub const fn new_raw(inner: alloy_provider::EthCall<'req, 'state, T, N>) -> Self {
pub const fn new_raw(inner: alloy_provider::XcbCall<'req, 'state, T, N>) -> Self {
Self::new(inner, &RAW_CODER)
}
}
Expand Down Expand Up @@ -91,13 +91,13 @@ where
}
}

impl<'req, 'state, T, N> From<alloy_provider::EthCall<'req, 'state, T, N>>
impl<'req, 'state, T, N> From<alloy_provider::XcbCall<'req, 'state, T, N>>
for EthCall<'req, 'state, 'static, (), T, N>
where
T: Transport + Clone,
N: Network,
{
fn from(inner: alloy_provider::EthCall<'req, 'state, T, N>) -> Self {
fn from(inner: alloy_provider::XcbCall<'req, 'state, T, N>) -> Self {
Self { inner, decoder: &RAW_CODER }
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ where
N: Network,
D: CallDecoder,
{
inner: <alloy_provider::EthCall<'req, 'state, T, N> as IntoFuture>::IntoFuture,
inner: <alloy_provider::XcbCall<'req, 'state, T, N> as IntoFuture>::IntoFuture,
decoder: &'coder D,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/contract/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Error;
use alloy_network::Ethereum;
use alloy_network::Core;
use alloy_primitives::{Address, LogData};
use alloy_provider::{FilterPollerBuilder, Network, Provider};
use alloy_rpc_types::{Filter, Log};
Expand All @@ -11,7 +11,7 @@ use std::{fmt, marker::PhantomData};

/// Helper for managing the event filter before querying or streaming its logs
#[must_use = "event filters do nothing unless you `query`, `watch`, or `stream` them"]
pub struct Event<T, P, E, N = Ethereum> {
pub struct Event<T, P, E, N = Core> {
/// The provider to use for querying or streaming logs.
pub provider: P,
/// The filter to use for querying or streaming logs.
Expand Down
4 changes: 2 additions & 2 deletions crates/contract/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{CallBuilder, Event, Interface, Result};
use alloy_dyn_abi::DynSolValue;
use alloy_json_abi::{Function, JsonAbi};
use alloy_network::{Ethereum, Network};
use alloy_network::{Core, Network};
use alloy_primitives::{Address, Selector};
use alloy_provider::Provider;
use alloy_rpc_types::Filter;
Expand All @@ -14,7 +14,7 @@ use std::marker::PhantomData;
/// A contract is an abstraction of an executable program on Ethereum. Every deployed contract has
/// an address, which is used to connect to it so that it may receive messages (transactions).
#[derive(Clone)]
pub struct ContractInstance<T, P, N = Ethereum> {
pub struct ContractInstance<T, P, N = Core> {
address: Address,
provider: P,
interface: Interface,
Expand Down
2 changes: 1 addition & 1 deletion crates/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use call::*;
// NOTE: please avoid changing the API of this module due to its use in the `sol!` macro.
#[doc(hidden)]
pub mod private {
pub use alloy_network::{Ethereum, Network};
pub use alloy_network::{Core, Network};
pub use alloy_provider::Provider;
pub use alloy_transport::Transport;
}
10 changes: 5 additions & 5 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
BuildResult, Ethereum, Network, NetworkSigner, TransactionBuilder, TransactionBuilderError,
BuildResult, Core, Network, NetworkSigner, TransactionBuilder, TransactionBuilderError,
};
use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction};
use alloy_primitives::{Bytes, ChainId, IcanAddress, TxKind, B1368, U256};
use alloy_rpc_types::{request::TransactionRequest, AccessList};

impl TransactionBuilder<Ethereum> for TransactionRequest {
impl TransactionBuilder<Core> for TransactionRequest {
fn network_id(&self) -> Option<ChainId> {
self.network_id
}
Expand Down Expand Up @@ -168,7 +168,7 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
self.populate_blob_hashes();
}

fn build_unsigned(self) -> BuildResult<TypedTransaction, Ethereum> {
fn build_unsigned(self) -> BuildResult<TypedTransaction, Core> {
if let Err((tx_type, missing)) = self.missing_keys() {
return Err((
self,
Expand All @@ -178,10 +178,10 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
Ok(self.build_typed_tx().expect("checked by missing_keys"))
}

async fn build<S: NetworkSigner<Ethereum>>(
async fn build<S: NetworkSigner<Core>>(
self,
signer: &S,
) -> Result<<Ethereum as Network>::TxEnvelope, TransactionBuilderError<Ethereum>> {
) -> Result<<Core as Network>::TxEnvelope, TransactionBuilderError<Core>> {
Ok(signer.sign_request(self).await?)
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/network/src/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ mod builder;
mod signer;
pub use signer::EthereumSigner;

/// Types for a mainnet-like Ethereum network.
/// Types for a mainnet-like Core network.
#[derive(Clone, Copy, Debug)]
pub struct Ethereum {
pub struct Core {
_private: (),
}

impl Network for Ethereum {
impl Network for Core {
type TxType = alloy_consensus::TxType;

type TxEnvelope = alloy_consensus::TxEnvelope;
Expand Down
2 changes: 1 addition & 1 deletion crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use transaction::{
};

mod ethereum;
pub use ethereum::{Ethereum, EthereumSigner};
pub use ethereum::{Core, EthereumSigner};

mod any;
pub use any::AnyNetwork;
Expand Down
71 changes: 36 additions & 35 deletions crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use crate::{
fillers::{
ChainIdFiller, FillerControlFlow, GasFiller, JoinFill, NonceFiller, SignerFiller, TxFiller,
EnergyFiller, FillerControlFlow, JoinFill, NetworkIdFiller, NonceFiller, SignerFiller,
TxFiller,
},
provider::SendableTx,
Provider, RootProvider,
};
use alloy_network::{Ethereum, Network};
use alloy_network::{Core, Network};
use alloy_rpc_client::{BuiltInConnectionString, ClientBuilder, RpcClient};
use alloy_transport::{BoxTransport, Transport, TransportError, TransportResult};
use std::marker::PhantomData;

/// The recommended filler.
type RecommendFiller =
JoinFill<JoinFill<JoinFill<Identity, GasFiller>, NonceFiller>, ChainIdFiller>;
JoinFill<JoinFill<JoinFill<Identity, EnergyFiller>, NonceFiller>, NetworkIdFiller>;

/// A layering abstraction in the vein of [`tower::Layer`]
///
/// [`tower::Layer`]: https://docs.rs/tower/latest/tower/trait.Layer.html
pub trait ProviderLayer<P: Provider<T, N>, T: Transport + Clone, N: Network = Ethereum> {
pub trait ProviderLayer<P: Provider<T, N>, T: Transport + Clone, N: Network = Core> {
/// The provider constructed by this layer.
type Provider: Provider<T, N>;

Expand Down Expand Up @@ -107,13 +108,13 @@ where
///
/// [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html
#[derive(Debug)]
pub struct ProviderBuilder<L, F, N = Ethereum> {
pub struct ProviderBuilder<L, F, N = Core> {
layer: L,
filler: F,
network: PhantomData<fn() -> N>,
}

impl ProviderBuilder<Identity, Identity, Ethereum> {
impl ProviderBuilder<Identity, Identity, Core> {
/// Create a new [`ProviderBuilder`].
pub const fn new() -> Self {
Self { layer: Identity, filler: Identity, network: PhantomData }
Expand All @@ -127,17 +128,17 @@ impl<N> Default for ProviderBuilder<Identity, Identity, N> {
}

impl<L, N> ProviderBuilder<L, Identity, N> {
/// Add preconfigured set of layers handling gas estimation, nonce
/// management, and chain-id fetching.
/// Add preconfigured set of layers handling energy estimation, nonce
/// management, and network-id fetching.
pub fn with_recommended_fillers(self) -> ProviderBuilder<L, RecommendFiller, N> {
self.filler(GasFiller).filler(NonceFiller::default()).filler(ChainIdFiller::default())
self.filler(EnergyFiller).filler(NonceFiller::default()).filler(NetworkIdFiller::default())
}

/// Add gas estimation to the stack being built.
/// Add energy estimation to the stack being built.
///
/// See [`GasFiller`]
pub fn with_gas_estimation(self) -> ProviderBuilder<L, JoinFill<Identity, GasFiller>, N> {
self.filler(GasFiller)
/// See [`EnergyFiller`]
pub fn with_energy_estimation(self) -> ProviderBuilder<L, JoinFill<Identity, EnergyFiller>, N> {
self.filler(EnergyFiller)
}

/// Add nonce management to the stack being built.
Expand All @@ -147,22 +148,22 @@ impl<L, N> ProviderBuilder<L, Identity, N> {
self.filler(NonceFiller::default())
}

/// Add a chain ID filler to the stack being built. The filler will attempt
/// to fetch the chain ID from the provider using
/// [`Provider::get_chain_id`]. the first time a transaction is prepared,
/// Add a network ID filler to the stack being built. The filler will attempt
/// to fetch the network ID from the provider using
/// [`Provider::fetch_network_id`]. the first time a transaction is prepared,
/// and will cache it for future transactions.
pub fn fetch_chain_id(self) -> ProviderBuilder<L, JoinFill<Identity, ChainIdFiller>, N> {
self.filler(ChainIdFiller::default())
pub fn fetch_network_id(self) -> ProviderBuilder<L, JoinFill<Identity, NetworkIdFiller>, N> {
self.filler(NetworkIdFiller::default())
}

/// Add a specific chain ID to the stack being built. The filler will
/// fill transactions with the provided chain ID, regardless of the chain ID
/// that the provider reports via [`Provider::get_chain_id`].
pub fn with_chain_id(
/// Add a specific network ID to the stack being built. The filler will
/// fill transactions with the provided network ID, regardless of the network ID
/// that the provider reports via [`Provider::get_network_id`].
pub fn with_network_id(
self,
chain_id: u64,
) -> ProviderBuilder<L, JoinFill<Identity, ChainIdFiller>, N> {
self.filler(ChainIdFiller::new(Some(chain_id)))
network_id: u64,
) -> ProviderBuilder<L, JoinFill<Identity, NetworkIdFiller>, N> {
self.filler(NetworkIdFiller::new(Some(network_id)))
}
}

Expand Down Expand Up @@ -206,7 +207,7 @@ impl<L, F, N> ProviderBuilder<L, F, N> {

/// Change the network.
///
/// By default, the network is `Ethereum`. This method must be called to configure a different
/// By default, the network is `Core`. This method must be called to configure a different
/// network.
///
/// ```ignore
Expand Down Expand Up @@ -336,12 +337,12 @@ impl<L, F, N> ProviderBuilder<L, F, N> {
// Enabled when the `anvil` feature is enabled, or when both in test and the
// `reqwest` feature is enabled.
#[cfg(any(test, feature = "anvil"))]
impl<L, F> ProviderBuilder<L, F, Ethereum> {
impl<L, F> ProviderBuilder<L, F, Core> {
/// Build this provider with anvil, using an Reqwest HTTP transport.
pub fn on_anvil(self) -> F::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Core>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Core>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
Expand All @@ -363,8 +364,8 @@ impl<L, F> ProviderBuilder<L, F, Ethereum> {
alloy_transport_http::Http<reqwest::Client>,
>>::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Core>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Core>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
Expand All @@ -383,8 +384,8 @@ impl<L, F> ProviderBuilder<L, F, Ethereum> {
f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
) -> F::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Core>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Core>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
Expand All @@ -411,8 +412,8 @@ impl<L, F> ProviderBuilder<L, F, Ethereum> {
alloy_transport_http::Http<reqwest::Client>,
>>::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Core>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Core>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
Expand Down
Loading

0 comments on commit 8ca6bcd

Please sign in to comment.