2

I'm trying to implement factory contracts in Mix but can't get the child to create and return an address. I deploy Factory but when I try to call the creator function,

contracts['Factory'].contract.Create(10)

Mix throws:

JavaScript  error   14:18:06    web3.js - line 3881 - Uncaught invalid address

contract Gizmo
{
    int public value;
    function Gizmo(int v) {
        value = v;
    }
}

contract Factory
{
    address gizmoAddr;
    function Create(int _value) returns (address) {
        gizmoAddr = new Gizmo(_value);
        return gizmoAddr;
    }
}

1 Answer 1

2

You need to specify an address that you are sending the transaction from

contracts['Factory'].contract.Create(10,{from: web3.eth.accounts[0],gas:100000});

You can change the gas as needed, but 100,000 should be enough for this.

6
  • Thanks. How do I call the gizmo by address in js, e.g contracts[0x97fea572b69e27ca44475534359d9bb932133fb3].contract.value()?
    – o0ragman0o
    May 16, 2016 at 5:52
  • You can't really return a value from an on-chain transaction, so the best way is probably to declare address constant gizmoAddr and then just use the ABI to create a new contract object in web3 May 16, 2016 at 10:55
  • Or you can put a function into Factory that lets you call Gizmo without needing to manually use Gizmo's address May 16, 2016 at 10:56
  • I'm not understanding this. I need factory to call the gizmo instance after it's created. I can't declare gizmoAddr as constant as it needs to be created dynamically. Also, to Factory, I added the function: function getGizmoVal() returns (int) { return gizmoAddr.val(); } but when I call it with JS responds with a different return value every time. :/
    – o0ragman0o
    May 17, 2016 at 12:13
  • function getGizmoVal() constant returns (int) works correctly
    – o0ragman0o
    May 17, 2016 at 13:08

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.