11//! Transaction related types
2- use alloy_consensus:: {
3- Signed , Transaction , Typed2718 , crypto:: RecoveryError , transaction:: Recovered ,
4- } ;
2+ use alloy_consensus:: { Transaction , Typed2718 , crypto:: RecoveryError } ;
53
64use alloy_eips:: eip2718:: Encodable2718 ;
75use alloy_primitives:: { Address , B256 , Bytes , TxHash } ;
86use alloy_rlp:: { Decodable , Encodable } ;
9- use alloy_rpc_types:: Transaction as RpcTransaction ;
107use bytes:: BufMut ;
118use foundry_evm:: traces:: CallTraceNode ;
129use foundry_primitives:: FoundryTxEnvelope ;
1310use revm:: interpreter:: InstructionResult ;
1411use serde:: { Deserialize , Serialize } ;
1512use std:: ops:: Deref ;
1613
17- /// A wrapper for [FoundryTxEnvelope] that allows impersonating accounts.
14+ /// Anvil's concrete impersonated transaction type.
15+ pub type MaybeImpersonatedTransaction = ImpersonatedTransaction < FoundryTxEnvelope > ;
16+
17+ /// A wrapper for a transaction envelope that allows impersonating accounts.
1818///
1919/// This is a helper that carries the `impersonated` sender so that the right hash
20- /// [FoundryTxEnvelope::impersonated_hash] can be created.
20+ /// can be created.
2121#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq , Eq ) ]
22- pub struct MaybeImpersonatedTransaction {
23- transaction : FoundryTxEnvelope ,
22+ pub struct ImpersonatedTransaction < T > {
23+ transaction : T ,
2424 impersonated_sender : Option < Address > ,
2525}
2626
27- impl Typed2718 for MaybeImpersonatedTransaction {
27+ impl < T : Typed2718 > Typed2718 for ImpersonatedTransaction < T > {
2828 fn ty ( & self ) -> u8 {
2929 self . transaction . ty ( )
3030 }
3131}
3232
33- impl MaybeImpersonatedTransaction {
33+ impl < T > ImpersonatedTransaction < T > {
3434 /// Creates a new wrapper for the given transaction
35- pub fn new ( transaction : FoundryTxEnvelope ) -> Self {
35+ pub fn new ( transaction : T ) -> Self {
3636 Self { transaction, impersonated_sender : None }
3737 }
3838
3939 /// Creates a new impersonated transaction wrapper using the given sender
40- pub fn impersonated ( transaction : FoundryTxEnvelope , impersonated_sender : Address ) -> Self {
40+ pub fn impersonated ( transaction : T , impersonated_sender : Address ) -> Self {
4141 Self { transaction, impersonated_sender : Some ( impersonated_sender) }
4242 }
4343
44+ /// Returns whether the transaction is impersonated
45+ pub fn is_impersonated ( & self ) -> bool {
46+ self . impersonated_sender . is_some ( )
47+ }
48+
49+ /// Returns the inner transaction.
50+ pub fn into_inner ( self ) -> T {
51+ self . transaction
52+ }
53+ }
54+
55+ impl ImpersonatedTransaction < FoundryTxEnvelope > {
4456 /// Recovers the Ethereum address which was used to sign the transaction.
4557 pub fn recover ( & self ) -> Result < Address , RecoveryError > {
4658 if let Some ( sender) = self . impersonated_sender {
@@ -49,41 +61,16 @@ impl MaybeImpersonatedTransaction {
4961 self . transaction . recover ( )
5062 }
5163
52- /// Returns whether the transaction is impersonated
53- pub fn is_impersonated ( & self ) -> bool {
54- self . impersonated_sender . is_some ( )
55- }
56-
5764 /// Returns the hash of the transaction
5865 pub fn hash ( & self ) -> B256 {
5966 if let Some ( sender) = self . impersonated_sender {
6067 return self . transaction . impersonated_hash ( sender) ;
6168 }
6269 self . transaction . hash ( )
6370 }
64-
65- /// Converts the transaction into an [`RpcTransaction`]
66- pub fn into_rpc_transaction ( self ) -> RpcTransaction {
67- let hash = self . hash ( ) ;
68- let from = self . recover ( ) . unwrap_or_default ( ) ;
69- let envelope = self . transaction . try_into_eth ( ) . expect ( "cant build deposit transactions" ) ;
70-
71- // NOTE: we must update the hash because the tx can be impersonated, this requires forcing
72- // the hash
73- let ( typed_tx, signature, _) = Into :: < Signed < _ > > :: into ( envelope) . into_parts ( ) ;
74- let inner_envelope = Signed :: new_unchecked ( typed_tx, signature, hash) . into ( ) ;
75-
76- RpcTransaction {
77- block_hash : None ,
78- block_number : None ,
79- transaction_index : None ,
80- effective_gas_price : None ,
81- inner : Recovered :: new_unchecked ( inner_envelope, from) ,
82- }
83- }
8471}
8572
86- impl Encodable2718 for MaybeImpersonatedTransaction {
73+ impl < T : Encodable2718 > Encodable2718 for ImpersonatedTransaction < T > {
8774 fn encode_2718_len ( & self ) -> usize {
8875 self . transaction . encode_2718_len ( )
8976 }
@@ -93,7 +80,7 @@ impl Encodable2718 for MaybeImpersonatedTransaction {
9380 }
9481}
9582
96- impl Encodable for MaybeImpersonatedTransaction {
83+ impl < T : Encodable > Encodable for ImpersonatedTransaction < T > {
9784 fn encode ( & self , out : & mut dyn bytes:: BufMut ) {
9885 self . transaction . encode ( out)
9986 }
@@ -111,32 +98,26 @@ impl From<FoundryTxEnvelope> for MaybeImpersonatedTransaction {
11198 }
11299}
113100
114- impl Decodable for MaybeImpersonatedTransaction {
101+ impl < T : Decodable > Decodable for ImpersonatedTransaction < T > {
115102 fn decode ( buf : & mut & [ u8 ] ) -> alloy_rlp:: Result < Self > {
116- FoundryTxEnvelope :: decode ( buf) . map ( Self :: new)
103+ T :: decode ( buf) . map ( Self :: new)
117104 }
118105}
119106
120- impl AsRef < FoundryTxEnvelope > for MaybeImpersonatedTransaction {
121- fn as_ref ( & self ) -> & FoundryTxEnvelope {
107+ impl < T > AsRef < T > for ImpersonatedTransaction < T > {
108+ fn as_ref ( & self ) -> & T {
122109 & self . transaction
123110 }
124111}
125112
126- impl Deref for MaybeImpersonatedTransaction {
127- type Target = FoundryTxEnvelope ;
113+ impl < T > Deref for ImpersonatedTransaction < T > {
114+ type Target = T ;
128115
129116 fn deref ( & self ) -> & Self :: Target {
130117 & self . transaction
131118 }
132119}
133120
134- impl From < MaybeImpersonatedTransaction > for RpcTransaction {
135- fn from ( value : MaybeImpersonatedTransaction ) -> Self {
136- value. into_rpc_transaction ( )
137- }
138- }
139-
140121/// Queued transaction
141122#[ derive( Clone , Debug , PartialEq , Eq ) ]
142123pub struct PendingTransaction {
0 commit comments