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.

@MaxMustermann2 MaxMustermann2 deleted the feat/pretty-cast-storage branch November 18, 2024 15:09
rplusq pushed a commit to rplusq/foundry that referenced this pull request Nov 29, 2024
* feat(cast storage): allow ugly printing of layout

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.

* fix: remove default value from help text

The default value is accessible via `cast storage --help`

* fix(cast storage): provide output json path

* test(cast): add storage_layout_simple_json test

* fix(cast storage): use `--json` flag to ugly print

* fix(cast storage): include values in json mode

* fix(cast-storage): quiet compilation in all cases

* chore: cargo clippy

* use fixtures, assert JSON

* only quiet if JSON mode, avoid unnecessary warning (if you pass an API key you already expect to fetch remote, very likely default)

---------

Co-authored-by: zerosnacks <[email protected]>
Co-authored-by: zerosnacks <[email protected]>
@grandizzy grandizzy added T-bug Type: bug C-cast Command: cast labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cast Command: cast T-bug Type: bug
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

3 participants