Skip to content

Instantly share code, notes, and snippets.

@KBPsystem777
Created February 11, 2025 04:09
Show Gist options
  • Save KBPsystem777/9ce5642b650f49e6802686233bb1dd1c to your computer and use it in GitHub Desktop.
Save KBPsystem777/9ce5642b650f49e6802686233bb1dd1c to your computer and use it in GitHub Desktop.
Classmate Connect Smart Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
/// @title ClassmateConnect - Parent contract for managing student profiles
contract ClassmateConnect {
struct Profile {
string nickname;
string bio;
address studentAddress;
}
struct Comment {
address commenter;
string message;
}
mapping(address => Profile) public profiles;
mapping(address => Comment[]) public comments;
mapping(address => bool) public registeredContracts;
event ProfileRegistered(
address indexed student,
string nickname,
string bio
);
event CommentAdded(
address indexed student,
address indexed commenter,
string message
);
/// @notice Register a student's profile (only callable by a student contract)
function registerProfile(
address _studentAddress,
string memory _nickname,
string memory _bio
) external {
require(
profiles[_studentAddress].studentAddress == address(0),
"Profile already exists"
);
profiles[_studentAddress] = Profile({
nickname: _nickname,
bio: _bio,
studentAddress: _studentAddress
});
emit ProfileRegistered(_studentAddress, _nickname, _bio);
}
/// @notice Get a student's profile
function getProfile(address _studentAddress)
external
view
returns (
string memory,
string memory,
address
)
{
Profile memory profile = profiles[_studentAddress];
require(profile.studentAddress != address(0), "Profile not found");
return (profile.nickname, profile.bio, profile.studentAddress);
}
/// @notice Add a comment to a student's profile
function addComment(
address _studentAddress,
address _commenter,
string memory _message
) external {
require(
profiles[_studentAddress].studentAddress != address(0),
"Profile not found"
);
require(bytes(_message).length > 0, "Comment cannot be empty");
comments[_studentAddress].push(
Comment({commenter: _commenter, message: _message})
);
emit CommentAdded(_studentAddress, _commenter, _message);
}
/// @notice Get comments for a student profile
function getComments(address _studentAddress)
external
view
returns (address[] memory, string[] memory)
{
uint256 length = comments[_studentAddress].length;
address[] memory commenters = new address[](length);
string[] memory messages = new string[](length);
for (uint256 i = 0; i < length; i++) {
commenters[i] = comments[_studentAddress][i].commenter;
messages[i] = comments[_studentAddress][i].message;
}
return (commenters, messages);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment