Skip to content
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

feat: make Pending transaction own the provider #1500

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl<T: Transport + Clone, P: Provider<T, N>, D: CallDecoder, N: Network> CallBu
///
/// Returns a builder for configuring the pending transaction watcher.
/// See [`Provider::send_transaction`] for more information.
pub async fn send(&self) -> Result<PendingTransactionBuilder<'_, T, N>> {
pub async fn send(&self) -> Result<PendingTransactionBuilder<T, N>> {
Ok(self.provider.send_transaction(self.request.clone()).await?)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ where
async fn send_transaction_internal(
&self,
mut tx: SendableTx<N>,
) -> TransportResult<PendingTransactionBuilder<'_, T, N>> {
) -> TransportResult<PendingTransactionBuilder<T, N>> {
tx = self.fill_inner(tx).await?;

if let Some(builder) = tx.as_builder() {
Expand Down
22 changes: 11 additions & 11 deletions crates/provider/src/heart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ pub enum PendingTransactionError {
#[must_use = "this type does nothing unless you call `register`, `watch` or `get_receipt`"]
#[derive(Debug)]
#[doc(alias = "PendingTxBuilder")]
pub struct PendingTransactionBuilder<'a, T, N: Network> {
pub struct PendingTransactionBuilder<T, N: Network> {
config: PendingTransactionConfig,
provider: &'a RootProvider<T, N>,
provider: RootProvider<T, N>,
}

impl<'a, T: Transport + Clone, N: Network> PendingTransactionBuilder<'a, T, N> {
impl<T: Transport + Clone, N: Network> PendingTransactionBuilder<T, N> {
/// Creates a new pending transaction builder.
pub const fn new(provider: &'a RootProvider<T, N>, tx_hash: TxHash) -> Self {
pub const fn new(provider: RootProvider<T, N>, tx_hash: TxHash) -> Self {
Self::from_config(provider, PendingTransactionConfig::new(tx_hash))
}

/// Creates a new pending transaction builder from the given configuration.
pub const fn from_config(
provider: &'a RootProvider<T, N>,
provider: RootProvider<T, N>,
config: PendingTransactionConfig,
) -> Self {
Self { config, provider }
Expand All @@ -107,17 +107,17 @@ impl<'a, T: Transport + Clone, N: Network> PendingTransactionBuilder<'a, T, N> {
}

/// Consumes this builder, returning the inner configuration.
pub const fn into_inner(self) -> PendingTransactionConfig {
pub fn into_inner(self) -> PendingTransactionConfig {
self.config
}

/// Returns the provider.
pub const fn provider(&self) -> &'a RootProvider<T, N> {
self.provider
pub const fn provider(&self) -> &RootProvider<T, N> {
&self.provider
}

/// Consumes this builder, returning the provider and the configuration.
pub const fn split(self) -> (&'a RootProvider<T, N>, PendingTransactionConfig) {
pub fn split(self) -> (RootProvider<T, N>, PendingTransactionConfig) {
(self.provider, self.config)
}

Expand Down Expand Up @@ -325,8 +325,8 @@ impl PendingTransactionConfig {
/// Wraps this configuration with a provider to expose watching methods.
pub const fn with_provider<T: Transport + Clone, N: Network>(
self,
provider: &RootProvider<T, N>,
) -> PendingTransactionBuilder<'_, T, N> {
provider: RootProvider<T, N>,
) -> PendingTransactionBuilder<T, N> {
PendingTransactionBuilder::from_config(provider, self)
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,10 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
async fn send_raw_transaction(
&self,
encoded_tx: &[u8],
) -> TransportResult<PendingTransactionBuilder<'_, T, N>> {
) -> TransportResult<PendingTransactionBuilder<T, N>> {
let rlp_hex = hex::encode_prefixed(encoded_tx);
let tx_hash = self.client().request("eth_sendRawTransaction", (rlp_hex,)).await?;
Ok(PendingTransactionBuilder::new(self.root(), tx_hash))
Ok(PendingTransactionBuilder::new(self.root().clone(), tx_hash))
}

/// Broadcasts a transaction to the network.
Expand All @@ -677,7 +677,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
async fn send_transaction(
&self,
tx: N::TransactionRequest,
) -> TransportResult<PendingTransactionBuilder<'_, T, N>> {
) -> TransportResult<PendingTransactionBuilder<T, N>> {
self.send_transaction_internal(SendableTx::Builder(tx)).await
}

Expand All @@ -688,7 +688,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
async fn send_tx_envelope(
&self,
tx: N::TxEnvelope,
) -> TransportResult<PendingTransactionBuilder<'_, T, N>> {
) -> TransportResult<PendingTransactionBuilder<T, N>> {
self.send_transaction_internal(SendableTx::Envelope(tx)).await
}

Expand All @@ -703,7 +703,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
async fn send_transaction_internal(
&self,
tx: SendableTx<N>,
) -> TransportResult<PendingTransactionBuilder<'_, T, N>> {
) -> TransportResult<PendingTransactionBuilder<T, N>> {
// Make sure to initialize heartbeat before we submit transaction, so that
// we don't miss it if user will subscriber to it immediately after sending.
let _handle = self.root().get_heart();
Expand All @@ -712,7 +712,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
SendableTx::Builder(mut tx) => {
alloy_network::TransactionBuilder::prep_for_submission(&mut tx);
let tx_hash = self.client().request("eth_sendTransaction", (tx,)).await?;
Ok(PendingTransactionBuilder::new(self.root(), tx_hash))
Ok(PendingTransactionBuilder::new(self.root().clone(), tx_hash))
}
SendableTx::Envelope(tx) => {
let mut encoded_tx = vec![];
Expand Down