There are multiple different ways to deploy contracts in Foundry.
The most basic ones use forge create
and require you to pass some OPTIONS in-line when you run the command.
Here is an example of deploying a contract called NFT
on Sepolia
, with a private key, using alchemy, and the contract is verified on Etherscan on deployment after deployment.
forge create --private-key <PRIVATE_KEY> --rpc-url https://eth-sepolia.g.alchemy.com/v2/<API_KEY> --constructor-args "NonFun" "NFT" --verify --etherscan-api-key <ETHERSCAN_API_KEY> NFT
Now if you want a more customizable and user-friendly version, Foundry has got you covered. This is where Foundry scripting comes into play. You can write entire deploy scripts that pull in variables straight from your .env
file to redeploy more efficiently.
Here is an example of a script that deploys the exact same contract with the same variables:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Script, console2} from "forge-std/Script.sol";
import {NFT} from "../src/NFT.sol";
contract NFTScript is Script {
function run() public {
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
NFT nft = new NFT("NonFun", "NFT");
vm.stopBroadcast();
}
}
The command to deploy the contract is now shortened to:
forge script NFTScript --rpc-url https://eth-sepolia.g.alchemy.com/v2/<API_KEY> --verify --etherscan-api-key <ETHERSCAN_API_KEY> --broadcast
At present, I don't know of a way to pass the remaining in-line variables into the script, but I'm sure if one doesn't exist, Foundry will eventually add a way.
Of course, there are many more slightly different variations in the way you deploy your smart contracts, here is a guide straight from the Foundry book.