Skip to content

EIP-1167 Clone Factory (#1162) #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions contracts/mocks/SimpleCloneFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pragma solidity ^0.4.24;

import "../utils/CloneFactory.sol";
import "../examples/SimpleToken.sol";

/**
* @title Simple Clone Contract Factory
*
* @dev This code (intended to be called from an implementor factory contract)
* will allow you to install a master copy of the SimpleToken contract, then easily
* (cheaply) create clones with separate state. The deployed bytecode just
* delegates all calls to the master contract address.
*
* 1) Be sure that the master contract is pre-initialized.
* 2) Do not allow your master contract to be self-destructed as it will cause
* all clones to stop working
*/
contract SimpleCloneFactory is CloneFactory {

address public originAddress;

constructor() public {
originAddress = new SimpleToken();
}

function createNew() public returns (bool) {
createClone(originAddress);
return true;
}
}
56 changes: 56 additions & 0 deletions contracts/utils/CloneFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pragma solidity ^0.4.24;

/*
The MIT License (MIT)

Copyright (c) 2018 Murray Software, LLC.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* @title EIP 1167: Minimal Proxy Contract
*
* @dev To simply and cheaply clone contract functionality in an immutable way,
* this standard specifies a minimal bytecode implementation that delegates
* all calls to a known, fixed address.
*
* https://eips.ethereum.org/EIPS/eip-1167
*/
contract CloneFactory {

event CloneCreated(address indexed originAddress, address clonedAddress);

function createClone(address target) internal returns (address result) {
// solium-disable-next-line
bytes memory clone = hex"3d602d80600a3d3981f3363d3d373d3d3d363d73bebebebebebebebebebebebebebebebebebebebe5af43d82803e903d91602b57fd5bf3";
bytes20 targetBytes = bytes20(target);
for (uint i = 0; i < 20; i++) {
clone[20 + i] = targetBytes[i];
}
// solium-disable-next-line security/no-inline-assembly
assembly {
let len := mload(clone)
let data := add(clone, 0x20)
result := create(0, data, len)
}
emit CloneCreated(target, result);
}
}
46 changes: 46 additions & 0 deletions test/CloneFactory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const SimpleToken = artifacts.require('SimpleToken');
const SimpleCloneFactory = artifacts.require('SimpleCloneFactory');

const BigNumber = web3.BigNumber;

require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();

contract('CloneFactory', function ([_, owner]) {
let factory;

describe('factory initialized', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop this describe block for now, no need to make the test suite too complicated.

beforeEach(async function () {
factory = await initFactory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #1380 we've stored this kind of stuff in this, it'd be great for consistency if we kept doing that :)

});

it('should create proxy contract with valid bytecode', async function () {
const proxy = await factory.createNew();
(await readContractCode(proxy.address)).should.be.equal(expectedBytcode());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test that the event was emitted, using our expectEvent test helper.

});
});

const initFactory = async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't abstract this (along withreadContractCode and expectedBytecode) into standalone functions until we actually need them (e.g. to prevent repeated code): I'd rather the tests be as explicit and simple as possible, and this obscures them somewhat.

const _factory = await SimpleCloneFactory.new();
const _originAddress = await _factory.originAddress();
return {
createNew: async function () {
const tx = await _factory.createNew();
return SimpleToken.at(tx.logs[0].args.clonedAddress);
},
originAddress: function () {
return _originAddress;
},
};
};

const readContractCode = async function (contractAddress) {
return web3.eth.getCode(contractAddress);
};

const expectedBytcode = function () {
const originAddress = factory.originAddress();
return '0x363d3d373d3d3d363d73' + originAddress.substring(2) + '5af43d82803e903d91602b57fd5bf3';
};
});