From 522c11230f4a1a57ef58974034f0730ad6bdfffe Mon Sep 17 00:00:00 2001 From: mingda Date: Sat, 25 Jul 2020 01:18:50 +0800 Subject: [PATCH] Use Clone Factory --- contracts/DODOZoo.sol | 18 ++++++------------ contracts/helper/CloneFactory.sol | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 contracts/helper/CloneFactory.sol diff --git a/contracts/DODOZoo.sol b/contracts/DODOZoo.sol index 928f829..01a98b9 100644 --- a/contracts/DODOZoo.sol +++ b/contracts/DODOZoo.sol @@ -9,6 +9,7 @@ pragma solidity 0.6.9; pragma experimental ABIEncoderV2; import {Ownable} from "./lib/Ownable.sol"; +import {ICloneFactory} from "./helper/CloneFactory.sol"; interface IDODO { function init( @@ -34,6 +35,8 @@ interface IDODO { */ contract DODOZoo is Ownable { address public _DODO_LOGIC_; + address public _CLONE_FACTORY_; + mapping(address => mapping(address => address)) internal _DODO_REGISTER_; // ============ Events ============ @@ -42,8 +45,9 @@ contract DODOZoo is Ownable { // ============ Constructor Function ============ - constructor(address _dodoLogic) public { + constructor(address _dodoLogic, address _cloneFactory) public { _DODO_LOGIC_ = _dodoLogic; + _CLONE_FACTORY_ = _cloneFactory; } // ============ Breed DODO Function ============ @@ -62,17 +66,7 @@ contract DODOZoo is Ownable { require(!isDODORegistered(baseToken, quoteToken), "DODO_REGISTERED"); // Adapted from https://github.com/optionality/clone-factory/blob/32782f82dfc5a00d103a7e61a17a5dedbd1e8e9d/contracts/CloneFactory.sol // create proxy - bytes20 targetBytes = bytes20(_DODO_LOGIC_); - assembly { - let clone := mload(0x40) - mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) - mstore(add(clone, 0x14), targetBytes) - mstore( - add(clone, 0x28), - 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000 - ) - newBornDODO := create(0, clone, 0x37) - } + newBornDODO = ICloneFactory(_CLONE_FACTORY_).clone(_DODO_LOGIC_); IDODO(newBornDODO).init( supervisor, maintainer, diff --git a/contracts/helper/CloneFactory.sol b/contracts/helper/CloneFactory.sol new file mode 100644 index 0000000..0476913 --- /dev/null +++ b/contracts/helper/CloneFactory.sol @@ -0,0 +1,30 @@ +/* + + Copyright 2020 DODO ZOO. + SPDX-License-Identifier: Apache-2.0 + +*/ + +pragma solidity 0.6.9; +pragma experimental ABIEncoderV2; + +interface ICloneFactory { + function clone(address prototype) external returns (address proxy); +} + +contract CloneFactory is ICloneFactory { + function clone(address prototype) external override returns (address proxy) { + bytes20 targetBytes = bytes20(prototype); + assembly { + let clone := mload(0x40) + mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000) + mstore(add(clone, 0x14), targetBytes) + mstore( + add(clone, 0x28), + 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000 + ) + proxy := create(0, clone, 0x37) + } + return proxy; + } +}