If you're new to Solidity, the programming language used for writing smart contracts on the Ethereum blockchain, building a CRUD (Create, Read, Update, Delete) contract is a great way to get started. In this article, we will walk you through the process of building a basic CRUD contract using Solidity, along with some explanations of the key concepts and functions used in the code.
Introduction
Solidity is a statically-typed, contract-oriented programming language designed for developing smart contracts on the Ethereum blockchain.
CRUD operations are essential functionalities in any database-driven application. CRUD stands for:
Create: Inserting new data into the database.
Read: Retrieving data from the database.
Update: Modifying existing data in the database.
Delete: Removing data from the database.
In the context of smart contracts, CRUD operations can be implemented using Solidity functions that interact with the contract's data storage
It's a fundamental concept in blockchain development and serves as a foundation for building more complex decentralized applications (dApps).
Contract Structure
Let's take a look at the structure of our CRUD contract.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract Crud{
struct employee{
string name;
string email;
uint256 age;
address walletAdress;
}
employee[] public Employeer;
uint256 public TotalEmployee;
// Constructor
constructor(){
TotalEmployee = 0;
}
// CRUD functions go here
// Helper functions go here
}
The contract starts with a struct definition for the employee data, which includes four fields: name, email, age, and walletAddress. This struct is used to represent the employee data that will be stored on the blockchain.
The Employeer array is used to store the employee data, and the TotalEmployee variable keeps track of the total number of employees in the array. The public keyword makes these variables accessible from outside the contract, so they can be read by other contracts or external applications.
CRUD Functions
The core functionality of our CRUD contract is implemented through four functions: create(), update(), delEmploye(), and compareStrings().
Create Function
The create() function allows you to add a new employee to the Employeer array. It takes four parameters: name, email, age, and walletAddress, which represent the employee's data. Inside the function, a new employee struct is created with the provided data, and then pushed into the Employeer array. The TotalEmployee variable is then incremented to keep track of the total number of employees.
function create (string memory name, string memory email, uint256 age, address walletAddress) public returns (uint256){
employee memory newEmployee = employee(name, email, age, walletAddress);
Employeer.push(newEmployee);
TotalEmployee++;
return TotalEmployee;
}
Update Function
The update() function allows you to update the name of an existing employee in the Employeer array. It takes two parameters: email and name, representing the email address of the employee to be updated and the new name to be set, respectively. Inside the function, a loop iterates through the Employeer array to find the employee with a matching email address. Once found, the name field of the corresponding employee struct is updated with the new name.
function update(string memory email, string memory name) external returns(bool){
for(uint i = 0; i < TotalEmployee; i++){
if(compareStrings(Employeer[i].email, email)){
Employeer[i].name = name;
return true;
}
}
return false;
}
Delete Function
The delete() function allows you to delete the struct of an existing employee in the Employeer array. It takes one parameters: email , representing the email address of the employee to be deleted. Inside the function, a loop iterates through the Employeer array to find the employee with a matching email address. Once found, the name field of the corresponding employee struct is deleted.
function delEmploye(string memory email)external returns(bool){
assert(TotalEmployee > 0);
for(uint i = 0 ; i < TotalEmployee; i++){
if(compareStrings(Employeer[i].email, email)){
Employeer[i] = Employeer[TotalEmployee - 1];
delete Employeer[TotalEmployee - 1];
TotalEmployee--;
return true;
}
}
return false;
}
Helper function
The compareStrings() function allows you to compare two strings. It takes two parameters: a and b, can be name or email. Inside the function, it returns a bool by using a hashing function that help convert the string into hash and then check if the two hash strings match.
function compareStrings(string memory a, string memory b) internal pure returns (bool) {
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
}
Conclusion:
In this article, we covered the implementation of a basic CRUD contract in Solidity. The contract allows users to create, read ,update and delete.
Top comments (0)