Using Foundry
Foundry 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 (https://book.getfoundry.sh/reference/forge/forge-verify-contract)
Get Started
1) Install Foundry
https://book.getfoundry.sh/getting-started/installation
2) Create a project
To start a new project with Foundry, use forge init
:
Let's check out what forge
generated for us:
Config File
foundry.toml
Deploying
Forge can deploy smart contracts to a given network with the forge create
command.
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:
KCC Testnet Example
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
.
Verifying a pre-existing contract
If you are verifying an already deployed contract, read on.
You can verify a contract on Etherscan, Sourcify or Blockscout with the forge verify-contract
command.
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
compiler version 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.
the number of optimizations, if the Solidity optimizer was activated. It is auto-detected if not specified.
the chain ID
Let's say you want to verify TestVerify
(see above). You set the number of optimizations 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.
Here's how to verify it:
It is recommended to use the --watch
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 forge verify-check
command:
Tips:
Setting the
—verifier=blockscout
flag allows you to not specify any Api key.While specifying
--verifier-url
flag omit the final slash (e.g.,--verifier-url=
https://scan-testnet.kcc.network/api
). 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 https://github.com/foundry-rs/foundry/blob/master/config/README.md).
Use Cast's
abi-encode
to ABI-encode arguments.In this example, we ran
cast abi-encode "
constructor(string,uint256)"
"counter-verify" 123 to ABI-encode the arguments.KCC Mainnet Scan
--verifier-url=
https://scan.kcc.io/api
Troubleshooting
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:
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 compiler version.
Last updated