cast/cmd/safe/
transaction.rs1use super::SafeRpcOpts;
2use crate::{
3 tempo,
4 tx::{CastTxBuilder, apply_poll_interval},
5};
6use alloy_consensus::{SignableTransaction, Signed};
7use alloy_network::{Ethereum, EthereumWallet, Network, ReceiptResponse};
8use alloy_primitives::{Address, B256, Bytes, hex};
9use alloy_provider::{Provider, fillers::RecommendedFillers};
10use alloy_rpc_types::Log;
11use alloy_signer::{Signature, Signer};
12use clap::Args;
13use eyre::{Context, Result, ensure};
14use foundry_cli::{
15 opts::{EthereumOpts, TransactionOpts},
16 utils::{LoadConfig, resolve_lane},
17};
18use foundry_common::{FoundryTransactionBuilder, provider::ProviderBuilder};
19use foundry_config::Config;
20use foundry_wallets::{WalletOpts, WalletSigner};
21use serde::Serialize;
22use std::time::Duration;
23use tempo_alloy::TempoNetwork;
24
25#[derive(Args, Debug)]
27pub(super) struct SafeSendOpts {
28 #[command(flatten)]
29 pub(super) rpc: Box<SafeRpcOpts>,
30
31 #[command(flatten)]
32 wallet: Box<WalletOpts>,
33
34 #[command(flatten)]
35 tx: Box<TransactionOpts>,
36}
37
38pub(super) struct SafeSendResult {
39 pub(super) tx_hash: B256,
40 pub(super) logs: Vec<Log>,
41}
42
43impl SafeSendOpts {
44 pub(super) async fn send(
46 self,
47 to: Address,
48 data: Bytes,
49 confirmations: u64,
50 timeout: Option<u64>,
51 poll_interval: Option<u64>,
52 ) -> Result<SafeSendResult> {
53 let Self { rpc, wallet, tx } = self;
54 let config = rpc.load_config()?;
55 let eth = EthereumOpts { rpc: rpc.rpc, wallet: *wallet, ..Default::default() };
56 let (network, signer, access_key) =
57 tempo::resolve_transaction_network_and_signer_with_config(&tx.tempo, ð, &config)
58 .await?;
59 ensure!(
60 access_key.is_none(),
61 "Tempo Accounts sessions are not yet supported by `cast safe create` or `cast safe execute`"
62 );
63 let call = SafeCall { to, data, confirmations, timeout, poll_interval };
64 if network.is_tempo() {
65 call.send::<TempoNetwork>(eth, *tx, signer, config).await
66 } else {
67 call.send::<Ethereum>(eth, *tx, signer, config).await
68 }
69 }
70}
71
72struct SafeCall {
73 to: Address,
74 data: Bytes,
75 confirmations: u64,
76 timeout: Option<u64>,
77 poll_interval: Option<u64>,
78}
79
80impl SafeCall {
81 async fn send<N>(
82 self,
83 eth: EthereumOpts,
84 mut tx_opts: TransactionOpts,
85 signer: Option<WalletSigner>,
86 config: Config,
87 ) -> Result<SafeSendResult>
88 where
89 N: Network + RecommendedFillers,
90 N::TxEnvelope: From<Signed<N::UnsignedTx>>,
91 N::UnsignedTx: SignableTransaction<Signature>,
92 N::TransactionRequest: FoundryTransactionBuilder<N>,
93 N::ReceiptResponse: Serialize,
94 {
95 ensure!(
96 tx_opts.value.is_none_or(|value| value.is_zero()),
97 "Safe outer transaction value must be zero"
98 );
99 ensure!(!tx_opts.blob, "blob transactions are not supported by `cast safe`");
100 ensure!(
101 tx_opts.auth.is_empty(),
102 "EIP-7702 authorizations are not supported by `cast safe`"
103 );
104 ensure!(
105 !tx_opts.tempo.has_sponsor_submission()
106 && tx_opts.tempo.sponsor_url.is_none()
107 && !tx_opts.tempo.print_sponsor_hash,
108 "Tempo sponsorship is not yet supported by `cast safe create` or `cast safe execute`"
109 );
110 ensure!(
111 tx_opts.tempo.session_id()?.is_none(),
112 "Tempo Accounts sessions are not yet supported by `cast safe create` or `cast safe execute`"
113 );
114
115 let timeout = self.timeout.unwrap_or(config.transaction_timeout);
116 resolve_lane(&mut tx_opts.tempo, &config.root)?;
117 tempo::print_expires(tx_opts.tempo.resolve_expires())?;
118 let signer = match signer {
119 Some(signer) => signer,
120 None => eth.wallet.signer().await?,
121 };
122 crate::tx::validate_from_address(eth.wallet.from, signer.address())?;
123 let from = signer.address();
124 let provider = ProviderBuilder::<N>::from_config(&config)?
125 .build_with_wallet(EthereumWallet::from(signer))?;
126 apply_poll_interval(&provider, self.poll_interval);
127 let builder = CastTxBuilder::new(&provider, tx_opts, &config)
128 .await?
129 .with_to(Some(self.to.into()))
130 .await?
131 .with_code_sig_and_args(None, Some(hex::encode_prefixed(self.data)), Vec::new())
132 .await?;
133 let (mut request, _) = builder.build(from).await?;
134 let fee_provider = (!config.eth_rpc_curl).then_some(&provider);
135 tempo::resolve_and_print_fee_token(fee_provider, &mut request, Some(from)).await?;
136
137 let receipt = provider
138 .send_transaction(request)
139 .await?
140 .with_required_confirmations(self.confirmations)
141 .with_timeout(Some(Duration::from_secs(timeout)))
142 .get_receipt()
143 .await?;
144 ensure!(receipt.status(), "Safe transaction reverted");
145 let tx_hash = receipt.transaction_hash();
146 let receipt = serde_json::to_value(receipt)?;
147 let logs = serde_json::from_value(receipt["logs"].clone())
148 .wrap_err("invalid logs in transaction receipt")?;
149 Ok(SafeSendResult { tx_hash, logs })
150 }
151}