|
| 1 | +pragma solidity ^0.6.0; |
| 2 | + |
| 3 | +//from https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol |
| 4 | +//explainer at https://blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract/ |
| 5 | +//also see https://eips.ethereum.org/EIPS/eip-1167 |
| 6 | + |
| 7 | +//WARNING: READ BELOW to understand differences with original contract |
| 8 | +/** |
| 9 | + * Modified from @optionality.io/clone-factory/contracts/CloneFactory.sol |
| 10 | + * - Added support for CREATE2-deployed minimal proxy. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * @dev EIP1167 Minimal Proxy |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | +The MIT License (MIT) |
| 19 | +Copyright (c) 2018 Murray Software, LLC. |
| 20 | +Permission is hereby granted, free of charge, to any person obtaining |
| 21 | +a copy of this software and associated documentation files (the |
| 22 | +"Software"), to deal in the Software without restriction, including |
| 23 | +without limitation the rights to use, copy, modify, merge, publish, |
| 24 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | +permit persons to whom the Software is furnished to do so, subject to |
| 26 | +the following conditions: |
| 27 | +The above copyright notice and this permission notice shall be included |
| 28 | +in all copies or substantial portions of the Software. |
| 29 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 30 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 32 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 33 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 34 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 35 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | +*/ |
| 37 | +//solhint-disable max-line-length |
| 38 | +//solhint-disable no-inline-assembly |
| 39 | + |
| 40 | +contract CloneFactory { |
| 41 | + //CHANGE from REFERENCE implementation by Leo VIGNA |
| 42 | + //Added create2 support |
| 43 | + function create2Clone(address target, bytes32 salt) internal returns (address result) { |
| 44 | + bytes20 targetBytes = bytes20(target); |
| 45 | + assembly { |
| 46 | + let clone := mload(0x40) |
| 47 | + mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) |
| 48 | + mstore(add(clone, 0x14), targetBytes) |
| 49 | + mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) |
| 50 | + result := create2(0, clone, 0x37, salt) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function createClone(address target) internal returns (address result) { |
| 55 | + bytes20 targetBytes = bytes20(target); |
| 56 | + assembly { |
| 57 | + let clone := mload(0x40) |
| 58 | + mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) |
| 59 | + mstore(add(clone, 0x14), targetBytes) |
| 60 | + mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) |
| 61 | + result := create(0, clone, 0x37) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function isClone(address target, address query) internal view returns (bool result) { |
| 66 | + bytes20 targetBytes = bytes20(target); |
| 67 | + assembly { |
| 68 | + let clone := mload(0x40) |
| 69 | + mstore(clone, 0x363d3d373d3d3d363d7300000000000000000000000000000000000000000000) |
| 70 | + mstore(add(clone, 0xa), targetBytes) |
| 71 | + mstore(add(clone, 0x1e), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000) |
| 72 | + |
| 73 | + let other := add(clone, 0x40) |
| 74 | + extcodecopy(query, other, 0, 0x2d) |
| 75 | + result := and(eq(mload(clone), mload(other)), eq(mload(add(clone, 0xd)), mload(add(other, 0xd)))) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments