forked from muqaddashahzad/base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-Storage
54 lines (46 loc) · 2.1 KB
/
3-Storage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
contract EmployeeStorage {
// Declare private state variables to store employee data
uint16 private shares; // Number of shares owned by the employee (private to contract)
uint32 private salary; // Monthly salary of the employee (private to contract)
uint256 public idNumber; // Unique identification number of the employee (publicly accessible)
string public name; // Name of the employee (publicly accessible)
// Constructor to initialize employee data when contract is deployed
constructor(uint16 _shares, string memory _name, uint32 _salary, uint _idNumber) {
shares = _shares; // Initialize shares
name = _name; // Initialize name
salary = _salary; // Initialize salary
idNumber = _idNumber; // Initialize idNumber
}
// View function to retrieve the number of shares owned by the employee
function viewShares() public view returns (uint16) {
return shares;
}
// View function to retrieve the monthly salary of the employee
function viewSalary() public view returns (uint32) {
return salary;
}
// Custom error declaration
error TooManyShares(uint16 _shares);
// Function to grant additional shares to the employee
function grantShares(uint16 _newShares) public {
// Check if the requested shares exceed the limit
if (_newShares > 5000) {
revert("Too many shares"); // Revert with error message
} else if (shares + _newShares > 5000) {
revert TooManyShares(shares + _newShares); // Revert with custom error message
}
shares += _newShares; // Grant the new shares
}
// Function used for testing packing of storage variables (not relevant to main functionality)
function checkForPacking(uint _slot) public view returns (uint r) {
assembly {
r := sload (_slot)
}
}
// Function to reset shares for debugging purposes (not relevant to main functionality)
function debugResetShares() public {
shares = 1000; // Reset shares to 1000
}
}