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

Student registry system #26

Open
wants to merge 3 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.e11b9ed9"
}
64 changes: 54 additions & 10 deletions contracts/IStudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "./Student.sol";
import "contracts/StudentStruct.sol";

/**
* @title IStudentRegistry
* @dev Interface for the StudentRegistry contract.
* Defines the functions available for interacting with the student registry.
*/
interface IStudentRegistry {


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

function getStudent(uint8 _studentID) external view returns (Student memory);
/**
* @dev pay Fees.
*/
function payFees() external payable;

/**
@notice Withdraws the contract's balance to the owner.
@dev This function should handle the transfer of funds from the contract to the owner.
@return success A boolean indicating whether the withdrawal was successful.
*/
function withdrawEarnings() external returns (bool);

/**
* @dev Register a student with the provided address, name, and age.
* @param _studentAddress The address of the student to register.
* @param _name The name of the student.
* @param _age The age of the student.
*/
function addStudent(address _studentAddress, string memory _name, uint8 _age) external payable;

/**
* @dev Authorize a student for registration by setting their status to authorized.
* @param _studentAddress The address of the student to authorize.
*/
function authorizeStudent(address _studentAddress) external;

/**
* @dev Retrieve a student information.
* @param _studentAddress The address of the student to retrieve.
*/
function getStudent(address _studentAddress) external view returns (Student memory);


/**
* @dev Update a student record.
* @param _studentAddress The new address of the student.
* @param _name The new name of the student.
* @param _age The new age of the student.
*/
function updateStudent(address _studentAddress, string memory _name, uint8 _age) external;


function getStudentFromMapping(address _studentAddr) external view returns (Student memory);
/**
* @dev Delete a student record.
* @param _studentAddress The address of the student to delete.
*/
function deleteStudent(address _studentAddress) external;

}
}
31 changes: 0 additions & 31 deletions contracts/MyStudentRegistry.sol

This file was deleted.

37 changes: 27 additions & 10 deletions contracts/Ownable.sol
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;


contract Ownable {

address private owner;

event ChangeOwner(address indexed oldOwner, address indexed newOwner);
address internal owner;
event replaceOwner(address indexed newOwner, address indexed oldOwner, string message);


/**
* @dev Set the owner of the contract to the address deploying the contract.
*/
constructor(){
owner = msg.sender;
owner = payable (msg.sender);
}


modifier onlyOwner {
require(owner == msg.sender, "Caller not owner");
/**
* @dev restrict access to the contract owner only.
*/
modifier onlyOwner() {
require(owner == msg.sender, "Unauthorized");
_;
}


/**
* @dev Retrieve the current owner of the contract.
* @return The address of the current owner.
*/

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


function changeOwner(address _newOwner) internal onlyOwner {

/**
* @dev Change the owner of the contract to a new address.
* @param _newOwner The address of the new owner.
* Emits a {changedOwner} event.
*/
function changedOwner(address _newOwner) internal onlyOwner {
require(_newOwner != address(0), "Owner can not be address zero");

emit ChangeOwner(owner, _newOwner);
emit replaceOwner(_newOwner, owner, "This owner has been changed");
owner = _newOwner;
}
}
48 changes: 48 additions & 0 deletions contracts/Payment.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./StudentStruct.sol";
import "./modifiers/Ownable.sol";
import "./modifiers/InputValidations.sol";

/**
* @title student Payment
* @dev This contract manages student registration payment, authorization, and details.
* It handles student registration payment.
*/
contract Payment is Ownable, InputValidation {

event PaymentStatus(bool indexed hasPaid, string message);

// Mapping of student address to payment receipt amount
mapping (address => uint256) internal PaymentDetails;


// Make payment
function payFees() public payable {
uint256 amount = msg.value;
require(amount == 1 ether, "1 eth is required");
PaymentDetails[msg.sender] = amount;
emit PaymentStatus(true, "Payment successful!!");
}

/**
@notice Withdraws the contract's balance to the owner's address.
@return withdrawal indicating the withdrawal was successful.
*/
function withdrawEarnings() public onlyOwner returns(bool) {
// get the amount of Ether stored in this contract
uint256 amount = address(this).balance;
require(amount > 0, "Empty Balance");
// send all Ether to owner
(bool withdrawal,) = owner.call{value: amount}("");
require(withdrawal, "Failed to send Ether");
return withdrawal;
}

// Function to transfer Ether from this contract to address from input
function transfer(address payable _to, uint256 _amount) public onlyOwner {
(bool success,) = _to.call{value: _amount}("");
require(success, "Failed to send Ether");
}
}
8 changes: 0 additions & 8 deletions contracts/Student.sol

This file was deleted.

146 changes: 68 additions & 78 deletions contracts/StudentRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,96 +1,86 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "./Ownable.sol";
import "./Student.sol";


import "contracts/modifiers/Ownable.sol";
import "./IStudentRegistry.sol";
/**
* @title MyStudentRegistry
* @dev This contract acts as a proxy to interact with another StudentRegistry contract.
* It forwards calls to the `StudentRegistry` contract for student management functions.
*/
contract StudentRegistry is Ownable {
//custom erros
error NameIsEmpty();
error UnderAge(uint8 age, uint8 expectedAge);

//custom data type


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

mapping(address => Student) public studentsMapping;


modifier isNotAddressZero() {
require(msg.sender != address(0), "Invalid Address");
_;
address public studentsContractAddress;

/**
* @dev Set the address of the Students contract.
* @param _studentsContractAddr The address of the Student contract.
*/
constructor(address _studentsContractAddr) {
studentsContractAddress = _studentsContractAddr;
}

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

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
});

students.push(student);
// add student to studentsMapping
studentsMapping[_studentAddr] = student;
/**
* @dev Register a student.
* @param _studentAddress The address of the student to register.
* @param _name The name of the student.
* @param _age The age of the student.
*/
function addStudents(address _studentAddress, string memory _name, uint8 _age) public {
IStudentRegistry(studentsContractAddress).addStudent(_studentAddress, _name, _age);
}

function getStudent(uint8 _studentId)
public
view
isNotAddressZero
returns (Student memory)
{
return students[_studentId - 1];
/**
* @dev Retrieve a student record.
* @param _studentAddr The address of the student to retrieve.
*/
function getStudents(address _studentAddr) public view returns (Student memory) {
return IStudentRegistry(studentsContractAddress).getStudent(_studentAddr);
}

function getStudentFromMapping(address _studentAddr)
public
view
isNotAddressZero
returns (Student memory)
{
return studentsMapping[_studentAddr];
/**
* @dev Update a student record.
* @param _studentAddr The new address of the student.
* @param _name The new name of the student.
* @param _age The new age of the student.
*/
function updateStudents(
address _studentAddr,
string memory _name,
uint8 _age
) public {
IStudentRegistry(studentsContractAddress).updateStudent(_studentAddr, _name, _age);
}

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

// delete studentsMapping[_studentAddr];
/**
* @dev Delete a student record.
* @param _studentAddr The address of the student to delete.
* @notice only owner can delete a student
*/
function removeStudent(address _studentAddr) public {
IStudentRegistry(studentsContractAddress).deleteStudent(_studentAddr);
}

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

studentsMapping[_studentAddr] = student;
/**
@notice Pay Fees.
@dev This function calls the payFees function in the external contract..
*/
function payFees() public payable {
return IStudentRegistry(studentsContractAddress).payFees{value: msg.value}();
}


function modifyOwner(address _newOwner) public {
changeOwner(_newOwner);
/**
@notice Withdraws the contract's balance from the external Student Registry contract.
@dev This function calls the withdraw function in the external contract.
@return success A boolean value indicating whether the withdrawal was successful.
*/
function withdrawEarnings() public returns (bool) {
return IStudentRegistry(studentsContractAddress).withdrawEarnings();
}
}

/**
* @dev Allow the contract to receive Ether.
*/
receive() external payable { }
}
Loading