0

Hey I am making a smart contract that create another smart contracts. Organization.sol create Session.sol . When I create a session using createdSession function I got it's address and I already had the abi. Now I need to deploy the contract from the browser(to use function like take_feedback or seeResult). Could anyone tell me how please. I run my contract in localhost using truffle develop command and MetaMask.

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);
     }
}

Web3js Code

    initSession: async function(_address) {
            var abi = web3.eth.contract(SessionArtifact.abi);
            var session = abi.at(_address);
            alert("your session is created!!") 
             /*Deploy Contract here!*/   
               //need to deploy contract first before calling functions
            /*Call  function */
            await session.take_feedback('0xa8ff46045fa2c6a0af361819b62126e1b0ec8909', 5).send({ from: this.account });

        },

1 Answer 1

0

If you have the address of the contract it is most likely already deployed.

You want to access the deployed contract I guess. For that you can just create a new contract with web3 and pass the address as the second parameter.

var sessionAddress = await organizationContract.createdSession(...);
var sessionContract = web3.eth.contract(SessionArtifact.abi, sessionAddress);
var creator = await sessionContract.methods.creator().call();
7
  • I edited my answer, try again please. Commented May 2, 2019 at 20:44
  • Unfortunately, It keep telling me "' creator method undefined ''' Commented May 2, 2019 at 21:04
  • 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(); var sessionContract = web3.eth.contract(SessionArtifact.abi, sessionAddress); var creator = await sessionContract.methods.creator.call(); console.log(creator); Commented May 2, 2019 at 21:04
  • I am sorry for how the code look like. I tried to make it more readable. use this link pastebin.com/xsqGp65Y Commented May 2, 2019 at 21:11
  • Try var creator = await sessionContract.methods.creator().call(); Commented May 2, 2019 at 21:52

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.