Solidity – Encountering Error When Trying to Run `forge create`: Troubleshooting Guide

forgefoundrysolidity

I am in the process of switching to foundry for contract development and testing. I have written some basic contracts, and am trying to use the alchemy RPC URL to deploy the contracts onto the testnet via MetaMask.

I have written tests which pass successfully, but when I try to run forge create ... I get this error:

Error:
(code: 3, message: execution reverted, data: Some(String("0x161b906f00000000000000000000000039463d1d009c32806a162df5d7e152d6b4709fda")))

…I haven't managed to find any recurrance of this error on forums etc, so was hoping for some help here.

Here's the create command I am running:

forge create --rpc-url https://eth-goerli.g.alchemy.com/v2/demo --private-key 0x1621e5a0ba99c1a1b10e31240c54241c60ce39f017f9d7fcd94c89012byb36b7 src/MyContractFactory.sol:MyContractFactory

Best Answer

You should include your Alchemy's API key into your RPC url, like stated in Alchemy's docs: https://eth-goerli.g.alchemy.com/v2/[YOUR-API-KEY]

However they do state that Goerli will be deprecated by January 2024, and they recommend using the Sepolia testnet instead.

Two other things I would consider in your command:

  1. Make sure you're CD'd into the directory you're working in.
  2. Write the name of the contract you're trying to write at the beginning of the statement, like so: forge create MyContractFactory.

Lastly, I really do hope the private key you just posted here is a dummy with no real funds, and in any case - I would store it in an .env file and use an argument instead.
(also relevant to API keys)


A suggested .env file would look like this, using the Sepolia testnet:

SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/[YOUR-API-KEY]
PRIVATE_KEY=0x1621e5a0ba99c1a1b10e31240c54241c60ce39f017f9d7fcd94c89012aca36b7

Don't forget to source .env in the terminal after saving the file.

Then, your create command would look like this:

forge create MyContractFactory --rpc-url $SEPOLIA_RPC_URL --private-key $PRIVATE_KEY