Skip to content

Commit 8be596d

Browse files
committed
Review changes: simplify get or insert, use common fmt
1 parent 398c5ab commit 8be596d

File tree

3 files changed

+10
-37
lines changed

3 files changed

+10
-37
lines changed

crates/cheatcodes/src/evm.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use alloy_rlp::Decodable;
1313
use alloy_sol_types::SolValue;
1414
use foundry_common::{
1515
fs::{read_json_file, write_json_file},
16-
slot_identifier::{SlotIdentifier, SlotInfo, format_value},
16+
slot_identifier::{SlotIdentifier, SlotInfo},
1717
};
1818
use foundry_evm_core::{
1919
ContextExt,
@@ -37,6 +37,7 @@ use std::{
3737
};
3838

3939
mod record_debug_step;
40+
use foundry_common::fmt::format_token_raw;
4041
use record_debug_step::{convert_call_trace_to_debug_step, flatten_call_trace};
4142
use serde::Serialize;
4243

@@ -189,8 +190,8 @@ impl Display for AccountStateDiffs {
189190
"@ {slot} ({}, {}): {} → {}",
190191
slot_info.label,
191192
slot_info.slot_type.dyn_sol_type,
192-
format_value(&decoded.previous_value),
193-
format_value(&decoded.new_value)
193+
format_token_raw(&decoded.previous_value),
194+
format_token_raw(&decoded.new_value)
194195
)?;
195196
} else {
196197
// Have slot info but no decoded values - show raw hex values
@@ -866,9 +867,7 @@ impl Cheatcode for startStateDiffRecordingCall {
866867
let Self {} = self;
867868
state.recorded_account_diffs_stack = Some(Default::default());
868869
// Enable mapping recording to track mapping slot accesses
869-
if state.mapping_slots.is_none() {
870-
state.mapping_slots = Some(Default::default());
871-
}
870+
state.mapping_slots.get_or_insert_default();
872871
Ok(Default::default())
873872
}
874873
}

crates/cheatcodes/src/evm/mapping.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use revm::{
1010
impl Cheatcode for startMappingRecordingCall {
1111
fn apply(&self, state: &mut Cheatcodes) -> Result {
1212
let Self {} = self;
13-
if state.mapping_slots.is_none() {
14-
state.mapping_slots = Some(Default::default());
15-
}
13+
state.mapping_slots.get_or_insert_default();
1614
Ok(Default::default())
1715
}
1816
}

crates/common/src/slot_identifier.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use crate::mapping_slots::MappingSlots;
77
use alloy_dyn_abi::{DynSolType, DynSolValue};
88
use alloy_primitives::{B256, U256, hex};
9+
use foundry_common_fmt::format_token_raw;
910
use foundry_compilers::artifacts::{Storage, StorageLayout, StorageType};
1011
use serde::Serialize;
1112
use std::{str::FromStr, sync::Arc};
@@ -175,8 +176,8 @@ impl Serialize for DecodedSlotValues {
175176
use serde::ser::SerializeStruct;
176177

177178
let mut state = serializer.serialize_struct("DecodedSlotValues", 2)?;
178-
state.serialize_field("previousValue", &format_value(&self.previous_value))?;
179-
state.serialize_field("newValue", &format_value(&self.new_value))?;
179+
state.serialize_field("previousValue", &format_token_raw(&self.previous_value))?;
180+
state.serialize_field("newValue", &format_token_raw(&self.new_value))?;
180181
state.end()
181182
}
182183
}
@@ -586,7 +587,7 @@ impl SlotIdentifier {
586587
&& let Ok(sol_type) = DynSolType::parse(key_type_label)
587588
&& let Ok(decoded) = sol_type.abi_decode(&key.0)
588589
{
589-
let decoded_key_str = format_value(&decoded);
590+
let decoded_key_str = format_token_raw(&decoded);
590591
decoded_keys.push(decoded_key_str.clone());
591592
label = format!("{label}[{decoded_key_str}]");
592593
} else {
@@ -663,31 +664,6 @@ fn get_array_base_indices(dyn_type: &DynSolType) -> String {
663664
}
664665
}
665666

666-
/// Formats a [`DynSolValue`] as a raw string without type information and only the value itself.
667-
pub fn format_value(value: &DynSolValue) -> String {
668-
match value {
669-
DynSolValue::Bool(b) => b.to_string(),
670-
DynSolValue::Int(i, _) => i.to_string(),
671-
DynSolValue::Uint(u, _) => u.to_string(),
672-
DynSolValue::FixedBytes(bytes, size) => hex::encode_prefixed(&bytes.0[..*size]),
673-
DynSolValue::Address(addr) => addr.to_string(),
674-
DynSolValue::Function(func) => func.as_address_and_selector().1.to_string(),
675-
DynSolValue::Bytes(bytes) => hex::encode_prefixed(bytes),
676-
DynSolValue::String(s) => s.clone(),
677-
DynSolValue::Array(values) | DynSolValue::FixedArray(values) => {
678-
let formatted: Vec<String> = values.iter().map(format_value).collect();
679-
format!("[{}]", formatted.join(", "))
680-
}
681-
DynSolValue::Tuple(values) => {
682-
let formatted: Vec<String> = values.iter().map(format_value).collect();
683-
format!("({})", formatted.join(", "))
684-
}
685-
DynSolValue::CustomStruct { name: _, prop_names: _, tuple } => {
686-
format_value(&DynSolValue::Tuple(tuple.clone()))
687-
}
688-
}
689-
}
690-
691667
/// Checks if a given type label represents a struct type.
692668
pub fn is_struct(s: &str) -> bool {
693669
s.starts_with("struct ")

0 commit comments

Comments
 (0)