-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
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"; | ||
nventuro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
jbogacz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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 () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would drop this |
||
beforeEach(async function () { | ||
factory = await initFactory(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since #1380 we've stored this kind of stuff in |
||
}); | ||
|
||
it('should create proxy contract with valid bytecode', async function () { | ||
const proxy = await factory.createNew(); | ||
(await readContractCode(proxy.address)).should.be.equal(expectedBytcode()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also test that the event was emitted, using our |
||
}); | ||
}); | ||
|
||
const initFactory = async function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't abstract this (along with |
||
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'; | ||
}; | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.