3

I'm trying to invoke a function by interacting 2 contracts. I defined 2 contracts ArrayTes and CallFunction within same file. CallFunction is calling a function secfun from contract ArrayTes

Intercontract.sol

pragma solidity ^0.4.23;


contract ArrayTes {

    uint256 public l;

    function secfun(uint256 a) public view returns(uint256) {

        l = l+a;
        return l;
    }
}


contract CallFunction {

    ArrayTes at;

    function CallFunction(address _t) {
        at = ArrayTes(_t);
    }

    function thirdfun(uint256 a) public view returns(uint256 c) {

        c = at.secfun(a);
        return c;
    }

}

But c is always returned as zero. I tried defining contracts on seperate file and importing it.

ArrayTes.sol

pragma solidity ^0.4.23;

contract ArrayTes {

    uint256 public l;

    function secfun(uint256 a) public view returns(uint256) {

        l = l + a;
        return l;
    }
}

CallFunction.sol

pragma solidity ^0.4.23;

import "./ArrayTes.sol";

contract CallFunction {

    address useraddress;
    constructor(address _t) public {

        useraddress = _t;
    }


    function thirdfun(uint256 a) public view returns(uint256 c) {

        ArrayTes instanceuser = ArrayTes(useraddress);
        c = instanceuser.secfun(a);
        return c;
    }

}

2_deploy_contracts.js

var ArrayTes = artifacts.require("./ArrayTes.sol");
var CallFunction = artifacts.require("./CallFunction.sol");

module.exports = function(deployer) {

    deployer.deploy(ArrayTes).then(function() {

        return deployer.deploy(CallFunction, ArrayTes.address);
    });
};

Still c value is returned zero. useraddress from CallFunction is returned as '0x'. When I commented code lines c = instanceuser.secfun(a);from CallFunction correct contract address ie; useraddress was returned. This code worked for me around 6 months back. Now its not working. I can't figure out the issue.Help will be appreciated :)

1

1.

function secfun(uint256 a) public view returns(uint256)

is declared as view, but tries modifying l

  1. address useraddress;

is not declared public, so of course it cannot be returned.

Here is my working solution, I think you can make it from here on:

CallFunction.sol

    pragma solidity ^0.4.23;

import "./ArrayTes.sol";

contract CallFunction {

    address public useraddress;
    constructor(address _t) public {

        useraddress = _t;
    }

    function thirdfun(uint256 a) public view returns(uint256 c) {

        ArrayTes instanceuser = ArrayTes(useraddress);
        c = instanceuser.secfun(a);
        return c;
    }

}

ArrayTes.sol

pragma solidity ^0.4.23;

contract ArrayTes {

    function secfun(uint256 a) public pure returns(uint256) {
        uint l;
        l = l + a;
        return l;
    }
}

2_deploy_contracts.js

var ArrayTes = artifacts.require("./ArrayTes.sol");
var CallFunction = artifacts.require("./CallFunction.sol");
var cf;
module.exports = function(deployer) {

    deployer.deploy(ArrayTes).then(function() {
        return deployer.deploy(CallFunction, ArrayTes.address);
    }).then(function(instance){
      cf = instance;
      return cf.thirdfun.call(3).then(function(result){
        console.log("The number is: " + result.toNumber());
      });
    }).then(function(){
      return cf.useraddress.call().then(function(theAddress){
        console.log("The address is: " + theAddress);
      });
    })
};

Have fun !

  • Thanks Nikita...I tried this code in remix...Address is returned correctly but c value returns zero...Here is the image : drive.google.com/file/d/1R58Kgz_Fbz8-hO4wBQiZljS9IPOvyZco/view – angelmaria Jun 18 '18 at 10:17
  • the image doesn't show how test.sol looks like - can you export to gist, please? Also, please try copy-pasting my solution to a truffle project and check the console output from truffle migrate --reset, it should definitely work. – Nikita Fuchs Jun 18 '18 at 12:31
  • I tried truffle migrate --reset still c value is zero. Here is the link : gist.github.com/angelmariadas/28498ca80994824efd7e479efb32a2ed – angelmaria Jun 19 '18 at 7:01
  • Did you copy it correctly into your project ? My above solution returns the desired value. That is tested even during deployment, your console will read: "The Number is: 3" and will do so with any other number entered in return cf.thirdfun.call(3).then... – Nikita Fuchs Jun 19 '18 at 17:10
  • 1
    I changed the solidity version to pragma solidity ^0.4.20; in Remix and it Worked!!!!!!!!!! :) Same issue here : github.com/ethereum/solidity/issues/3969... Updated version doesn't support this code?? – angelmaria Jun 20 '18 at 6:39
0

You will find a solution in these links:

If using parity POA network, add the following lines to the "params" section in chain.json (the chain spec file):

"eip140Transition": 0,
"eip211Transition": 0,
"eip214Transition": 0,
"eip658Transition": 0

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.