Skip to main content

foundry_primitives/transaction/
base.rs

1//! Base-specific transaction conversions.
2
3use alloy_primitives::{Address, Bytes};
4use alloy_rpc_types::TransactionRequest;
5use base_common_consensus::{BASE_TX_TYPE_ID as EIP8130_TRANSACTION_TYPE, TxBase};
6use base_common_rpc_types::{BaseTransactionRequest, TxBaseRequestFields};
7use serde::Serialize;
8
9/// Projects the complete AA body into a simulation request without inventing a single call.
10pub(super) fn simulation_request(
11    tx: TxBase,
12    from: Option<Address>,
13    sender_auth: Option<Bytes>,
14    payer_auth: Option<Bytes>,
15) -> BaseTransactionRequest {
16    let inner = TransactionRequest {
17        transaction_type: Some(EIP8130_TRANSACTION_TYPE),
18        chain_id: Some(tx.chain_id),
19        from,
20        nonce: Some(tx.nonce_sequence),
21        gas: Some(tx.gas_limit),
22        max_fee_per_gas: Some(tx.max_fee_per_gas),
23        max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas),
24        ..Default::default()
25    };
26    let fields = TxBaseRequestFields {
27        nonce_key: Some(tx.nonce_key),
28        account_changes: Some(tx.account_changes),
29        calls: Some(tx.calls),
30        valid_after: Some(tx.valid_after),
31        valid_before: Some(tx.valid_before),
32        metadata: Some(tx.metadata),
33        sender: tx.sender,
34        sender_auth,
35        payer: tx.payer,
36        payer_auth,
37    };
38    // The upstream request exposes no constructor for its private AA fields.
39    #[derive(Serialize)]
40    struct SimulationRequest {
41        #[serde(flatten)]
42        inner: TransactionRequest,
43        #[serde(flatten)]
44        fields: TxBaseRequestFields,
45    }
46    let value = serde_json::to_value(SimulationRequest { inner, fields })
47        .expect("serializable Base simulation request");
48    serde_json::from_value(value).expect("compatible Base simulation fields")
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crate::{FoundryTransactionRequest, FoundryTxEnvelope, FoundryTypedTx};
55    use alloy_network::NetworkTransactionBuilder;
56    use alloy_primitives::U256;
57    use base_common_consensus::{AccountChange, Call, Delegation, TxBaseSigned};
58
59    #[test]
60    fn eip8130_simulation_projection() {
61        let body = TxBase {
62            chain_id: 8453,
63            sender: Some(Address::repeat_byte(1)),
64            payer: Some(Address::repeat_byte(2)),
65            nonce_key: U256::from(7),
66            nonce_sequence: 9,
67            gas_limit: 100_000,
68            max_fee_per_gas: 20,
69            max_priority_fee_per_gas: 3,
70            valid_after: 100,
71            valid_before: 200,
72            calls: vec![vec![Call {
73                to: Address::repeat_byte(3),
74                value: U256::from(5),
75                data: Bytes::from_static(b"call"),
76            }]],
77            account_changes: vec![AccountChange::Delegation(Delegation {
78                target: Address::repeat_byte(4),
79            })],
80            metadata: Bytes::from_static(b"metadata"),
81        };
82        let signed = TxBaseSigned::new(
83            body.clone(),
84            Bytes::from_static(b"sender"),
85            Bytes::from_static(b"payer"),
86        );
87        let envelope = FoundryTxEnvelope::Eip8130(signed.clone());
88        for (request, from, sender_auth, payer_auth) in [
89            (
90                FoundryTransactionRequest::from(FoundryTypedTx::Eip8130(body.clone())),
91                None,
92                None,
93                None,
94            ),
95            (
96                FoundryTransactionRequest::from(envelope),
97                body.sender,
98                Some(signed.sender_auth().clone()),
99                Some(signed.payer_auth().clone()),
100            ),
101        ] {
102            assert!(!request.can_build());
103            let base = request.as_base().unwrap();
104            assert_eq!(
105                base.as_base_tx(),
106                Some(&TxBaseRequestFields {
107                    nonce_key: Some(body.nonce_key),
108                    account_changes: Some(body.account_changes.clone()),
109                    calls: Some(body.calls.clone()),
110                    valid_after: Some(body.valid_after),
111                    valid_before: Some(body.valid_before),
112                    metadata: Some(body.metadata.clone()),
113                    sender: body.sender,
114                    payer: body.payer,
115                    sender_auth,
116                    payer_auth,
117                })
118            );
119            assert_eq!(
120                base.as_ref(),
121                &TransactionRequest {
122                    transaction_type: Some(EIP8130_TRANSACTION_TYPE),
123                    chain_id: Some(body.chain_id),
124                    from,
125                    nonce: Some(body.nonce_sequence),
126                    gas: Some(body.gas_limit),
127                    max_fee_per_gas: Some(body.max_fee_per_gas),
128                    max_priority_fee_per_gas: Some(body.max_priority_fee_per_gas),
129                    ..Default::default()
130                }
131            );
132            let roundtrip: FoundryTransactionRequest =
133                serde_json::from_value(serde_json::to_value(&request).unwrap()).unwrap();
134            assert_eq!(request, roundtrip);
135        }
136    }
137}