feat: add alloy-compat for op prims (#14406)

This commit is contained in:
Matthias Seitz
2025-02-11 20:15:34 +01:00
committed by GitHub
parent 3f680fd6cc
commit 974b197d30
4 changed files with 68 additions and 0 deletions

3
Cargo.lock generated
View File

@ -8581,8 +8581,11 @@ version = "1.1.5"
dependencies = [
"alloy-consensus",
"alloy-eips",
"alloy-network",
"alloy-primitives",
"alloy-rlp",
"alloy-rpc-types-eth",
"alloy-serde",
"arbitrary",
"bytes",
"derive_more",

View File

@ -27,6 +27,9 @@ secp256k1 = { workspace = true, optional = true }
# op
op-alloy-consensus.workspace = true
alloy-rpc-types-eth = { workspace = true, optional = true }
alloy-network = { workspace = true, optional = true }
alloy-serde = { workspace = true, optional = true }
# codec
bytes = { workspace = true, optional = true }
@ -66,7 +69,10 @@ std = [
"alloy-rlp/std",
"reth-zstd-compressors?/std",
"op-alloy-consensus/std",
"alloy-rpc-types-eth?/std",
"alloy-serde?/std",
]
alloy-compat = ["dep:alloy-network", "dep:alloy-serde", "dep:alloy-rpc-types-eth"]
reth-codec = [
"dep:reth-codecs",
"std",
@ -89,6 +95,7 @@ serde = [
"rand?/serde",
"revm-primitives?/serde",
"secp256k1?/serde",
"alloy-rpc-types-eth?/serde",
]
serde-bincode-compat = [
"alloy-consensus/serde-bincode-compat",
@ -108,6 +115,8 @@ arbitrary = [
"alloy-primitives/arbitrary",
"revm-primitives?/arbitrary",
"rand",
"alloy-rpc-types-eth?/arbitrary",
"alloy-serde?/arbitrary",
]
optimism = [
"dep:revm-primitives",

View File

@ -0,0 +1,53 @@
//! Common conversions from alloy types.
use crate::OpTransactionSigned;
use alloc::string::ToString;
use alloy_consensus::TxEnvelope;
use alloy_network::{AnyRpcTransaction, AnyTxEnvelope};
use alloy_rpc_types_eth::Transaction as AlloyRpcTransaction;
use alloy_serde::WithOtherFields;
use op_alloy_consensus::OpTypedTransaction;
impl TryFrom<AnyRpcTransaction> for OpTransactionSigned {
type Error = alloy_rpc_types_eth::ConversionError;
fn try_from(tx: AnyRpcTransaction) -> Result<Self, Self::Error> {
use alloy_rpc_types_eth::ConversionError;
let WithOtherFields { inner: tx, other: _ } = tx;
let (transaction, signature, hash) = match tx.inner {
AnyTxEnvelope::Ethereum(TxEnvelope::Legacy(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(OpTypedTransaction::Legacy(tx), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip2930(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(OpTypedTransaction::Eip2930(tx), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip1559(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(OpTypedTransaction::Eip1559(tx), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip7702(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(OpTypedTransaction::Eip7702(tx), signature, hash)
}
_ => {
// TODO: support tx deposit: <https://github.com/alloy-rs/op-alloy/pull/427>
return Err(ConversionError::Custom("unknown transaction type".to_string()))
}
};
Ok(Self::new(transaction, signature, hash))
}
}
impl<T> From<AlloyRpcTransaction<T>> for OpTransactionSigned
where
Self: From<T>,
{
fn from(value: AlloyRpcTransaction<T>) -> Self {
value.inner.into()
}
}

View File

@ -11,6 +11,9 @@
extern crate alloc;
#[cfg(feature = "alloy-compat")]
mod alloy_compat;
pub mod bedrock;
pub mod predeploys;