diff --git a/crates/cheatcodes/src/evm/fork.rs b/crates/cheatcodes/src/evm/fork.rs index e1d66538de721..83942cdbefa5f 100644 --- a/crates/cheatcodes/src/evm/fork.rs +++ b/crates/cheatcodes/src/evm/fork.rs @@ -1,4 +1,7 @@ -use crate::{Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt, Result, Vm::*}; +use crate::{ + json::json_value_to_token, Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt, + Result, Vm::*, +}; use alloy_dyn_abi::DynSolValue; use alloy_primitives::{B256, U256}; use alloy_provider::Provider; @@ -375,18 +378,25 @@ fn rpc_call(url: &str, method: &str, params: &str) -> Result { let result = foundry_common::block_on(provider.raw_request(method.to_string().into(), params_json)) .map_err(|err| fmt_err!("{method:?}: {err}"))?; + let result_as_tokens = convert_to_bytes( + &json_value_to_token(&result).map_err(|err| fmt_err!("failed to parse result: {err}"))?, + ); - let result_as_tokens = match crate::json::json_value_to_token(&result) - .map_err(|err| fmt_err!("failed to parse result: {err}"))? - { - // Convert fixed bytes to bytes to prevent encoding issues. + Ok(result_as_tokens.abi_encode()) +} + +/// Convert fixed bytes and address values to bytes in order to prevent encoding issues. +fn convert_to_bytes(token: &DynSolValue) -> DynSolValue { + match token { + // Convert fixed bytes to prevent encoding issues. // See: DynSolValue::FixedBytes(bytes, size) => { - DynSolValue::Bytes(bytes.as_slice()[..size].to_vec()) + DynSolValue::Bytes(bytes.as_slice()[..*size].to_vec()) } DynSolValue::Address(addr) => DynSolValue::Bytes(addr.to_vec()), - val => val, - }; - - Ok(result_as_tokens.abi_encode()) + // Convert tuple values to prevent encoding issues. + // See: + DynSolValue::Tuple(vals) => DynSolValue::Tuple(vals.iter().map(convert_to_bytes).collect()), + val => val.clone(), + } } diff --git a/testdata/default/cheats/Fork2.t.sol b/testdata/default/cheats/Fork2.t.sol index 7b6b4275990fc..d0703ce7fa6ce 100644 --- a/testdata/default/cheats/Fork2.t.sol +++ b/testdata/default/cheats/Fork2.t.sol @@ -234,6 +234,12 @@ contract ForkTest is DSTest { uint256 decodedResult = vm.parseUint(vm.toString(result)); assertGt(decodedResult, 20_000_000); } + + // + function testRpcTransactionByHash() public { + string memory param = string.concat('["0xe1a0fba63292976050b2fbf4379a1901691355ed138784b4e0d1854b4cf9193e"]'); + vm.rpc("sepolia", "eth_getTransactionByHash", param); + } } contract DummyContract {