This guide will walk you through mastering Solidity concepts through a Project Based Approach. The project involves creating a Skill Badge System where badges can be issued and transferred, and deploying it to the Avalanche blockchain.
We will cover 100% of Solidity core concepts in a step-by-step manner, ensuring precise descriptions and practical applications.
- Solidity Documentation: Solidity_Link
- Basic Understanding of Programming: Familiarity with programming concepts.
- Node.js Installed: Download and install Node.js from Node.js Official Site.
- GitHub Account: Create one if you don’t have it at GitHub.
- Visual Studio Code: Link
- Remix: Remix
Ensure you have Node.js installed by running the following command:
node -v
If not installed, download it from Node.js Official Site and follow the installation instructions.
-
Create a new folder for your project:
mkdir skill-badge-system cd skill-badge-system
-
Initialize a new Node.js project:
npm init -y
-
Install Hardhat:
npm install --save-dev hardhat
-
Create a Hardhat project:
npx hardhat
-
Choose “Create a basic sample project” and install the required dependencies.
Install OpenZeppelin contracts and dotenv for managing environment variables:
npm install @openzeppelin/contracts dotenv
We’ll implement a Badge.sol
contract that demonstrates Solidity core concepts. Create the Badge.sol
file inside the contracts
folder and add the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20; // Specifies the Solidity version.
/**
* @title Skill Badge System
* @dev Demonstrates core Solidity concepts with a practical use case.
*/
contract Badge {
// Struct to store badge details.
struct BadgeInfo {
string name; // Name of the badge.
string description; // Description of the badge.
address owner; // Current owner of the badge.
}
// Mapping to store all issued badges by their ID.
mapping(uint256 => BadgeInfo) public badges;
// Counter for the total number of badges issued.
uint256 public badgeCount;
// Address of the contract deployer, designated as the badge issuer.
address public issuer;
// Events to notify when a badge is issued or transferred.
event BadgeIssued(uint256 badgeId, address recipient);
event BadgeTransferred(uint256 badgeId, address newOwner);
// Modifier to restrict access to the badge issuer.
modifier onlyIssuer() {
require(msg.sender == issuer, "Not authorized");
_;
}
// Constructor to set the deployer as the initial badge issuer.
constructor() {
issuer = msg.sender;
}
/**
* @dev Function to issue a new badge to a recipient.
* @param _name Name of the badge.
* @param _description Description of the badge.
* @param _recipient Address of the recipient.
*/
function issueBadge(
string memory _name,
string memory _description,
address _recipient
) public onlyIssuer {
badgeCount++; // Increment the badge ID counter.
badges[badgeCount] = BadgeInfo(_name, _description, _recipient); // Store badge details.
emit BadgeIssued(badgeCount, _recipient); // Trigger an event.
}
/**
* @dev Function to transfer an existing badge to a new owner.
* @param _badgeId ID of the badge to transfer.
* @param _newOwner Address of the new owner.
*/
function transferBadge(uint256 _badgeId, address _newOwner) public {
require(msg.sender == badges[_badgeId].owner, "Not badge owner"); // Verify ownership.
badges[_badgeId].owner = _newOwner; // Update the badge owner.
emit BadgeTransferred(_badgeId, _newOwner); // Trigger an event.
}
}
- State Variables: Variables like
badgeCount
andbadges
store persistent data on the blockchain. - Structs: Custom data types used to group related data, such as
BadgeInfo
for badge details. - Mappings: Efficient data structures that map keys to values, as seen with
badges
. - Modifiers: Reusable code enforcing rules like
onlyIssuer
, restricting access to certain functions. - Events: Emit logs for external systems to track contract actions.
- Constructor: A special function executed once at deployment to initialize contract state.
- Functions: Code blocks that define the contract's behavior, like
issueBadge
andtransferBadge
.
-
Create a
.env
file and add your Avalanche key:PRIVATE_KEY=<your_private_key> RPC_URL=https://api.avax.network/ext/bc/C/rpc
-
Write the deployment script in
scripts/deploy.js
:const hre = require("hardhat"); async function main() { const Badge = await hre.ethers.getContractFactory("Badge"); const badge = await Badge.deploy(); await badge.deployed(); console.log("Badge deployed to:", badge.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
-
Deploy the contract:
npx hardhat run scripts/deploy.js --network avalanche
- Environment Variables: Securely manage sensitive information using a
.env
file. - Deployment Script: Automate contract deployment with JavaScript.
- Hardhat Framework: Simplifies smart contract development, deployment, and testing.
-
Initialize Git:
git init git remote add origin https://github.com/yourusername/skill-badge-system.git git branch -M main
-
Push the code:
git add . git commit -m "Initial commit" git push -u origin main