Getting Started with Smart Contract Development
Go from zero to deployed smart contract in under 30 minutes. This guide walks you through everything you need to start building on Ethereum.
On this page
Prerequisites
- Basic programming knowledge (any language)
- Command line familiarity
- Understanding of blockchain basics (take our course)
Install Development Tools
First, let's install the essential tools you'll need for blockchain development.
Install Node.js
Download and install Node.js (v18 or higher) from nodejs.org
node --version
npm --versionInstall Git
Download from git-scm.com or use your package manager:
# macOS
brew install git
# Ubuntu/Debian
sudo apt install gitChoose a Framework
For this tutorial, we'll use Hardhat, the most popular Ethereum development framework.
Hardhat
RecommendedJavaScript/TypeScript based, great documentation, large ecosystem of plugins
Foundry
Rust-based, faster compilation, tests written in Solidity
Create a New Project
mkdir my-first-contract
cd my-first-contract
npm init -y
npm install --save-dev hardhat
npx hardhat initSelect "Create a JavaScript project" when prompted. This will create a project structure with example contracts and tests.
Write Your First Contract
Replace the contents of contracts/Lock.sol with this simple storage contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/// @title SimpleStorage
/// @notice A simple contract to store and retrieve a value
contract SimpleStorage {
uint256 private storedValue;
/// @notice Emitted when the stored value changes
event ValueChanged(uint256 newValue);
/// @notice Store a new value
/// @param _value The value to store
function store(uint256 _value) public {
storedValue = _value;
emit ValueChanged(_value);
}
/// @notice Retrieve the stored value
/// @return The currently stored value
function retrieve() public view returns (uint256) {
return storedValue;
}
}Compile the Contract
npx hardhat compileYou should see "Compiled 1 Solidity file successfully". If there are errors, check your Solidity syntax.
Test Your Contract
Create a test file at test/SimpleStorage.js:
const { expect } = require("chai");
describe("SimpleStorage", function () {
let simpleStorage;
beforeEach(async function () {
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
simpleStorage = await SimpleStorage.deploy();
});
it("Should store and retrieve a value", async function () {
// Store value 42
await simpleStorage.store(42);
// Retrieve and check
expect(await simpleStorage.retrieve()).to.equal(42);
});
it("Should emit ValueChanged event", async function () {
await expect(simpleStorage.store(100))
.to.emit(simpleStorage, "ValueChanged")
.withArgs(100);
});
});Run Tests
npx hardhat test✓ All tests should pass. Testing is crucial—never deploy untested code to mainnet!
Deploy to Testnet
Now let's deploy to Sepolia testnet. First, you'll need:
Configure Hardhat
Create a .env file (never commit this!):
SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_KEY
PRIVATE_KEY=your_wallet_private_key_hereUpdate hardhat.config.js:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.20",
networks: {
sepolia: {
url: process.env.SEPOLIA_RPC_URL,
accounts: [process.env.PRIVATE_KEY]
}
}
};Deploy Script
Create scripts/deploy.js:
async function main() {
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.deploy();
await simpleStorage.waitForDeployment();
console.log("SimpleStorage deployed to:", await simpleStorage.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Deploy!
npx hardhat run scripts/deploy.js --network sepoliaCongratulations!
You've deployed your first smart contract! View it on Sepolia Etherscan using the contract address from the console output.