0
// SPDX-License-Identifier: MIT

//SPDX-Licence-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract ChatApp{
    // USER STRUCT
    struct user{
        string name;
        friend[] friendlist;
    }
    struct friend{
        address pubkey;
        string name;
    }

    struct message{
        address sender;
        uint256 timestamp;
        string msg;
    }
    struct AllUserStruck{
        string name;
        address accountAddress;
    }
    AllUserStruck[] getAllUsers;
    mapping(address => user) userlist;
    mapping(bytes32 => message[]) allMessages;
    function checkUserExists(address pubkey) public view returns(bool){
        return bytes(userList[pubkey].name).length > 0;
    }
    //CREATE ACCOUNT
    function createAccount(string calldata name) external {
        require(theUserExists(msg.sender) == false, "User already exists");
        require(bytes(name).length>0, "Username cannot be empty");
        userList[msg.sender].name = name;
        getAllUsers.push(AllUsersStruck(name, msg.sender));
    }
    //GET USERNAME
    function getUsername(address pubkey) external view returns(string memory){
        
    }
    function addFriend(address friend_key, string calldata name) external{
        require(checkUserExists(msg.sender), "Create an account first");
        require(checkUserExists(friend_key), "User is not registered!");
        require(msg.sender|=friend_key, "Users cannot add themselves as friend");
        require(checkAlreadyFriend(msg.sender, friend_key)== false, "These users are already friends");

        _addFriend(msg.sender, friend_key, name);
        _addFriend(friend_key, msg.sender, userList[msg.sender].name);
    }
    //checkAlreadyFriends
    function checkAlreadyFriend (address pubkey1, address pubkey2) internal view returns(bool){
        if(userList[pubkey1].friendList.length > userList[pubkey2].friendList.length){
            address tmp = pubkey1;
            pubkey1 = pubkey2;
            pubkey = tmp;
        } 
        for(uint256 i = 0; i < userList[pubkey].friendList.length; i++){
            if(userList[pubkey1].friendList[i].pubkey == pubkey2)return true;
        }
        return false;
    }
    function _addFriend(address me, address friend_key, string memory name) internal{
        friend memory newFriend = friend(friend_key, name);
        userList[me].friendList.push(newFriend);
    }
    //GET MY FRIEND
    function getMyFriendList() external view returns(friend[] memory){
        return userList[msg.sender].friendList;
    }
    //get chat code
    function _getChatCode(address pubkey1, address pubkey2) internal pure returns(bytes32){
        if(pubkey1 < pubkey2){
            return keccak256(abi.encodePacked(pubkey1, pubkey2));
        } else return keccak256(abi.encodePacked(pubkey1, pubkey2));
    }
    //SEND MESSAGE
    function sendMessage(address friend_key, string calldata _msg ) external{
        require(checkUserExists(msg.sender), "Create an account first");
        require(checkUserExists(friend_key), "User is not registered");
        require(checkAlreadyFriends(msg.sender, friend_key), "You are not friend with the given user");
        bytes32 chatCode = _getChatCode(msg.sender, friend_key);
        message memory newMsg = message(msg.sender, block.timestamp, _msg);
        allMessages[chatCode].push(newMsg);
    }
    //READ MESSAGE
    function readMessage (address friend_key) external view returns(message[] memory){
        bytes32 chatCode = _getChatCode(msg.sender, friend_key);
        return allMessages[chatCode];
    }
    function getAllUsers() public view returns(AllUsersStruck[]-memory){
        return getAllUsers
    }
    

}
2
  • The error says you are missing a ; in some place.
    – Ismael
    Feb 3, 2023 at 14:09
  • I checked the full code didn't find it can you help
    – tikker
    Feb 3, 2023 at 14:16

3 Answers 3

0

At least the function getAllUsers has several errors: an extra - (dash), missing a ;.

AllUserStruck[] _getAllUsers;


function getAllUsers() public view returns(AllUsersStruck[] memory) {
    return _getAllUsers;
};
1
  • 1
    Also there is an array with that name so it will fail Feb 3, 2023 at 15:52
0
//There are various typos in this code and incorrect naming maybe correcting them might help like:

require(theUserExists(msg.sender) == false, "User already exists");  
      //statement should be                             
require(checkUserExists(msg.sender) == false, "User already exists");

`require(msg.sender|=friend_key, "Users cannot add themselves as friend");` 
//replace it with this
 `require(msg.sender != friend_key, "Users cannot add themselves as friend");`

`function checkAlreadyFriend (address pubkey1, address pubkey2) internal view returns(bool);`                     
//Function name is missing `s`               
`function checkAlreadyFriends (address pubkey1, address pubkey2) internal view returns(bool);`
// these are some I noticed you might want to check the code properly. if you have written it by yourself recheck the naming of the functions;
0
// here is your full corrected code It compiled without any errors check after deploying t=it on the frontend if it is working properly or not and do share the final results😊

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract ChatApp {
    // USER STRUCT
    struct User {
        string name;
        Friend[] friendList;
    }
    struct Friend {
        address pubkey;
        string name;
    }

    struct Message {
        address sender;
        uint256 timestamp;
        string msg;
    }

    struct AllUserStruct {
        string name;
        address accountAddress;
    }

    AllUserStruct[] public allUsers;
    mapping(address => User) public userList;
    mapping(bytes32 => Message[]) public allMessages;

    function checkUserExists(address pubkey) public view returns (bool) {
        return bytes(userList[pubkey].name).length > 0;
    }

    //CREATE ACCOUNT
    function createAccount(string memory name) external {
        require(checkUserExists(msg.sender) == false, "User already exists");
        require(bytes(name).length > 0, "Username cannot be empty");
        userList[msg.sender].name = name;
        allUsers.push(AllUserStruct(name, msg.sender));
    }

    //GET USERNAME
    function getUsername(address pubkey) external view returns (string memory) {
        return userList[pubkey].name;
    }

    function addFriend(address friend_key, string memory name) external {
        require(checkUserExists(msg.sender), "Create an account first");
        require(checkUserExists(friend_key), "User is not registered!");
        require(
            msg.sender != friend_key,
            "Users cannot add themselves as friend"
        );
        require(
            checkAlreadyFriend(msg.sender, friend_key) == false,
            "These users are already friends"
        );

        _addFriend(msg.sender, friend_key, name);
        _addFriend(friend_key, msg.sender, userList[msg.sender].name);
    }

    //checkAlreadyFriends
    function checkAlreadyFriend(address pubkey1, address pubkey2)
        internal
        view
        returns (bool)
    {
        if (
            userList[pubkey1].friendList.length >
            userList[pubkey2].friendList.length
        ) {
            address tmp = pubkey1;
            pubkey1 = pubkey2;
            pubkey2 = tmp;
        }

        for (uint256 i = 0; i < userList[pubkey1].friendList.length; i++) {
            if (userList[pubkey1].friendList[i].pubkey == pubkey2) return true;
        }
        return false;
    }

    function _addFriend(
        address me,
        address friend_key,
        string memory name
    ) internal {
        Friend memory newFriend = Friend(friend_key, name);
        userList[me].friendList.push(newFriend);
    }

    //GET MY FRIEND
    function getMyFriendList() external view returns (Friend[] memory) {
        return userList[msg.sender].friendList;
    }

    //get chat code
    function _getChatCode(address pubkey1, address pubkey2)
        internal
        pure
        returns (bytes32)
    {
        if (pubkey1 < pubkey2) {
            return keccak256(abi.encodePacked(pubkey1, pubkey2));
        } else return keccak256(abi.encodePacked(pubkey2, pubkey1));
    }

    //SEND MESSAGE
    function sendMessage(address friend_key, string memory _msg) external {
        require(checkUserExists(msg.sender), "Create an account first");
        require(checkUserExists(friend_key), "User is not registered");
        require(
            checkAlreadyFriend(msg.sender, friend_key),
            "You are not friend with the given user"
        );
        bytes32 chatCode = _getChatCode(msg.sender, friend_key);
        Message memory newMsg = Message(msg.sender, block.timestamp, _msg);
        allMessages[chatCode].push(newMsg);
    }

    //READ MESSAGE
    function readMessage(address friend_key)
        external
        view
        returns (Message[] memory)
    {
        bytes32 chatCode = _getChatCode(msg.sender, friend_key);
        return allMessages[chatCode];
    }

    function getAllUsers() public view returns (AllUserStruct[] memory) {
        return getAllUsers();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.