1

Normally a factory includes the bytecode of any contract it creates. Now let's think about a scenario where there is an update of the child contract code necessary. I don't want to change existing childs, but I want to inject different bytecode into the factory, so from now on it produces the new variant of the child contracts.

Sample code:

contract Factory {
  // ...
  function newChild() returns (address) {
    Child c = new Child();
    return c;
  }
  // ...
}

contract Child_v1 {
  // ...
  public getVersion() constant returns (uint) {
    return 1;
  }
}

When compiling this the bytecode of Child_v1 will be embedded into the Factory contract. Now I want to change the factory so it produces childs of the type Child_v2:

contract Child_v2 {
  // ...
  public getVersion() constant returns (uint) {
    return 2;
  }
}

Is there a way to change the embedded bytecode of Child_v1 in Factory? E.g. I could compile Child_v2 offline and inject the new bytecode into the factory, something like this:

contract Factory {
  // ...
  function newChild() returns (address){
    Child c = new Child(); // should use bytecode as provided below
    return c;
  }
  function setChildCode(bytes[] bytecode) {
    // Some magic that updates the bytecode used in newChild() above
  }
}

1 Answer 1

2

Did you consider to implement something like a strategy pattern? You'd just need to keep the same interface, but swap any implementation you need:

pragma solidity ^0.4.16;

    //interface
    contract Greetable {
        function Greetable() public {}
        function greet() constant public returns (bytes32);
    }

    //container contract
    contract GreeterContainer is Greetable {

    //handle to the actual imlementation
    Greetable greeter;

    function GreeterContainer(address greeter_address) public {
        greeter = Greetable(greeter_address);
    }

    //here you pass the address of the concrete implementation you need
    //you'll have to deploy it seperately and have its address
    function setGreeter(address _greeter_address) public {
        greeter = Greetable(_greeter_address);
    }

    //this is where the code is getting "injected" in some way
    function greet() constant public returns (bytes32) {
        return greeter.greet();
    }
}

here is the full example: https://gist.github.com/anonymous/2c42d6137295611f3c7db65cc995f8ca

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.