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.