1

I have been trying for while for accessing functions of one contract in another one as shown below. I actually want to call the functions changeName() and receiveName() defined in the foreignContract{} from myContract{}. The transaction is getting mined but the execution is getting failed.

pragma solidity ^0.4.0;

contract myContract {
    foreignContract created;

    function myContract() {
        created = foreignContract(msg.sender);
    }

    function changeNameMycontract(string name) returns(bool, address) {
        return (created.changeName(name), msg.sender);    
    }

    function ReturnName()returns(string) {
        return created.receiveName();
    }
}

contract foreignContract {
    string name;

    function changeName(string Name) returns(bool) {
        name = Name;
        return true;
    }

    function receiveName() returns(string) {
        return name;
    }
}

The error that I get is

Transact to myContract.changeNameMycontract errored:VM error.reverted revert the transaction has been reverted to the initial state. Note: the constructor should be payable if you send value.

I'm new to this solidity and blockchain concepts - any help would be really appreciated

  • Change your function to: function changeNameMycontract(string name) payable returns(bool,address) – Soggiorno Jun 27 '18 at 17:18
  • @Soggiorno Still the same error is there – Harikrishnan S Jun 27 '18 at 17:25
  • How are you calling the function? If you call it by sending a transaction the function should have the payable modifier. I would recommend calling it using an external call (solidity.readthedocs.io/en/v0.4.21/contracts.html) – Soggiorno Jun 27 '18 at 17:28
  • i have put the payable modifier in both changeNameMyContract() and ReturnName(). If you could please explain exactly what the use of payable modifier? – Harikrishnan S Jun 27 '18 at 17:32
  • If you send ETH to a contract that has no function with a payable modifier the transaction will be reverted. You don't need to send ETH to it though, as you can just use the call syntax (web3js.readthedocs.io/en/1.0/…) – Soggiorno Jun 27 '18 at 17:36
0
pragma solidity ^ 0.4.24;


import "./foreignContract.sol";

contract myContract {

 foreignContract created;
 constructor() public payable {
  created = new foreignContract();
 }

 function changeNameMycontract(string name) public returns(bool) {
  return created.changeName(name);

 }

 function ReturnName() public view returns(string) {
  return created.receiveName();
 }

}
 contract foreignContract {
 string name;


 function changeName(string Name) public returns(bool) {
  name = Name;
  return true;
 }

 function receiveName() public view returns(string) {
  return name;
 }
}

Try out the following corrected code It will compile and run without any Error. No payable modifier is required for the code as you are not sending any ETH, the Only modification was using "new" keyword for creating contract instance.

  • CHANGE this created = foreignContract(msg.sender); TO created = new foreignContract(); and it will work – Shwet Jun 27 '18 at 17:54

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.