Use Clone Factory

This commit is contained in:
mingda
2020-07-25 01:18:50 +08:00
parent d431baffb1
commit 522c11230f
2 changed files with 36 additions and 12 deletions

View File

@@ -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,

View File

@@ -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;
}
}