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 by running:
yarn generate
- Hardhat
- Foundry
It will automatically add the encrypted private key (DEPLOYER_PRIVATE_KEY_ENCRYPTED
) in your .env
file.
You will be prompted to enter a password which will be used to encrypt your private key. Make sure to remember this password as you'll need it for future deployments and account queries.
We are only storing the plain private key in memory, it's never stored in disk files for security reasons. Checkout the code here.
If you prefer to import your private key, run:
yarn account:import
You will get prompted to paste your private key and set the encryption password. It will store your encrypted private key in your .env
file.
It will automatically generate a new keystore with the name scaffold-eth-custom
.
You will be prompted to enter a password which will be used to encrypt your keystore. Make sure to remember this password as you'll need it for future deployments and account queries.
Now you'll need to open your packages/foundry/.env
file, and update ETH_KEYSTORE_ACCOUNT=scaffold-eth-custom
to start using the new keystore.
If you prefer to import your private key (you need to delete your scaffold-eth-custom
keystore first, if exists), run:
yarn account:import
You will get prompted to paste your private key and set the encryption password. It will store your encrypted private key in the scaffold-eth-custom
keystore file.
Also if you want to use your existing keystore account you can just update the ETH_KEYSTORE_ACCOUNT
in your .env
file, setting the name of your existing keystore.
You can check the configured (generated or imported) account and balances with:
yarn account
You will need to enter your password to decrypt the private key and view the account information and balances.
3. Deploy your smart contract(s)โ
- Hardhat
- Foundry
By default yarn deploy
will deploy all the contracts from your packages/hardhat/contracts
folder to the local network. You can change defaultNetwork
in:
packages/hardhat/hardhat.config.ts
By default yarn deploy
will deploy all the contracts from your packages/foundry/contracts
folder to the local network. You can change defaultNetwork
in:
packages/foundry/foundry.toml
3.1 Deploy specific contractsโ
To deploy specific contracts instead of all of them, you can follow these steps:
- Hardhat
- Foundry
- Add tags to the deploy scripts located in
packages/hardhat/deploy
. For example01_deploy_my_contract.ts
:
deployMyContract.tags = ["tagExample"];
- Run
yarn deploy --tags tagExample
to run all the scripts with the "tagExample" tag.
Each contract that you wish to deploy individually should have its own deployment script in
packages/foundry/script
. For example:DeployMyContract.s.sol
Run the specific deployment script using:
yarn deploy --file DeployMyContract.s.sol
If you don't specify the --file
parameter, the deployment will use the default Deploy.s.sol
file. This default file can be configured to deploy multiple contracts in a specific order when you run the yarn deploy
command.
3.2 Deploy to specific networksโ
Run the command below to deploy the smart contracts to the target network. Make sure to have your encryption password and some funds in your deployer account to pay for the transaction.
yarn deploy --network network_name
- Hardhat
- Foundry
You can also specify a tag:
yarn deploy --network sepolia --tags tagExample
Requires custom keystore setup, see generate accounts
You can also specify a file:
yarn deploy --network sepolia --file DeployMyContract.s.sol
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.