Skip to content
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

Add declare-deploy logic #2791

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Cast

### Added
- `sncast deploy` can now deploy contracts by name instead of class hash. [Read more here](https://foundry-rs.github.io/starknet-foundry/starknet/deploy.html)
- `sncast declare-deploy` allows to declare and deploy contract at once. [Read more here](https://foundry-rs.github.io/starknet-foundry/starknet/declare-deploy.html)

## [0.35.1] - 2024-12-16

### Forge
Expand Down
2 changes: 1 addition & 1 deletion crates/sncast/src/helpers/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet::providers::Provider;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::str::FromStr;

#[derive(Args, Debug, Clone)]
#[derive(Args, Debug, Clone, Default)]
pub struct FeeArgs {
/// Token that transaction fee will be paid in
#[clap(long, value_parser = parse_fee_token)]
Expand Down
76 changes: 75 additions & 1 deletion crates/sncast/src/helpers/scarb_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use scarb_api::{
get_contracts_artifacts_and_source_sierra_paths,
Expand All @@ -7,6 +7,14 @@ use scarb_api::{
};
use scarb_ui::args::PackagesFilter;
use shared::{command::CommandExt, print::print_as_warning};
use starknet::{
core::types::{
contract::{CompiledClass, SierraClass},
BlockId, FlattenedSierraClass,
},
providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider, ProviderError},
};
use starknet_types_core::felt::Felt;
use std::collections::HashMap;
use std::env;
use std::str::FromStr;
Expand Down Expand Up @@ -188,6 +196,72 @@ pub fn build_and_load_artifacts(
}
}

pub fn read_manifest_and_build_artifacts(
package: &Option<String>,
json: bool,
profile: Option<&String>,
) -> Result<HashMap<String, StarknetContractArtifacts>> {
let manifest_path = assert_manifest_path_exists()?;
let package_metadata = get_package_metadata(&manifest_path, package)?;

let default_profile = "dev".to_string();
let profile = profile.unwrap_or(&default_profile);

let build_config = BuildConfig {
scarb_toml_path: manifest_path,
json,
profile: profile.to_string(),
};

build_and_load_artifacts(&package_metadata, &build_config, false)
.context("Failed to build contract")
}

pub struct CompiledContract {
pub class: FlattenedSierraClass,
pub sierra_class_hash: Felt,
pub casm_class_hash: Felt,
}

impl TryFrom<&StarknetContractArtifacts> for CompiledContract {
type Error = anyhow::Error;

fn try_from(artifacts: &StarknetContractArtifacts) -> Result<Self, Self::Error> {
let sierra_class = serde_json::from_str::<SierraClass>(&artifacts.sierra)
.context("Failed to parse Sierra artifact")?
.flatten()?;

let compiled_class = serde_json::from_str::<CompiledClass>(&artifacts.casm)
.context("Failed to parse CASM artifact")?;

let sierra_class_hash = sierra_class.class_hash();
let casm_class_hash = compiled_class.class_hash()?;

Ok(Self {
class: sierra_class,
sierra_class_hash,
casm_class_hash,
})
}
}

impl CompiledContract {
pub async fn is_declared(&self, provider: &JsonRpcClient<HttpTransport>) -> Result<bool> {
let block_id = BlockId::Tag(starknet::core::types::BlockTag::Pending);
let class_hash = self.sierra_class_hash;

let response = provider.get_class(block_id, class_hash).await;

match response {
Ok(_) => Ok(true),
Err(ProviderError::StarknetError(
starknet::core::types::StarknetError::ClassHashNotFound,
)) => Ok(false),
Err(other) => bail!(other),
}
}
}

#[cfg(test)]
mod tests {
use crate::helpers::scarb_utils::{get_package_metadata, get_scarb_metadata};
Expand Down
Loading
Loading