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.17k stars 1.7k forks source link

feat(cast) add deploydata method #8973

Open pawurb opened 2 days ago

pawurb commented 2 days ago

Component

Cast

Describe the feature you would like

Hi. One way to deploy a contract with Alloy is to use an artifact. It requires an artifact file by compiling the contracts locally. Other methods require manually embedding the API and bytecode.

Maybe I've overlooked something but I did not find a simple way to get bytecode without local compilation or forge inspect that also require local setup. Etherscan shows bytecode (as Contract Creation Code) in the UI but I could not find an API to get it directly. eth_getCode returns the deployed bytecode.

I want to propose adding cast deploydata <address> method. It would produce a file which contains the contracts ABI and bytecode fetched from the etherscan API in this format. This data is all that's necessary to deploy a contract with Alloy sol! macro. I've created a POC here: https://github.com/pawurb/alloy_deploydata .

ABI fetching is already implemented. Tx where a contract was created can be fetched from etherscan like this:

https://api.etherscan.io/api?module=contract&action=getcontractcreation&contractaddresses=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&apikey=API_KEY

Returning:

{"status":"1","message":"OK","result":[{"contractAddress":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","contractCreator":"0x4f26ffbe5f04ed43630fdc30a87638d53d0b0876","txHash":"0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3"}]}

Later we can call:

https://api.etherscan.io/api?module=proxy&action=eth_getTransactionByHash&txhash=0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3&apikey=API_KEY

If in the result to is null it means that input is the bytecode we need.

If result is not null means that contract was deployed with some factory pattern or create2. In the case it's still possible to get the bytecode with debug_traceTransaction/callTracer. But it would require an rpc and could work as a fallback.

This feature would allow users to easily deploy and test contracts without compiling them locally.

We could also add cast bytecode which would return contract's bytecode.

Let me know if you'd accept this feature and I can look into creating a PR.

Additional context

No response

estherbreath commented 2 days ago

Hi @pawurb I am a smart contract and full stack developer. can I please work on this issue?