Skip to content

Commit 13a428b

Browse files
committed
Always use data transformer unless --calldata is passed
1 parent 01b33f6 commit 13a428b

File tree

7 files changed

+40
-27
lines changed

7 files changed

+40
-27
lines changed

crates/data-transformer/src/reverse_transformer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use starknet_types_core::felt::Felt;
88

99
#[derive(Debug, thiserror::Error)]
1010
pub enum ReverseTransformError {
11-
#[error(r#"Function with selector "{0}" not found in ABI of the contract"#)]
11+
#[error(r#"Function with selector "{0:#x}" not found in ABI of the contract"#)]
1212
FunctionNotFound(Felt),
1313
#[error(transparent)]
1414
TransformationError(#[from] TransformationError),

crates/data-transformer/src/shared/extraction.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ pub fn extract_function_from_selector(
66
abi: &[AbiEntry],
77
searched_selector: Felt,
88
) -> Option<AbiFunction> {
9+
const CONSTRUCTOR_AS_SELECTOR: Felt = Felt::from_hex_unchecked(
10+
"0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
11+
);
12+
13+
search_for_function(abi, searched_selector)
14+
.or_else(|| (searched_selector == CONSTRUCTOR_AS_SELECTOR).then(default_constructor))
15+
}
16+
17+
fn default_constructor() -> AbiFunction {
18+
AbiFunction {
19+
name: "constructor".to_string(),
20+
inputs: vec![],
21+
outputs: vec![],
22+
state_mutability: StateMutability::View,
23+
}
24+
}
25+
26+
fn search_for_function(abi: &[AbiEntry], searched_selector: Felt) -> Option<AbiFunction> {
927
abi.iter().find_map(|entry| match entry {
1028
AbiEntry::Function(func) => {
1129
let selector = get_selector_from_name(&func.name).ok()?;
@@ -23,9 +41,7 @@ pub fn extract_function_from_selector(
2341
state_mutability: StateMutability::View,
2442
})
2543
}
26-
AbiEntry::Interface(interface) => {
27-
extract_function_from_selector(&interface.items, searched_selector)
28-
}
44+
AbiEntry::Interface(interface) => search_for_function(&interface.items, searched_selector),
2945
_ => None,
3046
})
3147
}

crates/data-transformer/src/transformer/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use starknet::core::types::contract::{AbiEntry, AbiFunction};
1414
/// Interpret `calldata` as a comma-separated series of expressions in Cairo syntax and serialize it
1515
pub fn transform(calldata: &str, abi: &[AbiEntry], function_selector: &Felt) -> Result<Vec<Felt>> {
1616
let function = extract_function_from_selector(abi, *function_selector).with_context(|| {
17-
format!(r#"Function with selector "{function_selector}" not found in ABI of the contract"#)
17+
format!(
18+
r#"Function with selector "{function_selector:#x}" not found in ABI of the contract"#
19+
)
1820
})?;
1921

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

2729
fn split_expressions(input: &str, db: &SimpleParserDatabase) -> Result<Vec<Expr>> {
30+
if input.is_empty() {
31+
return Ok(Vec::new());
32+
}
2833
// We need to convert our comma-separated string of expressions into something that is a valid
2934
// Cairo expression, so we can parse it.
3035
//

crates/data-transformer/tests/integration/transformer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async fn test_function_not_found() {
4343

4444
result.unwrap_err().assert_contains(
4545
format!(
46-
r#"Function with selector "{}" not found in ABI of the contract"#,
46+
r#"Function with selector "{:#x}" not found in ABI of the contract"#,
4747
get_selector_from_name(selector).unwrap()
4848
)
4949
.as_str(),

crates/sncast/src/main.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,7 @@ impl Arguments {
175175
contract_class: ContractClass,
176176
selector: &Felt,
177177
) -> Result<Vec<Felt>> {
178-
if let Some(arguments) = self.arguments {
179-
let ContractClass::Sierra(sierra_class) = contract_class else {
180-
bail!("Transformation of arguments is not available for Cairo Zero contracts")
181-
};
182-
183-
let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str())
184-
.context("Couldn't deserialize ABI received from network")?;
185-
186-
transform(&arguments, &abi, selector)
187-
} else if let Some(calldata) = self.calldata {
178+
if let Some(calldata) = self.calldata {
188179
calldata
189180
.iter()
190181
.map(|data| {
@@ -194,7 +185,14 @@ impl Arguments {
194185
})
195186
.collect()
196187
} else {
197-
Ok(vec![])
188+
let ContractClass::Sierra(sierra_class) = contract_class else {
189+
bail!("Transformation of arguments is not available for Cairo Zero contracts")
190+
};
191+
192+
let abi: Vec<AbiEntry> = serde_json::from_str(sierra_class.abi.as_str())
193+
.context("Couldn't deserialize ABI received from network")?;
194+
195+
transform(&self.arguments.unwrap_or_default(), &abi, selector)
198196
}
199197
}
200198
}

crates/sncast/tests/e2e/call.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,11 @@ fn test_wrong_function_name() {
141141
];
142142

143143
let snapbox = runner(&args);
144-
let output = snapbox.assert().success();
144+
let output = snapbox.assert().failure();
145145

146146
assert_stderr_contains(
147147
output,
148-
indoc! {r"
149-
command: call
150-
error: Requested entrypoint does not exist in the contract
151-
"},
148+
r#"Error: Function with selector "0x2924aec1f107eca35a5dc447cee68cc6985fe404841c9aad477adfcbe596d0a" not found in ABI of the contract"#,
152149
);
153150
}
154151

crates/sncast/tests/e2e/invoke.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,11 @@ fn test_wrong_function_name() {
216216
];
217217

218218
let snapbox = runner(&args);
219-
let output = snapbox.assert().success();
219+
let output = snapbox.assert().failure();
220220

221221
assert_stderr_contains(
222222
output,
223-
indoc! {"
224-
command: invoke
225-
error: Transaction execution error [..]0x454e545259504f494e545f4e4f545f464f554e44[..]
226-
"},
223+
r#"Error: Function with selector "0x2e0f845a8d0319c5c37d558023299beec2a0155d415f41cca140a09e6877c67" not found in ABI of the contract"#,
227224
);
228225
}
229226

0 commit comments

Comments
 (0)