Skip to content

feat: add receive and fallback functions with their respective tests #66

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

Open
wants to merge 13 commits into
base: crowdfunding
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
4 changes: 3 additions & 1 deletion PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<!-- Thank you for your interest in contributing to BlockHeaderWeb3! -->

Assignment 1 link [here](https://github.com/BlockheaderWeb3-Community/cohort-6/blob/main/assignments/Assignment1.md)

Assignment 2 link [here](https://github.com/BlockheaderWeb3-Community/cohort-6/blob/main/assignments/Assignment2.md)

<!-- Describe the changes introduced in this pull request. -->
<!-- Include any context necessary for understanding the PR's purpose. -->
Expand All @@ -12,4 +15,3 @@

- [ ] Tests
- [ ] Documentation

Empty file added contracts/counter.frankhood.sol
Empty file.
78 changes: 78 additions & 0 deletions contracts/counter_alex.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;

/**
* @title Counter Contract
* @notice A simple counter contract that supports incrementing and decrementing operations.
* @dev Provides functions to increment, decrement and adjust the counter by a specified value.
*/

contract Counter {
/**
* @notice The current count value.
* @dev Public state variable initialized to zero.
*/
uint public countVal = 0;

/**
* @notice Retrieves the current count value.
* @return The current counter value.
*/
function getCount() public view returns (uint) {
return countVal;
}

/**
* @notice Increments the counter by one.
* @dev Ensures that the counter does not exceed the maximum value defined by runner().
*/
function increment() public {
uint maxValue = runner();
require(countVal < maxValue);
countVal++;
}

/**
* @notice Decrements the counter by one.
* @dev Prevents the counter from decrementing below zero.
*/
function decrement() public {
require(countVal > 0, "Counter cannot be negative");
countVal--;
}

/**
* @notice Increments the counter by a specified positive value.
* @param _val The value to add to the current count.
* @dev Validates that _val is greater than zero and that the resulting count does not exceed the maximum value.
*/
function incrementByVal(uint _val) public {
uint maxValue = runner();
require(_val > 0, "Must be greater than zero");
require(countVal + _val <= maxValue, "Counter cannot be negative");
countVal += _val;
}

/**
* @notice Decrements the counter by a specified positive value.
* @param _val The value to subtract from the current count.
* @dev Validates that _val is greater than zero and that the current count is sufficient to subtract _val without underflow.
*/
function decrementByVal(uint _val) public {
require(_val > 0, "Must be greater than zero");
require(countVal >= _val, "Underflow: Cannot decrement below zero"); // Prevent underflow

countVal -= _val;
}

/**
* @notice Computes the maximum value for a uint256.
* @return The maximum possible value of a uint256.
* @dev Uses unchecked arithmetic to derive the maximum uint256 value.
*/
function runner() public pure returns (uint) {
unchecked {
return uint256(0) - 1;
}
}
}
6 changes: 6 additions & 0 deletions contracts/crowdfunding_alex.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import "./reward_token.sol";
import "./reward_nft_alex.sol";
contract CrowdFund {}
6 changes: 6 additions & 0 deletions contracts/reward_nft_alex.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract RewardNFT {
// ...
}
6 changes: 6 additions & 0 deletions contracts/reward_token_alex.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract RewardToken {
// ...
}
158 changes: 158 additions & 0 deletions contracts/student_registry_alex.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;

/// @title StudentRegistry
/// @dev A contract to manage student registration, attendance, and interests.
contract StudentRegister {
/// @dev Enum to represent student attendance status.
enum Attendance {
Absent,
Present
}

/// @dev Struct to represent a student.
struct Student {
string Name; // Name of the student
Attendance attendance; // Attendance status of the student
string[] interest; // List of interests of the student
}

/// @dev Address of the contract owner.
address public owner;

/// @dev Mapping to store student data by their address.
mapping(address => Student) public students;

/// @dev Event emitted when a new student is registered.
event StudentCreated(address indexed _studentAddress, string _name);

/// @dev Event emitted when a student's attendance status is updated.
event AttendanceStatus(address indexed _studentAddress, Attendance _attendance);

/// @dev Event emitted when a new interest is added to a student's profile.
event InterestAdded(address indexed _studentAddress, string _interest);

/// @dev Event emitted when an interest is removed from a student's profile.
event InterestRemoved(address indexed _studentAddress, string _interest);

/// @dev Modifier to restrict access to the contract owner.
modifier OnlyOwner() {
require(msg.sender == owner, "Owner only function");
_;
}

/// @dev Modifier to ensure a student with the given address exists.
modifier studentExists(address _studentaddr) {
require(bytes(students[_studentaddr].Name).length != 0, "The specified student does not exist.");
_;
}

/// @dev Modifier to ensure a student with the given address does not exist.
modifier studentDoesNotExist(address _studentAddr) {
require(bytes(students[_studentAddr].Name).length == 0, "The specified student already exists.");
_;
}

/// @dev Constructor to set the contract owner.
constructor() {
owner = msg.sender;
}

/// @dev Registers a new student with a name, attendance status, and interests.
/// @param _name The name of the student.
/// @param _attendance The attendance status of the student.
/// @param _interests The list of interests of the student.
function registerStudent(string memory _name, Attendance _attendance, string[] memory _interests) public {
students[msg.sender].Name = _name;
students[msg.sender].attendance = _attendance;
students[msg.sender].interest = _interests;

emit StudentCreated(msg.sender, _name);
}

/// @dev Registers a new student with a default attendance status (Absent) and no interests.
/// @param _name The name of the student.
function registerNewStudent(string memory _name) public studentDoesNotExist(msg.sender) {
require(bytes(_name).length > 0, "The provided name is empty.");
students[msg.sender] = Student({Name: _name, attendance: Attendance.Absent, interest: new string[](0)});

emit StudentCreated(msg.sender, _name);
}

/// @dev Marks the attendance of a student.
/// @param _address The address of the student.
/// @param _attendance The attendance status to set (Absent or Present).
function markAttendance(address _address, Attendance _attendance) public studentExists(_address) {
students[_address].attendance = _attendance;
emit AttendanceStatus(_address, _attendance);
}

/// @dev Adds an interest to a student's profile.
/// @param _address The address of the student.
/// @param _interest The interest to add.
function addInterests(address _address, string memory _interest) public studentExists(_address) {
require(bytes(_interest).length > 0, "The provided interest is empty.");
require(bytes(_interest).length <= 5, "Maximum of 5 interests can be added.");

for (uint256 i = 0; i < students[_address].interest.length; i++) {
require(
keccak256(bytes(students[_address].interest[i])) != keccak256(bytes(_interest)),
"Interest already exists."
);
}
students[_address].interest.push(_interest);
emit InterestAdded(_address, _interest);
}

/// @dev Removes an interest from a student's profile.
/// @param _addr The address of the student.
/// @param _interest The interest to remove.
function removeInterest(address _addr, string memory _interest) public studentExists(_addr) {
require(bytes(students[_addr].Name).length >= 0, "Interest cannot be empty.");
bool indexFound = false;
uint indexToRemove;
for (uint i = 0; i < students[_addr].interest.length; i++) {
if (keccak256(bytes(students[_addr].interest[i])) == keccak256(bytes(_interest))) {
indexFound = true;
indexToRemove = i;
break;
}
}
require(indexFound, "The provided interest does not exist.");

// Move the last element into the place of the element to remove
students[_addr].interest[indexToRemove] = students[_addr].interest[students[_addr].interest.length - 1];

// Remove the last element
students[_addr].interest.pop();

emit InterestRemoved(_addr, _interest);
}

/// @dev Retrieves the name of a student.
/// @param _addr The address of the student.
/// @return The name of the student.
function getStudentName(address _addr) public view studentExists(_addr) returns (string memory) {
return students[_addr].Name;
}

/// @dev Retrieves the attendance status of a student.
/// @param _address The address of the student.
/// @return The attendance status of the student.
function getStudentAttendance(address _address) public view studentExists(_address) returns (Attendance) {
return students[_address].attendance;
}

/// @dev Retrieves the list of interests of a student.
/// @param _address The address of the student.
/// @return The list of interests of the student.
function getStudentInterests(address _address) public view studentExists(_address) returns (string[] memory) {
return students[_address].interest;
}

/// @dev Transfers ownership of the contract to a new address.
/// @param _newOwner The address of the new owner.
function transferOwnership(address _newOwner) public OnlyOwner {
owner = _newOwner;
}
}
70 changes: 70 additions & 0 deletions contracts/with-foundry/Counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract Counter {
uint256 public number;
address public owner;

constructor() {
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}

function setNumber(uint256 newNumber) public {
number = newNumber;
}

// test increment function
function increment() public onlyOwner {
number++;
}

// test decrement function
function decrement() public onlyOwner {
number--;
}

// test add by value function
function add(uint256 x) public onlyOwner {
number += x;
}

// test substract by value function
function sub(uint256 x) public onlyOwner {
number -= x;
}

// test multiply by value function
function mul(uint256 x) public onlyOwner {
number *= x;
}

// test divide by value function
function div(uint256 x) public onlyOwner {
number /= x;
}

// test if a number is even
function isEven(uint256 x) public view onlyOwner returns (bool) {
return x % 2 == 0;
}

// test if a number is odd
function isOdd(uint256 x) public view onlyOwner returns (bool) {
return x % 2 != 0;
}

// test reset function
function reset() public onlyOwner {
number = 0;
}

// test get total function
function getTotal() public view onlyOwner returns (uint256) {
return number;
}
}
Loading