11//! Optimism specific types related to transactions.
22
3- use alloy_consensus:: { Transaction as _, Typed2718 } ;
3+ use alloy_consensus:: { Transaction as _, Typed2718 , transaction :: Recovered } ;
44use alloy_eips:: { eip2930:: AccessList , eip7702:: SignedAuthorization } ;
55use alloy_primitives:: { Address , B256 , BlockHash , Bytes , ChainId , TxKind , U256 } ;
66use alloy_serde:: OtherFields ;
7- use op_alloy_consensus:: OpTxEnvelope ;
7+ use op_alloy_consensus:: { OpTxEnvelope , transaction :: OpTransactionInfo } ;
88use serde:: { Deserialize , Serialize } ;
99
1010mod request;
@@ -29,6 +29,38 @@ pub struct Transaction {
2929 pub deposit_receipt_version : Option < u64 > ,
3030}
3131
32+ impl Transaction {
33+ /// Returns a rpc [`Transaction`] with a [`OpTransactionInfo`] and
34+ /// [`Recovered<OpTxEnvelope>`] as input.
35+ pub fn from_transaction ( tx : Recovered < OpTxEnvelope > , tx_info : OpTransactionInfo ) -> Self {
36+ let base_fee = tx_info. inner . base_fee ;
37+ let effective_gas_price = if tx. is_deposit ( ) {
38+ // For deposits, we must always set the `gasPrice` field to 0 in rpc
39+ // deposit tx don't have a gas price field, but serde of `Transaction` will take care of
40+ // it
41+ 0
42+ } else {
43+ base_fee
44+ . map ( |base_fee| {
45+ tx. effective_tip_per_gas ( base_fee) . unwrap_or_default ( ) + base_fee as u128
46+ } )
47+ . unwrap_or_else ( || tx. max_fee_per_gas ( ) )
48+ } ;
49+
50+ Self {
51+ inner : alloy_rpc_types_eth:: Transaction {
52+ inner : tx,
53+ block_hash : tx_info. inner . block_hash ,
54+ block_number : tx_info. inner . block_number ,
55+ transaction_index : tx_info. inner . index ,
56+ effective_gas_price : Some ( effective_gas_price) ,
57+ } ,
58+ deposit_nonce : tx_info. deposit_meta . deposit_nonce ,
59+ deposit_receipt_version : tx_info. deposit_meta . deposit_receipt_version ,
60+ }
61+ }
62+ }
63+
3264impl Typed2718 for Transaction {
3365 fn ty ( & self ) -> u8 {
3466 self . inner . ty ( )
0 commit comments