Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(world): add isInstalled to Module #2056

Merged
merged 5 commits into from
Jan 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";
import { Module } from "@latticexyz/world/src/Module.sol";
import { WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol";
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { InstalledModules } from "@latticexyz/world/src/codegen/tables/InstalledModules.sol";

import { Puppet } from "../puppet/Puppet.sol";
import { createPuppet } from "../puppet/createPuppet.sol";
@@ -32,16 +31,14 @@ contract ERC20Module is Module {

function _requireDependencies() internal view {
// Require PuppetModule to be installed
if (InstalledModules.get(PUPPET_MODULE_NAME, keccak256(new bytes(0))) == address(0)) {
if (!isInstalled(PUPPET_MODULE_NAME, abi.encode(keccak256(new bytes(0))))) {
holic marked this conversation as resolved.
Show resolved Hide resolved
revert Module_MissingDependency(string(bytes.concat(PUPPET_MODULE_NAME)));
}
}

function install(bytes memory args) public {
// Require the module to not be installed with these args yet
if (InstalledModules.get(MODULE_NAME, keccak256(args)) != address(0)) {
revert Module_AlreadyInstalled();
}
requireNotInstalled(MODULE_NAME, args);

// Extract args
(bytes14 namespace, ERC20MetadataData memory metadata) = abi.decode(args, (bytes14, ERC20MetadataData));
17 changes: 17 additions & 0 deletions packages/world/src/Module.sol
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ pragma solidity >=0.8.21;
import { WorldContextConsumer } from "./WorldContext.sol";
import { IModule, MODULE_INTERFACE_ID } from "./IModule.sol";
import { IERC165, ERC165_INTERFACE_ID } from "./IERC165.sol";
import { InstalledModules } from "./codegen/tables/InstalledModules.sol";

/**
* @title Module
@@ -21,4 +22,20 @@ abstract contract Module is IModule, WorldContextConsumer {
) public pure virtual override(IERC165, WorldContextConsumer) returns (bool) {
return interfaceId == MODULE_INTERFACE_ID || interfaceId == ERC165_INTERFACE_ID;
}

/**
* Check if a module with the given name and arguments is installed.
*/
function isInstalled(bytes16 moduleName, bytes memory args) public view returns (bool) {
return InstalledModules.get(moduleName, keccak256(args)) != address(0);
}

/**
* Revert if the module with the given name and arguments is already installed.
*/
function requireNotInstalled(bytes16 moduleName, bytes memory args) public view {
if (isInstalled(moduleName, args)) {
revert Module_AlreadyInstalled();
}
}
}