Skip to content

Commit

Permalink
feat: add builder style functions to ethcallbundle (alloy-rs#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and lwedge99 committed Oct 8, 2024
1 parent 31dfc38 commit 6d4a054
Showing 1 changed file with 90 additions and 1 deletion.
91 changes: 90 additions & 1 deletion crates/rpc-types-mev/src/eth_calls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{u256_numeric_string, Privacy, Validity};

use alloy_eips::BlockNumberOrTag;
use alloy_eips::{eip2718::Encodable2718, BlockNumberOrTag};
use alloy_primitives::{keccak256, Address, Bytes, Keccak256, B256, U256};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -32,6 +32,95 @@ pub struct EthCallBundle {
pub base_fee: Option<u128>,
}

impl EthCallBundle {
/// Creates a new bundle from the given [`Encodable2718`] transactions.
pub fn from_2718<I, T>(txs: I) -> Self
where
I: IntoIterator<Item = T>,
T: Encodable2718,
{
Self::from_raw_txs(txs.into_iter().map(|tx| tx.encoded_2718()))
}

/// Creates a new bundle with the given transactions.
pub fn from_raw_txs<I, T>(txs: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<Bytes>,
{
Self { txs: txs.into_iter().map(Into::into).collect(), ..Default::default() }
}

/// Adds an [`Encodable2718`] transaction to the bundle.
pub fn append_2718_tx(self, tx: impl Encodable2718) -> Self {
self.append_raw_tx(tx.encoded_2718())
}

/// Adds an EIP-2718 envelope to the bundle.
pub fn append_raw_tx(mut self, tx: impl Into<Bytes>) -> Self {
self.txs.push(tx.into());
self
}

/// Adds multiple [`Encodable2718`] transactions to the bundle.
pub fn extend_2718_txs<I, T>(self, tx: I) -> Self
where
I: IntoIterator<Item = T>,
T: Encodable2718,
{
self.extend_raw_txs(tx.into_iter().map(|tx| tx.encoded_2718()))
}

/// Adds multiple calls to the block.
pub fn extend_raw_txs<I, T>(mut self, txs: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<Bytes>,
{
self.txs.extend(txs.into_iter().map(Into::into));
self
}

/// Sets the block number for the bundle.
pub const fn with_block_number(mut self, block_number: u64) -> Self {
self.block_number = block_number;
self
}

/// Sets the state block number for the bundle.
pub fn with_state_block_number(
mut self,
state_block_number: impl Into<BlockNumberOrTag>,
) -> Self {
self.state_block_number = state_block_number.into();
self
}

/// Sets the timestamp for the bundle.
pub const fn with_timestamp(mut self, timestamp: u64) -> Self {
self.timestamp = Some(timestamp);
self
}

/// Sets the gas limit for the bundle.
pub const fn with_gas_limit(mut self, gas_limit: u64) -> Self {
self.gas_limit = Some(gas_limit);
self
}

/// Sets the difficulty for the bundle.
pub const fn with_difficulty(mut self, difficulty: U256) -> Self {
self.difficulty = Some(difficulty);
self
}

/// Sets the base fee for the bundle.
pub const fn with_base_fee(mut self, base_fee: u128) -> Self {
self.base_fee = Some(base_fee);
self
}
}

/// Response for `eth_callBundle`
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
Expand Down

0 comments on commit 6d4a054

Please sign in to comment.