diff --git a/contracts/IStudentRegistry.sol b/contracts/IStudentRegistry.sol index b87e8bbe..0c169528 100644 --- a/contracts/IStudentRegistry.sol +++ b/contracts/IStudentRegistry.sol @@ -7,11 +7,18 @@ interface IStudentRegistry { function addStudent( address _studentAddr, string memory _name, - uint8 _age - ) external; + uint8 _age, + uint256 _price + ) external payable; function getStudent(uint8 _studentID) external view returns (Student memory); function getStudentFromMapping(address _studentAddr) external view returns (Student memory); + function confirmAllStudents() external payable; + +} + +interface IOwnable{ + function getBalance() external view returns (uint256); } diff --git a/contracts/MyStudentRegistry.sol b/contracts/MyStudentRegistry.sol index 10562f12..f0b9123d 100644 --- a/contracts/MyStudentRegistry.sol +++ b/contracts/MyStudentRegistry.sol @@ -8,19 +8,23 @@ contract MyStudentRegistry is Ownable { address private StudentRegistryContractAddress; - constructor(address _studentRgistry){ - StudentRegistryContractAddress = _studentRgistry; + constructor(address _studentRegistry){ + StudentRegistryContractAddress = _studentRegistry; } function registerStudent( address _studentAddr, string memory _name, uint8 _age - ) public onlyOwner { + ) public payable { - IStudentRegistry(StudentRegistryContractAddress).addStudent(_studentAddr, _name, _age); + require(msg.value == 1 ether, "You need to send exactly 1 ether to register"); + IStudentRegistry(StudentRegistryContractAddress).addStudent{value: msg.value}(_studentAddr, _name, _age, msg.value); } + function confirmAllStudents() public payable { + IStudentRegistry(StudentRegistryContractAddress).confirmAllStudents(); + } function getStudent2( uint8 _studentId diff --git a/contracts/Ownable.sol b/contracts/Ownable.sol index 624bf251..097b734a 100644 --- a/contracts/Ownable.sol +++ b/contracts/Ownable.sol @@ -5,15 +5,17 @@ pragma solidity >=0.7.0 <0.9.0; contract Ownable { address private owner; + uint256 public fee; event ChangeOwner(address indexed oldOwner, address indexed newOwner); - constructor(){ - owner = msg.sender; + constructor() payable { + owner = payable (msg.sender); + fee = 1 ether; } - modifier onlyOwner { + modifier onlyOwner { require(owner == msg.sender, "Caller not owner"); _; } @@ -30,4 +32,18 @@ contract Ownable { emit ChangeOwner(owner, _newOwner); owner = _newOwner; } + + // Function to withdraw all Ether from this contract. + function withdraw() public payable onlyOwner { + // get the amount of Ether stored in this contract + uint256 amount = address(this).balance; + require(amount > 0, "No student has registered yet."); + + // send all Ether to owner + (bool success,) = owner.call{value: amount}(""); + require(success, "Failed to send Ether"); + } + + + } \ No newline at end of file diff --git a/contracts/Student.sol b/contracts/Student.sol index f7246ea0..5bc285bb 100644 --- a/contracts/Student.sol +++ b/contracts/Student.sol @@ -5,4 +5,6 @@ pragma solidity ^0.8.24; string name; uint256 studentId; uint8 age; + bool hasPaid; } + diff --git a/contracts/StudentRegistry.sol b/contracts/StudentRegistry.sol index 591618e1..a6721cfd 100644 --- a/contracts/StudentRegistry.sol +++ b/contracts/StudentRegistry.sol @@ -8,26 +8,40 @@ contract StudentRegistry is Ownable { //custom erros error NameIsEmpty(); error UnderAge(uint8 age, uint8 expectedAge); - - //custom data type - + event Log(address indexed sender, uint256 amount); + //custom data type //dynamic array of students - Student[] private students; + // Student[] private students; + + // Temporary register + Student[] public tempRegister; + mapping(address => Student) private tempStudentsMapping; - mapping(address => Student) public studentsMapping; + // Permanent register + Student[] public permanentRegister; + mapping(address => Student) private permanentStudentsMapping; + receive() external payable {} modifier isNotAddressZero() { require(msg.sender != address(0), "Invalid Address"); _; } + // Register Student - Required to pay 1 ether function addStudent( address _studentAddr, string memory _name, - uint8 _age - ) public isNotAddressZero { + uint8 _age, + uint256 _price + ) public payable isNotAddressZero { + // Ensure the student exists by checking if the studentAddr is not zero address + require(tempStudentsMapping[_studentAddr].studentAddr == address(0), "Student already exists."); + // Check the value sent with the transaction + require(msg.value == fee, "You need to pay exactly 1 ether"); + require(msg.value == _price, "Amount and Msg.Value doesn't match"); + if (bytes(_name).length == 0) { revert NameIsEmpty(); } @@ -36,17 +50,43 @@ contract StudentRegistry is Ownable { revert UnderAge({age: _age, expectedAge: 18}); } - uint256 _studentId = students.length + 1; + uint256 _studentId = tempRegister.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; + // (bool sent,) = address(0).call{value: msg.value}(""); + // require(sent, "Failed to send Ether"); + + // Store the student in the temporary register and mapping + tempRegister.push(student); + tempStudentsMapping[_studentAddr] = student; + } + + + // Function to move all students from the temporary register to the permanent register + function confirmAllStudents() public payable onlyOwner { + // Loop through all students in the tempRegister + for (uint256 i = 0; i < tempRegister.length; i++) { + Student memory student = tempRegister[i]; + + // Add the student to the permanent register and mapping + permanentRegister.push(student); + permanentStudentsMapping[student.studentAddr] = student; + + // Remove the student from the temporary mapping + delete tempStudentsMapping[student.studentAddr]; + } + + // Clear the temporary register array + delete tempRegister; + + // Make withdrawal on confirm + return withdraw(); } function getStudent(uint8 _studentId) @@ -55,8 +95,8 @@ contract StudentRegistry is Ownable { isNotAddressZero returns (Student memory) { - return students[_studentId - 1]; - } + return permanentRegister[_studentId - 1]; + } function getStudentFromMapping(address _studentAddr) public @@ -64,7 +104,7 @@ contract StudentRegistry is Ownable { isNotAddressZero returns (Student memory) { - return studentsMapping[_studentAddr]; + return permanentStudentsMapping[_studentAddr]; } function deleteStudent(address _studentAddr) @@ -73,7 +113,7 @@ contract StudentRegistry is Ownable { isNotAddressZero { require( - studentsMapping[_studentAddr].studentAddr != address(0), + permanentStudentsMapping[_studentAddr].studentAddr != address(0), "Student does not exist" ); @@ -83,14 +123,31 @@ contract StudentRegistry is Ownable { studentAddr: address(0), name: "", age: 0, - studentId: 0 + studentId: 0, + hasPaid: false }); - studentsMapping[_studentAddr] = student; + permanentStudentsMapping[_studentAddr] = student; } function modifyOwner(address _newOwner) public { changeOwner(_newOwner); } + + function getBalance() public view returns (uint256) { + return address(this).balance; + } + + // Function to get the number of permanent registered students + function getPermanentTotalStudents() public view returns (uint256) { + return permanentRegister.length; + } + + // Function to get the number of temp registered students + function getTemperaryTotalStudents() public view returns (uint256) { + return tempRegister.length; + } + + }