Skip to content

Commit

Permalink
Add trait method Transaction::is_dynamic_fee (#1638)
Browse files Browse the repository at this point in the history
* Add trait method Transaction::is_dynamic_fee

* Fix docs
  • Loading branch information
emhane authored Nov 12, 2024
1 parent e747b1e commit 185e58c
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ impl Transaction for TxEip1559 {
self.max_priority_fee_per_gas
}

fn is_dynamic_fee(&self) -> bool {
true
}

fn kind(&self) -> TxKind {
self.to
}
Expand Down
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ impl Transaction for TxEip2930 {
self.gas_price
}

fn is_dynamic_fee(&self) -> bool {
false
}

fn kind(&self) -> TxKind {
self.to
}
Expand Down
15 changes: 15 additions & 0 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ impl Transaction for TxEip4844Variant {
}
}

fn is_dynamic_fee(&self) -> bool {
match self {
Self::TxEip4844(tx) => tx.is_dynamic_fee(),
Self::TxEip4844WithSidecar(tx) => tx.is_dynamic_fee(),
}
}

fn kind(&self) -> TxKind {
match self {
Self::TxEip4844(tx) => tx.to,
Expand Down Expand Up @@ -600,6 +607,10 @@ impl Transaction for TxEip4844 {
self.max_priority_fee_per_gas
}

fn is_dynamic_fee(&self) -> bool {
true
}

fn kind(&self) -> TxKind {
self.to.into()
}
Expand Down Expand Up @@ -784,6 +795,10 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.priority_fee_or_price()
}

fn is_dynamic_fee(&self) -> bool {
self.tx.is_dynamic_fee()
}

fn kind(&self) -> TxKind {
self.tx.kind()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ impl Transaction for TxEip7702 {
self.max_priority_fee_per_gas
}

fn is_dynamic_fee(&self) -> bool {
true
}

fn kind(&self) -> TxKind {
self.to.into()
}
Expand Down
10 changes: 10 additions & 0 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ impl Transaction for TxEnvelope {
}
}

fn is_dynamic_fee(&self) -> bool {
match self {
Self::Legacy(tx) => tx.tx().is_dynamic_fee(),
Self::Eip2930(tx) => tx.tx().is_dynamic_fee(),
Self::Eip1559(tx) => tx.tx().is_dynamic_fee(),
Self::Eip4844(tx) => tx.tx().is_dynamic_fee(),
Self::Eip7702(tx) => tx.tx().is_dynamic_fee(),
}
}

fn kind(&self) -> TxKind {
match self {
Self::Legacy(tx) => tx.tx().kind(),
Expand Down
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ impl Transaction for TxLegacy {
self.gas_price
}

fn is_dynamic_fee(&self) -> bool {
false
}

fn kind(&self) -> TxKind {
self.to
}
Expand Down
7 changes: 7 additions & 0 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub trait Transaction: fmt::Debug + any::Any + Send + Sync + 'static {
.map_or(Some(fee), |priority_fee| Some(fee.min(priority_fee)))
}

/// Returns `true` if the transaction supports dynamic fees.
fn is_dynamic_fee(&self) -> bool;

/// Returns the transaction kind.
fn kind(&self) -> TxKind;

Expand Down Expand Up @@ -280,4 +283,8 @@ impl<T: Transaction> Transaction for alloy_serde::WithOtherFields<T> {
fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
self.inner.authorization_list()
}

fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}
}
10 changes: 10 additions & 0 deletions crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ impl Transaction for TypedTransaction {
}
}

fn is_dynamic_fee(&self) -> bool {
match self {
Self::Legacy(tx) => tx.is_dynamic_fee(),
Self::Eip2930(tx) => tx.is_dynamic_fee(),
Self::Eip1559(tx) => tx.is_dynamic_fee(),
Self::Eip4844(tx) => tx.is_dynamic_fee(),
Self::Eip7702(tx) => tx.is_dynamic_fee(),
}
}

fn kind(&self) -> TxKind {
match self {
Self::Legacy(tx) => tx.kind(),
Expand Down
14 changes: 14 additions & 0 deletions crates/network/src/any/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ impl TransactionTrait for AnyTypedTransaction {
self.max_priority_fee_per_gas().or_else(|| self.gas_price()).unwrap_or_default()
}

fn is_dynamic_fee(&self) -> bool {
match self {
Self::Ethereum(inner) => inner.is_dynamic_fee(),
Self::Unknown(inner) => inner.is_dynamic_fee(),
}
}

fn kind(&self) -> alloy_primitives::TxKind {
match self {
Self::Ethereum(inner) => inner.kind(),
Expand Down Expand Up @@ -273,6 +280,13 @@ impl TransactionTrait for AnyTxEnvelope {
self.max_priority_fee_per_gas().or_else(|| self.gas_price()).unwrap_or_default()
}

fn is_dynamic_fee(&self) -> bool {
match self {
Self::Ethereum(inner) => inner.is_dynamic_fee(),
Self::Unknown(inner) => inner.is_dynamic_fee(),
}
}

fn kind(&self) -> alloy_primitives::TxKind {
match self {
Self::Ethereum(inner) => inner.kind(),
Expand Down
9 changes: 9 additions & 0 deletions crates/network/src/any/unknowns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ impl alloy_consensus::Transaction for UnknownTypedTransaction {
self.gas_price().or(self.max_priority_fee_per_gas()).unwrap_or_default()
}

fn is_dynamic_fee(&self) -> bool {
self.fields.get_deserialized::<U128>("maxFeePerGas").is_some()
|| self.fields.get_deserialized::<U128>("maxFeePerBlobGas").is_some()
}

fn kind(&self) -> TxKind {
self.fields
.get("to")
Expand Down Expand Up @@ -262,6 +267,10 @@ impl alloy_consensus::Transaction for UnknownTxEnvelope {
self.inner.priority_fee_or_price()
}

fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}

fn kind(&self) -> TxKind {
self.inner.kind()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ impl<T: TransactionTrait> TransactionTrait for Transaction<T> {
self.inner.priority_fee_or_price()
}

fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}

fn kind(&self) -> TxKind {
self.inner.kind()
}
Expand Down

0 comments on commit 185e58c

Please sign in to comment.