foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.34k stars 1.76k forks source link

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

Closed MaxMustermann2 closed 6 days ago

MaxMustermann2 commented 1 week ago

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...
MaxMustermann2 commented 1 week ago

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

https://github.com/foundry-rs/foundry/blob/a79dfaed6fc6f88cda5f314a25d1b484d9d8c051/crates/cast/tests/cli/main.rs#L1116 but for json format

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

MaxMustermann2 commented 1 week ago

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 commented 1 week ago

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 commented 6 days ago

Thanks @MaxMustermann2! Merged

MaxMustermann2 commented 6 days ago

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.