foundry_common/transactions/
builder.rs1use crate::tempo::configurable_account::ConfigurableAccountSigner;
2use alloy_consensus::{
3 BlobTransactionSidecar, BlobTransactionSidecarEip7594, BlobTransactionSidecarVariant,
4};
5use alloy_eips::{Encodable2718, eip7702::SignedAuthorization};
6use alloy_network::{
7 AnyNetwork, Ethereum, Network, NetworkTransactionBuilder, NetworkWallet, TransactionBuilder,
8};
9use alloy_primitives::{Address, B256, Bytes, Signature, TxKind, U256};
10use alloy_provider::Provider;
11use alloy_rpc_types::{TransactionRequest, serde_helpers::WithOtherFields};
12use eyre::Result;
13use std::num::NonZeroU64;
14use tempo_alloy::{
15 TempoNetwork,
16 accounts::TempoAccountsWallet,
17 rpc::{ConfigurableAccountSimulationSpec, TempoTransactionRequest},
18};
19use tempo_primitives::{
20 SignatureType, TempoTxType,
21 transaction::{Call, TempoSignedAuthorization},
22};
23
24#[cfg(feature = "base")]
25use base_common_network::Base;
26#[cfg(feature = "base")]
27use base_common_rpc_types::BaseTransactionRequest;
28
29#[cfg(feature = "optimism")]
30use op_alloy_network::Optimism;
31#[cfg(feature = "optimism")]
32use op_alloy_rpc_types::OpTransactionRequest;
33
34pub trait FoundryTransactionBuilder<N: Network>: NetworkTransactionBuilder<N> {
62 fn reset_gas_limit(&mut self);
64
65 fn max_fee_per_blob_gas(&self) -> Option<u128> {
67 None
68 }
69
70 fn set_max_fee_per_blob_gas(&mut self, _max_fee_per_blob_gas: u128) {}
72
73 fn with_max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self {
75 self.set_max_fee_per_blob_gas(max_fee_per_blob_gas);
76 self
77 }
78
79 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
84 None
85 }
86
87 fn set_blob_versioned_hashes(&mut self, _hashes: Vec<B256>) {}
89
90 fn with_blob_versioned_hashes(mut self, hashes: Vec<B256>) -> Self {
92 self.set_blob_versioned_hashes(hashes);
93 self
94 }
95
96 fn blob_sidecar(&self) -> Option<&BlobTransactionSidecarVariant> {
98 None
99 }
100
101 fn set_blob_sidecar(&mut self, _sidecar: BlobTransactionSidecarVariant) {}
106
107 fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecarVariant) -> Self {
109 self.set_blob_sidecar(sidecar);
110 self
111 }
112
113 fn blob_sidecar_4844(&self) -> Option<&BlobTransactionSidecar> {
115 self.blob_sidecar().and_then(|s| s.as_eip4844())
116 }
117
118 fn set_blob_sidecar_4844(&mut self, sidecar: BlobTransactionSidecar) {
120 self.set_blob_sidecar(BlobTransactionSidecarVariant::Eip4844(sidecar));
121 }
122
123 fn with_blob_sidecar_4844(mut self, sidecar: BlobTransactionSidecar) -> Self {
125 self.set_blob_sidecar_4844(sidecar);
126 self
127 }
128
129 fn blob_sidecar_7594(&self) -> Option<&BlobTransactionSidecarEip7594> {
131 self.blob_sidecar().and_then(|s| s.as_eip7594())
132 }
133
134 fn set_blob_sidecar_7594(&mut self, sidecar: BlobTransactionSidecarEip7594) {
136 self.set_blob_sidecar(BlobTransactionSidecarVariant::Eip7594(sidecar));
137 }
138
139 fn with_blob_sidecar_7594(mut self, sidecar: BlobTransactionSidecarEip7594) -> Self {
141 self.set_blob_sidecar_7594(sidecar);
142 self
143 }
144
145 fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
147 None
148 }
149
150 fn set_authorization_list(&mut self, _authorization_list: Vec<SignedAuthorization>) {}
152
153 fn with_authorization_list(mut self, authorization_list: Vec<SignedAuthorization>) -> Self {
155 self.set_authorization_list(authorization_list);
156 self
157 }
158
159 fn fee_token(&self) -> Option<Address> {
161 None
162 }
163
164 fn tempo_calls(&self) -> Vec<(TxKind, &[u8])> {
166 Vec::new()
167 }
168
169 fn has_tempo_call_list(&self) -> bool {
171 false
172 }
173
174 fn set_first_call_input(&mut self, input: Bytes) {
176 self.set_input(input);
177 }
178
179 fn is_tempo_aa(&self) -> bool {
181 false
182 }
183
184 fn supports_fee_token(&self) -> bool {
186 false
187 }
188
189 fn set_fee_token(&mut self, _fee_token: Address) {}
191
192 fn with_fee_token(mut self, fee_token: Address) -> Self {
194 self.set_fee_token(fee_token);
195 self
196 }
197
198 fn nonce_key(&self) -> Option<U256> {
200 None
201 }
202
203 fn set_nonce_key(&mut self, _nonce_key: U256) {}
205
206 fn with_nonce_key(mut self, nonce_key: U256) -> Self {
208 self.set_nonce_key(nonce_key);
209 self
210 }
211
212 fn browser_wallet_gas_estimation_request(&self) -> Self
217 where
218 Self: Clone,
219 {
220 self.clone()
221 }
222
223 fn key_id(&self) -> Option<Address> {
225 None
226 }
227
228 fn set_key_id(&mut self, _key_id: Address) {}
233
234 fn with_key_id(mut self, key_id: Address) -> Self {
236 self.set_key_id(key_id);
237 self
238 }
239
240 fn set_configurable_account_simulation(&mut self, _spec: ConfigurableAccountSimulationSpec) {}
243
244 fn valid_before(&self) -> Option<NonZeroU64> {
246 None
247 }
248
249 fn set_valid_before(&mut self, _valid_before: NonZeroU64) {}
251
252 fn with_valid_before(mut self, valid_before: NonZeroU64) -> Self {
254 self.set_valid_before(valid_before);
255 self
256 }
257
258 fn valid_after(&self) -> Option<NonZeroU64> {
260 None
261 }
262
263 fn set_valid_after(&mut self, _valid_after: NonZeroU64) {}
265
266 fn with_valid_after(mut self, valid_after: NonZeroU64) -> Self {
268 self.set_valid_after(valid_after);
269 self
270 }
271
272 fn fee_payer_signature(&self) -> Option<Signature> {
274 None
275 }
276
277 fn set_fee_payer_signature(&mut self, _signature: Signature) {}
279
280 fn with_fee_payer_signature(mut self, signature: Signature) -> Self {
282 self.set_fee_payer_signature(signature);
283 self
284 }
285
286 fn compute_sponsor_hash(&self, _from: Address) -> Option<B256> {
292 None
293 }
294
295 fn prepare_with_tempo_wallet<'a>(
297 &'a mut self,
298 _provider: &'a impl Provider<N>,
299 _wallet: &'a TempoAccountsWallet,
300 ) -> impl Future<Output = Result<TempoAccountsWallet>> + Send + 'a
301 where
302 Self: Send,
303 {
304 std::future::ready(Err(eyre::eyre!(
305 "Tempo access-key wallets are not supported for this network"
306 )))
307 }
308
309 fn convert_create_to_call(&mut self) {}
315
316 fn clear_batch_to(&mut self) {}
323
324 fn sign_with_tempo_wallet(
326 self,
327 _wallet: &TempoAccountsWallet,
328 ) -> impl Future<Output = Result<Vec<u8>>> + Send {
329 std::future::ready(Err(eyre::eyre!(
330 "Tempo access-key wallets are not supported for this network"
331 )))
332 }
333
334 fn sign_with_configurable_account(
336 self,
337 _signer: &ConfigurableAccountSigner,
338 ) -> impl Future<Output = Result<Vec<u8>>> + Send {
339 std::future::ready(Err(eyre::eyre!(
340 "Tempo configurable accounts are not supported for this network"
341 )))
342 }
343}
344
345impl FoundryTransactionBuilder<Ethereum> for TransactionRequest {
346 fn reset_gas_limit(&mut self) {
347 self.gas = None;
348 }
349
350 fn max_fee_per_blob_gas(&self) -> Option<u128> {
351 self.max_fee_per_blob_gas
352 }
353
354 fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
355 self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas);
356 }
357
358 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
359 self.blob_versioned_hashes.as_deref()
360 }
361
362 fn set_blob_versioned_hashes(&mut self, hashes: Vec<B256>) {
363 self.blob_versioned_hashes = Some(hashes);
364 }
365
366 fn blob_sidecar(&self) -> Option<&BlobTransactionSidecarVariant> {
367 self.sidecar.as_ref()
368 }
369
370 fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecarVariant) {
371 self.sidecar = Some(sidecar);
372 self.populate_blob_hashes();
373 }
374
375 fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
376 self.authorization_list.as_ref()
377 }
378
379 fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
380 self.authorization_list = Some(authorization_list);
381 }
382}
383
384impl FoundryTransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
385 fn reset_gas_limit(&mut self) {
386 self.gas = None;
387 }
388
389 fn max_fee_per_blob_gas(&self) -> Option<u128> {
390 self.max_fee_per_blob_gas
391 }
392
393 fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
394 self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas);
395 }
396
397 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
398 self.blob_versioned_hashes.as_deref()
399 }
400
401 fn set_blob_versioned_hashes(&mut self, hashes: Vec<B256>) {
402 self.blob_versioned_hashes = Some(hashes);
403 }
404
405 fn blob_sidecar(&self) -> Option<&BlobTransactionSidecarVariant> {
406 self.sidecar.as_ref()
407 }
408
409 fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecarVariant) {
410 self.sidecar = Some(sidecar);
411 self.populate_blob_hashes();
412 }
413
414 fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
415 self.authorization_list.as_ref()
416 }
417
418 fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
419 self.authorization_list = Some(authorization_list);
420 }
421}
422
423#[cfg(feature = "base")]
424impl FoundryTransactionBuilder<Base> for BaseTransactionRequest {
425 fn reset_gas_limit(&mut self) {
426 self.as_mut().gas = None;
427 }
428
429 fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
430 self.as_ref().authorization_list.as_ref()
431 }
432
433 fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
434 self.as_mut().authorization_list = Some(authorization_list);
435 }
436}
437
438#[cfg(feature = "optimism")]
439impl FoundryTransactionBuilder<Optimism> for OpTransactionRequest {
440 fn reset_gas_limit(&mut self) {
441 self.as_mut().gas = None;
442 }
443
444 fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
445 self.as_ref().authorization_list.as_ref()
446 }
447
448 fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
449 self.as_mut().authorization_list = Some(authorization_list);
450 }
451}
452
453const TEMPO_BROWSER_WEBAUTHN_DATA_SIZE: u16 = 1_400;
458
459impl FoundryTransactionBuilder<TempoNetwork> for TempoTransactionRequest {
460 fn reset_gas_limit(&mut self) {
461 self.gas = None;
462 }
463
464 fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
465 self.authorization_list.as_ref()
466 }
467
468 fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
472 if self.is_tempo_aa()
473 && let Ok(tempo_authorization_list) = authorization_list
474 .iter()
475 .map(|auth| {
476 let signature = auth.signature()?;
477 Ok(TempoSignedAuthorization::new_unchecked(
478 auth.inner().clone(),
479 signature.into(),
480 ))
481 })
482 .collect::<Result<_, alloy_primitives::SignatureError>>()
483 {
484 self.tempo_authorization_list = tempo_authorization_list;
485 } else {
486 self.authorization_list = Some(authorization_list);
487 }
488 }
489
490 fn fee_token(&self) -> Option<Address> {
491 self.fee_token
492 }
493
494 fn tempo_calls(&self) -> Vec<(TxKind, &[u8])> {
495 self.calls
496 .iter()
497 .map(|call| (call.to, call.input.as_ref()))
498 .chain(self.inner.to.map(|to| {
499 (to, self.inner.input.input().map_or(&[] as &[u8], |input| input.as_ref()))
500 }))
501 .collect()
502 }
503
504 fn has_tempo_call_list(&self) -> bool {
505 !self.calls.is_empty()
506 }
507
508 fn set_first_call_input(&mut self, input: Bytes) {
509 match self.calls.first_mut() {
510 Some(call) => call.input = input,
511 None => self.set_input(input),
512 }
513 }
514
515 fn is_tempo_aa(&self) -> bool {
516 NetworkTransactionBuilder::<TempoNetwork>::output_tx_type(self) == TempoTxType::AA
517 }
518
519 fn supports_fee_token(&self) -> bool {
520 true
521 }
522
523 fn set_fee_token(&mut self, fee_token: Address) {
524 self.fee_token = Some(fee_token);
525 }
526
527 fn nonce_key(&self) -> Option<U256> {
528 self.nonce_key
529 }
530
531 fn set_nonce_key(&mut self, nonce_key: U256) {
532 self.nonce_key = Some(nonce_key);
533 }
534
535 fn browser_wallet_gas_estimation_request(&self) -> Self {
536 let mut request = self.clone();
537 if request.key_type.is_none() {
538 request.key_type = Some(SignatureType::WebAuthn);
539 request.key_data = Some(TEMPO_BROWSER_WEBAUTHN_DATA_SIZE.to_be_bytes().into());
540 } else if matches!(request.key_type, Some(SignatureType::WebAuthn))
541 && request.key_data.is_none()
542 {
543 request.key_data = Some(TEMPO_BROWSER_WEBAUTHN_DATA_SIZE.to_be_bytes().into());
544 }
545 request.convert_create_to_call();
546 request
547 }
548
549 fn key_id(&self) -> Option<Address> {
550 self.key_id
551 }
552
553 fn set_key_id(&mut self, key_id: Address) {
554 self.key_id = Some(key_id);
555 }
556
557 fn set_configurable_account_simulation(&mut self, spec: ConfigurableAccountSimulationSpec) {
558 self.configurable_account_simulation = Some(spec);
559 }
560
561 fn valid_before(&self) -> Option<NonZeroU64> {
562 self.valid_before
563 }
564
565 fn set_valid_before(&mut self, valid_before: NonZeroU64) {
566 self.valid_before = Some(valid_before);
567 }
568
569 fn valid_after(&self) -> Option<NonZeroU64> {
570 self.valid_after
571 }
572
573 fn set_valid_after(&mut self, valid_after: NonZeroU64) {
574 self.valid_after = Some(valid_after);
575 }
576
577 fn fee_payer_signature(&self) -> Option<Signature> {
578 self.fee_payer_signature
579 }
580
581 fn set_fee_payer_signature(&mut self, signature: Signature) {
582 self.fee_payer_signature = Some(signature);
583 }
584
585 fn compute_sponsor_hash(&self, from: Address) -> Option<B256> {
586 let tx = self.clone().build_aa().ok()?;
587 Some(tx.fee_payer_signature_hash(from))
588 }
589
590 async fn prepare_with_tempo_wallet<'a>(
591 &'a mut self,
592 provider: &'a impl Provider<TempoNetwork>,
593 wallet: &'a TempoAccountsWallet,
594 ) -> Result<TempoAccountsWallet>
595 where
596 Self: Send,
597 {
598 wallet.prepare_request(provider, self).await.map_err(Into::into)
599 }
600
601 fn convert_create_to_call(&mut self) {
602 if self.calls.is_empty() && self.inner.to.is_some_and(|to| to.is_create()) {
603 let input = self.inner.input.input().cloned().unwrap_or_default();
604 let value = self.inner.value.unwrap_or(U256::ZERO);
605 self.calls.push(Call { to: TxKind::Create, value, input });
606 self.inner.input = Default::default();
607 self.inner.value = None;
608 self.inner.to = None;
609 }
610 }
611
612 fn clear_batch_to(&mut self) {
613 if !self.calls.is_empty() {
614 self.inner.to = None;
615 self.inner.value = None;
616 }
617 }
618
619 async fn sign_with_tempo_wallet(self, wallet: &TempoAccountsWallet) -> Result<Vec<u8>> {
620 wallet.sign_request(self).await.map(|envelope| envelope.encoded_2718()).map_err(Into::into)
621 }
622
623 async fn sign_with_configurable_account(
624 self,
625 signer: &ConfigurableAccountSigner,
626 ) -> Result<Vec<u8>> {
627 signer.sign_request(self).await.map(|envelope| envelope.encoded_2718())
628 }
629}