Skip to content

Commit c39ccf5

Browse files
committed
wip
1 parent 6823370 commit c39ccf5

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/spec/src/vm.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,19 @@ interface Vm {
18731873
pure
18741874
returns (bytes32[] memory);
18751875

1876+
/// Parses a string of JSON data and coerces it to type corresponding to `typeDescription`.
1877+
#[cheatcode(group = Json)]
1878+
function parseJsonType(string calldata json, string calldata typeDescription) external pure returns (bytes memory);
1879+
/// Parses a string of JSON data at `key` and coerces it to type corresponding to `typeDescription`.
1880+
#[cheatcode(group = Json)]
1881+
function parseJsonType(string calldata json, string calldata key, string calldata typeDescription) external pure returns (bytes memory);
1882+
/// Parses a string of JSON data at `key` and coerces it to type array corresponding to `typeDescription`.
1883+
#[cheatcode(group = Json)]
1884+
function parseJsonTypeArray(string calldata json, string calldata key, string calldata typeDescription)
1885+
external
1886+
pure
1887+
returns (bytes memory);
1888+
18761889
/// Returns an array of all the keys in a JSON object.
18771890
#[cheatcode(group = Json)]
18781891
function parseJsonKeys(string calldata json, string calldata key) external pure returns (string[] memory keys);

crates/cheatcodes/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ macro_rules! impl_from {
286286

287287
impl_from!(
288288
alloy_sol_types::Error,
289+
alloy_dyn_abi::Error,
289290
alloy_primitives::SignatureError,
290291
FsPathError,
291292
hex::FromHexError,

crates/cheatcodes/src/json.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Implementations of [`Json`](spec::Group::Json) cheatcodes.
22
33
use crate::{string, Cheatcode, Cheatcodes, Result, Vm::*};
4-
use alloy_dyn_abi::{DynSolType, DynSolValue, Resolver};
4+
use alloy_dyn_abi::{parser::RootType, DynSolType, DynSolValue, Resolver, Specifier};
55
use alloy_primitives::{hex, Address, B256, I256};
66
use alloy_sol_types::SolValue;
77
use foundry_common::fs;
8-
use foundry_compilers::resolver::parse;
98
use foundry_config::fs_permissions::FsAccessKind;
109
use serde_json::{Map, Value};
1110
use std::{borrow::Cow, collections::BTreeMap, fmt::Write};
@@ -136,6 +135,28 @@ impl Cheatcode for parseJsonBytes32ArrayCall {
136135
}
137136
}
138137

138+
impl Cheatcode for parseJsonType_0Call {
139+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
140+
let Self { json, typeDescription } = self;
141+
parse_json_coerce(json, "$", &resolve_type(&typeDescription)?).map(|v| v.abi_encode())
142+
}
143+
}
144+
145+
impl Cheatcode for parseJsonType_1Call {
146+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
147+
let Self { json, key, typeDescription } = self;
148+
parse_json_coerce(json, key, &resolve_type(&typeDescription)?).map(|v| v.abi_encode())
149+
}
150+
}
151+
152+
impl Cheatcode for parseJsonTypeArrayCall {
153+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
154+
let Self { json, key, typeDescription } = self;
155+
let ty = resolve_type(&typeDescription)?;
156+
parse_json_coerce(json, key, &DynSolType::Array(Box::new(ty))).map(|v| v.abi_encode())
157+
}
158+
}
159+
139160
impl Cheatcode for parseJsonKeysCall {
140161
fn apply(&self, _state: &mut Cheatcodes) -> Result {
141162
let Self { json, key } = self;
@@ -344,7 +365,7 @@ pub(super) fn parse_json_array(array: &[Value], ty: &DynSolType) -> Result<DynSo
344365
}
345366

346367
pub(super) fn parse_json_map(map: &Map<String, Value>, ty: &DynSolType) -> Result<DynSolValue> {
347-
let Some((name, fields, types)) = ty.as_custom_struct() else {
368+
let Some((_, fields, types)) = ty.as_custom_struct() else {
348369
bail!("expected struct type");
349370
};
350371

@@ -550,6 +571,15 @@ where
550571
s
551572
}
552573

574+
fn resolve_type(type_description: &str) -> Result<DynSolType> {
575+
if let Ok(ty) = DynSolType::parse(type_description) {
576+
return Ok(ty);
577+
};
578+
579+
let struct_schema: StructSchema = serde_json::from_str(type_description)?;
580+
Ok(struct_schema.resolve()?)
581+
}
582+
553583
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
554584
struct StructSchema {
555585
resolver: Resolver,

testdata/cheats/Vm.sol

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)