Smart Contract to Store Employee Details
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol.
What are Smart Contracts?
Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. A contract is a fundamental block of building an application on the Ethereum.
Example: In the below example, we aim at creating a smart contract to demonstrate how to store and retrieve the details of the employees of an organization using structure, arrays, and functions.
- Create a smart contract, StructDemo having structure Employee with data members as empid, name, department, designation. Create a dynamic array of Employee as emps.
- Create a function addEmployee() which takes the data of the employee and stores into a dynamic array called emps.
- Create a function getEmployee() which takes the employee id, searches the record in the emps array, and returns the details like name, department, and designation.
Step 1: Open Remix-IDE.
Step 2: Select File Explorer from the left side icons and select Solidity in the environment. Click on New option below the Solidity environment. Enter the file name as StructDemo.sol and Click on the OK button.
Step 3: Enter the following Solidity Code. Select the same solidity version as in your code.
Step 4: Compile the file StructDemo.sol from the Solidity Compiler tab.
Step 5: Deploy the smart contract from the Deploy and Run Transaction tab.
Step 6: Then add the employee details through addEmployee() then after that, you can view details of any employee using employee id through getEmployee().