-
Notifications
You must be signed in to change notification settings - Fork 254
Add --no-abi flag to sncast declare, declare-from & deploy
#4269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
die-herdplatte
wants to merge
9
commits into
foundry-rs:master
Choose a base branch
from
die-herdplatte:declare-no-abi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a1f3717
Add `--no-abi` flag to `sncast` `declare`, `declare-from` & `deploy`
die-herdplatte c714dc4
Update CHANGELOG.md
die-herdplatte 88e644f
Fix hint generation for `sncast deploy` when `--no-abi` is passed to …
die-herdplatte fcf2075
Merge remote-tracking branch 'upstream/master' into declare-no-abi
die-herdplatte 399f5d3
Use local ABI instead of fetching when contract class is declared bef…
die-herdplatte 99bc434
Move `--no-abi` to command-specific instead of common args
die-herdplatte 8075cd6
Add mutual exclusivity comments to `declare-from` docs
die-herdplatte d022941
Add test case for `sncast deploy --contract-name --no-abi --construct…
die-herdplatte 979f2c9
`scarb fmt`
die-herdplatte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| use std::borrow::Borrow; | ||
| use std::num::{NonZeroU8, NonZeroU16}; | ||
| use std::str::FromStr; | ||
|
|
||
| use crate::starknet_commands::declare::declare; | ||
| use crate::starknet_commands::declare::declare_with_artifacts; | ||
| use crate::starknet_commands::declare_from::{ContractSource, DeclareFrom}; | ||
| use crate::starknet_commands::deploy::{DeployArguments, DeployCommonArgs}; | ||
| use crate::starknet_commands::get::Get; | ||
|
|
@@ -49,7 +50,7 @@ use sncast::{ | |
| use starknet_commands::ledger::{self, Ledger}; | ||
| use starknet_commands::verify::Verify; | ||
| use starknet_rust::core::types::ContractClass; | ||
| use starknet_rust::core::types::contract::{AbiEntry, SierraClass}; | ||
| use starknet_rust::core::types::contract::{AbiEntry, CompiledClass, SierraClass}; | ||
| use starknet_rust::core::utils::get_selector_from_name; | ||
| use starknet_rust::providers::Provider; | ||
| use starknet_types_core::felt::Felt; | ||
|
|
@@ -240,24 +241,34 @@ pub struct Arguments { | |
| pub arguments: Option<String>, | ||
| } | ||
|
|
||
| enum AbiOrContractClass<C> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this enum and add a function similar to: fn abi_from_contract_class(contract_class: &ContractClass) -> Result<Vec<AbiEntry>> {
let ContractClass::Sierra(sierra_class) = contract_class else {
bail!("Transformation of arguments is not available for Cairo Zero contracts")
};
serde_json::from_str(sierra_class.abi.as_str())
.context("Couldn't deserialize ABI received from network")
}then replace all occurrences of |
||
| Abi(Vec<AbiEntry>), | ||
| ContractClass(C), | ||
| } | ||
|
|
||
| impl Arguments { | ||
| fn try_into_calldata( | ||
| self, | ||
| contract_class: &ContractClass, | ||
| abi_or_contract_class: AbiOrContractClass<impl Borrow<ContractClass>>, | ||
| selector: &Felt, | ||
| ) -> Result<Vec<Felt>> { | ||
| if let Some(calldata) = self.calldata { | ||
| calldata_to_felts(&calldata) | ||
| } else { | ||
| let ContractClass::Sierra(sierra_class) = contract_class else { | ||
| bail!("Transformation of arguments is not available for Cairo Zero contracts") | ||
| }; | ||
| return calldata_to_felts(&calldata); | ||
| } | ||
|
|
||
| let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str()) | ||
| .context("Couldn't deserialize ABI received from network")?; | ||
| let abi = match abi_or_contract_class { | ||
| AbiOrContractClass::Abi(abi) => abi, | ||
| AbiOrContractClass::ContractClass(contract_class) => { | ||
| let ContractClass::Sierra(sierra_class) = contract_class.borrow() else { | ||
| bail!("Transformation of arguments is not available for Cairo Zero contracts") | ||
| }; | ||
|
|
||
| transform(&self.arguments.unwrap_or_default(), &abi, selector) | ||
| } | ||
| serde_json::from_str::<Vec<_>>(sierra_class.abi.as_str()) | ||
| .context("Couldn't deserialize ABI received from network")? | ||
| } | ||
| }; | ||
|
|
||
| transform(&self.arguments.unwrap_or_default(), &abi, selector) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -363,6 +374,7 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| declare.common.fee_args, | ||
| declare.common.dry_run_args, | ||
| declare.common.nonce, | ||
| declare.no_abi, | ||
| account, | ||
| &artifacts, | ||
| wait_config, | ||
|
|
@@ -399,6 +411,7 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| let network_flag = generate_network_flag(&rpc, &config); | ||
| Some(DeployCommandMessage::new( | ||
| &contract_definition.abi, | ||
| declare.no_abi, | ||
| response, | ||
| &config.account, | ||
| &config.accounts_file, | ||
|
|
@@ -441,6 +454,7 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| let result = with_account!(&account, |account| { | ||
| starknet_commands::declare_from::declare_from( | ||
| contract_source, | ||
| declare_from.no_abi, | ||
| &declare_from.common, | ||
| account, | ||
| wait_config, | ||
|
|
@@ -474,6 +488,10 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| } | ||
|
|
||
| Commands::Deploy(deploy) => { | ||
| if deploy.common.contract_identifier.contract_name.is_none() && deploy.no_abi { | ||
| bail!("`--no-abi` can only be used with `--contract-name`"); | ||
| } | ||
|
|
||
| let Deploy { | ||
| common: | ||
| DeployCommonArgs { | ||
|
|
@@ -486,15 +504,18 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| dry_run_args, | ||
| rpc, | ||
| mut nonce, | ||
| no_abi, | ||
| .. | ||
| } = deploy; | ||
|
|
||
| let provider = rpc.get_provider(&config, ui).await?; | ||
|
|
||
| let account = get_account(&config, &provider, &rpc, ui).await?; | ||
|
|
||
| let (class_hash, declare_response) = if let Some(class_hash) = identifier.class_hash { | ||
| (class_hash, None) | ||
| let (class_hash, declare_response, local_abi) = if let Some(class_hash) = | ||
| identifier.class_hash | ||
| { | ||
| (class_hash, None, None) | ||
| } else if let Some(contract_name) = identifier.contract_name { | ||
| let manifest_path = assert_manifest_path_exists()?; | ||
| let package_metadata = get_package_metadata(&manifest_path, &package)?; | ||
|
|
@@ -511,14 +532,26 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| ) | ||
| .expect("Failed to build contract"); | ||
|
|
||
| let contract_artifacts = artifacts | ||
| .get(&contract_name) | ||
| .expect("Failed to get contract artifacts"); | ||
| let contract_definition: SierraClass = | ||
| serde_json::from_str(&contract_artifacts.sierra) | ||
| .context("Failed to parse sierra artifact")?; | ||
| let casm_contract_definition: CompiledClass = | ||
| serde_json::from_str(&contract_artifacts.casm) | ||
| .context("Failed to parse casm artifact")?; | ||
| let local_abi = contract_definition.abi.clone(); | ||
|
|
||
| let declare_result = with_account!(&account, |account| { | ||
| declare( | ||
| contract_name, | ||
| fee_args, | ||
| dry_run_args, | ||
| declare_with_artifacts( | ||
| contract_definition, | ||
| casm_contract_definition, | ||
| &fee_args, | ||
| &dry_run_args, | ||
| nonce, | ||
| no_abi, | ||
| account, | ||
| &artifacts, | ||
| WaitForTx { | ||
| wait: true, | ||
| wait_params: wait_config.wait_params, | ||
|
|
@@ -537,10 +570,11 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| match declare_result { | ||
| Ok(DeclareResponse::AlreadyDeclared(AlreadyDeclaredResponse { | ||
| class_hash, | ||
| })) => (class_hash.into_(), None), | ||
| })) => (class_hash.into_(), None, Some(local_abi)), | ||
| Ok(DeclareResponse::Success(declare_transaction_response)) => ( | ||
| declare_transaction_response.class_hash.into_(), | ||
| Some(declare_transaction_response), | ||
| Some(local_abi), | ||
| ), | ||
| Ok(DeclareResponse::DryRun(_)) => { | ||
| unreachable!( | ||
|
|
@@ -565,10 +599,16 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| // safe to unwrap because "constructor" is a standardized name | ||
| let selector = get_selector_from_name("constructor").unwrap(); | ||
|
|
||
| let contract_class = get_contract_class(class_hash, &provider).await?; | ||
|
|
||
| let arguments: Arguments = arguments.into(); | ||
| let calldata = arguments.try_into_calldata(&contract_class, &selector)?; | ||
| let calldata = arguments.try_into_calldata( | ||
| match local_abi { | ||
| Some(abi) => AbiOrContractClass::Abi(abi), | ||
| None => AbiOrContractClass::ContractClass( | ||
| get_contract_class(class_hash, &provider).await?, | ||
| ), | ||
| }, | ||
| &selector, | ||
| )?; | ||
|
|
||
| let result = with_account!(&account, |account| { | ||
| starknet_commands::deploy::deploy( | ||
|
|
@@ -624,7 +664,10 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| let selector = get_selector_from_name(&function) | ||
| .context("Failed to convert entry point selector to FieldElement")?; | ||
|
|
||
| let calldata = arguments.try_into_calldata(&contract_class, &selector)?; | ||
| let calldata = arguments.try_into_calldata( | ||
| AbiOrContractClass::ContractClass(&contract_class), | ||
| &selector, | ||
| )?; | ||
|
|
||
| let result = starknet_commands::call::call( | ||
| contract_address, | ||
|
|
@@ -677,7 +720,10 @@ async fn run_async_command(cli: Cli, config: CastConfig, ui: &UI) -> Result<Exit | |
| let class_hash = get_class_hash_by_address(&provider, contract_address).await?; | ||
| let contract_class = get_contract_class(class_hash, &provider).await?; | ||
|
|
||
| let calldata = arguments.try_into_calldata(&contract_class, &selector)?; | ||
| let calldata = arguments.try_into_calldata( | ||
| AbiOrContractClass::ContractClass(&contract_class), | ||
| &selector, | ||
| )?; | ||
|
|
||
| let result = with_account!(&account, |account| { | ||
| starknet_commands::invoke::invoke( | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
--no-abiis used, the declared class hash points to a class with an empty ABI. However, the generated deploy suggestion can still include--arguments, which requiressncastto fetch the ABI from that class hash in order to transform Cairo-like arguments into calldata. That suggested command will therefore fail or be misleading for classes with constructor args.e.g. (constructor params
x: felt252, y: felt252):I think we should either omit the
--argumentshint when--no-abiis passed, or preferably switch the suggestion to raw--constructor-calldataplaceholders, since raw calldata does not depend on the ABI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar problem occurs when
--no-abiis used withdeploy --contract-name ... --arguments ...e.g.