I recently started learning Solidity and hope to become a smart contract engineer one day, so I decided to start with some small functional contracts and progress slowly.
I decided that the topic of this exercise would be "Simple CRUD of data".
The specific functions are as follows:
About Create function:
This data is about the student, it includes the student ID, phone number, email, wallet address and will have an index number when the data is created.
About Read function:
Searchable index by student ID and address.
Search data by index and studentID.
About Update function:
Updates data by student ID and can only be updated by the contract owner.
About Delete function:
Data can only be deleted by the contract owner.
Code:
Start of the program
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.13;
contract StudentData{
address owner;
struct Data{
uint id; string stdid; string phone; string mail; address addr;
// | Index | Student ID | Phone Number | Mail | Wallet Address |
}
mapping(address=>uint) AddrtoData; // Mapping data from address
mapping(string=>Data) StdtoData; // Mapping data from stdid
mapping(uint=>string) IdtoStdid; // Mapping stdid from iid
Initial values and some parameters.
Construction astruct
and designationmapping
.
Make a funtion to check contract owner
modifier onlyOwner() {
require(msg.sender == owner, "not owner"); // check msg.sender is owner or not
_;
}
//get contract owner
constructor ( address initOwner) {
owner = initOwner;
}
Function Create
uint idnum;
//create new data
function ceateData(string memory nstdid, string memory nphone, string memory nmail, address naddr) public onlyOwner{
idnum++;
AddrtoData[naddr] = idnum;
IdtoStdid[idnum] = nstdid;
StdtoData[nstdid] = Data(idnum,nstdid,nphone,nmail,naddr);
}
Function Read
//find id by student id
function findIdbyStdid(string memory nstdid) public view returns (uint id){
require(StdtoData[nstdid].id != 0, "StudentID not Found");
id = StdtoData[nstdid].id;
}
//find id by address
function findIdbyAddr(address naddr) public view returns (uint id){
require(AddrtoData[naddr] != 0, "Address not Found");
id = AddrtoData[naddr];
}
//view data by id
function readDatabyId(uint nid) public view returns (string memory rstdid, string memory rphone, string memory rmail, address raddr){
require(StdtoData[IdtoStdid[nid]].id != 0, "Data not Found");
return(IdtoStdid[nid],StdtoData[IdtoStdid[nid]].phone,StdtoData[IdtoStdid[nid]].mail,StdtoData[IdtoStdid[nid]].addr);
}
//view data by student id
function readDatabyStdid(string memory nstdid) public view returns (string memory rstdid, string memory rphone, string memory rmail, address raddr){
require(StdtoData[nstdid].id != 0, "Data not Found");
return(nstdid,StdtoData[nstdid].phone,StdtoData[nstdid].mail,StdtoData[nstdid].addr);
}
Function Update
// update data
function updateData(string memory nstdid, string memory nphone, string memory nmail, address naddr) public onlyOwner{
require(StdtoData[nstdid].id != 0, "StudentID not Found");
StdtoData[nstdid] = Data(StdtoData[nstdid].id, nstdid, nphone, nmail, naddr);
}
Function Delete
// delet data
function deleteData(string memory nstdid) public onlyOwner{
require(StdtoData[nstdid].id != 0, "StudentID not Found");
delete StdtoData[nstdid];
delete IdtoStdid[StdtoData[nstdid].id];
delete AddrtoData[StdtoData[nstdid].addr];
}
After coding it, I compiled and deployed it with Remix and there seems to be no obvious errors.
This is the end of the practice of Solidity.
Feel free to give some suggestions based on my code, whether it's a way to improve performance, shorten code, or save gas when deploying or running contracts, the proposed approach is welcome.
If you have any topics that you would like me to use for practice, you can just ask in the comments and I would appreciate your contribution!
Thanks for stopping by here and reading my post.
!PIZZA
!PIZZA
The rewards earned on this comment will go directly to the person sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.
@life-hacker! The Hive.Pizza team manually curated this post.
PIZZA Holders sent $PIZZA tips in this post's comments:
curtis90 tipped life-hacker (x1)
Join us in Discord!