Forks will be deprecated on March 31, 2025. Please migrate to Tenderly Virtual TestNets or contact our support for assistance with automatic migration.
Virtual TestNets
Frameworks
Foundry

Using Foundry

You can deploy smart contracts written in Solidity to a Virtual TestNet using Foundry. For debugging purposes, it’s recommended to verify smart contracts as you deploy them.

Running Foundry scripts

You can use forge commands, including running scripts.

Use the --slow flag when running Foundry scripts to prevent transaction batching. With this, a transaction is sent only after it’s preceding transaction is confirmed.

counter-script.bash
	forge script script/Counter.s.sol:CounterScript \
	--slow
  --verify \
  --verifier-url $TENDERLY_VERIFIER_URL \
  --rpc-url $TENDERLY_VIRTUAL_TESTNET_RPC_URL \
  --private-key $PRIVATE_KEY  \
  --etherscan-api-key $TENDERLY_ACCESS_TOKEN \
  --broadcast \

Verification

You can easily verify contracts deployed via forge create, forge verify-contract, and forge script, by providing a Tenderly-specific verifier URL.

Foundry verification uses Tenderly’s Etherscan-compliant verification API through --verifier-url:

   $TENDERLY_VIRTUAL_TESTNET_RPC/verify/etherscan

For extensive instructions on how to verify contracts using Foundry, see the following guide:

⚠️

By default, contracts verified on a TestNet are private. But depending on the visibility setting of the Public Explorer, the code may be visible externally. Before verifying a contract, check if the Public Explorer option is enabled and how you’ve set the verification visibility.

Enable verification

Append the unknown_chain configuration to foundry.toml, containing the following information:

  • key - Tenderly Access Token.
  • chain - the chain ID.
  • url - verifier URL, you get from RPC URL by appending /verify/etherscan.
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
 
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
 
[etherscan]
#                   Paste TENDERLY_ACCESS_KEY    Chain ID        Verifier URL: ${TENDERLY_VIRTUAL_TESTNET_RPC}/verify/etherscan
unknown_chain = { key = "${TENDERLY_ACCESS_KEY}", chain = 73571, url = "${TENDERLY_VIRTUAL_TESTNET_RPC_URL}/verify/etherscan" }

Deploy and verify contracts

To verify contracts during deployment, run forge create with --verify and --verifier-url flags as follows:

Fund the deployer address using the unlimited faucet before running the deployment script.

## pate to your TestNet RPC URL
TENDERLY_ACCESS_KEY=... # paste your access key here
TENDERLY_VIRTUAL_TESTNET_RPC=... # Virtual TestNet RPC URL
TENDERLY_VERIFIER_URL=$TENDERLY_VIRTUAL_TESTNET_RPC/verify/etherscan
 
PRIVATE_KEY=... # the deployer private key - if needed
 
forge create Counter \
--private-key $PRIVATE_KEY  \
--rpc-url $TENDERLY_VIRTUAL_TESTNET_RPC \
--etherscan-api-key $TENDERLY_ACCESS_KEY \
--broadcast \
--verify \
--verifier-url $TENDERLY_VERIFIER_URL

Verify existing contracts

To verify a contract that’s already deployed, use the forge verify-contract command:

COUNTER_ADDRESS=0x...
## pate to your TestNet RPC URL
TENDERLY_ACCESS_KEY=... # paste your access key here
TENDERLY_VIRTUAL_TESTNET_RPC=... # Virtual TestNet RPC URL
TENDERLY_VERIFIER_URL=$TENDERLY_VIRTUAL_TESTNET_RPC/verify/etherscan
 
forge verify-contract $COUNTER_ADDRESS  \
Counter \
--etherscan-api-key $TENDERLY_ACCESS_KEY \
--verifier-url $TENDERLY_VERIFIER_URL \
--watch