This project demonstrates a simple Ethereum smart contract for storing and retrieving a numerical value on the blockchain. It covers essential smart contract concepts such as state variables, functions, and blockchain interaction, making it an ideal starting point for Solidity development.
A smart contract is a self-executing program that runs on a blockchain. It:
- Contains code (functions) and data (state).
- Executes automatically when conditions are met.
- Cannot be modified once deployed (immutable).
- Runs decentralized on the blockchain.
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers manage and automate recurring tasks while remaining extensible for advanced use cases.
Key Features:
- Flexible and Extensible: Customize and extend functionality with plugins.
- Built-in Network: Comes with Hardhat Network for local testing.
- Comprehensive Debugging: Provides advanced debugging and error reporting tools.
You can start a local Ethereum network using Hardhat by running:
npx hardhat node- A local Ethereum blockchain is launched for testing.
- Multiple accounts are automatically created.
- Each account is pre-funded with test ETH.
- Private keys for these accounts are displayed, allowing interaction via scripts or wallets.
- Test smart contracts without real ETH.
- Simulate multiple users (e.g., owner, buyer, seller).
- Run transactions locally before deploying to a live network.
To deploy the contract to the local Hardhat network, run:
npx hardhat run scripts/deploy.ts --network localhost- The smart contract is compiled and deployed to the local blockchain.
- A contract address is generated.
- You can interact with the contract using this address.
npx hardhat console --network localhostconst contract = await ethers.getContractAt(
"BasicStorage",
"<DEPLOYED_CONTRACT_ADDRESS>"
);Replace <DEPLOYED_CONTRACT_ADDRESS> with your actual contract address from deployment.
await contract.getNumber();await contract.setNumber(99);await contract.getNumber();It should now return 99.
To ensure the smart contract works correctly, you can run automated tests using Hardhat.
Run the following command:
npx hardhat test- ✅ First Test: Ensures the contract is deployed with the correct initial value.
- ✅ Second Test: Verifies that
setNumber()correctly updates the stored value.
If successful, the output should show:
BasicStorage
✔ Should deploy the contract with an initial value
✔ Should update the stored number
2 passing (1s)
These tests help confirm that the contract behaves as expected before deploying to a live network.
This project includes GitHub Actions workflows for automated testing:
- Runs tests automatically when you push changes to
mainordev. - Ensures that any updates do not break existing functionality.
- Tests are executed using Node.js 22 in a GitHub Actions workflow.
- Allows manually running tests from the GitHub Actions UI.
- Useful for verifying changes before pushing them.
- Go to the GitHub Actions tab.
- Select Run Hardhat Tests (Manual).
- Click Run workflow.
This ensures that smart contracts are thoroughly tested before deployment.