Foundry Integration
Build a Foundry project from scratch, configure it for Cronos EVM, and deploy a sample contract with forge script.
Foundry is a Solidity-native toolchain: tests, deploy scripts and fixtures are all written in Solidity, and there is no Node.js dependency.
Prerequisites
A wallet private key: With a small amount of CRO to pay for gas fees. (For testnet TCRO, use the faucet at https://faucet.cronos.com/.)
An RPC endpoint: See details here.
A Cronos Explorer API key, see how to obtain it here.
Step 1. Install Foundry
Run the following in your terminal:
curl -L https://foundry.paradigm.xyz | bash && foundryupThis installs forge, cast, anvil and chisel.
Verify the installation with:
forge --versionStep 2. Create and initialize the project
forge init my-cronos-project
cd my-cronos-projectFoundry creates the following structure:
my-cronos-project/
├── src/ # Your contracts
├── test/ # Tests
├── script/ # Deployment scripts
└── foundry.toml # Project configurationStep 3. Write your contract
forge init creates a sample contract Counter.sol at src/. You can remove it and add your contracts to src/.
Step 3.1. Install Dependencies
For example, to add OpenZeppelin as a dependency:
Then add the remapping to foundry.toml:
Pin the versions. An unpinned forge install tracks the default branch, so a later forge update can silently change your bytecode — and bytecode that no longer matches is bytecode you cannot verify.
Step 4. Configure .env
Create a .env file in the project root:
Then load it before running any scripts:
Check how to get a Cronos Explorer API key here.
Never commit your .env file to version control. Add it to .gitignore to keep your private key safe.
.env holds your private key in plain text. For mainnet work, prefer Foundry's encrypted keystore (cast wallet import) and pass --account <name> instead of relying on PRIVATE_KEY.
Step 5. Configure Cronos
Set the compiler, both RPC aliases and both verification endpoints in foundry.toml , example config:
Step 6. Compile
Step 7. Test
forge init creates a sample test for Counter.sol at test/. To run it:
Step 8. Deploy
Foundry deploys with Solidity scripts, forge init creates a sample script at src/. To run it:
--rpc-url cronos_testnet refers to the alias defined in [rpc_endpoints]. Swap it for cronos_mainnet to deploy to mainnet. Drop --broadcast to simulate without sending anything.
Step 9. Verify
Run forge verify-contract after deployment:
For contracts that take parameters in contructor, add --constructor-args and cast abi-encode. For example, the verification command for an ERC20 token that takes name and symbol as constructor parameters will look like:
Last updated
Was this helpful?