use alloy_consensus::{error::ValueError, transaction::Recovered, EthereumTxEnvelope, TxEip4844};
use alloy_primitives::Address;
use alloy_rpc_types_eth::{Transaction, TransactionInfo, TransactionRequest};
use core::{convert::Infallible, error};
pub trait FromConsensusTx<T>: Sized {
type TxInfo;
type Err: error::Error;
fn from_consensus_tx(tx: T, signer: Address, tx_info: Self::TxInfo) -> Result<Self, Self::Err>;
}
impl<TxIn: alloy_consensus::Transaction, T: alloy_consensus::Transaction + From<TxIn>>
FromConsensusTx<TxIn> for Transaction<T>
{
type TxInfo = TransactionInfo;
type Err = Infallible;
fn from_consensus_tx(
tx: TxIn,
signer: Address,
tx_info: Self::TxInfo,
) -> Result<Self, Self::Err> {
Ok(Self::from_transaction(Recovered::new_unchecked(tx.into(), signer), tx_info))
}
}
pub trait TryIntoSimTx<T>
where
Self: Sized,
{
fn try_into_sim_tx(self) -> Result<T, ValueError<Self>>;
}
impl TryIntoSimTx<EthereumTxEnvelope<TxEip4844>> for TransactionRequest {
fn try_into_sim_tx(self) -> Result<EthereumTxEnvelope<TxEip4844>, ValueError<Self>> {
Self::build_typed_simulate_transaction(self)
}
}
pub trait TxInfoMapper<T> {
type Out;
type Err;
fn try_map(&self, tx: &T, tx_info: TransactionInfo) -> Result<Self::Out, Self::Err>;
}
impl<T> TxInfoMapper<T> for () {
type Out = TransactionInfo;
type Err = Infallible;
fn try_map(&self, _tx: &T, tx_info: TransactionInfo) -> Result<Self::Out, Self::Err> {
Ok(tx_info)
}
}