Skip to content

Commit

Permalink
feat: add block and transaction generics to otterscan and txpool types (
Browse files Browse the repository at this point in the history
#1183)

* feat: add transaction generic to ots types

* txpool generics
  • Loading branch information
klkvr authored Aug 26, 2024
1 parent 2033626 commit e2cc14f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
16 changes: 11 additions & 5 deletions crates/provider/src/ext/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ use alloy_transport::{Transport, TransportResult};
#[allow(unused, unreachable_pub)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait TxPoolApi<T, N = Ethereum>: Send + Sync {
pub trait TxPoolApi<T, N: Network = Ethereum>: Send + Sync {
/// Returns the content of the transaction pool.
///
/// Lists the exact details of all the transactions currently pending for inclusion in the next
/// block(s), as well as the ones that are being scheduled for future execution only.
///
/// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_content) for more details
async fn txpool_content(&self) -> TransportResult<TxpoolContent>;
async fn txpool_content(&self) -> TransportResult<TxpoolContent<N::TransactionResponse>>;

/// Returns the content of the transaction pool filtered by a specific address.
///
/// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_contentFrom) for more details
async fn txpool_content_from(&self, from: Address) -> TransportResult<TxpoolContentFrom>;
async fn txpool_content_from(
&self,
from: Address,
) -> TransportResult<TxpoolContentFrom<N::TransactionResponse>>;

/// Returns a textual summary of each transaction in the pool.
///
Expand Down Expand Up @@ -50,11 +53,14 @@ where
T: Transport + Clone,
N: Network,
{
async fn txpool_content(&self) -> TransportResult<TxpoolContent> {
async fn txpool_content(&self) -> TransportResult<TxpoolContent<N::TransactionResponse>> {
self.client().request("txpool_content", ()).await
}

async fn txpool_content_from(&self, from: Address) -> TransportResult<TxpoolContentFrom> {
async fn txpool_content_from(
&self,
from: Address,
) -> TransportResult<TxpoolContentFrom<N::TransactionResponse>> {
self.client().request("txpool_contentFrom", (from,)).await
}

Expand Down
30 changes: 17 additions & 13 deletions crates/rpc-types-trace/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ pub struct InternalIssuance {
/// Custom `Block` struct that includes transaction count for Otterscan responses
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OtsBlock {
pub struct OtsBlock<T = Transaction> {
/// The block information.
#[serde(flatten)]
pub block: Block,
pub block: Block<T>,
/// The number of transactions in the block.
#[doc(alias = "tx_count")]
pub transaction_count: usize,
}

impl From<Block> for OtsBlock {
fn from(block: Block) -> Self {
impl<T> From<Block<T>> for OtsBlock<T> {
fn from(block: Block<T>) -> Self {
Self { transaction_count: block.transactions.len(), block }
}
}
Expand All @@ -138,8 +138,8 @@ pub struct OtsSlimBlock {
pub transaction_count: usize,
}

impl From<Block> for OtsSlimBlock {
fn from(block: Block) -> Self {
impl<T> From<Block<T>> for OtsSlimBlock {
fn from(block: Block<T>) -> Self {
Self {
header: block.header,
uncles: block.uncles,
Expand All @@ -162,8 +162,8 @@ pub struct BlockDetails {
pub total_fees: U256,
}

impl From<Rich<Block>> for BlockDetails {
fn from(rich_block: Rich<Block>) -> Self {
impl<T> From<Rich<Block<T>>> for BlockDetails {
fn from(rich_block: Rich<Block<T>>) -> Self {
Self {
block: rich_block.inner.into(),
issuance: Default::default(),
Expand All @@ -174,7 +174,11 @@ impl From<Rich<Block>> for BlockDetails {

impl BlockDetails {
/// Create a new `BlockDetails` struct.
pub fn new(rich_block: Rich<Block>, issuance: InternalIssuance, total_fees: U256) -> Self {
pub fn new<T>(
rich_block: Rich<Block<T>>,
issuance: InternalIssuance,
total_fees: U256,
) -> Self {
Self { block: rich_block.inner.into(), issuance, total_fees }
}
}
Expand Down Expand Up @@ -220,9 +224,9 @@ pub struct OtsReceipt {

/// Custom struct for otterscan `getBlockTransactions` RPC response
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct OtsBlockTransactions {
pub struct OtsBlockTransactions<T = Transaction> {
/// The full block information with transaction count.
pub fullblock: OtsBlock,
pub fullblock: OtsBlock<T>,
/// The list of transaction receipts.
pub receipts: Vec<OtsTransactionReceipt>,
}
Expand All @@ -232,10 +236,10 @@ pub struct OtsBlockTransactions {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[doc(alias = "TxWithReceipts")]
pub struct TransactionsWithReceipts {
pub struct TransactionsWithReceipts<T = Transaction> {
/// The list of transactions.
#[doc(alias = "transactions")]
pub txs: Vec<Transaction>,
pub txs: Vec<T>,
/// The list of transaction receipts.
pub receipts: Vec<OtsTransactionReceipt>,
/// Indicates if this is the first page of results.
Expand Down
16 changes: 8 additions & 8 deletions crates/rpc-types-txpool/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ impl Serialize for TxpoolInspectSummary {
///
/// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_content) for more details
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxpoolContent {
pub struct TxpoolContent<T = Transaction> {
/// pending tx
pub pending: BTreeMap<Address, BTreeMap<String, Transaction>>,
pub pending: BTreeMap<Address, BTreeMap<String, T>>,
/// queued tx
pub queued: BTreeMap<Address, BTreeMap<String, Transaction>>,
pub queued: BTreeMap<Address, BTreeMap<String, T>>,
}

impl TxpoolContent {
impl<T> TxpoolContent<T> {
/// Removes the transactions from the given sender
pub fn remove_from(&mut self, sender: &Address) -> TxpoolContentFrom {
pub fn remove_from(&mut self, sender: &Address) -> TxpoolContentFrom<T> {
TxpoolContentFrom {
pending: self.pending.remove(sender).unwrap_or_default(),
queued: self.queued.remove(sender).unwrap_or_default(),
Expand All @@ -134,11 +134,11 @@ impl TxpoolContent {
///
/// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_contentFrom) for more details
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxpoolContentFrom {
pub struct TxpoolContentFrom<T = Transaction> {
/// pending tx
pub pending: BTreeMap<String, Transaction>,
pub pending: BTreeMap<String, T>,
/// queued tx
pub queued: BTreeMap<String, Transaction>,
pub queued: BTreeMap<String, T>,
}

/// Transaction Pool Inspect
Expand Down

0 comments on commit e2cc14f

Please sign in to comment.