Skip to content

Always use data transformer unless --calldata is passed #3314

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

Draft
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion crates/data-transformer/src/reverse_transformer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet_types_core::felt::Felt;

#[derive(Debug, thiserror::Error)]
pub enum ReverseTransformError {
#[error(r#"Function with selector "{0}" not found in ABI of the contract"#)]
#[error(r#"Function with selector "{0:#x}" not found in ABI of the contract"#)]
FunctionNotFound(Felt),
#[error(transparent)]
TransformationError(#[from] TransformationError),
Expand Down
22 changes: 19 additions & 3 deletions crates/data-transformer/src/shared/extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ pub fn extract_function_from_selector(
abi: &[AbiEntry],
searched_selector: Felt,
) -> Option<AbiFunction> {
const CONSTRUCTOR_AS_SELECTOR: Felt = Felt::from_hex_unchecked(
"0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
);

search_for_function(abi, searched_selector)
.or_else(|| (searched_selector == CONSTRUCTOR_AS_SELECTOR).then(default_constructor))
}

fn default_constructor() -> AbiFunction {
AbiFunction {
name: "constructor".to_string(),
inputs: vec![],
outputs: vec![],
state_mutability: StateMutability::View,
}
}

fn search_for_function(abi: &[AbiEntry], searched_selector: Felt) -> Option<AbiFunction> {
abi.iter().find_map(|entry| match entry {
AbiEntry::Function(func) => {
let selector = get_selector_from_name(&func.name).ok()?;
Expand All @@ -23,9 +41,7 @@ pub fn extract_function_from_selector(
state_mutability: StateMutability::View,
})
}
AbiEntry::Interface(interface) => {
extract_function_from_selector(&interface.items, searched_selector)
}
AbiEntry::Interface(interface) => search_for_function(&interface.items, searched_selector),
_ => None,
})
}
7 changes: 6 additions & 1 deletion crates/data-transformer/src/transformer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use starknet::core::types::contract::{AbiEntry, AbiFunction};
/// Interpret `calldata` as a comma-separated series of expressions in Cairo syntax and serialize it
pub fn transform(calldata: &str, abi: &[AbiEntry], function_selector: &Felt) -> Result<Vec<Felt>> {
let function = extract_function_from_selector(abi, *function_selector).with_context(|| {
format!(r#"Function with selector "{function_selector}" not found in ABI of the contract"#)
format!(
r#"Function with selector "{function_selector:#x}" not found in ABI of the contract"#
)
})?;

let db = SimpleParserDatabase::default();
Expand All @@ -25,6 +27,9 @@ pub fn transform(calldata: &str, abi: &[AbiEntry], function_selector: &Felt) ->
}

fn split_expressions(input: &str, db: &SimpleParserDatabase) -> Result<Vec<Expr>> {
if input.is_empty() {
return Ok(Vec::new());
}
// We need to convert our comma-separated string of expressions into something that is a valid
// Cairo expression, so we can parse it.
//
Expand Down
2 changes: 1 addition & 1 deletion crates/data-transformer/tests/integration/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn test_function_not_found() {

result.unwrap_err().assert_contains(
format!(
r#"Function with selector "{}" not found in ABI of the contract"#,
r#"Function with selector "{:#x}" not found in ABI of the contract"#,
get_selector_from_name(selector).unwrap()
)
.as_str(),
Expand Down
20 changes: 9 additions & 11 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,7 @@ impl Arguments {
contract_class: ContractClass,
selector: &Felt,
) -> Result<Vec<Felt>> {
if let Some(arguments) = self.arguments {
let ContractClass::Sierra(sierra_class) = contract_class else {
bail!("Transformation of arguments is not available for Cairo Zero contracts")
};

let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str())
.context("Couldn't deserialize ABI received from network")?;

transform(&arguments, &abi, selector)
} else if let Some(calldata) = self.calldata {
if let Some(calldata) = self.calldata {
calldata
.iter()
.map(|data| {
Expand All @@ -194,7 +185,14 @@ impl Arguments {
})
.collect()
} else {
Ok(vec![])
let ContractClass::Sierra(sierra_class) = contract_class else {
bail!("Transformation of arguments is not available for Cairo Zero contracts")
};

let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str())
.context("Couldn't deserialize ABI received from network")?;

transform(&self.arguments.unwrap_or_default(), &abi, selector)
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions crates/sncast/tests/e2e/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,11 @@ fn test_wrong_function_name() {
];

let snapbox = runner(&args);
let output = snapbox.assert().success();
let output = snapbox.assert().failure();

assert_stderr_contains(
output,
indoc! {r"
command: call
error: Requested entrypoint does not exist in the contract
"},
r#"Error: Function with selector "0x2924aec1f107eca35a5dc447cee68cc6985fe404841c9aad477adfcbe596d0a" not found in ABI of the contract"#,
);
}

Expand Down
7 changes: 2 additions & 5 deletions crates/sncast/tests/e2e/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,11 @@ fn test_wrong_function_name() {
];

let snapbox = runner(&args);
let output = snapbox.assert().success();
let output = snapbox.assert().failure();

assert_stderr_contains(
output,
indoc! {"
command: invoke
error: Transaction execution error [..]0x454e545259504f494e545f4e4f545f464f554e44[..]
"},
r#"Error: Function with selector "0x2e0f845a8d0319c5c37d558023299beec2a0155d415f41cca140a09e6877c67" not found in ABI of the contract"#,
);
}

Expand Down
Loading