Solidity: getStudentNotifications Retrieves notifications to other metamask account instead of one metamask account

33 views Asked by At

I have two metamask accounts, one is Vamshi (student) and Mike(University). I use Vamshi's account to call the function sendRecordsToUniversity and the university address, I use here is Mike's. so the notifications are stored in Mike's info. But when I call the function getStudentNotifications using mike's account, I get the empty array and while using Vamshi's account I get the notifications. But the data should be displayed in mike's account. Help me with this. Here, this is the smart contract I wrote

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

import "hardhat/console.sol";

contract Agent {
     event ContractDeployed(address indexed _contractAddress);

    constructor() {
        emit ContractDeployed(address(this));
        
    }
    
    
    struct student {
        string fname;
        string lname;
        string university;
        uint enroll;
        uint gender;
        string branch;
        string phone;
        string emailId;
    }

   struct university {
    string name;
    string uaddress;
    string emailId;
    string record;
    address[] studentList;
    string[][] studentNotifications;  // Array to store student notifications
    string[] otherPartyNotifications; // Array to store other party notifications
   }

    struct otherP{
        string name;
        string emailId;
        string record;
        string oaddress;
    }

    
    uint creditPool;

   string[] public studentNames; 
   string[] public universityNames; 
   string[] public otherPNames; 

    mapping(string => address) public studentList;
    mapping(string => address) public universityList;
    mapping(string => address) public otherPList;

    mapping(address => student) studentInfo;
    mapping(address => university) universityInfo;
    mapping(address => otherP) otherPInfo;
    mapping(address => address) Empty;
    mapping(address => mapping(uint => string)) universityEnrollToHash;

    // might not be necessary
    // mapping(address => string) patientRecords;

    function add_student(
        string memory _fname,
        string memory _lname,
        string memory _university,
        uint _enroll,
        uint _gender,
        string memory _branch,
        string memory _phone,
        string memory _emailId
        
    ) public returns (string memory) {
        address addr = msg.sender;
        student memory s;
        s.fname = _fname;
        s.lname=_lname;
        s.university=_university;
        s.enroll=_enroll;
        s.gender=_gender;
        s.branch=_branch;
        s.phone=_phone;
        s.emailId = _emailId;
        
        studentInfo[msg.sender] = s;
        studentList[_fname]=addr;
        studentNames.push(_fname);
        return _fname;
    }

    function add_university(
        string memory _name,
        string memory _uaddress,
        string memory _emailId
    ) public returns (string memory) {
        address addr = msg.sender;
        university memory u;
        u.name = _name;
        u.uaddress = _uaddress;
        u.emailId = _emailId;
       
        universityInfo[msg.sender] = u;
        universityList[_name]=addr;
        universityNames.push(_name);
        return u.name;
    }
function add_otherP(
        string memory _name,
        string memory _oaddress,
        string memory _emailId
       
    ) public returns (string memory){
        address addr = msg.sender;
        otherP memory o;
        o.name = _name;
        o.oaddress=_oaddress;
        o.emailId = _emailId;
        // o.record = _hash;
        otherPInfo[msg.sender] = o;
        otherPList[_name]=addr;
        otherPNames.push(_name);
        return o.name;
    }
   event Received(address sender, uint256 value);
    // Fallback function
    receive() external payable {
        emit Received(msg.sender, msg.value);
    }
    function get_student(address addr) public view returns (student memory) {
    return studentInfo[addr];
}


    function get_university(
        address addr
    )
        public
        view
        returns (
            string memory,
            string memory,
            string memory
        )
    {
        return (
            universityInfo[addr].name,
            universityInfo[addr].uaddress,
            universityInfo[addr].emailId

        );
    }
    function get_otherP(
        address addr
    )
        public
        view
        returns (
            string memory,
            string memory,
            string memory
        )
    {
        return (
            otherPInfo[addr].name,
            otherPInfo[addr].emailId,
            otherPInfo[addr].oaddress

        );
    }

     function getAddressType(address _addr) public view returns (uint) {
    if (bytes(studentInfo[_addr].fname).length > 0) {
        return 1;
    } else if (bytes(universityInfo[_addr].name).length > 0) {
        return 2;
    } else if (bytes(otherPInfo[_addr].name).length > 0) {
        return 3;
    } else {
        return 4;
    }
}
     function get_student_list() public view returns (string[] memory) {
        return studentNames;
    }
    function get_university_list() public view returns (string[] memory) {
        return universityNames;
    }
    function get_otherP_list() public view returns (string[] memory) {
        return otherPNames;
    }
    //for approving and storing in the university records
    function approveRecord(
        
    uint _enroll,
    string memory _hash
    ) public {
    address addr = msg.sender;
    // Store the hash using the retrieved university address and enrollment number
    universityEnrollToHash[addr][_enroll] = _hash;
   }
   event StudentNotificationAdded(string universityName, address universityAddress, string[][] notifications);

   function sendRecordsToUniversity( string memory ipfsHash) public returns (string memory, address) {
    // Check if the university exists
    address addr = msg.sender;
    string memory universityName=studentInfo[addr].university;
    address universityAddress =universityList[universityName];
    uint enroll=studentInfo[addr].enroll;
    
    // Add the IPFS hash to the university's notifications array
   string[] memory notification = new string[](2);
    notification[0] = uint2str(enroll); // Convert uint to string
    notification[1] = ipfsHash;
    
    universityInfo[universityAddress].studentNotifications.push(notification);
    // Emit an event with the fetched notifications
    emit StudentNotificationAdded(universityName, universityAddress, universityInfo[universityAddress].studentNotifications);
   
    // Return universityName and universityAddress
    return (universityName, universityAddress);
   }
// Function to convert uint to string (helper function)
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
    if (_i == 0) {
        return "0";
    }
    uint j = _i;
    uint len;
    while (j != 0) {
        len++;
        j /= 10;
    }
    bytes memory bstr = new bytes(len);
    uint k = len;
    while (_i != 0) {
        k = k-1;
        uint8 temp = (48 + uint8(_i - _i / 10 * 10));
        bytes1 b1 = bytes1(temp);
        bstr[k] = b1[0];
        _i /= 10;
    }
    return string(bstr);
}
function getStudentNotifications() public view returns (string[][] memory) {
    address addr = msg.sender;
    string memory universityName = studentInfo[addr].university;
    address universityAddress = universityList[universityName];
    
    return universityInfo[universityAddress].studentNotifications;
}
function deleteStudentNotification(address addr, uint index) public {
    string memory universityName = studentInfo[addr].university;
    address universityAddress = universityList[universityName];
    
    require(index < universityInfo[universityAddress].studentNotifications.length, "Index out of bounds");

    // Delete the array at the specified index
    delete universityInfo[universityAddress].studentNotifications[index];
}

   function getHashByEnrollAndUniversity(string memory universityName, uint _enroll) public view returns (string memory) {
    // Retrieve the address of the university using the universityName
    address universityAddress = universityList[universityName];
    // Ensure that the university exists
    require(universityAddress != address(0), "University not found");
    // Retrieve the hash associated with the given enrollment number and university address
    string memory hash = universityEnrollToHash[universityAddress][_enroll];
    
    return hash;
}

 }

And this is the code block i use to call the functions

const handleSubmit = async () => {
    try {

      const result = await ipfs.add(JSON.stringify(values));
      const ipfsHash = result.path;

      submitToUniversity(ipfsHash);

    } catch (error) {
      console.error('Error while submitting to IPFS:', error);
      toast.error("An error occurred while uploading the document");
    }
  }
  const submitToUniversity = async (ipfsHash) => {
    try {
      const response = await contract.sendRecordsToUniversity( ipfsHash);
     
      contract.on("StudentNotificationAdded", (universityName, universityAddress, notifications) => {
        console.log("Student notification added - University Name:", universityName);
        console.log("University Address:", universityAddress);
        console.log("Notifications:", notifications);
        toast.success("Successfully submitted to university");
    });
      
      
    }
    catch (err) {
      console.log(err);
      toast.error("Error while submitting to university");
    }
  }

This is to call the getstudentNotification

const fetchStudentNotifications = async () => {
    const publicKey = await signer.getAddress();
    try{
      console.log(publicKey);
      const result = await contract.getStudentNotifications();
   console.log(result);
      setStudent(result);
    }
    catch(err){
      console.log("Error while fetching the notifications", err);
    }
    
  }

I want to get the data to mike's account instead of Vamshi's account

0

There are 0 answers