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-module-metadata): add metadata module #3026

Merged
merged 24 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
12 changes: 11 additions & 1 deletion packages/cli/src/deploy/configToModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ export async function configToModules<config extends World>(
// TODO: remove/replace `forgeOutDir`
forgeOutDir: string,
): Promise<readonly Module[]> {
const configModules = [
{
name: "metadata",
artifactPath: "@latticexyz/world-modules/out/MetadataModule.sol/MetadataModule.json",
holic marked this conversation as resolved.
Show resolved Hide resolved
root: false,
args: [],
},
...config.modules,
];

const modules = await Promise.all(
config.modules.map(async (mod): Promise<Module> => {
configModules.map(async (mod): Promise<Module> => {
let artifactPath = mod.artifactPath;

// Backwards compatibility
Expand Down
8 changes: 3 additions & 5 deletions packages/world-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
"src"
],
"scripts": {
"build": "pnpm run build:mud && pnpm run build:abi && pnpm run build:abi-ts && pnpm run build:js",
"build:abi": "forge build",
"build:abi-ts": "abi-ts",
"build": "pnpm run build:mud && pnpm run build:js",
"build:js": "tsup",
"build:mud": "mud build",
"build:mud": "for dir in src/modules/metadata; do (cd \"$dir\" && pwd && mud tablegen); done && mud build",
"clean": "pnpm run clean:abi && pnpm run clean:js && pnpm run clean:mud",
"clean:abi": "forge clean",
"clean:js": "rimraf dist",
"clean:mud": "rimraf src/codegen",
"clean:mud": "rimraf src/**/codegen",
"dev": "tsup --watch",
"gas-report": "gas-report --save gas-report.json",
"lint": "solhint --config ./.solhint.json 'src/**/*.sol'",
Expand Down
2 changes: 2 additions & 0 deletions packages/world-modules/src/modules/metadata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
codegen/index.sol
codegen/world
43 changes: 43 additions & 0 deletions packages/world-modules/src/modules/metadata/MetadataModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { Module } from "@latticexyz/world/src/Module.sol";
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol";
import { ResourceIds } from "@latticexyz/store/src/codegen/tables/ResourceIds.sol";
import { NamespaceOwner } from "@latticexyz/world/src/codegen/tables/NamespaceOwner.sol";
import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol";

import { MetadataSystem } from "./MetadataSystem.sol";
import { Metadata } from "./codegen/tables/Metadata.sol";

contract MetadataModule is Module {
using WorldResourceIdInstance for ResourceId;

MetadataSystem private immutable metadataSystem = new MetadataSystem();
ResourceId private immutable metadataSystemId =
WorldResourceIdLib.encode(RESOURCE_SYSTEM, Metadata._tableId.getNamespace(), "MetadataSystem");

function installRoot(bytes memory) public pure {
revert Module_RootInstallNotSupported();
}

function install(bytes memory) public {
IBaseWorld world = IBaseWorld(_world());

ResourceId namespace = Metadata._tableId.getNamespaceId();
if (!ResourceIds.getExists(namespace)) {
world.registerNamespace(namespace);
}

if (NamespaceOwner.getOwner(namespace) == _msgSender()) {
if (!ResourceIds.getExists(Metadata._tableId)) {
Metadata.register();
}
if (!ResourceIds.getExists(metadataSystemId)) {
world.registerSystem(metadataSystemId, metadataSystem, true);
world.registerFunctionSelector(metadataSystemId, "setMetadata(bytes32,bytes32,string)");
}
}
}
}
15 changes: 15 additions & 0 deletions packages/world-modules/src/modules/metadata/MetadataSystem.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { AccessControl } from "@latticexyz/world/src/AccessControl.sol";
import { Metadata } from "./codegen/tables/Metadata.sol";

contract MetadataSystem is System {
function setMetadata(ResourceId resource, bytes32 name, string memory value) external {
AccessControl.requireExistence(resource);
AccessControl.requireOwner(resource, _msgSender());
Metadata.set(resource, name, value);
}
}
Loading
Loading