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

feat(etherscan directory output) #816

Merged
merged 1 commit into from
Mar 24, 2022

Conversation

jubos
Copy link
Contributor

@jubos jubos commented Feb 26, 2022

  • Inspired by etherscan.deth.net, this feature will expand a verified contract's
    source code paths to a directory for easy consumption with your favorite
    editor / IDE.

  • Tested via the following examples that test a few of the edge cases.

cargo run --bin cast -- etherscan-source 0x8d04a8c79cEB0889Bdd12acdF3Fa9D207eD3Ff63 -d blitmap
This results in a directory tree with all the source files of the contract.

cargo run --bin cast -- etherscan-source 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413 -d thedao
This results in a single file output.

cargo run --bin cast -- etherscan-source 0xb5c31a0e22cae98ac08233e512bd627885aa24e5 -d unverified
This returns an error since the following contract is not verified yet on Etherscan

Motivation

When learning about smart contracts it is a nice improvement to able the browse the contract code in your normal developer environment.

Solution

This feature parses the etherscan API source structure and writes it to directory.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

awesome!
just tested this and works as expected.
since this is primarily etherscan related, I'd suggest we add support for this directly into ethers-etherscan::ContractMetadata instead

perhaps some like fn into_source_tree|expand() -> Result<SourceTree> and the sourcetree (maybe a better name?) object would essentially hold path and content of all files?

and then SourceTree::write_to(dir: Path) would dump them into a dir

cast/src/lib.rs Outdated Show resolved Hide resolved
cast/src/lib.rs Outdated Show resolved Hide resolved
cast/src/lib.rs Outdated Show resolved Hide resolved
cast/src/lib.rs Outdated Show resolved Hide resolved
cast/src/lib.rs Outdated Show resolved Hide resolved
@jubos
Copy link
Contributor Author

jubos commented Feb 26, 2022

Thanks for the review. All good feedback. I will poke around the etherscan module in ethers-rs and come back.

@onbjerg onbjerg added the T-feature Type: feature label Mar 2, 2022
@gakonst
Copy link
Member

gakonst commented Mar 4, 2022

Hey @jubos thanks for taking a stab at this PR! Wonder what your plans are for getting this over the line? Happy to help with any specific code you don't have time to write

@jubos
Copy link
Contributor Author

jubos commented Mar 4, 2022

Planning to get it over the line this weekend.

@jubos
Copy link
Contributor Author

jubos commented Mar 5, 2022

I made this PR to ethers-rs: gakonst/ethers-rs#990 that incorporates a lot of @mattsse's suggestions. We can discuss over there and this PR will end up being very abbreviated.

@mattsse
Copy link
Member

mattsse commented Mar 5, 2022

I made this PR to ethers-rs: gakonst/ethers-rs#990 that incorporates a lot of @mattsse's suggestions. We can discuss over there and this PR will end up being very abbreviated.

Great! so we can drastically simplify this once merged?

@gakonst
Copy link
Member

gakonst commented Mar 7, 2022

@jubos merged on ethers!

@jubos
Copy link
Contributor Author

jubos commented Mar 19, 2022

Added this PR to centralize the error handling for unverified contracts. gakonst/ethers-rs#1065.

Once that lands, can simplify this and other callsites in foundry.

* Inspired by etherscan.deth.net, this feature will expand a verified contract's
  source code paths to a directory for easy consumption with your favorite
  editor / IDE.

* Tested via the following examples that test a few of the edge cases.
> cargo run --bin cast -- etherscan-source 0x8d04a8c79cEB0889Bdd12acdF3Fa9D207eD3Ff63 -d blitmap
This results in a directory tree with all the source files of the contract.

> cargo run --bin cast -- etherscan-source 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413 -d thedao
This results in a single file output.

> cargo run --bin cast -- etherscan-source 0xb5c31a0e22cae98ac08233e512bd627885aa24e5 -d output-dir
This returns an error since the following contract is not verified yet on Etherscan
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

lgtm

@gakonst
Copy link
Member

gakonst commented Mar 20, 2022

This is great @jubos! Love it.

Testing cargo run --bin cast -- etherscan-source 0x8d04a8c79cEB0889Bdd12acdF3Fa9D207eD3Ff63 -d blitmap it creates a blitmap/Blitmap/ directory, wonder if instead it should be just blitmap?

Similarly, testing cargo run --bin cast -- etherscan-source 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413 -d thedao produces thedao/DAO/DAO (notice that there's no .sol extension).

If the above 2 are unintended, do you mind addressing it so that there's no double directory and that all files have a .sol extension? Otherwise we're good for merge, and would encourage you to do a twitter ann of this as a feature :)

@jubos
Copy link
Contributor Author

jubos commented Mar 21, 2022

The double directory is intended since there can be multiple top level contracts exposed by the etherscan API. I haven't encountered an example of this in the wild, but the data structure is an array, so creating a top level container directory with the name of the contract seems to be correct. FWIW, deth.net has the same behavior.

On the .sol extension, this is just the pass through of the etherscan API result (which doesn't have an extension for the DAO). I will default it a .sol extension if an extension is not present as a quality of life improvement and update the commit.

@jubos
Copy link
Contributor Author

jubos commented Mar 24, 2022

Added the .sol change to ethers-rs: gakonst/ethers-rs#1077.

We could land this commit as is and then will get this improved behavior when the ether-rs commit lands.

@gakonst gakonst merged commit e881b75 into foundry-rs:master Mar 24, 2022
mattsse pushed a commit to mattsse/foundry that referenced this pull request Mar 28, 2022
* Inspired by etherscan.deth.net, this feature will expand a verified contract's
  source code paths to a directory for easy consumption with your favorite
  editor / IDE.

* Tested via the following examples that test a few of the edge cases.
> cargo run --bin cast -- etherscan-source 0x8d04a8c79cEB0889Bdd12acdF3Fa9D207eD3Ff63 -d blitmap
This results in a directory tree with all the source files of the contract.

> cargo run --bin cast -- etherscan-source 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413 -d thedao
This results in a single file output.

> cargo run --bin cast -- etherscan-source 0xb5c31a0e22cae98ac08233e512bd627885aa24e5 -d output-dir
This returns an error since the following contract is not verified yet on Etherscan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-feature Type: feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants