Skip to content

Commit efc4406

Browse files
refactor(evm core types): change find_cast return type from String to ParamType (#415)
Co-authored-by: Jon-Becker <[email protected]>
1 parent e68c8f4 commit efc4406

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/common/src/ether/evm/core/types.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,18 @@ pub fn byte_size_to_type(byte_size: usize) -> (usize, Vec<String>) {
284284
}
285285

286286
/// Given a string (typically a line of decompiled source code), extract a type cast if one exists.
287-
// TODO: instead of returning a String, return a ParamType
287+
///
288288
/// ```
289289
/// use heimdall_common::ether::evm::core::types::find_cast;
290+
/// use ethers::abi::ParamType;
290291
///
291292
/// let line = "uint256(0x000011)";
292293
/// let (range, cast_type) = find_cast(line).expect("failed to find type cast");
293294
/// assert_eq!(range, 8..16);
294295
/// assert_eq!(&line[range], "0x000011");
295-
/// assert_eq!(cast_type, "uint256");
296+
/// assert_eq!(cast_type, ParamType::Uint(256));
296297
/// ```
297-
pub fn find_cast(line: &str) -> Result<(Range<usize>, String), Error> {
298+
pub fn find_cast(line: &str) -> Result<(Range<usize>, ParamType), Error> {
298299
// find the start of the cast
299300
match TYPE_CAST_REGEX.find(line).expect("Failed to find type cast.") {
300301
Some(m) => {
@@ -304,7 +305,7 @@ pub fn find_cast(line: &str) -> Result<(Range<usize>, String), Error> {
304305

305306
// find where the cast ends
306307
let range = find_balanced_encapsulator(&line[end..], ('(', ')'))?;
307-
Ok((end + range.start..end + range.end, cast_type))
308+
Ok((end + range.start..end + range.end, to_type(&cast_type)))
308309
}
309310
None => Err(Error::ParseError("failed to find type cast".to_string())),
310311
}

crates/decompile/src/utils/postprocessors/bitwise.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ pub fn simplify_casts(line: &str) -> String {
131131

132132
let cleaned_cast_pre = cleaned[0..cast_range.start - 1].to_string();
133133
let cleaned_cast_post = cleaned[cast_range.end + 1..].to_string();
134-
let cleaned_cast =
135-
cleaned[cast_range.start - 1..cast_range.end + 1].to_string().replace(&cast, "");
134+
let cleaned_cast = cleaned[cast_range.start - 1..cast_range.end + 1]
135+
.to_string()
136+
.replace(&cast.to_string(), "");
136137

137138
cleaned = format!("{cleaned_cast_pre}{cleaned_cast}{cleaned_cast_post}");
138139

0 commit comments

Comments
 (0)