Skip to content

Commit

Permalink
fix(forge): panic when logging negative numbers (gakonst#441)
Browse files Browse the repository at this point in the history
* Added workaround for LogNamedDecimalIntFilter not handling negative ints

* Added test for LogNamedDecimalIntFilter with negative values
  • Loading branch information
lattejed authored Jan 13, 2022
1 parent f8f595e commit 53984da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
17 changes: 12 additions & 5 deletions evm-adapters/src/sputnik/cheatcodes/cheatcode_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,17 @@ pub(crate) fn convert_log(log: Log) -> Option<String> {
LogNamedBytes32Filter(inner) => {
format!("{}: 0x{}", inner.key, hex::encode(inner.val))
}
LogNamedDecimalIntFilter(inner) => format!(
"{}: {:?}",
inner.key,
ethers::utils::parse_units(inner.val, inner.decimals.as_u32()).unwrap()
),
LogNamedDecimalIntFilter(inner) => {
let val: String = inner.val.to_string();
let neg = val.trim().starts_with('-');
let val = val.trim().trim_start_matches('-');
format!(
"{}: {}{:?}",
inner.key,
if neg { "-" } else { "" },
ethers::utils::parse_units(val, inner.decimals.as_u32()).unwrap()
)
}
LogNamedDecimalUintFilter(inner) => {
format!(
"{}: {}",
Expand Down Expand Up @@ -1701,6 +1707,7 @@ mod tests {
"addr: 0x2222222222222222222222222222222222222222",
"key: 0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d",
"key: 123000000000000000000",
"key: -123000000000000000000",
"key: 0.000000000000001234",
"key: 123",
"key: 1234",
Expand Down
1 change: 1 addition & 0 deletions evm-adapters/testdata/DebugLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ contract DebugLogs is DSTest {
emit log_named_address("addr", 0x2222222222222222222222222222222222222222);
emit log_named_bytes32("key", keccak256(abi.encodePacked("foo")));
emit log_named_decimal_int("key", 123, 18);
emit log_named_decimal_int("key", -123, 18);
emit log_named_decimal_uint("key", 1234, 18);
emit log_named_int("key", 123);
emit log_named_uint("key", 1234);
Expand Down

0 comments on commit 53984da

Please sign in to comment.