This guide walks you through writing, testing, and deploying a smart contract on Ethereum (or testnets like Sepolia/Goerli). We’ll use Solidity, Remix IDE, and MetaMask.
1. Prerequisites
Before you start, ensure you have:
- MetaMask installed (Download here)
- Testnet ETH (Get free ETH from Sepolia Faucet)
- Basic understanding of Solidity (Ethereum’s smart contract language)
2. Writing the Smart Contract
We’ll create a simple “Hello World” contract that stores and retrieves a message.
Solidity Code
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
string private message;
// Constructor: Set initial message
constructor(string memory _initialMessage) {
message = _initialMessage;
}
// Function to update the message
function setMessage(string memory _newMessage) public {
message = _newMessage;
}
// Function to read the message
function getMessage() public view returns (string memory) {
return message;
}
}
Key Components
pragma solidity ^0.8.0→ Specifies Solidity version.string private message→ State variable (stored on blockchain).constructor()→ Runs once at deployment.setMessage()→ Updates the stored message (costs gas).getMessage()→ Reads the message (free, no gas).
3. Compiling & Deploying
Using Remix IDE (Browser-Based)
- Go to Remix IDE
- Create a new file (
HelloWorld.sol) and paste the code. - Compile:
- Go to the Solidity Compiler tab.
- Select compiler version 0.8.0+.
- Click Compile.
- Deploy:
- Go to the Deploy & Run Transactions tab.
- Select Injected Provider – MetaMask (connect wallet).
- Choose Sepolia/Goerli testnet.
- Enter an initial message (e.g.,
"Hello, Ethereum!") in the constructor field. - Click Deploy (confirm in MetaMask).
4. Interacting with the Contract
Once deployed:
- Call
getMessage()→ Returns the stored message (no gas). - Call
setMessage("New Message")→ Updates the message (costs gas).
5. Verifying on Etherscan
- Copy the contract address from Remix.
- Go to Etherscan (or Sepolia Etherscan).
- Search for your contract address.
- Click “Verify & Publish” to make the code public.
6. Advanced: Deploying with Hardhat
For larger projects, use Hardhat (local development):
bash
npm install --save-dev hardhat npx hardhat init
Edit scripts/deploy.js:
javascript
async function main() {
const HelloWorld = await ethers.getContractFactory("HelloWorld");
const hello = await HelloWorld.deploy("Hello, Hardhat!");
await hello.deployed();
console.log("Contract deployed to:", hello.address);
}
Run:
bash
npx hardhat run scripts/deploy.js --network sepolia
7. Gas Optimization Tips
- Use
memoryinstead ofstoragewhere possible. - Avoid loops in transactions.
- Use
view/purefunctions for read-only ops.

