Skip to content

Commit

Permalink
Refactored call, deploy and invoke command handlers
Browse files Browse the repository at this point in the history
commit-id:13b07d29
  • Loading branch information
integraledelebesgue committed Oct 3, 2024
1 parent 67f3995 commit 26f846e
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 88 deletions.
16 changes: 16 additions & 0 deletions crates/sncast/src/helpers/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ pub enum FeeSettings {
},
}

impl From<ScriptFeeSettings> for FeeSettings {
fn from(value: ScriptFeeSettings) -> Self {
match value {
ScriptFeeSettings::Eth { max_fee } => FeeSettings::Eth { max_fee },
ScriptFeeSettings::Strk {
max_gas,
max_gas_unit_price,
..
} => FeeSettings::Strk {
max_gas,
max_gas_unit_price,
},
}
}
}

pub trait PayableTransaction {
fn error_message(&self, token: &str, version: &str) -> String;
fn validate(&self) -> Result<()>;
Expand Down
13 changes: 13 additions & 0 deletions crates/sncast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ pub async fn get_account<'a>(
Ok(account)
}

pub async fn get_contract_class(
class_hash: Felt,
provider: &JsonRpcClient<HttpTransport>,
) -> Result<ContractClass> {
provider
.get_class(BlockId::Tag(BlockTag::Latest), class_hash)
.await
.map_err(handle_rpc_error)
.context(format!(
"Couldn't retrieve contract class with hash: {class_hash:#x}"
))
}

async fn build_account(
account_data: AccountData,
chain_id: Felt,
Expand Down
88 changes: 71 additions & 17 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sncast::{
chain_id_to_network_name, get_account, get_block_id, get_chain_id, get_default_state_file_name,
NumbersFormat, ValidatedWaitParams, WaitForTx,
};
use starknet::accounts::ConnectedAccount;
use starknet::core::utils::get_selector_from_name;
use starknet::providers::Provider;
use starknet_commands::account::list::print_account_list;
Expand Down Expand Up @@ -221,9 +222,19 @@ async fn run_async_command(
}

Commands::Deploy(deploy) => {
let provider = deploy.rpc.get_provider(&config).await?;

deploy.validate()?;

let fee_token = deploy.token_from_version();

let Deploy {
constructor_calldata,
fee_args,
rpc,
..
} = deploy;

let provider = rpc.get_provider(&config).await?;

let account = get_account(
&config.account,
&config.accounts_file,
Expand All @@ -232,9 +243,24 @@ async fn run_async_command(
)
.await?;

let result = starknet_commands::deploy::deploy(deploy, &account, wait_config)
.await
.map_err(handle_starknet_command_error);
let fee_settings = fee_args
.clone()
.fee_token(fee_token)
.try_into_fee_settings(&provider, account.block_id())
.await?;

let result = starknet_commands::deploy::deploy(
deploy.class_hash,
&constructor_calldata,
deploy.salt,
deploy.unique,
fee_settings,
deploy.nonce,
&account,
wait_config,
)
.await
.map_err(handle_starknet_command_error);

print_command_result("deploy", &result, numbers_format, output_format)?;
print_block_explorer_link_if_allowed(
Expand All @@ -247,16 +273,24 @@ async fn run_async_command(
Ok(())
}

Commands::Call(call) => {
let provider = call.rpc.get_provider(&config).await?;
Commands::Call(Call {
contract_address,
function,
calldata,
block_id,
rpc,
}) => {
let provider = rpc.get_provider(&config).await?;

let block_id = get_block_id(&block_id)?;

let block_id = get_block_id(&call.block_id)?;
let entry_point_selector = get_selector_from_name(&function)
.context("Failed to convert entry point selector to FieldElement")?;

let result = starknet_commands::call::call(
call.contract_address,
get_selector_from_name(&call.function)
.context("Failed to convert entry point selector to FieldElement")?,
call.calldata,
contract_address,
entry_point_selector,
calldata,
&provider,
block_id.as_ref(),
)
Expand All @@ -268,21 +302,41 @@ async fn run_async_command(
}

Commands::Invoke(invoke) => {
let provider = invoke.rpc.get_provider(&config).await?;

invoke.validate()?;

let fee_token = invoke.token_from_version();

let Invoke {
contract_address,
function,
calldata,
fee_args,
rpc,
nonce,
..
} = invoke;

let provider = rpc.get_provider(&config).await?;

let account = get_account(
&config.account,
&config.accounts_file,
&provider,
config.keystore,
)
.await?;

let fee_args = fee_args.fee_token(fee_token);

let selector = get_selector_from_name(&function)
.context("Failed to convert entry point selector to FieldElement")?;

let result = starknet_commands::invoke::invoke(
invoke.clone(),
get_selector_from_name(&invoke.function)
.context("Failed to convert entry point selector to FieldElement")?,
contract_address,
calldata,
nonce,
fee_args,
selector,
&account,
wait_config,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/starknet_commands/account/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Add {
pub name: String,

/// Address of the account
#[clap(short, long, requires = "private_key_input")]
#[clap(long, requires = "private_key_input")]
pub address: Felt,

/// Type of the account
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/starknet_commands/account/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct Create {
pub add_profile: Option<String>,

/// Custom contract class hash of declared contract
#[clap(short, long, requires = "account_type")]
#[clap(long, requires = "account_type")]
pub class_hash: Option<Felt>,

#[clap(flatten)]
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/starknet_commands/account/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt::Display;
)]
pub struct List {
/// Display private keys
#[arg(short = 'p', long = "display-private-keys")]
#[arg(long = "display-private-keys")]
pub display_private_keys: bool,
}

Expand Down
8 changes: 4 additions & 4 deletions crates/sncast/src/starknet_commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ use starknet::providers::{JsonRpcClient, Provider};
#[command(about = "Call a contract instance on Starknet", long_about = None)]
pub struct Call {
/// Address of the called contract (hex)
#[clap(short = 'a', long)]
#[clap(long)]
pub contract_address: Felt,

/// Name of the contract function to be called
#[clap(short, long)]
#[clap(long)]
pub function: String,

/// Arguments of the called function (list of hex)
#[clap(short, long, value_delimiter = ' ', num_args = 1..)]
#[clap(long, value_delimiter = ' ', num_args = 1..)]
pub calldata: Vec<Felt>,

/// Block identifier on which call should be performed.
/// Possible values: pending, latest, block hash (0x prefixed string)
/// and block number (u64)
#[clap(short, long, default_value = "pending")]
#[clap(long, default_value = "pending")]
pub block_id: String,

#[clap(flatten)]
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/starknet_commands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::Arc;
#[command(about = "Declare a contract to starknet", long_about = None)]
pub struct Declare {
/// Contract name
#[clap(short = 'c', long = "contract-name")]
#[clap(long = "contract-name")]
pub contract: String,

#[clap(flatten)]
Expand Down
37 changes: 17 additions & 20 deletions crates/sncast/src/starknet_commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Deploy {
pub class_hash: Felt,

/// Calldata for the contract constructor
#[clap(short, long, value_delimiter = ' ', num_args = 1..)]
#[clap(long, value_delimiter = ' ', num_args = 1..)]
pub constructor_calldata: Vec<Felt>,

/// Salt for the address
Expand Down Expand Up @@ -61,29 +61,27 @@ impl_payable_transaction!(Deploy, token_not_supported_for_deployment,
DeployVersion::V3 => FeeToken::Strk
);

#[allow(clippy::ptr_arg, clippy::too_many_arguments)]
pub async fn deploy(
deploy: Deploy,
class_hash: Felt,
calldata: &Vec<Felt>,
salt: Option<Felt>,
unique: bool,
fee_settings: FeeSettings,
nonce: Option<Felt>,
account: &SingleOwnerAccount<&JsonRpcClient<HttpTransport>, LocalWallet>,
wait_config: WaitForTx,
) -> Result<DeployResponse, StarknetCommandError> {
let fee_settings = deploy
.fee_args
.clone()
.fee_token(deploy.token_from_version())
.try_into_fee_settings(account.provider(), account.block_id())
.await?;

let salt = extract_or_generate_salt(deploy.salt);
let factory = ContractFactory::new(deploy.class_hash, account);
let salt = extract_or_generate_salt(salt);
let factory = ContractFactory::new(class_hash, account);
let result = match fee_settings {
FeeSettings::Eth { max_fee } => {
let execution =
factory.deploy_v1(deploy.constructor_calldata.clone(), salt, deploy.unique);
let execution = factory.deploy_v1(calldata.clone(), salt, unique);
let execution = match max_fee {
None => execution,
Some(max_fee) => execution.max_fee(max_fee),
};
let execution = match deploy.nonce {
let execution = match nonce {
None => execution,
Some(nonce) => execution.nonce(nonce),
};
Expand All @@ -93,8 +91,7 @@ pub async fn deploy(
max_gas,
max_gas_unit_price,
} => {
let execution =
factory.deploy_v3(deploy.constructor_calldata.clone(), salt, deploy.unique);
let execution = factory.deploy_v3(calldata.clone(), salt, unique);

let execution = match max_gas {
None => execution,
Expand All @@ -104,7 +101,7 @@ pub async fn deploy(
None => execution,
Some(max_gas_unit_price) => execution.gas_price(max_gas_unit_price),
};
let execution = match deploy.nonce {
let execution = match nonce {
None => execution,
Some(nonce) => execution.nonce(nonce),
};
Expand All @@ -119,9 +116,9 @@ pub async fn deploy(
DeployResponse {
contract_address: get_udc_deployed_address(
salt,
deploy.class_hash,
&udc_uniqueness(deploy.unique, account.address()),
&deploy.constructor_calldata,
class_hash,
&udc_uniqueness(unique, account.address()),
calldata,
),
transaction_hash: result.transaction_hash,
},
Expand Down
22 changes: 10 additions & 12 deletions crates/sncast/src/starknet_commands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ use starknet::signers::LocalWallet;
#[command(about = "Invoke a contract on Starknet")]
pub struct Invoke {
/// Address of contract to invoke
#[clap(short = 'a', long)]
#[clap(long)]
pub contract_address: Felt,

/// Name of the function to invoke
#[clap(short, long)]
#[clap(long)]
pub function: String,

/// Calldata for the invoked function
#[clap(short, long, value_delimiter = ' ', num_args = 1..)]
#[clap(long, value_delimiter = ' ', num_args = 1..)]
pub calldata: Vec<Felt>,

#[clap(flatten)]
Expand Down Expand Up @@ -55,23 +55,21 @@ impl_payable_transaction!(Invoke, token_not_supported_for_invoke,
);

pub async fn invoke(
invoke: Invoke,
contract_address: Felt,
calldata: Vec<Felt>,
nonce: Option<Felt>,
fee_args: FeeArgs,
function_selector: Felt,
account: &SingleOwnerAccount<&JsonRpcClient<HttpTransport>, LocalWallet>,
wait_config: WaitForTx,
) -> Result<InvokeResponse, StarknetCommandError> {
let fee_args = invoke
.fee_args
.clone()
.fee_token(invoke.token_from_version());

let call = Call {
to: invoke.contract_address,
to: contract_address,
selector: function_selector,
calldata: invoke.calldata.clone(),
calldata,
};

execute_calls(account, vec![call], fee_args, invoke.nonce, wait_config).await
execute_calls(account, vec![call], fee_args, nonce, wait_config).await
}

pub async fn execute_calls(
Expand Down
Loading

0 comments on commit 26f846e

Please sign in to comment.