Hello, future blockchain developer! In Module Four, we learned advanced Solidity concepts like mappings, events, inheritance, modifiers, and error handling. We built more complex contracts like tokens and voting systems. Now it is time to learn how to test and deploy your smart contracts.
Testing ensures your contract works correctly and is secure. Deployment puts your contract on the blockchain so others can use it. In this module, we will learn how to test contracts using Hardhat and deploy them to test networks like Sepolia. By the end of this module, you will be able to deploy your own smart contracts.
Ada is a developer in Lagos. She had written a smart contract for a token. She wanted to share it with her friends. But first, she needed to test it and deploy it.
She used Hardhat to test her contract. She wrote tests to check every function. She made sure everything worked. Then she deployed her contract to the Sepolia testnet. She shared the address with her friends. They could interact with her token. Ada was proud of her first deployed contract.
Definition: Testing is the process of checking if your smart contract works as expected.
Why important: Testing catches bugs and prevents security issues.
Simple explanation: Like checking your work before submitting it.
Real-life example: A developer tests a contract before deploying.
School example: A student checks their homework before submitting.
Home example: A family tests a recipe before serving.
Nigerian example: A Nigerian developer tests contracts before launch.
Illustration:
+----------------------+
| Testing = |
| Check before |
| deploying |
| → Catch bugs |
| → Ensure security |
+----------------------+
Mini summary: Testing ensures your contract works correctly.
Definition: Hardhat is a development environment for Ethereum. It helps you compile, test, and deploy smart contracts.
Why important: Hardhat is one of the most popular tools for smart contract development.
Simple explanation: Like a workshop for building and testing contracts.
Real-life example: A developer uses Hardhat to test contracts.
School example: A student uses Hardhat for a project.
Home example: A hobbyist uses Hardhat to learn.
Nigerian example: A Nigerian developer uses Hardhat for dApps.
Illustration:
Hardhat:
🔨 Compile
🧪 Test
🚀 Deploy
Mini summary: Hardhat is a tool for developing smart contracts.
Definition: Setting up Hardhat involves installing it and creating a project.
Why important: You need Hardhat to test and deploy contracts.
Simple explanation: Like installing a game before playing.
Real-life example: A developer runs npm install --save-dev hardhat.
School example: A student sets up Hardhat for a project.
Home example: A hobbyist installs Hardhat.
Nigerian example: A Nigerian developer sets up Hardhat.
Illustration:
npm init -y
npm install --save-dev hardhat
npx hardhat
Mini summary: Set up Hardhat to start developing.
Definition: Tests are code that checks if your contract functions work correctly.
Why important: Tests give you confidence that your contract is safe.
Simple explanation: Like checking if a button works.
Real-life example: A test for a transfer function.
School example: A student tests a function.
Home example: A family tests a new appliance.
Nigerian example: A developer tests a token contract.
Illustration:
describe("Token", function () {
it("Should transfer tokens", async function () {
// Test code
});
});
Mini summary: Tests check if your contract works correctly.
Definition: Testnets are blockchain networks used for testing without real money.
Why important: You can deploy and test for free.
Simple explanation: Like a practice version of the blockchain.
Real-life example: Sepolia is a testnet.
School example: A student uses a testnet for practice.
Home example: A family uses a testnet to learn.
Nigerian example: A developer deploys to Sepolia.
Illustration:
Testnets:
🔵 Sepolia
🔵 Goerli
🔵 Rinkeby (deprecated)
Mini summary: Testnets are free environments for testing.
Definition: Deploying means putting your contract on the blockchain.
Why important: Deployment makes your contract available to users.
Simple explanation: Like publishing a website.
Real-life example: A developer deploys a token contract.
School example: A student deploys a project.
Home example: A family deploys a family contract.
Nigerian example: A developer deploys to Sepolia.
Illustration:
npx hardhat run scripts/deploy.js --network sepolia
Mini summary: Deployment puts your contract on the blockchain.
Definition: After deployment, you can interact with your contract using a wallet or frontend.
Why important: This is how users use your contract.
Simple explanation: Like using an app after it's built.
Real-life example: A user interacts with a token contract.
School example: A student interacts with a deployed contract.
Home example: A family interacts with a contract.
Nigerian example: A user interacts with a Nigerian dApp.
Illustration:
Interact with Contract:
1. Connect wallet
2. Call functions
3. View data
Mini summary: Interaction allows users to use your contract.
Definition: You have learned how to test and deploy smart contracts.
Why important: You can now share your contracts with the world.
Simple explanation: You know how to test and deploy your contracts.
Real-life example: You can deploy a token or voting contract.
School example: You can deploy a project.
Home example: You can deploy a family contract.
Nigerian example: You can deploy a Nigerian dApp.
Illustration:
+----------------------+
| You can deploy |
| Smart Contracts! 🎉 |
| → Test with Hardhat |
| → Deploy to testnet |
| → Interact with it |
+----------------------+
Mini summary: You are now ready to deploy smart contracts!
npm install --save-dev hardhatnpx hardhattest folder.npx hardhat testscripts folder.npx hardhat compilenpx hardhat run scripts/deploy.js --network sepolia
// test/Token.js
const { expect } = require("chai");
describe("Token", function () {
it("Should deploy and assign initial supply", async function () {
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy(1000);
await token.deployed();
expect(await token.totalSupply()).to.equal(1000);
});
});
Write Contract → Compile → Test → Deploy → Interact
| Testnet | Mainnet |
|---|---|
| Free to use | Costs real money |
| For testing | For production |
| No real value | Real value |
const hre = require("hardhat");
async function main() {
const Token = await hre.ethers.getContractFactory("Token");
const token = await Token.deploy(1000);
await token.deployed();
console.log("Token deployed to:", token.address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
In this module, we learned about testing and deploying smart contracts. We discovered that testing is crucial for catching bugs and ensuring security. We learned about Hardhat, testnets, and how to deploy a contract to a testnet. With this knowledge, you can now test and deploy your own smart contracts. Great job!
Match the word with its meaning:
| Word | Meaning |
|---|---|
| 1. Testing | A) A development tool |
| 2. Hardhat | B) Free blockchain for testing |
| 3. Testnet | C) Putting a contract on the blockchain |
| 4. Deployment | D) Checking if your contract works |
| 5. Interaction | E) Using a deployed contract |
(Answers: 1-D, 2-A, 3-B, 4-C, 5-E)
Scenario 1: You have written a smart contract. Before deploying, what should you do?
Scenario 2: You want to deploy your contract without spending real money. What would you do?
In groups of 3, set up Hardhat and deploy a simple contract to a testnet. Share the contract address.
Write a simple contract, test it with Hardhat, and deploy it to Sepolia. Record the contract address.
Deploy a simple token contract to Sepolia. Test it using Hardhat. Share the contract address with the class.
Write a test for a simple contract. Run the test and share the results.
Deploy a voting contract to Sepolia. Write tests for the contract. Share the address and test results.
Fill-in-the-blank: 1-Testing, 2-Hardhat, 3-testnet, 4-Deployment, 5-Interaction
True/False: 1-F, 2-T, 3-F, 4-T, 5-F
Matching: 1-D, 2-A, 3-B, 4-C, 5-E
In Module Six, we will learn about building dApps — connecting your smart contract to a frontend. We will use ethers.js and React to build a complete dApp. Before the next session, think about how you would like your dApp to look.
See you in Module Six! 🎉
End of Module Five · Testing and Deployment