Using Foundry
Last updated
Last updated
is a smart contract development toolchain. Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command line and via Solidity scripts.
Forge is a command-line tool that ships with Foundry. Forge tests, builds, and deploys your smart contracts. Forge supports contract verification out of the box ()
To start a new project with Foundry, use :
Let's check out what forge
generated for us:
foundry.toml
Forge can deploy only one contract at a time.
To deploy a contract, you must provide a RPC URL (env: ETH_RPC_URL
) and the private key of the account that will deploy the contract.
Deploy the MyContract
to the network:
Solidity files may contain multiple contracts. :MyContract
above specifies which contract to deploy from the src/MyContract.sol
file.
Use the --constructor-args
flag to pass arguments to the constructor:
Ddeploy the TestVerify
to the KCC Testnet:
Additionally, we can tell Forge to verify our contract on Etherscan, Sourcify or Blockscout, if the network is supported, by passing --verify
.
If you are verifying an already deployed contract, read on.
You must provide:
the contract address
the contract name or the path to the contract <path>:<contractname>
Moreover, you may need to provide:
the constructor arguments in the ABI-encoded format, if there are any
the number of optimizations, if the Solidity optimizer was activated. It is auto-detected if not specified.
the chain ID
Here's how to verify it:
Setting the —verifier=blockscout
flag allows you to not specify any Api key.
In this example, we ran cast abi-encode "
constructor(string,uint256)"
"counter-verify" 123 to ABI-encode the arguments.
EIP-1559 not activated
EIP-1559 is not supported or not activated on the RPC server. Pass the --legacy
flag to use legacy transactions instead of the EIP-1559 ones. If you do development in a local environment, you can use Hardhat instead of Ganache.
Compiler version commit for verify
If you want to check the exact commit you are running locally, try: ~/.svm/0.x.y/solc-0.x.y --version
where x
and y
are major and minor version numbers respectively. The output of this will be something like:
Forge can deploy smart contracts to a given network with the command.
You can verify a contract on Etherscan, Sourcify or Blockscout with the command.
used for build, with 8 hex digits from the commit version prefix (the commit will usually not be a nightly build). It is auto-detected if not specified.
Let's say you want to verify TestVerify
(see above). You set the to 1 million, compiled it with v0.8.13, and deployed it, as shown above, to the KCC Testnet (chain ID: 322). Note that --num-of-optimizations
will default to 0 if not set on verification, while it defaults to 200 if not set on deployment, so make sure you pass --num-of-optimizations 200
if you left the default compilation settings.
It is recommended to use the flag along with verify-contract
command in order to poll for the verification result.
If the --watch
flag was not supplied, you can check the verification status with the command:
While specifying --verifier-url
flag omit the final slash (e.g., --verifier-url=
). Otherwise, you will encounter an “Failed to deserialize response” error.
You can specify most configuration options (e.g., evm version, disabling optimizations) via the usual Forge configuration (see ).
Use Cast's to ABI-encode arguments.
KCC Mainnet Scan --verifier-url=
Note: You cannot just paste the entire string "0.8.12+commit.f00d7308.Darwin.appleclang" as the argument for the compiler-version. But you can use the 8 hex digits of the commit to look up exactly what you should copy and paste from .