0

I had created a contract(Organisation.sol) that create other contracts(Session.sol). When I call any method from(Organisation.sol) using web3js it work well. But If I ever tried to call any method from (Session.sol) metamask will give me this error

ALERT: Trying to call a function on a non-contract address.

Solidity Code

pragma solidity 0.5.0;
//Session

contract Session {

      address public creator;  // The address of parent Organization

      string sessionName;
      string description;
      uint startTime;
      uint endTime;
      address[] lecturer;
      address[] attendes; 

      int[] result; 
      mapping(address => int) public attendes_feedback; //institution boardMembers
      modifier onTime(uint _startTime){
            require(now < _startTime);
            _;
      }
      constructor (string memory _sessionName, string memory _description, uint _startTime,uint _endTime,address[] memory   _lecturer,address[] memory  _attendes) public onTime(_startTime){

              sessionName =  _sessionName;
              description = _description;
              startTime = _startTime;
              endTime = _endTime;
              attendes = _attendes;
              lecturer = _lecturer;
              initAttendes(attendes);
      } 

    function initAttendes(address[] memory _attendes) private{
           for(uint i=0 ; i < _attendes.length ; i++){
            attendes_feedback[_attendes[i]] = -1;
        }
    }

    function Time() public view returns (bool){
       return (now >=  startTime  && now <= endTime);          
     }

    modifier checkTime(){
        require(Time());
        _;
      }
    function take_feedback(address _voter,uint8 _feedback)  public checkTime {
          require(attendes_feedback[_voter] != 0);
          attendes_feedback[_voter] = _feedback;
          result[_feedback]++;
    }

  function seeResult() public view returns(int[] memory){
          return result;
  }

 }

contract Organization {

     address creator;
     event sessionnCreated(string name,address sessionAddress ,address creator);

     /*  modifier onlyCreator(){
        require(msg.sender == creator);
        _;
    } */
     function createdSession(
      string memory _sessionName,
      string memory _description,
      uint _startTime,
      uint _endTime,
      address[] memory _lecturer,
      address[] memory _attendes
     ) public   returns(address) {

        Session sessionAddress = new Session(_sessionName , _description , _startTime , _endTime, _lecturer,_attendes );
        emit sessionnCreated(_sessionName,address(sessionAddress),creator);
        return address(sessionAddress);
     }
}

Session Contract initialization:

 var sessionContract = web3.eth.contract(SessionArtifact.abi).at(sessionAddress);

Then I tried to initialize contract in a different way.

var sessionContract = web3.eth.contract(SessionArtifact.abi, sessionAddress);

Then Calling Session Contract Method

sessionContract.take_feedback('0xa8ff46045fa2c6a0af361819b62126e1b0ec8909', 5).call().send({ from: this.account });

Then I tried calling function in a different way.

sessionContract.take_feedback('0xa8ff46045fa2c6a0af361819b62126e1b0ec8909', 5, function(error, result) {
               if (!error) {
                   console.log(result);
               } else
                   console.error(error);
           }); 

But all my attempts give me the same error in metamask:

ALERT: Trying to call a function on a non-contract address.

Edit: How I got the address

createdSession: async function(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes) { 
var sessionAddress; 
const { createdSession } = this.meta.methods; 
await createdSession(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes).send({ from: this.account }); sessionAddress = await createdSession(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes).call(); alert("Contract created successfully contact addresss is " + sessionAddress); 
}, 
  • 1
    sessionAddress is almost certainly wrong. Could you share how you got that address? – user19510 May 4 at 15:29
  • createdSession: async function(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes) { var sessionAddress; const { createdSession } = this.meta.methods; await createdSession(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes).send({ from: this.account }); sessionAddress = await createdSession(_sessionName, _description, _startTime, _endTime, _lecturer, _attendes).call(); alert("Contract created successfully contact addresss is " + sessionAddress); }, – yehia tarek May 4 at 18:31
  • @smarx I had edited the Question with the code It will be more easy to read – yehia tarek May 4 at 18:36
  • @smarx I don't think so Why it could be wrong? I print the output and it's the address? – yehia tarek May 4 at 18:37
  • That code looks wrong. You're essentially getting the address that a second contract WOULD be created at, rather than the address of the one you actually created. – user19510 May 4 at 21:24

Your Answer

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

Browse other questions tagged or ask your own question.