Deploy Your Smart Contracts
To deploy your smart contracts to a live network, there are a few things you need to adjust.
1. Configure your networkโ
Scaffold-ETH 2 comes with a selection of predefined networks. To add your custom network:
- Hardhat
- Foundry
Go to packages/hardhat/hardhat.config.ts
and add your network to the networks
object.
networks: {
// ... other networks
base: {
url: "https://mainnet.base.org",
accounts: [deployerPrivateKey]
},
}
Go to packages/foundry/foundry.toml
and add your network to the rpc_endpoints
object.
[rpc_endpoints]
...other chains
base = "https://mainnet.base.org"
Here are the Alchemy docs for information on specific networks.
You can also add your custom network by following the recipe here.
2. Generate a new account or add one to deploy the contract(s) from.โ
The deployer account is the account that will deploy your contracts. Additionally, the deployer account will be used to execute any function calls that are part of your deployment script.
You can generate a random account / private key or add your crypto wallet's private key.
To create a random account and add the DEPLOYER_PRIVATE_KEY
to the .env
file, run:
yarn generate
- Hardhat
- Foundry
If you prefer to manually set your own private key, you will need to add DEPLOYER_PRIVATE_KEY=yourWalletPrivateKey
to packages/hardhat/.env
.
If you prefer to manually set your own private key, you will need to add DEPLOYER_PRIVATE_KEY=yourWalletPrivateKey
to packages/foundry/.env
.
You can check the configured (generated or manually set) account and balances with:
yarn account
3. Deploy your smart contract(s)โ
By default yarn deploy
will deploy contract to the local network. You can change defaultNetwork
in:
- Hardhat
- Foundry
packages/hardhat/hardhat.config.ts
packages/foundry/foundry.toml
Run the command below to deploy the smart contract to the target network. Make sure to have some funds in your deployer account to pay for the transaction.
yarn deploy --network network_name
eg: yarn deploy --network sepolia
4. Verify your smart contractโ
You can verify your smart contract on Etherscan by running:
yarn verify --network network_name
eg: yarn verify --network sepolia
This command works in both Hardhat and Foundry, verifying all the deployed contracts. However, the verification method differs depending on the Solidity framework you're using...
- Hardhat
- Foundry
Hardhat uses etherscan-verify from hardhat-deploy.
Additionally, in Hardhat, there's an alternative method for contract verification. You can use hardhat-verify to verify your contracts, passing in the network name, contract address and constructor arguments (if any):
yarn hardhat-verify --network network_name contract_address "Constructor arg 1"`
If the chain you're using is not supported by any of the verifying methods, you can add new supported chains to your chosen method, either etherscan-verify or hardhat-verify.
Foundry uses VerifyAll.s.sol
script located in packages/foundry/script
.
Configuration of Third-Party Services for Production-Grade Apps.โ
By default, Scaffold-ETH 2 provides predefined API keys for popular services such as Alchemy and Etherscan. This allows you to begin developing and testing your applications more easily, avoiding the need to register for these services.
For production-grade applications, it's recommended to obtain your own API keys (to prevent rate limiting issues). You can configure these at:
- Hardhat
- Foundry
ALCHEMY_API_KEY
variable inpackages/hardhat/.env
andpackages/nextjs/.env.local
. You can create API keys from the Alchemy dashboard.ETHERSCAN_API_KEY
variable inpackages/hardhat/.env
using your generated API key. You can get your key here.
ALCHEMY_API_KEY
variable inpackages/nextjs/.env.local
. You can create API keys from the Alchemy dashboard.ETHERSCAN_API_KEY
variable inpackages/foundry/.env
using your generated API key. You can get your key here.
It's recommended to store envs for nextjs in Vercel/system env config for live apps and use .env.local for local testing.