|
| 1 | +use crate::helpers::account::{check_account_exists, get_account_from_devnet, is_devnet_account}; |
| 2 | +use crate::helpers::configuration::CastConfig; |
1 | 3 | use crate::helpers::constants::{DEFAULT_STATE_FILE_SUFFIX, WAIT_RETRY_INTERVAL, WAIT_TIMEOUT};
|
| 4 | +use crate::helpers::rpc::RpcArgs; |
2 | 5 | use crate::response::errors::SNCastProviderError;
|
3 | 6 | use anyhow::{Context, Error, Result, anyhow, bail};
|
4 | 7 | use camino::Utf8PathBuf;
|
5 | 8 | use clap::ValueEnum;
|
6 | 9 | use conversions::serde::serialize::CairoSerialize;
|
7 | 10 | use foundry_ui::UI;
|
| 11 | +use foundry_ui::components::warning::WarningMessage; |
8 | 12 | use helpers::constants::{KEYSTORE_PASSWORD_ENV_VAR, UDC_ADDRESS};
|
9 | 13 | use rand::RngCore;
|
10 | 14 | use rand::rngs::OsRng;
|
@@ -85,7 +89,7 @@ pub const MAINNET: Felt =
|
85 | 89 | pub const SEPOLIA: Felt =
|
86 | 90 | Felt::from_hex_unchecked(const_hex::const_encode::<10, true>(b"SN_SEPOLIA").as_str());
|
87 | 91 |
|
88 |
| -#[derive(ValueEnum, Clone, Copy, Debug)] |
| 92 | +#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)] |
89 | 93 | pub enum Network {
|
90 | 94 | Mainnet,
|
91 | 95 | Sepolia,
|
@@ -245,6 +249,51 @@ pub async fn get_nonce(
|
245 | 249 | }
|
246 | 250 |
|
247 | 251 | pub async fn get_account<'a>(
|
| 252 | + config: &CastConfig, |
| 253 | + provider: &'a JsonRpcClient<HttpTransport>, |
| 254 | + rpc_args: &RpcArgs, |
| 255 | + keystore: Option<&Utf8PathBuf>, |
| 256 | + ui: &UI, |
| 257 | +) -> Result<SingleOwnerAccount<&'a JsonRpcClient<HttpTransport>, LocalWallet>> { |
| 258 | + let chain_id = get_chain_id(provider).await?; |
| 259 | + let network_name = chain_id_to_network_name(chain_id); |
| 260 | + let account = &config.account; |
| 261 | + let is_devnet_account = is_devnet_account(account); |
| 262 | + |
| 263 | + if is_devnet_account |
| 264 | + && let Some(network) = rpc_args.network |
| 265 | + && (network == Network::Mainnet || network == Network::Sepolia) |
| 266 | + { |
| 267 | + bail!(format!( |
| 268 | + "Devnet accounts cannot be used with `--network {network}`" |
| 269 | + )); |
| 270 | + } |
| 271 | + |
| 272 | + let accounts_file = &config.accounts_file; |
| 273 | + let exists_in_accounts_file = check_account_exists(account, &network_name, accounts_file)?; |
| 274 | + |
| 275 | + match (is_devnet_account, exists_in_accounts_file) { |
| 276 | + (true, true) => { |
| 277 | + ui.println(&WarningMessage::new(format!( |
| 278 | + "Using account {account} from accounts file {accounts_file}. \ |
| 279 | + To use an inbuilt devnet account, please rename your existing account or use an account with a different number." |
| 280 | + ))); |
| 281 | + ui.print_blank_line(); |
| 282 | + return get_account_from_accounts_file(account, accounts_file, provider, keystore) |
| 283 | + .await; |
| 284 | + } |
| 285 | + (true, false) => { |
| 286 | + let url = rpc_args.get_url(&config.url).context("Failed to get url")?; |
| 287 | + return get_account_from_devnet(account, provider, &url).await; |
| 288 | + } |
| 289 | + _ => { |
| 290 | + return get_account_from_accounts_file(account, accounts_file, provider, keystore) |
| 291 | + .await; |
| 292 | + } |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +pub async fn get_account_from_accounts_file<'a>( |
248 | 297 | account: &str,
|
249 | 298 | accounts_file: &Utf8PathBuf,
|
250 | 299 | provider: &'a JsonRpcClient<HttpTransport>,
|
|
0 commit comments