0

I am trying to get two contracts to interact:

pragma solidity ^0.5.10;


contract C2 {

    address public addr;

    constructor(address per) public {
        addr = per;
    }

    function remote_get_number() external view returns(uint) {
        return C1(addr).get_a_number();
    }

    function remote_set_number(uint num) external {
        C1(addr).set_a_number(num);
    }
}

contract C1 {

    uint public a_number;

    function get_a_number() external view returns(uint) {
        return(a_number);
    }

    function set_a_number(uint num) external  {
        a_number = num;
    }
}

I create an instance of C1, then create a C2 instance using the address of the C1 instance as the argument of the C2 constructor.

If I call, say remote_set_number(42), it works correctly; calling get_a_number() returns 42. Likewise for any uint value. However, when I call remote_get_number(), event though all that does is return the result of a C1(addr).remote_get_number() call, the value returned is always 0.

How do I get a call of a function in one contract to correctly return a value when it is called by a function in another? It seems to me that this follows the patterns given in the Solidity documentation, so I have no idea why it's not working. Can anyone shed a little light?

(This was done using Injected Web3 on a private blockchain in Remix)

UPDATE:

I tried updating to the latest stable geth (1.9.3), and now I get the following error for any interaction between contracts: Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Internal JSON-RPC error.

I have tried it with two different blockchains, with different genesis blocks. #1:

{
    "config": {
        "chainId":123 ,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },

    "nonce": "0x0000000000000042",     "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",     "gasLimit": "0x8000000",     "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0xDB725f67f09228Ea1075f36a0fDb568B9aeBd54C",
    "alloc":{
      "DB725f67f09228Ea1075f36a0fDb568B9aeBd54C":{
        "balance":"6660000080800000000777"
      }
  }
}

and #2:

{
    "config": {
        "chainId":123 ,
        "homesteadBlock": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "petersburgBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },

    "nonce": "0x0000000000000042",     "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",     "gasLimit": "0x8000000",     "difficulty": "0x80",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0xB10Ec08f54343133875f0244e2A435C19789ec28",
    "alloc":{
      "B10Ec08f54343133875f0244e2A435C19789ec28":{
        "balance":"6660010005550000100777"
      }
  }
}

I have tried using a different versions of solidity - but I've had the same results with Solidity 0.4.1, 0.4.4, 0.5.1, and 0.5.11.

0

The contract looks fine and I confirmed with a little superficial fiddling in Remix using the JavaScript VM.

A common stumbling block when transitioning to "real" chains is the need to wait for transactions to confirm. If you are setting and checking without someone first mining a block that contains the set transaction, then you will read the old value (because it is still current).

When you send a transaction, the receipt you get is not confirmation that the transaction has executed successfully - only confirmation that your node knows you want it sent. Big difference.

Try slowing everything down and make sure something is mining.

Hope it helps.

  • Could you share the changes you used to get it working? I can confirm that on my end, it also worked fine in the JavaScript VM - but I just tried calling remote_get_number() again, two hours since the last time remote_set_number was called, and it still returns 0. However, get_a_number returns the correct value. Thanks! – Dave Cochran Sep 9 at 22:50
  • I didn't do anything special or change the contract. "Injected Web3" makes me wonder what you are using for a blockchain and who's mining. No need to wait two hours. Just until you can confirm your transactions are mined. Geth? – Rob Hitchens - B9lab Sep 9 at 22:58
  • Yeah - it's a private blockchain, and geth is mining. It definitely is mining - I checked. The other functions are all working, anyway - so if it is the miner that is the culprit, it seems to specifically dislike returned values from calls to other contracts' functions. – Dave Cochran Sep 9 at 23:21
  • What version of geth? – Rob Hitchens - B9lab Sep 10 at 0:03
  • Geth Version: 1.8.21-stable Go Version: go1.11.4 – Dave Cochran Sep 10 at 1:17

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.