Skip to content

Commit

Permalink
feat(world): add world factory (#1385)
Browse files Browse the repository at this point in the history
Co-authored-by: alvrs <[email protected]>
  • Loading branch information
johngrantuk and alvrs authored Sep 13, 2023
1 parent b67863b commit 64014e4
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/world/abi/Create2.sol/Create2.abi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions packages/world/abi/Create2Factory.sol/Create2Factory.abi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions packages/world/abi/Create2Factory.sol/Create2Factory.abi.json.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions packages/world/abi/IWorldFactory.sol/IWorldFactory.abi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions packages/world/abi/IWorldFactory.sol/IWorldFactory.abi.json.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions packages/world/abi/WorldFactory.sol/WorldFactory.abi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions packages/world/abi/WorldFactory.sol/WorldFactory.abi.json.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions packages/world/src/factories/Create2.sol
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)
}
}
}
}
19 changes: 19 additions & 0 deletions packages/world/src/factories/Create2Factory.sol
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);
}
}
10 changes: 10 additions & 0 deletions packages/world/src/factories/IWorldFactory.sol
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;
}
34 changes: 34 additions & 0 deletions packages/world/src/factories/WorldFactory.sol
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);
}
}
Loading

0 comments on commit 64014e4

Please sign in to comment.