Skip to content

Commit

Permalink
Remove macro :(((((((((((((((((((((((((((((((((
Browse files Browse the repository at this point in the history
  • Loading branch information
kkawula committed Jan 9, 2025
1 parent 93fee3b commit bddfce7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 57 deletions.
38 changes: 7 additions & 31 deletions crates/sncast/src/helpers/version.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
#[macro_export]
macro_rules! generate_version_parser {
($enum_name:ident, $variant1:ident, $variant2:ident ) => {
use shared::print::print_as_warning;
use anyhow::Error;
use anyhow::Error;
use clap::ValueEnum;
use shared::print::print_as_warning;

impl std::str::FromStr for $enum_name {
type Err = String;
const DEPRECATION_MESSAGE: &str = "The '--version' flag is deprecated and will be removed in the future. Version 3 will become the only type of transaction available.";

fn from_str(s: &str) -> Result<Self, Self::Err> {
let var1 = stringify!($variant1).to_lowercase();
let var2 = stringify!($variant2).to_lowercase();

match s.to_lowercase().as_str() {
v if v == var1 => Ok($enum_name::$variant1),
v if v == var2 => Ok($enum_name::$variant2),
_ => Err(format!(
"Invalid value '{}'. Possible values: {}, {}",
s, var1, var2
)),
}
}
}

pub fn parse_version(s: &str) -> Result<$enum_name, String> {
let deprecation_message = "The '--version' flag is deprecated and will be removed in the future. Version 3 will become the only type of transaction available.";
print_as_warning(&Error::msg(deprecation_message));

let parsed_enum = s.parse::<$enum_name>()?;

Ok(parsed_enum)
}
};
pub fn parse_version<T: ValueEnum>(s: &str) -> Result<T, String> {
print_as_warning(&Error::msg(DEPRECATION_MESSAGE));
T::from_str(s, true)
}
8 changes: 4 additions & 4 deletions crates/sncast/src/starknet_commands/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use sncast::helpers::constants::{BRAAVOS_BASE_ACCOUNT_CLASS_HASH, KEYSTORE_PASSW
use sncast::helpers::error::token_not_supported_for_deployment;
use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction};
use sncast::helpers::rpc::RpcArgs;
use sncast::helpers::version::parse_version;
use sncast::response::structs::InvokeResponse;

use sncast::{
apply_optional, chain_id_to_network_name, check_account_file_exists, generate_version_parser,
apply_optional, chain_id_to_network_name, check_account_file_exists,
get_account_data_from_accounts_file, get_account_data_from_keystore, get_keystore_password,
handle_rpc_error, handle_wait_for_tx, impl_payable_transaction, AccountType, WaitForTx,
};
Expand Down Expand Up @@ -38,7 +40,7 @@ pub struct Deploy {
pub fee_args: FeeArgs,

/// Version of the account deployment (can be inferred from fee token)
#[clap(short, long, value_parser = parse_version)]
#[clap(short, long, value_parser = parse_version::<AccountDeployVersion>)]
pub version: Option<AccountDeployVersion>,

#[clap(flatten)]
Expand All @@ -51,8 +53,6 @@ pub enum AccountDeployVersion {
V3,
}

generate_version_parser!(AccountDeployVersion, V1, V3);

impl_payable_transaction!(Deploy, token_not_supported_for_deployment,
AccountDeployVersion::V1 => FeeToken::Eth,
AccountDeployVersion::V3 => FeeToken::Strk
Expand Down
10 changes: 3 additions & 7 deletions crates/sncast/src/starknet_commands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ use scarb_api::StarknetContractArtifacts;
use sncast::helpers::error::token_not_supported_for_declaration;
use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction};
use sncast::helpers::rpc::RpcArgs;
use sncast::helpers::version::parse_version;
use sncast::response::errors::StarknetCommandError;
use sncast::response::structs::{
AlreadyDeclaredResponse, DeclareResponse, DeclareTransactionResponse,
};
use sncast::{
apply_optional, generate_version_parser, handle_wait_for_tx, impl_payable_transaction,
ErrorData, WaitForTx,
};
use sncast::{apply_optional, handle_wait_for_tx, impl_payable_transaction, ErrorData, WaitForTx};
use starknet::accounts::AccountError::Provider;
use starknet::accounts::{ConnectedAccount, DeclarationV2, DeclarationV3};
use starknet::core::types::{DeclareTransactionResult, StarknetError};
Expand Down Expand Up @@ -47,7 +45,7 @@ pub struct Declare {
pub package: Option<String>,

/// Version of the declaration (can be inferred from fee token)
#[clap(short, long, value_parser = parse_version)]
#[clap(short, long, value_parser = parse_version::<DeclareVersion>)]
pub version: Option<DeclareVersion>,

#[clap(flatten)]
Expand All @@ -60,8 +58,6 @@ pub enum DeclareVersion {
V3,
}

generate_version_parser!(DeclareVersion, V2, V3);

impl_payable_transaction!(Declare, token_not_supported_for_declaration,
DeclareVersion::V2 => FeeToken::Eth,
DeclareVersion::V3 => FeeToken::Strk
Expand Down
9 changes: 3 additions & 6 deletions crates/sncast/src/starknet_commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use conversions::IntoConv;
use sncast::helpers::error::token_not_supported_for_deployment;
use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction};
use sncast::helpers::rpc::RpcArgs;
use sncast::helpers::version::parse_version;
use sncast::response::errors::StarknetCommandError;
use sncast::response::structs::DeployResponse;
use sncast::{
extract_or_generate_salt, generate_version_parser, impl_payable_transaction, udc_uniqueness,
};
use sncast::{extract_or_generate_salt, impl_payable_transaction, udc_uniqueness};
use sncast::{handle_wait_for_tx, WaitForTx};
use starknet::accounts::AccountError::Provider;
use starknet::accounts::{Account, ConnectedAccount, SingleOwnerAccount};
Expand Down Expand Up @@ -45,7 +44,7 @@ pub struct Deploy {
pub nonce: Option<Felt>,

/// Version of the deployment (can be inferred from fee token)
#[clap(short, long, value_parser = parse_version)]
#[clap(short, long, value_parser = parse_version::<DeployVersion>)]
pub version: Option<DeployVersion>,

#[clap(flatten)]
Expand All @@ -70,8 +69,6 @@ pub enum DeployVersion {
V3,
}

generate_version_parser!(DeployVersion, V1, V3);

impl_payable_transaction!(Deploy, token_not_supported_for_deployment,
DeployVersion::V1 => FeeToken::Eth,
DeployVersion::V3 => FeeToken::Strk
Expand Down
10 changes: 3 additions & 7 deletions crates/sncast/src/starknet_commands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use conversions::IntoConv;
use sncast::helpers::error::token_not_supported_for_invoke;
use sncast::helpers::fee::{FeeArgs, FeeSettings, FeeToken, PayableTransaction};
use sncast::helpers::rpc::RpcArgs;
use sncast::helpers::version::parse_version;
use sncast::response::errors::StarknetCommandError;
use sncast::response::structs::InvokeResponse;
use sncast::{
apply_optional, generate_version_parser, handle_wait_for_tx, impl_payable_transaction,
WaitForTx,
};
use sncast::{apply_optional, handle_wait_for_tx, impl_payable_transaction, WaitForTx};
use starknet::accounts::AccountError::Provider;
use starknet::accounts::{Account, ConnectedAccount, ExecutionV1, ExecutionV3, SingleOwnerAccount};
use starknet::core::types::{Call, InvokeTransactionResult};
Expand Down Expand Up @@ -41,7 +39,7 @@ pub struct Invoke {
pub nonce: Option<Felt>,

/// Version of invoke (can be inferred from fee token)
#[clap(short, long, value_parser = parse_version)]
#[clap(short, long, value_parser = parse_version::<InvokeVersion>)]
pub version: Option<InvokeVersion>,

#[clap(flatten)]
Expand All @@ -54,8 +52,6 @@ pub enum InvokeVersion {
V3,
}

generate_version_parser!(InvokeVersion, V1, V3);

impl_payable_transaction!(Invoke, token_not_supported_for_invoke,
InvokeVersion::V1 => FeeToken::Eth,
InvokeVersion::V3 => FeeToken::Strk
Expand Down
5 changes: 3 additions & 2 deletions crates/sncast/src/starknet_commands/multicall/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::starknet_commands::invoke::{execute_calls, parse_version, InvokeVersion};
use crate::starknet_commands::invoke::{execute_calls, InvokeVersion};
use anyhow::anyhow;
use anyhow::{Context, Result};
use camino::Utf8PathBuf;
Expand All @@ -8,6 +8,7 @@ use sncast::helpers::constants::UDC_ADDRESS;
use sncast::helpers::error::token_not_supported_for_invoke;
use sncast::helpers::fee::{FeeArgs, FeeToken, PayableTransaction};
use sncast::helpers::rpc::RpcArgs;
use sncast::helpers::version::parse_version;
use sncast::response::errors::handle_starknet_command_error;
use sncast::response::structs::InvokeResponse;
use sncast::{extract_or_generate_salt, impl_payable_transaction, udc_uniqueness, WaitForTx};
Expand All @@ -31,7 +32,7 @@ pub struct Run {
pub fee_args: FeeArgs,

/// Version of invoke (can be inferred from fee token)
#[clap(short, long, value_parser = parse_version)]
#[clap(short, long, value_parser = parse_version::<InvokeVersion>)]
pub version: Option<InvokeVersion>,

#[clap(flatten)]
Expand Down

0 comments on commit bddfce7

Please sign in to comment.