revm_statetest_types/
transaction.rsuse revm::{
context_interface::transaction::TransactionType,
primitives::{Address, Bytes, B256, U256},
};
use serde::{Deserialize, Serialize};
use crate::{deserializer::deserialize_maybe_empty, TestAuthorization};
#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionParts {
pub data: Vec<Bytes>,
pub gas_limit: Vec<U256>,
pub gas_price: Option<U256>,
pub nonce: U256,
pub secret_key: B256,
#[serde(default)]
pub sender: Option<Address>,
#[serde(default, deserialize_with = "deserialize_maybe_empty")]
pub to: Option<Address>,
pub value: Vec<U256>,
pub max_fee_per_gas: Option<U256>,
pub max_priority_fee_per_gas: Option<U256>,
#[serde(default)]
pub access_lists: Vec<Option<Vec<AccessListItem>>>,
pub authorization_list: Option<Vec<TestAuthorization>>,
#[serde(default)]
pub blob_versioned_hashes: Vec<B256>,
pub max_fee_per_blob_gas: Option<U256>,
}
impl TransactionParts {
pub fn tx_type(&self, access_list_index: usize) -> Option<TransactionType> {
let mut tx_type = TransactionType::Legacy;
if let Some(access_list) = self.access_lists.get(access_list_index) {
if access_list.is_some() {
tx_type = TransactionType::Eip2930;
}
}
if self.max_fee_per_gas.is_some() {
tx_type = TransactionType::Eip1559;
}
if self.max_fee_per_blob_gas.is_some() {
self.to?;
tx_type = TransactionType::Eip4844;
}
if self.authorization_list.is_some() {
self.to?;
tx_type = TransactionType::Eip7702;
}
Some(tx_type)
}
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TxPartIndices {
pub data: usize,
pub gas: usize,
pub value: usize,
}
#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AccessListItem {
pub address: Address,
pub storage_keys: Vec<B256>,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn decode_tx_parts() {
let tx = r#"{
"nonce": "0x00",
"maxPriorityFeePerGas": "0x00",
"maxFeePerGas": "0x07",
"gasLimit": [
"0x0423ff"
],
"to": "0x0000000000000000000000000000000000001000",
"value": [
"0x00"
],
"data": [
"0x"
],
"accessLists": [
[
{
"address": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
}
]
],
"authorizationList": [
{
"chainId": "0x00",
"address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"nonce": "0x00",
"v": "0x01",
"r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
"s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
"signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
}
],
"sender": "0x8a0a19589531694250d570040a0c4b74576919b8",
"secretKey": "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f"
}"#;
let _: TransactionParts = serde_json::from_str(tx).unwrap();
}
}