GithubHelp home page GithubHelp logo

clone-factory's Introduction

clone-factory

Simple clone contract factory Build Status

Check out the contracts/CloneFactory.sol file for details. This code (intended to be called from an implementor factory contract) will allow you to install a master copy of a contract, then easily (cheaply) create clones with separate state. The deployed bytecode just delegates all calls to the master contract address.

npm install @optionality.io/clone-factory

import "./Thing.sol";
import "@optionality.io/clone-factory/contracts/CloneFactory.sol";
import "zeppelin-solidity/contracts/ownership/Ownable.sol";


contract ThingFactory is Ownable, CloneFactory {

  address public libraryAddress;

  event ThingCreated(address newThingAddress);

  function ThingFactory(address _libraryAddress) public {
    libraryAddress = _libraryAddress;
  }

  function setLibraryAddress(address _libraryAddress) public onlyOwner {
    libraryAddress = _libraryAddress;
  }

  function createThing(string _name, uint _value) public onlyOwner {
    address clone = createClone(libraryAddress);
    Thing(clone).init(_name, _value);
    ThingCreated(clone);
  }
}

This will inexpensively create a mimimalist forwarding shim contract that will delegate all calls to the contract libraryAddress

WARNINGS

  • Be sure that the master contract is pre-initialized. You can usually accomplish this in your constructor as the only time the master contract constructor is called is during the master contract's creation. Clone contracts do not call the constructor, but are initialized with an inline initialization method (as demonstrated above).
  • Do not allow your master contract to be self-destructed as it will cause all clones to stop working, thus freezing their state and balances.

Use vanity contract addresses for even CHEAPER clone contracts

Using vanity-eth generate a vanity contract address with up to 4 bytes of leading zeros and use the CloneFactory16 - CloneFactory18 versions to deploy even smaller clones. HT to wjmelements for pointing this out!

ContractProbe contract

Using this deployed contract d'apps, wallets, or other contracts can detect if an address contains a clone and to what address it redirects to. This contract is deployed at the following addresses:

Build Notes

To run the scripts/r2.js script, you need to have radare2 installed along with the evm module (r2pm install evm).

License

Code released under the MIT License.

clone-factory's People

Contributors

lsaether avatar nateawelch avatar yarrumretep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

clone-factory's Issues

Immutably owned clones

My cloned proxy deploys costs less the 1/10th of what they did before I found EIP 1167, so thanks for the awesome contract!

Some projects with vote locking (such as https://dao.curve.fi) require smart wallets to have restrictions on transfer in order to be approved. I've been thinking of how to accomplish this and I have a working, but likely not optimal, solution (which also uses CREATE2 and so is one way to solve #18).

At first I was just going to remove the functions for changing the owner, but because the proxy contract has a delegatecall, it would be able to change the state anyway.

Then I was going to try using the new immutable keyword, but we are using an init function instead of a standard constructor to do the setup.

Here's my current solution:

  1. My factory contract appends the immutable owner's address to the end of the contract. This ensures that create2 gives a new address for every user.

https://github.com/SatoshiAndKin/argobytes-contracts-brownie/blob/slim/contracts/abstract/clonefactory/CloneFactory.sol#L56

  1. And then my proxy contract gets its owner from the contract's code:

https://github.com/SatoshiAndKin/argobytes-contracts-brownie/blob/slim/contracts/abstract/ArgobytesAuth.sol#L41

My testnet deploy script works and I'm working on adding more tests this week. I wanted to post this here in case this is helpful for someone else (or in case its a terrible idea and I should be stopped). I'm very new to assembly and so this is probably not perfect. Any suggestions are welcome.

Estimate right amount of gas to receive ether

Hello guys,

I am cloning a simple contract that makes a forward of the received ethers to another Ethereum address:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

contract SimpleDepositProxy {
    
  address payable recipient;
  event DepositEvent(address indexed payer, uint amount);

  function init(address payable _recipient) public {
    recipient = _recipient;
  }

  receive() payable external {
    emit DepositEvent(address(this), msg.value);
    recipient.transfer(msg.value);
  }
}

This costs about 87342 gas to send ethers.
However when I estimate the necessary gas to send ethers to the cloned contract it always return the default 21000.
I wonder if there is a way to make the cloned contract estimate the right amount of gas.

Thanks!

I can't call this function

Child(clone).init(address(this));
Child contract has init function.
TypeError: Explicit type conversion not allowed from non-payable "address" to "contract Child", which has a payable fallback function.

Are custom errors bubbled up too?

EIP-1167 says that:

...

  • handles error return bubbling for revert messages

Solidity v0.8.4 added support for custom errors:

error Unauthorized();

contract MyContract {
    function foo() external {
        revert Unauthorized();
    }
}

Are these bubbled up just like revert reasons?

Outdated version of Solidity compiler version.

Hi,
I am trying to use this library. But the problem is this library is using the 0.4.23 version of the solidity compiler. The library is not compatible with the 0.8 version of the solidity compiler. Can any contributor of this library update it to the latest version?

How would one update the address the proxy is using?

It'd be really useful to be able to update the address the proxy uses after the fact (to upgrade underlying contract code). How would one go about that? Extend the contract template with some asm to define a method to update the address? Presumably this would make the clones a bit larger, once you take into consideration security considerations.

Is this feasible, and/or could you provide some guidance for how I'd add support for that?

Odd behaviour with msg.sender

Hi,

I'm not sure if this is actually a bug or just a consequence of this technique. I ran into this using Ownable contracts in that when the master is cloned, msg.sender is simply 0x0. I've reproduced it using this repo setting owner = msg.sender in the constructor. I would think that msg.sender would be the address of the DummyFactory.

Any ideas?

Thanks again!

https://github.com/mrwillis/clone-factory-repro

Requirements on library and which factory to choose

Hi!

This library is awesome. I'm trying to apply it to my projects but just want to clarify a couple things. Apologies if this is in the wrong place I just couldn't find any other medium to communicate.

  1. What are the requirements on the library contract? Does it actually have to be a solidity library? I see in the test that you simply first deploy a Thing and then feed the Thing's address into the ThingFactory.

  2. Which CloneFactory do I choose? I see in the contracts folder that there are 4 - CloneFactory, CloneFactory16, CloneFactory17, and CloneFactory18. What do each of those numbers mean?

Thank you!

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Use in production?

Hi, apologies for raising a github issue for what is certainly just an informational question.

Is anyone that you know of using this in production on the mainnet? We are considering using this as part of a dApp we're building and would love to know if it's "battle tested" in production anywhere.

This is a really amazing and much needed piece of infrastructure to provide for the eth ecosystem, so thank you. I appreciate your efforts to get this standardized via EIP 1167.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.