Project for a Solidity smart contract called MedicationVerification
. This contract allows a regulatory authority to register and verify medications. It includes functionality for storing medication details, registering new medications, verifying existing ones, and retrieving medication information.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MedicationVerification {
// Struct to hold medication details
struct Medication {
uint256 id;
string name;
string manufacturer;
string batchNumber;
uint256 expiryDate;
bool isVerified;
}
// State variable to store medications
mapping(uint256 => Medication) public medications;
// Event to emit when a medication is registered
event MedicationRegistered(uint256 indexed id, string name, string manufacturer);
// Event to emit when a medication is verified
event MedicationVerified(uint256 indexed id, string name);
// Address of the contract owner (e.g., regulatory authority)
address public owner;
// Modifier to check if the caller is the owner
modifier onlyOwner() {
require(msg.sender == owner, “Caller is not the owner”);
_;
}
constructor() {
owner = msg.sender;
}
// Function to register a new medication
function registerMedication(uint256 _id, string memory _name, string memory _manufacturer, string memory _batchNumber, uint256 _expiryDate) public onlyOwner {
// Ensure the medication is not already registered
require(medications[_id].id == 0
Here’s a step-by-step guide for creating your project, deploying it, checking for vulnerabilities, and ensuring compliance:
Step 1: Create and Upload the Project on GitHub
- Create a GitHub Repository:
- Go to GitHub and create a new repository.
- Name the repository
MedicationVerification
. - Initialize with a README file.
2. Clone the Repository Locally:
git clone https://github.com/yourusername/MedicationVerification.git
cd MedicationVerification
3. Add Your Smart Contract:
- Create a file named
MedicationVerification.sol
and paste the contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MedicationVerification {
// Struct to hold medication details
struct Medication {
uint256 id;
string name;
string manufacturer;
string batchNumber;
uint256 expiryDate;
bool isVerified;
}
// State variable to store medications
mapping(uint256 => Medication) public medications;
// Event to emit when a medication is registered
event MedicationRegistered(uint256 indexed id, string name, string manufacturer);
// Event to emit when a medication is verified
event MedicationVerified(uint256 indexed id, string name);
// Address of the contract owner (e.g., regulatory authority)
address public owner;
// Modifier to check if the caller is the owner
modifier onlyOwner() {
require(msg.sender == owner, “Caller is not the owner”);
_;
}
constructor() {
owner = msg.sender;
}
// Function to register a new medication
function registerMedication(uint256 _id, string memory _name, string memory _manufacturer, string memory _batchNumber, uint256 _expiryDate) public onlyOwner {
// Ensure the medication is not already registered
require(medications[_id].id == 0, “Medication already registered”);
// Create new medication struct and save in the mapping
medications[_id] = Medication({
id: _id,
name: _name,
manufacturer: _manufacturer,
batchNumber: _batchNumber,
expiryDate: _expiryDate,
isVerified: false
});
// Emit the medication registered event
emit MedicationRegistered(_id, _name, _manufacturer);
}
// Function to verify a medication
function verifyMedication(uint256 _id) public {
// Check if the medication is registered
require(medications[_id].id != 0, “Medication not found”);
// Set the medication as verified
medications[_id].isVerified = true;
// Emit the medication verified event
emit MedicationVerified(_id, medications[_id].name);
}
// Function to check medication details
function getMedicationDetails(uint256 _id) public view returns (Medication memory) {
return medications[_id];
}
}
Step 2: Deploy the Project from Remix to OpenZeppelin
- Deploy Using Remix:
- Go to Remix IDE.
- Create a new file and paste your smart contract code.
- Compile the contract using the Solidity compiler.
- Deploy the contract to a testnet (e.g., Rinkeby) using MetaMask.
- Verify and Publish on Etherscan:
- After deployment, verify and publish the contract on Etherscan.
- Provide the source code and constructor arguments.
Step 3: Check for Vulnerabilities
- Use OpenZeppelin Contracts:
- Integrate OpenZeppelin contracts to ensure security best practices.
- Example: Use OpenZeppelin’s
Ownable
contract for ownership management:
- // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import “@openzeppelin/contracts/access/Ownable.sol”;
contract MedicationVerification is Ownable {
// … (rest of the contract code)
}
2.Run Security Analysis:
- Use tools like MythX or Slither to analyze your contract for vulnerabilities.
- Example with Slither:
slither MedicationVerification.sol
Step 4: Ensure Compliance
- Legal Compliance:
- Ensure your contract adheres to relevant regulations and laws.
- Consult with legal experts if necessary.
- Documentation:
- Add comprehensive documentation to your GitHub repository.
- Include a README file with a project description, deployment instructions, and usage examples
Example GitHub README.md - # Medication Verification
A Solidity smart contract for registering and verifying medications, ensuring their authenticity.
## Features
– Register new medications.
– Verify registered medications.
– Retrieve medication details.
## Deployment
1. Clone the repository.
2. Deploy the contract using Remix and MetaMask.
3. Verify and publish the contract on Etherscan.
## Usage
– Register a medication (owner only).
– Verify a medication.
– Retrieve medication details.
## Security
– Uses OpenZeppelin’s Ownable contract.
– Analyzed with Slither for vulnerabilities.
## License
This project is licensed under the MIT License.
Deploying to GitHub Pages for Your Portfolio
- Create a Portfolio Website:
- Add a new repository for your portfolio.
- Create a simple HTML page showcasing your projects.
- Link to your
MedicationVerification
repository.
- Enable GitHub Pages:
- Go to the repository settings.
- Enable GitHub Pages from the
main
branch.
By following these steps, you can create a professional and compliant Solidity project, deploy it securely, and showcase it on your portfolio.