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

fix(cheatcodes): coerce root values #6441

Merged
merged 3 commits into from
Nov 27, 2023
Merged

fix(cheatcodes): coerce root values #6441

merged 3 commits into from
Nov 27, 2023

Conversation

DaniPopes
Copy link
Member

Motivation

Fixes #6437

Don't know why we wouldn't coerce root values too, cc @odyslam as initial implementer

Solution

@DaniPopes DaniPopes changed the title Dani/6437 fix(cheatcodes): coerce root values Nov 27, 2023
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

not sure why, can't think of anything why this should not be the case

is this on top of #6439 ?

@DaniPopes
Copy link
Member Author

Initial implementation for reference:

fn parse_json(json_str: &str, key: &str, coerce: Option<DynSolType>) -> Result {
trace!(%json_str, %key, ?coerce, "parsing json");
let json =
serde_json::from_str(json_str).map_err(|err| fmt_err!("Failed to parse JSON: {err}"))?;
match key {
// Handle the special case of the root key. We want to return the entire JSON object
// in this case.
"." => {
let values = jsonpath_lib::select(&json, "$")?;
let res = parse_json_values(values, key)?;
// encode the bytes as the 'bytes' solidity type
let abi_encoded = encode_abi_values(res);
Ok(abi_encoded.into())
}
_ => {
let values = jsonpath_lib::select(&json, &canonicalize_json_key(key))?;
trace!(?values, "selected json values");
// values is an array of items. Depending on the JsonPath key, they
// can be many or a single item. An item can be a single value or
// an entire JSON object.
if let Some(coercion_type) = coerce {
ensure!(
values.iter().all(|value| !value.is_object()),
"You can only coerce values or arrays, not JSON objects. The key '{key}' returns an object",
);
ensure!(!values.is_empty(), "No matching value or array found for key {key}");
let to_string = |v: &Value| {
let mut s = v.to_string();
s.retain(|c: char| c != '"');
s
};
trace!(target : "forge::evm", ?values, "parsing values");
return if let Some(array) = values[0].as_array() {
parse::parse_array(array.iter().map(to_string), &coercion_type)
} else {
parse::parse(&to_string(values[0]), &coercion_type)
}
}
let res = parse_json_values(values, key)?;
// encode the bytes as the 'bytes' solidity type
let abi_encoded = encode_abi_values(res);
Ok(abi_encoded.into())
}
}
}

root key always encodes as bytes, i don't believe this worked before either

Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@DaniPopes DaniPopes merged commit f0166cc into master Nov 27, 2023
19 checks passed
@DaniPopes DaniPopes deleted the dani/6437 branch November 27, 2023 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vm.parseJsonAddressArray throwing error with no data.
3 participants