Skip to content

Commit 195055a

Browse files
Soubhik-10mattsse
andauthored
feat: added Transaction conversion from consensus for rpc (#529)
closes #528 --------- Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
1 parent 30282c4 commit 195055a

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

crates/rpc-types/src/transaction.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Optimism specific types related to transactions.
22
3-
use alloy_consensus::{Transaction as _, Typed2718};
3+
use alloy_consensus::{Transaction as _, Typed2718, transaction::Recovered};
44
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
55
use alloy_primitives::{Address, B256, BlockHash, Bytes, ChainId, TxKind, U256};
66
use alloy_serde::OtherFields;
7-
use op_alloy_consensus::OpTxEnvelope;
7+
use op_alloy_consensus::{OpTxEnvelope, transaction::OpTransactionInfo};
88
use serde::{Deserialize, Serialize};
99

1010
mod 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+
3264
impl Typed2718 for Transaction {
3365
fn ty(&self) -> u8 {
3466
self.inner.ty()

0 commit comments

Comments
 (0)