1

I'm trying to call a contract function from another contract, using web3.js.

When using ganache or remix everything works fine: web3.js returns the events fired and the gas used (33938 in this example) and the relevant fields in the contracts are being changed.

However, when I am using geth all of the above works, but only as long as I don't call another contract's function.
When doing so, the events are not returned, the fields are not changed and the gas returned is either 90000 or the gas I am sending in the web3 send options (e.g c1.methods.transferToC2(1).send({from: <my account address>, gas: 2000000});

This is not exactly the contracts, but for general understanding of the problem, the relevant contracts structure is something like:

Contract c1 () {
    event Event(uint256 someNumber);
    address private c2Address = 0x208bcebf6cfa80fe2862fcdacdeb6ad9d3533017;
    address private someAddress = 0x5e7105a34ed27f0b72285f2f951394f3da3ea5eb;
    function transferToC2(uint256 someNumber) public  {
        Event(someNumber);
        IERC20(c2Address).foo(someAddress, someNumber);
    }
}

Contract c2 is ERC20 () { 

}

interface IERC20 {
    function foo(address someAddress, uint256 someNumber) public returns (bool);
}

contract ERC20 is IERC20 {
    mapping (address => uint256) internal someMapping;
    function foo(address someAddress, uint256 someNumber) public  {
        someMapping[someAddress] = someNumber;
        return true;
    }
}

I am deploying the contracts with truffle migrate --network <network> --reset and the output is something like:

Deploying c1...
... 0x703293ce078c3c50e6da8725c6e9a4b91ae904c2943929c8e4189069dccc3de8
c1: 0xed8b7d949b961b1135199e08a9180f5328234426
Deploying c2...
... 0x1b54c170d4fccc13724ee9000078ff437b49883438413e5bf8da68d9343ca6d8
c2: 0x208bcebf6cfa80fe2862fcdacdeb6ad9d3533017

Inside the geth node it seems that my account address's balance is always growing, I assume as a result of the mining.

  • It is impossible to respond accurately if you do not give us the contract's source. I'd check if the address of the second contract is correct, if contract A calls contract B inside B msg.sender will be contract A not the original sender. If B is an ERC20 token and A want to transfer tokens from the user it has to call transferFrom` (user has to previously approve contract A to do so). – Ismael Sep 18 '18 at 16:23
  • @ismael this is why I gave an example similar to my contracts. I'm not interested at the moment in transfering the tokens, but in understanding what's not working when calling a different contract's function. As far as I care all the names would not have been related to transfers. I've edited my answer for it to be more understood. – Achi Even-dar Sep 20 '18 at 8:42
  • I tried your example contracts and they works correctly both in remix and geth. I'd check the address of the second contract is correct, the second contract has extra verification that might fail. What version of solidity are you using? Are byzantinum opcodes enabled in the network you are testing? – Ismael Sep 20 '18 at 14:53
  • @Ismael Thanks for the direction to byzantium, it's working now when adding the byzantiumBlock to the genesis.json file – Achi Even-dar Sep 25 '18 at 14:12
1

Following Ismael's direction, I added to the genesis.json the byzantiumBlock definition e.g:

"config": {
    "chainId": 6035,
    "homesteadBlock": 0,
    "byzantiumBlock": 0 //added this
},

And the problem stopped.

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.