Skip to content

Commit 84ea6a2

Browse files
Added sncast_std recommended version check (#1510)
commit-id:5b848aa5
1 parent f0275a9 commit 84ea6a2

File tree

8 files changed

+92
-3
lines changed

8 files changed

+92
-3
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sncast/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ num-traits.workspace = true
3838
cairo-felt.workspace = true
3939
cairo-vm.workspace = true
4040
blockifier.workspace = true
41+
semver.workspace = true
42+
console.workspace = true
4143

4244
[dev-dependencies]
4345
ctor.workspace = true

crates/sncast/src/response/print.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use anyhow::Result;
1+
use anyhow::{Error, Result};
2+
use console::style;
23
use serde_json::Value;
34
use starknet::core::types::FieldElement;
45
use std::{collections::HashMap, fmt::Display, str::FromStr};
@@ -163,6 +164,11 @@ fn apply_numbers_formatting(value: OutputValue, formatting: NumbersFormat) -> Ou
163164
}
164165
}
165166

167+
pub fn print_as_warning(error: &Error) {
168+
let warning_tag = style("Warning:").color256(11);
169+
println!("{warning_tag} {error}");
170+
}
171+
166172
#[cfg(test)]
167173
mod tests {
168174
use serde_json::{Map, Value};

crates/sncast/src/starknet_commands/script.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44
use crate::starknet_commands::declare::BuildConfig;
55
use crate::starknet_commands::{call, declare, deploy, invoke};
66
use crate::{get_account, get_nonce, WaitForTx};
7-
use anyhow::{ensure, Context, Result};
7+
use anyhow::{anyhow, ensure, Context, Result};
88
use blockifier::execution::common_hints::ExecutionMode;
99
use blockifier::execution::deprecated_syscalls::DeprecatedSyscallSelector;
1010
use blockifier::execution::entry_point::{
@@ -35,10 +35,13 @@ use runtime::{
3535
CheatcodeHandlingResult, EnhancedHintError, ExtendedRuntime, ExtensionLogic, StarknetRuntime,
3636
SyscallHandlingResult,
3737
};
38-
use scarb_api::ScarbCommand;
38+
use scarb_api::{package_matches_version_requirement, ScarbCommand};
39+
use scarb_metadata::Metadata;
40+
use semver::{Comparator, Op, Version, VersionReq};
3941
use sncast::helpers::scarb_utils::{
4042
get_package_metadata, get_scarb_manifest, get_scarb_metadata_with_deps, CastConfig,
4143
};
44+
use sncast::response::print::print_as_warning;
4245
use sncast::response::structs::ScriptResponse;
4346
use starknet::accounts::Account;
4447
use starknet::core::types::{BlockId, BlockTag::Pending, FieldElement};
@@ -352,6 +355,32 @@ pub fn run(
352355
}
353356
}
354357

358+
fn sncast_std_version_requirement() -> VersionReq {
359+
let version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
360+
let comparator = Comparator {
361+
op: Op::Exact,
362+
major: version.major,
363+
minor: Some(version.minor),
364+
patch: Some(version.patch),
365+
pre: version.pre,
366+
};
367+
VersionReq {
368+
comparators: vec![comparator],
369+
}
370+
}
371+
372+
fn warn_if_sncast_std_not_compatible(scarb_metadata: &Metadata) -> Result<()> {
373+
let sncast_std_version_requirement = sncast_std_version_requirement();
374+
if !package_matches_version_requirement(
375+
scarb_metadata,
376+
"sncast_std",
377+
&sncast_std_version_requirement,
378+
)? {
379+
print_as_warning(&anyhow!("Package sncast_std version does not meet the recommended version requirement {sncast_std_version_requirement}, it might result in unexpected behaviour"));
380+
}
381+
Ok(())
382+
}
383+
355384
fn compile_script(path_to_scarb_toml: Option<Utf8PathBuf>) -> Result<Utf8PathBuf> {
356385
let scripts_manifest_path = path_to_scarb_toml.unwrap_or_else(|| {
357386
get_scarb_manifest()
@@ -370,6 +399,9 @@ fn compile_script(path_to_scarb_toml: Option<Utf8PathBuf>) -> Result<Utf8PathBuf
370399
.context("failed to compile script with scarb")?;
371400

372401
let metadata = get_scarb_metadata_with_deps(&scripts_manifest_path)?;
402+
403+
warn_if_sncast_std_not_compatible(&metadata)?;
404+
373405
let package_metadata = get_package_metadata(&metadata, &scripts_manifest_path)?;
374406

375407
let filename = format!("{}.sierra.json", package_metadata.name);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "old_sncast_std"
3+
version = "0.1.0"
4+
5+
[dependencies]
6+
starknet = ">=2.3.0"
7+
sncast_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.13.1" }
8+
9+
[lib]
10+
sierra = true
11+
casm = true
12+
13+
[[target.starknet-contract]]
14+
sierra = true
15+
casm = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod map_script;
2+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use sncast_std::{
2+
declare, deploy, invoke, call, DeclareResult, DeployResult, InvokeResult, CallResult, get_nonce
3+
};
4+
5+
fn main() {}

crates/sncast/tests/e2e/script/general.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,28 @@ async fn test_fail_when_using_starknet_syscall() {
101101
error: Got an exception while executing a hint: Custom Hint Error: Starknet syscalls are not supported
102102
"});
103103
}
104+
105+
#[tokio::test]
106+
async fn test_incompatible_sncast_std_version() {
107+
let script_name = "map_script";
108+
let args = vec![
109+
"--accounts-file",
110+
"../../../accounts/accounts.json",
111+
"--account",
112+
"user4",
113+
"--url",
114+
URL,
115+
"script",
116+
&script_name,
117+
];
118+
119+
let snapbox = Command::new(cargo_bin!("sncast"))
120+
.current_dir(SCRIPTS_DIR.to_owned() + "/old_sncast_std/scripts")
121+
.args(args);
122+
123+
snapbox.assert().success().stdout_matches(indoc! {r"
124+
...
125+
Warning: Package sncast_std version does not meet the recommended version requirement =0.13.1, it might result in unexpected behaviour
126+
...
127+
"});
128+
}

0 commit comments

Comments
 (0)