2

Please help me!

Lets say I have two contracts deployed at different times in solidity in mist-wallet:

contract E{
    address public sender;
    uint public x;

    function set(uint _x) {
        x = _x;
        sender = msg.sender;
    }

    function get() constant returns(uint) {
        sender = msg.sender;
        return x;
    }
}

and say the address is 0x98789aD9797ad908097ad9870987DDD97897aD (this is a made up address by the way)

Now I want to have another contract to communicate with E and manipulate the x parameter from time to time

contract F {
    uint public x;
    address addressE = 0x98789aD9797ad908097ad9870987DDD97897aD ;
    address public sender;

    function F() {
    }

    function setE(uint num) returns(bool) {
         x=num;
        return addressE.callcode('set(uint256)', num);
    }

    function getE() returns(uint) {
        return ?????????????;  /*  WHAT DO I PUT HERE? */
    }

}

In the getE() function, what code do I put so it can get value of x from addressE?

please let me know, thanks sorry if this is a very simple question

6

Since you asked for simple, I thought I'd simplify.

Contract E is compiled and deployed separately. I added an event log to make it easier to see what's happening (e.g. in Remix).

contract E {
  uint public x;

  event LogUpdate(uint newXValue);

  function set(uint _x) public returns(bool success) {
    x = _x;
    LogUpdate(x);
    return true;
  }

}

Now you want to deploy a second contract that will talk to it. Since you know the interface, you can do that.

You can do it this way:

return addressE.call(bytes4(sha3('set(uint256)')), num);

On grounds of readability, I would avoid that unless the following can't work for some reason.

In the second source file (with F), describe the interface to "E".

contract EInterface {
    uint public x;
    function set(uint _x) returns(bool success) {} 
}

And let the compiler sort out the function signatures:

EInterface e = EInterface(addressE);

There will a thing called "e" of type EInterface which is an EInterface located at addressE (where addressE is the address of previously deployed contract).

Call the functions with ease:

return e.set(num);
return e.x();

Make life easy, and pass E's address into F's constructor.

function F(address EAddress)

In a single source file to simplify rudimentary testing.

pragma solidity ^0.4.6;

// Compile and deploy E by itself. 

contract E {

    uint public x;

    event LogUpdate(uint newXValue);

    function set(uint _x) public returns(bool success) {
        x = _x;
        LogUpdate(x);
        return true;
    }

}

// These go in one source file.
// The compiler can "see" the E Interface as it compiles F.

contract EInterface {
    uint public x;
    function set(uint _x) returns(bool success) {} // undefined function is part of the "Interface". 
}

contract F {

    address addressE;

    // E is already deployed.
    // Conveniently pass E's address to F

    function F(address EAddress) {
        addressE = EAddress;
    }

    function setE(uint num) public returns(bool) {
        // valid ... 
        // return addressE.call(bytes4(sha3('set(uint256)')), num);
        EInterface e = EInterface(addressE); // "EInterface" is understood from interface code above.
        return e.set(num);
    }

    function getE() public constant returns(uint E) {
        EInterface e = EInterface(addressE);
        return e.x();  /*  WHAT DO I PUT HERE? */
    }

}

In Remix to show it working.

Create E

enter image description here

Create F, Set X

enter image description here

Get X

enter image description here

Hope it helps.

  • Thank you so much Rob! Yes it helped very much. Now I'm wondering, what if the uint in contract E you don't want it to be public, but protected where only a few contracts can access it, like contract F. How would you set this up? – shams May 3 '17 at 23:08
  • Thanks for working example! Your answer should get more upvotes. – Honor LT Aug 28 '17 at 8:27

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.