Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My BlockHeader Assignment #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"solidity.compileUsingRemoteVersion": "v0.8.14+commit.80d49f37"
"solidity.compileUsingRemoteVersion": "v0.8.24+commit.80d49f37"
}
39 changes: 39 additions & 0 deletions contracts/IStudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "./Student.sol";
interface IStudentRegistry {


function addStudent(
address _studentAddr,
string memory _name,
uint8 _age
) external;

function getStudent(uint8 _studentID) external view returns (Student memory);

function getStudentFromMapping(address _studentAddr) external view returns (Student memory);

function registerStudent(
address _studentAddr,
uint8 _age,
string memory _name
) external payable returns (bool);

function authorizeStudentRegistration(
address _studentAddr
) external returns (bool);

function updateStudent(
address _studentAddr,
uint8 _age,
string memory _name
) external returns (Student memory);

function withdraw() external returns (bool);

function getBalance() external view returns (uint256);

function deleteStudent(address _studentAddr) external;

}
38 changes: 38 additions & 0 deletions contracts/MyStudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "./IStudentRegistry.sol";
import "./Student.sol";
import "./Ownable.sol";


// Pay 1 Eth to register a student
// Collect student's name, age, address
// Admin should add registered students to the Registry
// Check if the students has paid.(hasPaid => Student)
//

contract MyStudentRegistry is Ownable {

address private StudentRegistryContractAddress;

constructor(address _studentRgistry){
StudentRegistryContractAddress = _studentRgistry;
}

function registerStudent(
address _studentAddr,
string memory _name,
uint8 _age
) public onlyOwner {

IStudentRegistry(StudentRegistryContractAddress).addStudent(_studentAddr, _name, _age);
}


function getStudent2(
uint8 _studentId
) public view returns (Student memory) {

return IStudentRegistry(StudentRegistryContractAddress).getStudent(_studentId);
}
}
33 changes: 33 additions & 0 deletions contracts/Ownable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;


contract Ownable {

address payable private owner;

event ChangeOwner(address indexed oldOwner, address indexed newOwner);

constructor(){
owner = payable (msg.sender);
}


modifier onlyOwner {
require(owner == msg.sender, "Caller not owner");
_;
}


function getOwner() public view returns (address){
return owner;
}


function changeOwner(address payable _newOwner) internal onlyOwner {
require(_newOwner != address(0), "Owner can not be address zero");

emit ChangeOwner(owner, _newOwner);
owner = _newOwner;
}
}
9 changes: 9 additions & 0 deletions contracts/Student.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
struct Student {
address studentAddr;
string name;
uint256 studentId;
uint8 age;
bool hasPaid;
}
165 changes: 134 additions & 31 deletions contracts/StudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,89 +1,192 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "./Ownable.sol";
import "./Student.sol";

contract StudentRegistry {
//custom data type
struct Student {
address studentAddr;
string name;
uint256 studentId;
uint8 age;
}

address public owner;
contract StudentRegistry is Ownable {
//custom erros
error NameIsEmpty();
error NotRegistered();
error UnderAge(uint8 age, uint8 expectedAge);

constructor() {
owner = msg.sender;
}
//Event Registration
event Registration(address indexed _studentAddress, string _message);



//dynamic array of students
Student[] private students;

uint256 private studentsCount;
mapping(address => Student) private studentsPool;
mapping(address => Student) public studentsMapping;


modifier onlyOwner () {
require( owner == msg.sender, "You fraud!!!");
modifier isNotAddressZero() {
require(msg.sender != address(0), "Invalid Address");
_;
}
modifier isOfAge(uint8 _age) {
if (_age < 18) {
revert UnderAge(_age, 18);
}
_;
}

modifier isNotAddressZero () {
require(msg.sender != address(0), "Invalid Address");
modifier isValidName(string memory _name) {
if (bytes(_name).length <= 0) {
revert NameIsEmpty();
}
_;
}

modifier isRegistered(address _studentAddr) {
if (!studentsPool[_studentAddr].hasPaid) {
revert NotRegistered();
}
_;
}



// RegisterStudent
function registerStudent(
address _studentAddr,
uint8 _age,
string memory _name
)
public
payable
isNotAddressZero
isOfAge(_age)
isValidName(_name)
returns (bool)
{
require(!studentsPool[_studentAddr].hasPaid, "Duplicate Registration");
uint256 regFee = msg.value;
require(regFee == 1 ether, "Registration Fee is 1Eth");

studentsCount += 1;
studentsPool[_studentAddr] = Student({
studentAddr: _studentAddr,
name: _name,
age: _age,
studentId: studentsCount,
hasPaid: true
});

emit Registration(_studentAddr, "Registration Successful");
return true;
}

// authorizeStudentRegistration
function authorizeStudentRegistration(
address _studentAddr
)
public
isNotAddressZero
onlyOwner
isRegistered(_studentAddr)
returns (bool)
{
require(
!studentsMapping[_studentAddr].hasPaid,
"Duplicate Registration"
);
studentsMapping[_studentAddr] = studentsPool[_studentAddr];

emit Registration(_studentAddr, "Enlisted Successfully");
return true;
}


function addStudent(
address _studentAddr,
string memory _name,
uint8 _age
) public onlyOwner isNotAddressZero {
) public isNotAddressZero {
if (bytes(_name).length == 0) {
revert NameIsEmpty();
}

require( bytes(_name).length > 0, "Name cannot be blank");
require( _age >= 18, "This student is under age");
if (_age < 18) {
revert UnderAge({age: _age, expectedAge: 18});
}

uint256 _studentId = students.length + 1;
Student memory student = Student({
studentAddr: _studentAddr,
name: _name,
age: _age,
studentId: _studentId
studentId: _studentId,
hasPaid: true
});

students.push(student);
// add student to studentsMapping
studentsMapping[_studentAddr] = student;
}

function getStudent(uint8 _studentId) public isNotAddressZero view returns (Student memory) {
function getStudent(uint8 _studentId)
public
view
isNotAddressZero
returns (Student memory)
{
return students[_studentId - 1];
}




function getStudentFromMapping(address _studentAddr)
public
isNotAddressZero
view
isNotAddressZero
returns (Student memory)
{
return studentsMapping[_studentAddr];
}



function deleteStudent(address _studentAddr) public onlyOwner isNotAddressZero{

require(studentsMapping[_studentAddr].studentAddr != address(0), "Student does not exist");
function deleteStudent(address _studentAddr)
public
onlyOwner
isNotAddressZero
{
require(
studentsMapping[_studentAddr].studentAddr != address(0),
"Student does not exist"
);

// delete studentsMapping[_studentAddr];

Student memory student = Student({
studentAddr: address(0),
name: "",
age: 0,
studentId: 0
studentId: 0,
hasPaid: true
});

studentsMapping[_studentAddr] = student;
}


function modifyOwner(address payable _newOwner) public {
changeOwner(_newOwner);
}

// Withdraw
function withdraw() public isNotAddressZero onlyOwner returns (bool) {
uint256 balance = address(this).balance;
require(balance > 0, "Empty Balance");

(bool withdrawn, ) = payable(getOwner()).call{value: balance}("");
require(withdrawn, "Withdrawal failed");

return withdrawn;
}

// Getting the current balance
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}