Skip to content

Commit

Permalink
feat: raw_tx_by_hash (#6827)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
loocapro and mattsse authored Mar 1, 2024
1 parent e7018ca commit 2009784
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/rpc/rpc-api/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ pub trait EthApi {
#[method(name = "getTransactionByHash")]
async fn transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Transaction>>;

/// Returns information about a raw transaction by block hash and transaction index position.
#[method(name = "getRawTransactionByBlockHashAndIndex")]
async fn raw_transaction_by_block_hash_and_index(
&self,
hash: B256,
index: Index,
) -> RpcResult<Option<Bytes>>;

/// Returns information about a transaction by block hash and transaction index position.
#[method(name = "getTransactionByBlockHashAndIndex")]
async fn transaction_by_block_hash_and_index(
Expand All @@ -110,6 +118,15 @@ pub trait EthApi {
index: Index,
) -> RpcResult<Option<Transaction>>;

/// Returns information about a raw transaction by block number and transaction index
/// position.
#[method(name = "getRawTransactionByBlockNumberAndIndex")]
async fn raw_transaction_by_block_number_and_index(
&self,
number: BlockNumberOrTag,
index: Index,
) -> RpcResult<Option<Bytes>>;

/// Returns information about a transaction by block number and transaction index position.
#[method(name = "getTransactionByBlockNumberAndIndex")]
async fn transaction_by_block_number_and_index(
Expand Down
20 changes: 20 additions & 0 deletions crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ where
Ok(EthTransactions::transaction_by_hash(self, hash).await?.map(Into::into))
}

/// Handler for: `eth_getRawTransactionByBlockHashAndIndex`
async fn raw_transaction_by_block_hash_and_index(
&self,
hash: B256,
index: Index,
) -> Result<Option<Bytes>> {
trace!(target: "rpc::eth", ?hash, ?index, "Serving eth_getRawTransactionByBlockHashAndIndex");
Ok(EthApi::raw_transaction_by_block_and_tx_index(self, hash, index).await?)
}

/// Handler for: `eth_getTransactionByBlockHashAndIndex`
async fn transaction_by_block_hash_and_index(
&self,
Expand All @@ -175,6 +185,16 @@ where
Ok(EthApi::transaction_by_block_and_tx_index(self, hash, index).await?)
}

/// Handler for: `eth_getRawTransactionByBlockNumberAndIndex`
async fn raw_transaction_by_block_number_and_index(
&self,
number: BlockNumberOrTag,
index: Index,
) -> Result<Option<Bytes>> {
trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getRawTransactionByBlockNumberAndIndex");
Ok(EthApi::raw_transaction_by_block_and_tx_index(self, number, index).await?)
}

/// Handler for: `eth_getTransactionByBlockNumberAndIndex`
async fn transaction_by_block_number_and_index(
&self,
Expand Down
14 changes: 14 additions & 0 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,20 @@ where

Ok(None)
}

pub(crate) async fn raw_transaction_by_block_and_tx_index(
&self,
block_id: impl Into<BlockId>,
index: Index,
) -> EthResult<Option<Bytes>> {
if let Some(block) = self.block_with_senders(block_id.into()).await? {
if let Some(tx) = block.transactions().nth(index.into()) {
return Ok(Some(tx.envelope_encoded()))
}
}

Ok(None)
}
}
/// Represents from where a transaction was fetched.
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down

0 comments on commit 2009784

Please sign in to comment.