2

If I try to deploy the following with truffle, I get an error:

Error encountered, bailing. Network state unknown. Review successful transactions manually. Error: The contract code couldn't be stored, please check your gas amount. at Object.callback (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:222830:46)

contract Foo{

    function a(uint _amount);

}

contract Bar {

    Foo foo;

    function Bar(address _foo){
        foo = Foo(_foo);
    }

}

So, what's the right way to include references to other contracts without including the whole contract? I'm using Truffle v3.4.9

I don't want to craft the function signature manually like this:

fooAddress.call(bytes4(bytes32(sha3("a(uint256)"))), 100);
2

Revised

In the code above Foo.a() is described as an abstract interface (takes one uint in, is public (default) and returns nothing) but the actual workings are not defined - there is no {} block.

contract FooInterface{  
    function a(uint _amount) public pure returns(uint amount);  
}

Foo could be more explicitly named FooInterface to avoid confusion with the actual Foo that implements the function. This is all rather pointless unless there is such thing, so let's suppose it's there with this contrived example.

contract Foo is FooInterface {   
    function a(uint _amount) public pure returns(uint amount) {
        return _amount;
    }
}

You don't deploy the FooInterface, but you deploy a Foo. Such is implied by the constructor for Bar which is asking for actual address of Foo.

contract Bar {

    Foo foo;

    function Bar(address _foo) public {
        foo = Foo(_foo);
    }

    function testFoo(uint amount) public view returns(uint number) {
        return (foo.a(amount));
    }

}

Once Bar is deployed and properly bound to Foo by the address passed to the constructor, then the testFoo() function will. It's a contrived example that passes the input to Foo and returns it to Bar which returns it to the user.

contract FooInterface{
    function a(uint _amount) public pure returns(uint amount);
}

contract Foo is FooInterface {

    function a(uint _amount) public pure returns(uint amount) {
        return _amount;
    }
}


contract Bar {

    Foo foo;

    function Bar(address _foo) public {
        foo = Foo(_foo);
    }

    function testFoo(uint amount) public view returns(uint number) {
        return (foo.a(amount));
    }

}

Hope it helps.

  • Accounts are unlocked. I'm executing the deployment in this order: deployer.deploy(Foo).then(function(){ deployer.deploy(Bar, Foo.address); }); And still getting same error. – John Smith Sep 16 '17 at 11:45
  • Thanks. The problem was that the Foo, as an abstract contract, needs to be used by a concrete contract. – John Smith Sep 16 '17 at 11:56
  • Hey @JohnSmith, I am having the same problem. What do you mean by needs to be used by a concrete contract? Appreciate the help – aviral agrawal Oct 5 '17 at 20:24
  • Thanks, John. I've changed my answer with a little elaboration on abstract versus concrete. – Rob Hitchens - B9lab Oct 6 '17 at 0:26
  • I think what Op is trying to do is get a reference to an existing contract Foo that is already deployed somewhere. He is passing the address of Foo into the constructor of Bar, so Foo can interact with Bar. Your explanation why it's not compiling is correct (compiler can't create the public member Foo of contract Bar), but the underlying question how to do this is still unanswered. – TripleSpeeder Nov 5 '17 at 8:45

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.