Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reth_rpc_types::Transaction to reth_primitives::Transaction conversion util function #7214

Closed
Eikix opened this issue Mar 19, 2024 · 2 comments · Fixed by #7551
Closed
Labels
C-enhancement New feature or request D-good-first-issue Nice and easy! A great choice to get started

Comments

@Eikix
Copy link

Eikix commented Mar 19, 2024

Describe the feature

Hi all!

Context

I've noticed that one can go from reth_primitives::TransactionSignedEcRecovered to reth_rpc_types:Transaction with rpc-types-compat::from_recovered util.
I'd like to have a util to go the opposite route, to the best of my knowledge it does not exist.

Proposed added code

In rpc-types-compat crate, add a util: rpc_transaction_to_primitive

pub fn rpc_transaction_to_primitive(
    rpc_transaction: reth_rpc_types::Transaction,
) -> Result<reth_primitives::Transaction, ConversionError> {
    match rpc_transaction.transaction_type {
        None => Err(ConversionError::TransactionConversionError("Transaction type not found".to_string())),
        Some(transaction_type) => match transaction_type.to::<u64>() {
            0 => Ok(reth_primitives::Transaction::Legacy(TxLegacy {
                nonce: rpc_transaction.nonce.to::<u64>(),
                gas_price: rpc_transaction
                    .gas_price
                    .ok_or(ConversionError::TransactionConversionError("Gas price not found".to_string()))?
                    .to::<u128>(),
                gas_limit: rpc_transaction
                    .gas
                    .try_into()
                    .map_err(|_| ConversionError::ValueOutOfRange("Block number too large".to_string()))?,
                to: rpc_transaction.to.map_or_else(|| TransactionKind::Create, TransactionKind::Call),
                value: rpc_transaction.value.into(),
                input: rpc_transaction.input,
                chain_id: rpc_transaction.chain_id.map(|id| id.to::<u64>()),
            })),
            1 => Ok(reth_primitives::Transaction::Eip2930(TxEip2930 {
                chain_id: rpc_transaction
                    .chain_id
                    .ok_or(ConversionError::TransactionConversionError("Chain id not found".to_string()))?
                    .to::<u64>(),
                nonce: rpc_transaction.nonce.to::<u64>(),
                gas_price: rpc_transaction
                    .gas_price
                    .ok_or(ConversionError::TransactionConversionError("Gas price not found".to_string()))?
                    .to::<u128>(),
                gas_limit: rpc_transaction
                    .gas
                    .try_into()
                    .map_err(|_| ConversionError::ValueOutOfRange("Block number too large".to_string()))?,
                to: rpc_transaction.to.map_or_else(|| TransactionKind::Create, TransactionKind::Call),
                value: rpc_transaction.value.into(),
                access_list: AccessList(
                    rpc_transaction
                        .access_list
                        .unwrap_or_default()
                        .into_iter()
                        .map(|access_list| AccessListItem {
                            address: access_list.address,
                            storage_keys: access_list.storage_keys,
                        })
                        .collect(),
                ),
                input: rpc_transaction.input,
            })),
            2 => Ok(reth_primitives::Transaction::Eip1559(TxEip1559 {
                chain_id: rpc_transaction
                    .chain_id
                    .ok_or(ConversionError::TransactionConversionError("Chain id not found".to_string()))?
                    .to::<u64>(),
                nonce: rpc_transaction.nonce.to::<u64>(),
                gas_limit: rpc_transaction
                    .gas
                    .try_into()
                    .map_err(|_| ConversionError::ValueOutOfRange("Block number too large".to_string()))?,
                max_fee_per_gas: rpc_transaction
                    .max_fee_per_gas
                    .ok_or(ConversionError::TransactionConversionError("Max fee per gas not found".to_string()))?
                    .to::<u128>(),
                max_priority_fee_per_gas: rpc_transaction
                    .max_priority_fee_per_gas
                    .ok_or(ConversionError::TransactionConversionError(
                        "Max priority fee per gas not found".to_string(),
                    ))?
                    .to::<u128>(),
                to: rpc_transaction.to.map_or_else(|| TransactionKind::Create, TransactionKind::Call),
                value: rpc_transaction.value.into(),
                access_list: AccessList(
                    rpc_transaction
                        .access_list
                        .unwrap_or_default()
                        .into_iter()
                        .map(|access_list| AccessListItem {
                            address: access_list.address,
                            storage_keys: access_list.storage_keys,
                        })
                        .collect(),
                ),
                input: rpc_transaction.input,
            })),
            _ => Err(ConversionError::TransactionConversionError("Invalid transaction type".to_string())),
        },
    }
}

The above code is done for kakarot zkevm's rpc layer that heavily relies on reth types (thank you btw 🙏)

Can adapt API / Rework as this is a first /fast try.

Thanks!

Additional context

No response

@Eikix Eikix added C-enhancement New feature or request S-needs-triage This issue needs to be labelled labels Mar 19, 2024
@DaniPopes DaniPopes removed the S-needs-triage This issue needs to be labelled label Apr 9, 2024
@DaniPopes
Copy link
Member

cc @mattsse

@mattsse
Copy link
Collaborator

mattsse commented Apr 9, 2024

supportive, this should be a tryFrom impl in reth-primitives

@mattsse mattsse added the D-good-first-issue Nice and easy! A great choice to get started label Apr 9, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Reth Tracker Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement New feature or request D-good-first-issue Nice and easy! A great choice to get started
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants