Skip to main content

cast/cmd/safe/
mod.rs

1use alloy_network::Ethereum;
2use alloy_provider::Provider;
3use clap::{Args, Parser, ValueEnum};
4use eyre::{Result, ensure};
5use foundry_cli::{
6    opts::{ChainValueParser, RpcOpts},
7    utils::LoadConfig,
8};
9use foundry_common::provider::{ProviderBuilder, RetryProvider};
10use foundry_config::{
11    Chain,
12    figment::{Figment, providers::Serialized},
13};
14
15mod contracts;
16mod delegates;
17mod deploy;
18mod execute;
19mod proposal;
20mod service;
21mod signing;
22mod simulate;
23mod transaction;
24
25/// Safe transaction operations.
26#[derive(Debug, Parser)]
27pub enum SafeSubcommand {
28    /// Deploy a Safe account.
29    ///
30    /// Examples:
31    /// - cast safe create $OWNER --threshold 1 --rpc-url $RPC --ledger
32    /// - cast safe create $OWNER_1 $OWNER_2 $OWNER_3 --threshold 2 --rpc-url $RPC --account
33    ///   deployer
34    #[command(verbatim_doc_comment)]
35    Create(deploy::CreateArgs),
36
37    /// Register a transaction-service delegate for a Safe owner.
38    AddDelegate(delegates::AddDelegateArgs),
39
40    /// List transaction-service delegates registered for a Safe.
41    ListDelegates(delegates::ListDelegatesArgs),
42
43    /// Remove a transaction-service delegate for a Safe owner.
44    RemoveDelegate(delegates::RemoveDelegateArgs),
45
46    /// Create, sign, and submit a Safe transaction proposal.
47    Propose(proposal::ProposeArgs),
48
49    /// Sign and submit a confirmation for a proposed Safe transaction.
50    Sign(proposal::SignArgs),
51
52    /// Simulate a proposed Safe transaction without requiring owner signatures.
53    ///
54    /// This simulates the inner CALL or DELEGATECALL in the Safe's context. It does not validate
55    /// the Safe nonce, owner signatures, threshold, or guard hooks. Reimbursed transactions
56    /// (`gasPrice > 0`) are rejected because SimulateTxAccessor does not enforce `safeTxGas`.
57    #[command(verbatim_doc_comment)]
58    Simulate(simulate::SimulateArgs),
59
60    /// Execute a confirmed Safe transaction onchain.
61    Execute(execute::ExecuteArgs),
62}
63
64#[derive(Clone, Copy, Debug, Default, ValueEnum)]
65#[repr(u8)]
66pub enum SafeOperation {
67    #[default]
68    Call = 0,
69    DelegateCall = 1,
70}
71
72impl SafeSubcommand {
73    pub async fn run(self) -> Result<()> {
74        match self {
75            Self::Create(args) => args.run().await,
76            Self::AddDelegate(args) => args.run().await,
77            Self::ListDelegates(args) => args.run().await,
78            Self::RemoveDelegate(args) => args.run().await,
79            Self::Propose(args) => args.run().await,
80            Self::Sign(args) => args.run().await,
81            Self::Simulate(args) => args.run().await,
82            Self::Execute(args) => args.run().await,
83        }
84    }
85}
86
87/// Builds the read-only provider for `rpc` and returns it together with its chain ID.
88async fn rpc_provider(rpc: &SafeRpcOpts) -> Result<(RetryProvider<Ethereum>, u64)> {
89    let config = rpc.load_config()?;
90    let provider = ProviderBuilder::<Ethereum>::from_config(&config)?.build()?;
91    let chain_id = provider.get_chain_id().await?;
92    if let Some(chain) = config.chain {
93        let configured_chain_id = chain.id();
94        ensure!(
95            configured_chain_id == chain_id,
96            "Configured chain ID {configured_chain_id} does not match RPC chain ID {chain_id}.\n\
97             Check --chain, CHAIN, chain_id in the selected Foundry profile, or FOUNDRY_CHAIN_ID."
98        );
99    }
100    Ok((provider, chain_id))
101}
102
103/// RPC options with an optional expected chain for Safe operations.
104#[derive(Args, Debug)]
105pub(super) struct SafeRpcOpts {
106    #[command(flatten)]
107    rpc: RpcOpts,
108
109    /// Expected chain name or EIP-155 chain ID. Must match the RPC chain.
110    ///
111    /// Overrides `FOUNDRY_CHAIN_ID` and the selected profile's `chain_id`.
112    #[arg(short = 'c', long, alias = "chain-id", env = "CHAIN", value_parser = ChainValueParser::default())]
113    chain: Option<Chain>,
114}
115
116impl LoadConfig for SafeRpcOpts {
117    fn figment(&self) -> Figment {
118        let mut figment = self.rpc.figment();
119        if let Some(chain) = self.chain {
120            figment = figment.merge(Serialized::global("chain_id", chain.id()));
121        }
122        figment
123    }
124}