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

Counter App #38

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
11 changes: 9 additions & 2 deletions contracts/IStudentRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 8 additions & 4 deletions contracts/MyStudentRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions contracts/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
_;
}
Expand All @@ -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");
}



}
2 changes: 2 additions & 0 deletions contracts/Student.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ pragma solidity ^0.8.24;
string name;
uint256 studentId;
uint8 age;
bool hasPaid;
}

93 changes: 75 additions & 18 deletions contracts/StudentRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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)
Expand All @@ -55,16 +95,16 @@ contract StudentRegistry is Ownable {
isNotAddressZero
returns (Student memory)
{
return students[_studentId - 1];
}
return permanentRegister[_studentId - 1];
}

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

function deleteStudent(address _studentAddr)
Expand All @@ -73,7 +113,7 @@ contract StudentRegistry is Ownable {
isNotAddressZero
{
require(
studentsMapping[_studentAddr].studentAddr != address(0),
permanentStudentsMapping[_studentAddr].studentAddr != address(0),
"Student does not exist"
);

Expand All @@ -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;
}


}
3 changes: 3 additions & 0 deletions frontend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
53 changes: 53 additions & 0 deletions frontend/app/abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export const abi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "action",
"type": "string"
},
{
"indexed": false,
"internalType": "uint256",
"name": "from",
"type": "uint256"
}
],
"name": "Log",
"type": "event"
},
{
"inputs": [],
"name": "decrement",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
Binary file added frontend/app/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions frontend/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
Loading