-
Notifications
You must be signed in to change notification settings - Fork 196
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
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
cf86cdb
this is done by mud build
holic 3f7b422
add metadata module
holic 24a1685
add metadata module by default
holic e27c5a7
make it a peer dep
holic 83b32f5
CLI as workspace root dep
holic fd86ee3
Revert "CLI as workspace root dep"
holic 00f2e7f
remove unused dep
holic 9c77cdc
add world-modules to threejs contracts
holic 648be0c
build world-modules without cli
holic b522dbf
missing import
holic 86aa271
rename
holic 1f85850
rework module
holic 9866564
move metadata module into its own package
holic 9ad0ae9
Merge remote-tracking branch 'origin/main' into holic/metadata-module
holic c96282d
don't ignore metadata codegen
holic 86af5d2
update gas report
holic 6274a0b
tags
holic 4b90b1a
add natspec
holic ccd881a
fixes
holic 3d62a3e
simplify/clarify
holic 7d4c85c
gas report
holic f6d872b
Create spotty-camels-occur.md
holic dec7d2f
make install optional
holic 43dba06
update comments
holic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@latticexyz/cli": patch | ||
"@latticexyz/world-module-metadata": patch | ||
--- | ||
|
||
Added metadata module to be automatically installed during world deploy. This module allows for tagging any resource with arbitrary metadata. Internally, we'll use this to tag resources with labels onchain so that we can use labels to create a MUD project from an existing world. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createRequire } from "node:module"; | ||
import { findUp } from "find-up"; | ||
import { GetContractArtifactResult, getContractArtifact } from "./getContractArtifact"; | ||
|
||
export type ImportContractArtifactOptions = { | ||
/** | ||
* Path to `package.json` where `artifactPath`s are resolved relative to. | ||
* | ||
* Defaults to nearest `package.json` relative to `process.cwd()`. | ||
*/ | ||
packageJsonPath?: string; | ||
/** | ||
* Import path to contract's forge/solc JSON artifact with the contract's compiled bytecode. | ||
* | ||
* This path is resolved using node's module resolution relative to `configPath`, so this supports both | ||
* relative file paths (`../path/to/MyModule.json`) as well as JS import paths (`@latticexyz/world-contracts/out/CallWithSignatureModule.sol/CallWithSignatureModule.json`). | ||
*/ | ||
artifactPath: string; | ||
}; | ||
|
||
export async function importContractArtifact({ | ||
packageJsonPath, | ||
artifactPath, | ||
}: ImportContractArtifactOptions): Promise<GetContractArtifactResult> { | ||
let artfactJson; | ||
try { | ||
const requirePath = packageJsonPath ?? (await findUp("package.json", { cwd: process.cwd() })); | ||
if (!requirePath) throw new Error("Could not find package.json to import relative to."); | ||
|
||
const require = createRequire(requirePath); | ||
artfactJson = require(artifactPath); | ||
} catch (error) { | ||
console.error(); | ||
console.error("Could not import contract artifact at", artifactPath); | ||
console.error(); | ||
throw error; | ||
} | ||
|
||
return getContractArtifact(artfactJson); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cache | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "solhint:recommended", | ||
"rules": { | ||
"compiler-version": ["error", ">=0.8.0"], | ||
"avoid-low-level-calls": "off", | ||
"func-visibility": ["warn", { "ignoreConstructors": true }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Metadata world module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[profile.default] | ||
solc = "0.8.24" | ||
ffi = false | ||
fuzz_runs = 256 | ||
optimizer = true | ||
optimizer_runs = 3000 | ||
verbosity = 2 | ||
allow_paths = ["../../node_modules", "../"] | ||
src = "src" | ||
out = "out" | ||
bytecode_hash = "none" | ||
extra_output_files = [ | ||
"abi", | ||
"evm.bytecode" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"file": "test/MetadataModule.t.sol", | ||
"test": "testDeleteResourceTag", | ||
"name": "delete resource tag", | ||
"gasUsed": 70301 | ||
}, | ||
{ | ||
"file": "test/MetadataModule.t.sol", | ||
"test": "testInstall", | ||
"name": "install metadata module", | ||
"gasUsed": 1106562 | ||
}, | ||
{ | ||
"file": "test/MetadataModule.t.sol", | ||
"test": "testSetResourceTag", | ||
"name": "set resource tag", | ||
"gasUsed": 116708 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "metadata", | ||
userTypes: { | ||
ResourceId: { filePath: "@latticexyz/store/src/ResourceId.sol", type: "bytes32" }, | ||
}, | ||
tables: { | ||
ResourceTag: { | ||
schema: { | ||
resource: "ResourceId", | ||
tag: "bytes32", | ||
value: "bytes", | ||
}, | ||
key: ["resource", "tag"], | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{ | ||
"name": "@latticexyz/world-module-metadata", | ||
"version": "2.1.0", | ||
"description": "Metadata world module", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/latticexyz/mud.git", | ||
"directory": "packages/world-module-metadata" | ||
}, | ||
"license": "MIT", | ||
"type": "module", | ||
"exports": { | ||
"./mud.config": "./dist/mud.config.js", | ||
"./out/*": "./out/*" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"mud.config": [ | ||
"./dist/mud.config.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"out", | ||
"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:js": "tsup", | ||
"build:mud": "tsx ./ts/build.ts", | ||
"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", | ||
"dev": "tsup --watch", | ||
"gas-report": "gas-report --save gas-report.json", | ||
"lint": "solhint --config ./.solhint.json 'src/**/*.sol'", | ||
"test": "forge test", | ||
"test:ci": "pnpm run test" | ||
}, | ||
"dependencies": { | ||
"@latticexyz/schema-type": "workspace:*", | ||
"@latticexyz/store": "workspace:*", | ||
"@latticexyz/world": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@latticexyz/abi-ts": "workspace:*", | ||
"@latticexyz/gas-report": "workspace:*", | ||
"@types/node": "^18.15.11", | ||
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0", | ||
"forge-std": "https://github.com/foundry-rs/forge-std.git#74cfb77e308dd188d2f58864aaf44963ae6b88b1", | ||
"solhint": "^3.3.7", | ||
"tsup": "^6.7.0", | ||
"tsx": "^3.12.6", | ||
"vitest": "0.34.6" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ds-test/=node_modules/ds-test/src/ | ||
forge-std/=node_modules/forge-std/src/ | ||
@latticexyz/=node_modules/@latticexyz/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 { requireOwner } from "./common.sol"; | ||
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol"; | ||
import { ResourceIds } from "@latticexyz/store/src/codegen/tables/ResourceIds.sol"; | ||
import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol"; | ||
|
||
import { MetadataSystem } from "./MetadataSystem.sol"; | ||
import { ResourceTag } from "./codegen/tables/ResourceTag.sol"; | ||
|
||
/** | ||
* @title MetadataModule | ||
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) | ||
* @dev Adds metadata tables and systems for annotating data in MUD apps. | ||
* For example, tagging resources with labels for better UX when reconstructing a MUD project from a world using onchain state. | ||
*/ | ||
contract MetadataModule is Module { | ||
using WorldResourceIdInstance for ResourceId; | ||
|
||
MetadataSystem private immutable metadataSystem = new MetadataSystem(); | ||
|
||
function installRoot(bytes memory) public pure { | ||
revert Module_RootInstallNotSupported(); | ||
} | ||
|
||
function install(bytes memory) public { | ||
IBaseWorld world = IBaseWorld(_world()); | ||
|
||
ResourceId namespace = ResourceTag._tableId.getNamespaceId(); | ||
if (!ResourceIds.getExists(namespace)) { | ||
world.registerNamespace(namespace); | ||
} | ||
requireOwner(namespace, address(this)); | ||
|
||
if (!ResourceIds.getExists(ResourceTag._tableId)) { | ||
ResourceTag.register(); | ||
} | ||
|
||
ResourceId metadataSystemId = WorldResourceIdLib.encode( | ||
RESOURCE_SYSTEM, | ||
namespace.getNamespace(), | ||
"MetadataSystem" | ||
); | ||
// TODO: add support for upgrading system and registering new function selectors | ||
if (!ResourceIds.getExists(metadataSystemId)) { | ||
world.registerSystem(metadataSystemId, metadataSystem, true); | ||
world.registerFunctionSelector(metadataSystemId, "getResourceTag(bytes32,bytes32)"); | ||
world.registerFunctionSelector(metadataSystemId, "setResourceTag(bytes32,bytes32,bytes)"); | ||
world.registerFunctionSelector(metadataSystemId, "deleteResourceTag(bytes32,bytes32)"); | ||
} | ||
|
||
world.transferOwnership(namespace, _msgSender()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this pattern feels a little weird to me, but curious to hear what you think:
on first install, metadata module creates
metadata
namespace, registers tables/systems, hands ownership back to original calleron second install (idempotent, would upgrade systems or add tables), you'd first need to hand ownership of the namespace to the module, then it hands it back after install
should the world or something else own the metadata namespace? if so, how would you perform an upgrade to the metadata module?
or maybe we should consider the metadata module more of a "locked in" thing, rename it to something more specific like "resource tag module" and if we want more metadata, we add as another module and namespace rather than trying to figure out how to upgrade this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this pattern is fine! makes it possible to upgrade, which is nice