Skip to content
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

fix(cast storage): respect --json for layout #9332

Merged
merged 15 commits into from
Nov 18, 2024

Conversation

MaxMustermann2
Copy link
Contributor

@MaxMustermann2 MaxMustermann2 commented Nov 15, 2024

Motivation

Prior to this change, cast storage $ADDRESS --rpc-url $RPC_URL --etherscan-api-key $ETHERSCAN_API_KEY always provided a prettified output, ignoring the --json flag.

Solution

This change makes cast storage respect the --json flag. If --slot is omitted and this flag is provided, the storage layout JSON is printed to stdout, instead of the pretty layout. The values are printed as a separate list within the JSON.

Example

This example is included as a test named storage_layout_simple_json.

cast --json storage 0x13b0D85CcB8bf860b6b79AF3029fCA081AE9beF2 \
    --rpc-url "https://eth-mainnet.g.alchemy.com/v2/"$ALCHEMY_API_KEY \
    --etherscan-api-key $ETHERSCAN_API_KEY > stdout.txt 2> stderr.txt

stdout.txt

{
  "storage": [
    {
      "astId": 7,
      "contract": "contracts/Create2Deployer.sol:Create2Deployer",
      "label": "_owner",
      "offset": 0,
      "slot": "0",
      "type": "t_address"
    },
    {
      "astId": 122,
      "contract": "contracts/Create2Deployer.sol:Create2Deployer",
      "label": "_paused",
      "offset": 20,
      "slot": "0",
      "type": "t_bool"
    }
  ],
  "types": {
    "t_address": {
      "encoding": "inplace",
      "label": "address",
      "numberOfBytes": "20"
    },
    "t_bool": {
      "encoding": "inplace",
      "label": "bool",
      "numberOfBytes": "1"
    }
  },
  "values": [
    "0x0000000000000000000000000000000000000000000000000000000000000000",
    "0x0000000000000000000000000000000000000000000000000000000000000000"
  ]
}

stderr.txt

Warning: No matching artifacts found, fetching source code from Etherscan...

Prior to this change, `cast storage $ADDRESS --rpc-url $RPC_URL
--etherscan-api-key $ETHERSCAN_API_KEY` always provided a prettified
output.

This change adds a `--pretty` flag to `cast storage` which defaults to
`true` thus retaining backwards compatibility. Passing `--pretty=false`
to `cast storage` results in the json output of the storage layout being
produced instead.
@MaxMustermann2 MaxMustermann2 changed the title feat(cast storage): allow ugly printing of layout feat(cast storage): allow exporting the JSON layout Nov 15, 2024
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you! left some comments, please also add a test similar with

casttest!(storage_layout_simple, |_prj, cmd| {
but for json format

crates/cast/bin/cmd/storage.rs Outdated Show resolved Hide resolved
crates/cast/bin/cmd/storage.rs Outdated Show resolved Hide resolved
@MaxMustermann2
Copy link
Contributor Author

thank you! left some comments, please also add a test similar with

casttest!(storage_layout_simple, |_prj, cmd| {

but for json format

@grandizzy Thanks for your review. Updated with your suggestions.

@MaxMustermann2 MaxMustermann2 changed the title feat(cast storage): allow exporting the JSON layout fix(cast storage): respect --json for layout Nov 16, 2024
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but checking the test you just added there's only the layout printed because of the early return on json format (and missing some important fields as the storage values), I guess we want to include them as well, or only the layout?

@MaxMustermann2
Copy link
Contributor Author

Looks good, but checking the test you just added there's only the layout printed because of the early return on json format (and missing some important fields as the storage values), I guess we want to include them as well, or only the layout?

I added a values entry to the JSON. Let me know if that works; thanks!

@MaxMustermann2
Copy link
Contributor Author

MaxMustermann2 commented Nov 16, 2024

The nextest fails because the Alchemy API key is rate limited.

image

I have fixed the clippy workflow.

cargo clippy --workspace \
    --all-targets \
    --all-features > /dev/null 2>/dev/null && echo $?
0

@zerosnacks zerosnacks self-assigned this Nov 18, 2024
Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation LGTM, made some minor modifications in regards to test as defaulting to quiet if shell is in JSON mode

Pending other reviewers cc @grandizzy

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

@zerosnacks zerosnacks merged commit d275a49 into foundry-rs:master Nov 18, 2024
21 checks passed
@zerosnacks
Copy link
Member

Thanks @MaxMustermann2! Merged

@MaxMustermann2
Copy link
Contributor Author

Thank you both @grandizzy and @zerosnacks for your feedback and the commit. This will help with a Github workflow that I am working on; much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

3 participants