1

I want to understand a few things regarding the implementation of clone factory pattern in smart contract. Do we need an upgradeable smart contract to implement the cloning mechanism (the clones library in openzeppelin smart contracts) or it can be done with a simple non upgradeable contract as well.

1 Answer 1

1
contract CloneFactory {

  function createClone(address target) internal returns (address result) {
    bytes20 targetBytes = bytes20(target);
    assembly {
      let clone := mload(0x40)
      mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
      mstore(add(clone, 0x14), targetBytes)
      mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
      result := create(0, clone, 0x37)
    }
  }

}

is all that is needed to create clones of a contract (implementation can be found here). All this does is delegate all calls to an implementation contract 'target'. This is a so-called "minimal proxy" as per EIP-1167. This contract has no special functionality by itself.

If you want your contract to be upgradeable there are multiple standards, but it is not a must.

3
  • but optionality is using a very outdated version of solidity
    – mzaidi
    Jun 8, 2022 at 9:50
  • That doesn't really matter. You can change the compiler version if you like, but the compiled bytecode will hardly change.
    – phaze
    Jun 8, 2022 at 10:36
  • @mzaidi, OpenZeppelin has a more recent implementation, although the assembly code is probably quite similar.
    – Jose4Linux
    Aug 19, 2022 at 22:06

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.