1

I'm trying to create a contract which calls boolean method on another contract. However, for some reason it doesn't work.

Here is an example:

pragma solidity ^0.4.23;

contract Season {
    address[] public requests;

    constructor() public {
        requests.push(new Request());
    }

    function getRequestsByFilter() public view returns(bool) {
        Request r = Request(requests[0]);
        bool ret = r.isMatch();
        return ret;
    }
}

contract Request {  
    function isMatch() public pure returns(bool) {
        return true;
    }    
}

It compiles fine but if you call R.foo from web3.js you get false instead of true.

Here is what I see in UI:

enter image description here

I don't understand why it happens and how can I call this method.


Here is smaller example:

pragma solidity ^0.4.23;

contract Season {
    function getRequestsByFilter() public returns(bool) {
        Request r = new Request();
        bool ret = r.isMatch();
        return ret;
    }
}

contract Request {  
    function isMatch() public pure returns(bool) {
        return true;
    }    
}
  • 1
    It works for me...I guess just make sure you pass in "hello" correctly to foo, but it should work fine – thefett Apr 26 '18 at 16:53
  • @thefett it doesn't matter what I pass because parameter is ignored - isMatch function always return true for any input. However, I'm surprised it works for you. I'm running parity 1.9.5 and it doesn't work there. I'm using this OSS plaground. – Alex Zhukovskiy Apr 26 '18 at 16:57
  • hmm..not really a parity guy, but if you can use web3, try just doing a 'getCode' before your call to see if the contracts deployed correctly: github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgetcode – thefett Apr 26 '18 at 17:15
  • @thefett yep, it works fine: pastebin.com/9pEpFeNP – Alex Zhukovskiy Apr 26 '18 at 17:21
0

Here, the Hub deploys Spoke contracts that store a simple string.

To iterate over the strings stored in all the Spokes you 1) get the spokesCount from the Hub with hub.getSpokeCount() 2) get the spokeAddress from the Hub, by row number with hub.spokes(row) 3) instantiate a Spoke with spoke = Spoke.at(spokeAddress), 4) invoke functions in the spokes such as Set with spoke.setString("foo"), and Get with spoke.storedString()

pragma solidity 0.4.23;

contract Owned {

    address public owner;

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

}

contract Spoke {

    string public storedString;

    event LogSetHubString(address sender, string _string);

    function setString(string _string) public returns(bool success) {
        emit LogSetHubString(msg.sender, _string);
        storedString = _string;
        return true;
    }

}

contract Hub is Owned {

    address[] public spokes;
    mapping(address => bool) public isSpoke;

    event LogNewSpoke(address sender, address spoke);

    constructor() public { owner = msg.sender; }

    function getSpokeCount() public view returns(uint count) { return spokes.length; }

    function newSpoke() public onlyOwner returns(address newContract) {
        Spoke s = new Spoke();
        spokes.push(address(s));
        isSpoke[address(s)] = true;
        emit LogNewSpoke(msg.sender, address(s));
        return address(s);
    }

}

Hope it helps.

  • Thank you for an answer. I didn't get what do you mean under can't tell what exactly is wanted. I have an array of items. I want to iterate through this array and call isMatch function on every contract. As simplified example I have the only value in my contract and I want to call a function on it. However, when I call it it returns default value instead of true. – Alex Zhukovskiy Apr 27 '18 at 8:47
  • That changes things a little. Do the storage in the spokes. To iterate over the list you 1) get the spokesCount from the Hub, 2) get the spokeAddress from the Hub, by row number, 3) instantiate a Spoke s = Spoke(spokeAddress), 4) invoke functions in the spokes with s.setValue(data), s.functionName(args). – Rob Hitchens - B9lab Apr 27 '18 at 12:05
  • It seems to be a parity bug, for some reason it doesn't allow one contract to interact with anouther one. It doesn't happen in other implementations such as ganache. In other words, this code is perfectly legal, just a network bug. – Alex Zhukovskiy Apr 27 '18 at 14:47
0

It was my blockchain bug. It was resolving by applying byzantium EIPs which fixes this issue, specifically saying: 140, 211, 214, 658. It seems that this behavior is fixed by 214 alone, but others are good too.

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.