SolidityIn

Deploying to a testnet

Lesson 26 of 40 · Practice

Before spending real money, deploy to a testnet. Sepolia is the standard Ethereum testnet for apps. It works like mainnet, but its ETH is free and has no value. You get test ETH from a faucet. Faucets change often, so search for a current Sepolia faucet.

The easiest first deployment is Remix. Compile your contract, open the Deploy tab, choose Injected Provider to connect MetaMask, switch the wallet to Sepolia and press Deploy. Confirm the transaction in your wallet. Remix then shows the deployed contract with a button for every function.

For real projects, deploy with a script so the process is repeatable. In Foundry a script is a contract that calls vm.startBroadcast and then creates your contract. Never paste a private key into code or commit it to Git. Use an environment variable or a keystore, and a fresh wallet that holds only test funds.

After deploying, verify the source code on a block explorer such as Etherscan. Verification publishes your code and lets anyone read and call the contract in the browser, which builds trust. Foundry can do it with forge verify-contract, or with the --verify flag on forge script.

Before mainnet, test thoroughly, get a review or an audit, and consider adding a pause switch and a plan for what to do if something goes wrong. Mainnet deployments cannot be undone, so double-check constructor arguments and the network before you press the button.

// script/Deploy.s.sol
import "forge-std/Script.sol";
import "../src/Counter.sol";

contract Deploy is Script {
    function run() external {
        uint256 key = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(key);
        Counter c = new Counter();
        vm.stopBroadcast();
        console.log("Deployed at", address(c));
    }
}

// Run it with:
// forge script script/Deploy.s.sol --rpc-url $SEPOLIA_RPC_URL --broadcast --verify

Try it yourself

Deploy your Counter to Sepolia from Remix, copy its address, and use that address in the ethers.js example from the Frontends module.

Quick check: Why deploy to a testnet first?