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): Make serializeString always serialize values into a string, add serializeJson cheatcode #4602

Closed
Closed
Changes from 2 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
15 changes: 12 additions & 3 deletions evm/src/executor/inspector/cheatcodes/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ fn serialize_json(
) -> Result {
let parsed_value =
serde_json::from_str(value).unwrap_or_else(|_| Value::String(value.to_string()));
serialize_json_value(state, object_key, value_key, parsed_value)
}

fn serialize_json_value(
state: &mut Cheatcodes,
object_key: &str,
value_key: &str,
parsed_value: Value,
) -> Result<Bytes, Bytes> {
let json = if let Some(serialization) = state.serialized_jsons.get_mut(object_key) {
serialization.insert(value_key.to_string(), parsed_value);
serialization.clone()
Expand All @@ -319,8 +328,8 @@ fn serialize_json(
Ok(abi::encode(&[Token::String(stringified)]).into())
}

/// Converts an array to it's stringified version, adding the appropriate quotes around it's
/// ellements. This is to signify that the elements of the array are string themselves.
/// Converts an array to its stringified version, adding the appropriate quotes around its
/// elements. This is to signify that the elements of the array are string themselves.
fn array_str_to_str<T: UIfmt>(array: &Vec<T>) -> String {
format!(
"[{}]",
Expand Down Expand Up @@ -544,7 +553,7 @@ pub fn apply(state: &mut Cheatcodes, call: &HEVMCalls) -> Option<Result> {
serialize_json(state, &inner.0, &inner.1, &array_str_to_str(&inner.2))
}
HEVMCalls::SerializeString0(inner) => {
serialize_json(state, &inner.0, &inner.1, &inner.2.pretty())
serialize_json_value(state, &inner.0, &inner.1, Value::String(inner.2.to_string()))
}
HEVMCalls::SerializeString1(inner) => {
serialize_json(state, &inner.0, &inner.1, &array_str_to_str(&inner.2))
Expand Down