-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: JSON parsing cheatcodes #2153
Comments
returning it as abi-coded bytes is quite clever and probably the simplest solution to the problem that we do not know how to encode the json value. also providing types for transactions (receipts) etc. out of the box is probably a good idea. A more advanced solution would be to apply some preprocessing akin to rust macros that generates the necessary solidity glue code for custom data types first |
Where I think this would be interesting/helpful is in working with deployment artifacts. We could then write integration tests(!) and interact with both L1 and L2 nodes using the scripting functionality (AFAIUI). |
Bonus if the ABI-coder stuff for JSON can be re-used for #858 as well (similar thoughts on ABI encoding and passing that) |
There are a number of JSON path crates in Rust as well, e.g. https://docs.rs/jsonpath-rust/latest/jsonpath_rust/ I'd prefer we don't use C bindings as it might complicate our cross platform builds |
Note that part of the scope of this issue involves changing broadcast artifacts such that txs/receipts are saved with the correct types to facilitate type inference when reading JSON. For example, gas and nonce should be numbers instead of hex strings, etc. from @mattsse in #2217 (comment)
|
I think that this would be very useful for parsing addresses out of deployment artifacts for chainops. A network could be configured and then the corresponding addresses could be read from json files and then pulled into a script. This is one of the nicer features of hardhat scripts. |
As part of this workstream, I consider the ability to write JSON files as well. I am considering the following API:
Another way to go about it would be to use |
So keys would be something like |
@onbjerg you are right. It wouldn't work for arbitrary objects and paths, only if you want to add a value at the top level of the json object. Another idea is the following:
Example:
I don't love it, but I can't think of something better |
|
the core problem is that we need to find a way to map fields to (name+type), there's no way around this. one way to solve this would be with helper methods, for example: then you'd need one function per type: function serialize(MyStruct m, Serializer s) returns bytes {
SerliazeMap map = s.serialize_map();
map.serialize_entry("value", m.value);
...
return map.end()
}
with a custom json serializer we can then simplify this to function toJson(MyStruct m) {
return serialize(m, cheats.jsonSerializer());
} where |
Last week was quite eventful, so didn't have the time to work on this. @mattsse thanks for the spec. I will riff on that in code and report back.e |
I recently wrote a small Solidity library (quabi) using |
Component
Forge
Describe the feature you would like
The ability to easily parse JSON files within Solidity is very useful for things like reading deploy script outputs, config files, etc. Here is a proposed spec on how this should be implemented:
Cheatcodes
The
readFile
cheatcode already exists, and we just add aparseJson
cheatcode which takes a string of JSON andkey
, specified using the same syntax asjq
. It returns the data of each key as ABI-encodedbytes
.This implies
parseJson
will need to infer data types from the JSON to ABI-encode them appropriately. In particular, we need to distinguish between a hex string that'sbytes
(bytes get right-padded) vs. a hex string that's a number (numbers get left-padded). I think ethers.js has a convention for distinguishing these, we should check that convention, use the same one, document it, and make sure it's followed when the JSON output files from scripts are written. I think the convention is0x1234
for a number and[0x12, 0x34]
for a bytes but am not certain.forge-std
We'll also add new forge-std helpers to
Test.sol
, as shown below.Example Usage
The above would result in the following sample usage.
Additional context
No response
The text was updated successfully, but these errors were encountered: