-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandcastModule.sol
More file actions
67 lines (53 loc) · 2.5 KB
/
RandcastModule.sol
File metadata and controls
67 lines (53 loc) · 2.5 KB
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { InstalledModules } from "@latticexyz/world/src/codegen/index.sol";
import { Module } from "@latticexyz/world/src/Module.sol";
import { WorldContextConsumer } from "@latticexyz/world/src/WorldContext.sol";
import { revertWithBytes } from "@latticexyz/world/src/revertWithBytes.sol";
import { ResourceAccess } from "@latticexyz/world/src/codegen/tables/ResourceAccess.sol";
import { Randcast } from "./tables/Randcast.sol";
import { RandcastConfig } from "./tables/RandcastConfig.sol";
import { RandcastSystem } from "./RandcastSys.sol";
import "./RandcastLib.sol" as RandcastLib;
import { MODULE_NAME, RANDCAST_TABLE_ID, CONFIG_TABLE_ID, SYSTEM_ID } from "./constants.sol";
/**
* This module creates a table that stores a nonce, and
* a public system that returns an incremented nonce each time.
*/
contract RandcastModule is Module {
event SystemAddress(address);
RandcastSystem private immutable randcastSystem = new RandcastSystem();
function getName() public pure returns (bytes16) {
return MODULE_NAME;
}
function installRoot(bytes memory encodedArgs) public {
// Naive check to ensure this is only installed once
// TODO: only revert if there's nothing to do
requireNotInstalled(__self, encodedArgs);
IBaseWorld world = IBaseWorld(_world());
// Register table
Randcast._register(RANDCAST_TABLE_ID);
RandcastConfig._register(CONFIG_TABLE_ID);
// Register system
(bool success, bytes memory data) =
address(world).delegatecall(abi.encodeCall(world.registerSystem, (SYSTEM_ID, randcastSystem, false)));
if (!success) revertWithBytes(data);
// Register system's functions
(success, data) = address(world).delegatecall(
abi.encodeCall(
world.registerRootFunctionSelector,
(SYSTEM_ID, "fulfillRandomness(bytes32,uint256,bytes32)", "fulfillRandomness(bytes32,uint256,bytes32)")
)
);
if (!success) revertWithBytes(data);
(address consumerWrapperAddress, address adapterAddress) = RandcastLib.getCoreComponentAddress();
RandcastConfig._setConsumerWrapperAddress(CONFIG_TABLE_ID, consumerWrapperAddress);
RandcastConfig._setAdapterAddress(CONFIG_TABLE_ID, adapterAddress);
// Grant access
ResourceAccess._set(SYSTEM_ID, consumerWrapperAddress, true);
}
function install(bytes memory /* args */ ) public pure{
revert Module_NonRootInstallNotSupported();
}
}