-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
Co-authored-by: alvrs <[email protected]>
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
library Create2 { | ||
/** | ||
* @dev Deploys a contract using `CREATE2`. The address where the contract | ||
* will be deployed can be known in advance. | ||
* | ||
* The bytecode for a contract can be obtained from Solidity with | ||
* `type(contractName).creationCode`. | ||
* | ||
* Requirements: | ||
* | ||
* - `bytecode` must not be empty. | ||
* - `salt` must have not been used for `bytecode` already. | ||
*/ | ||
function deploy(bytes memory byteCode, uint256 salt) internal returns (address addr) { | ||
assembly { | ||
addr := create2(0, add(byteCode, 0x20), mload(byteCode), salt) | ||
if iszero(extcodesize(addr)) { | ||
revert(0, 0) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
import { Create2 } from "./Create2.sol"; | ||
|
||
/** | ||
@dev Helper Contract to facilitate create2 deployment of Contracts. | ||
*/ | ||
contract Create2Factory { | ||
event ContractDeployed(address addr, uint256 salt); | ||
|
||
/** | ||
* @dev Deploys a new Contract using create2. | ||
*/ | ||
function deployContract(bytes memory byteCode, uint256 salt) public { | ||
address addr = Create2.deploy(byteCode, salt); | ||
emit ContractDeployed(addr, salt); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
interface IWorldFactory { | ||
event WorldDeployed(address indexed newContract); | ||
|
||
function worldCount() external view returns (uint256); | ||
|
||
function deployWorld() external; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
import { Create2 } from "./Create2.sol"; | ||
import { World } from "../World.sol"; | ||
import { IWorldFactory } from "./IWorldFactory.sol"; | ||
import { IBaseWorld } from "../interfaces/IBaseWorld.sol"; | ||
import { IModule } from "../interfaces/IModule.sol"; | ||
import { ROOT_NAMESPACE } from "../constants.sol"; | ||
|
||
contract WorldFactory is IWorldFactory { | ||
IModule public coreModule; | ||
uint256 public worldCount; | ||
|
||
constructor(IModule _coreModule) { | ||
coreModule = _coreModule; | ||
} | ||
|
||
/** | ||
@dev Deploy a new World, install the CoreModule and transfer ownership to the caller | ||
*/ | ||
function deployWorld() public { | ||
// Deploy a new World and increase the WorldCount | ||
bytes memory bytecode = type(World).creationCode; | ||
address worldAddress = Create2.deploy(bytecode, worldCount++); | ||
IBaseWorld world = IBaseWorld(worldAddress); | ||
|
||
// Initialize the World and transfer ownership to the caller | ||
world.installRootModule(coreModule, new bytes(0)); | ||
world.transferOwnership(ROOT_NAMESPACE, msg.sender); | ||
|
||
emit WorldDeployed(worldAddress); | ||
} | ||
} |