1

here a shortcut from my both contracts. Is it possible to read out and change single elements(from contract B) of the struct (Contract A) ? e.g. from contract B like p.submission(_id).studentaddress ? Or is it still the case that a struct is an internal element. What could be a way to read out and change elemtens from contract A from an other contract ?

Contract A:

  uint public SubCount = 0; 
struct Submission {
    uint id;
    address studentsaddress;
    Student student;
    Examiner examiner;
    Thesis thesis;
    // examniner accepts allocation
    bool accepted;
    // examiner finished correction
    bool corrected;
    // examiner marks submission as verified
    bool verified;
}

 // save submission in mapping with id as key
mapping(uint => Submission) public submissions;
   function createSubmission(address _studentsaddress,Student memory _student, Examiner memory _examiner, Thesis memory _thesis) public{
    SubCount ++; 
    submissions[SubCount] = Submission(SubCount,_studentsaddress, _student, _examiner, _thesis, false, false, false); 
    emit SubmissionCreated(SubCount, _studentsaddress, _student, _examiner, _thesis, false, false, false);

Contract B:

Submission p;                          
function setp(address _addr) external {
    p = Submission(_addr);
}

function readSubmission(uint _u) public returns(uint id, address studentsaddress, Student memory student, Examiner memory examiner, Thesis memory thesis, bool accepted, bool corrected, bool verified){
        return(p.submissions(_u)); 
           }
0

As far as I know, you cannot access the storage of a contract from another contract directly. Given contracts C1 and C2, C1 can only access the storage of C2 by invoking a method on C2 that returns data from C2's storage. You can use the following ways to accomplish this:

  1. Inherit contract A in B and then call contract's A method .

  2. Add a function in contract A that updates the address of a student. First deploy contract A and then use the address of the deployed contract by passing this address to setAddressA function. Afterwards use setp to access the changeAddress function inside contract A from contract B. See my example below:

Contract A

pragma solidity ^0.8.1;

contract A{
    uint public SubCount = 0; 
    event SubmissionCreated(uint, address,bool,bool,bool);
    
    struct Submission {
        uint id;
        address studentsaddress;
         //  Student student;
         //   Examiner examiner;
         //   Thesis thesis;
        // examniner accepts allocation
        bool accepted;
        // examiner finished correction
        bool corrected;
        // examiner marks submission as verified
        bool verified;
    }

     // save submission in mapping with id as key
    mapping(uint => Submission) public submissions;

   function createSubmission(address _studentsaddress) public{
        SubCount ++; 
        submissions[SubCount] = Submission(SubCount,_studentsaddress, false, false, false); 
        emit SubmissionCreated(SubCount, _studentsaddress, false, false, false);
   }
   
   function changeAddress(uint _studentid, address _address) external {
       submissions[_studentid].studentsaddress=_address;
   }
}

Contract B

pragma solidity ^0.8.1;
import "./A.sol";

contract B {

    address addressA;
    
    function setAddressA(address _addressA) external{
        addressA = _addressA;
    }
    
    function setp(uint studentid,  address _newStudentAddress) external {
        A a = A(addressA);
        
        a.changeAddress(studentid,_newStudentAddress);
    }
}  
2
  • Hi, thanks for your answer ! Is there also an other way to do it ? – Marc Apr 4 at 16:35
  • @Marc Yes! I have updated my answer. – mahamzaidi Apr 4 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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