Why Build on Base?
Base is fully EVM-compatible, meaning any Solidity smart contract you can deploy on Ethereum mainnet can be deployed on Base with minimal or no modifications. The advantages are significant: gas costs are dramatically lower (great for testing and iteration), and the network has a fast-growing user base actively looking for new dApps to use.
This guide walks you through deploying a simple smart contract on Base's mainnet using Hardhat, one of the most popular Ethereum development frameworks.
Prerequisites
- Node.js (v18 or later) installed on your machine
- A wallet with a small amount of ETH on Base mainnet for gas
- Basic familiarity with Solidity and JavaScript/TypeScript
- An account on a node provider (Alchemy, QuickNode, or use the public Base RPC)
Step 1: Set Up Your Hardhat Project
Open your terminal and run the following commands to initialize a new Hardhat project:
mkdir my-base-contract
cd my-base-contract
npm init -y
npm install --save-dev hardhat
npx hardhat init
When prompted, select "Create a JavaScript project" and follow the setup steps. This generates a sample project structure with a contracts folder, a scripts folder, and a Hardhat config file.
Step 2: Install Required Dependencies
You'll need a few additional packages:
npm install --save-dev @nomicfoundation/hardhat-toolbox dotenv
Create a .env file in your project root to store sensitive values:
PRIVATE_KEY=your_wallet_private_key_here
BASE_RPC_URL=https://mainnet.base.org
Important: Never commit your .env file to version control. Add it to your .gitignore immediately.
Step 3: Configure Hardhat for Base
Open hardhat.config.js and update it to include the Base network:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.24",
networks: {
base: {
url: process.env.BASE_RPC_URL || "https://mainnet.base.org",
accounts: [process.env.PRIVATE_KEY],
chainId: 8453,
},
"base-sepolia": {
url: "https://sepolia.base.org",
accounts: [process.env.PRIVATE_KEY],
chainId: 84532,
},
},
};
Step 4: Write Your Smart Contract
In the contracts/ folder, create a file called SimpleStorage.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract SimpleStorage {
uint256 private storedValue;
event ValueUpdated(uint256 newValue);
function setValue(uint256 _value) public {
storedValue = _value;
emit ValueUpdated(_value);
}
function getValue() public view returns (uint256) {
return storedValue;
}
}
Step 5: Write a Deployment Script
In the scripts/ folder, create deploy.js:
const { ethers } = require("hardhat");
async function main() {
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const contract = await SimpleStorage.deploy();
await contract.waitForDeployment();
console.log("SimpleStorage deployed to:", await contract.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Step 6: Deploy to Base
To deploy to Base mainnet, run:
npx hardhat run scripts/deploy.js --network base
For testing first, deploy to the Base Sepolia testnet (get free testnet ETH from the Base Sepolia faucet):
npx hardhat run scripts/deploy.js --network base-sepolia
Step 7: Verify Your Contract
You can verify your contract on Basescan (Base's block explorer) to make the source code publicly readable. Add the Basescan API key to your config and run the Hardhat verify task. Verified contracts build user trust and are essential for any production dApp.
Next Steps
- Explore Base's developer documentation at docs.base.org for deeper guides.
- Consider using Foundry as an alternative to Hardhat for faster test compilation.
- Look into The Graph for indexing your contract's events on Base.