This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Factory.sol
76 lines (71 loc) · 2.74 KB
/
Factory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: CAL
pragma solidity =0.8.10;
import {IFactory} from "./IFactory.sol";
// solhint-disable-next-line max-line-length
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
/// @title Factory
/// @notice Base contract for deploying and registering child contracts.
abstract contract Factory is IFactory, ReentrancyGuard {
/// @dev state to track each deployed contract address. A `Factory` will
/// never lie about deploying a child, unless `isChild` is overridden to do
/// so.
mapping(address => bool) private contracts;
/// Implements `IFactory`.
///
/// `_createChild` hook must be overridden to actually create child
/// contract.
///
/// Implementers may want to overload this function with a typed equivalent
/// to expose domain specific structs etc. to the compiled ABI consumed by
/// tooling and other scripts. To minimise gas costs for deployment it is
/// expected that the tooling will consume the typed ABI, then encode the
/// arguments and pass them to this function directly.
///
/// @param data_ ABI encoded data to pass to child contract constructor.
function _createChild(bytes calldata data_)
internal
virtual
returns (address)
{} // solhint-disable-line no-empty-blocks
/// Implements `IFactory`.
///
/// Calls the `_createChild` hook that inheriting contracts must override.
/// Registers child contract address such that `isChild` is `true`.
/// Emits `NewChild` event.
///
/// @param data_ Encoded data to pass down to child contract constructor.
/// @return New child contract address.
function createChild(bytes calldata data_)
external
virtual
override
nonReentrant
returns (address)
{
// Create child contract using hook.
address child_ = _createChild(data_);
// Ensure the child at this address has not previously been deployed.
require(!contracts[child_], "DUPLICATE_CHILD");
// Register child contract address to `contracts` mapping.
contracts[child_] = true;
// Emit `NewChild` event with child contract address.
emit IFactory.NewChild(msg.sender, child_);
return child_;
}
/// Implements `IFactory`.
///
/// Checks if address is registered as a child contract of this factory.
///
/// @param maybeChild_ Address of child contract to look up.
/// @return Returns `true` if address is a contract created by this
/// contract factory, otherwise `false`.
function isChild(address maybeChild_)
external
view
virtual
override
returns (bool)
{
return contracts[maybeChild_];
}
}