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

docs(world): add modules to newdocs #1856

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all 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
37 changes: 37 additions & 0 deletions next-docs/pages/world/modules.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# Modules

Modules are onchain installation scripts that create resources and their associated configuration when called by a `World`.
This is somewhat similar to one of the use cases for [foundry scripts](https://book.getfoundry.sh/tutorials/solidity-scripting), except that modules are deployed onchain and can be used by any `World` on the same chain.

## Module installation

Modules can be installed using [`World.installModule(address moduleAddress, bytes memory initData)`](https://github.com/latticexyz/mud/blob/main/packages/world/src/modules/core/implementations/ModuleInstallationSystem.sol#L17-L37).
When you do this, the `World` calls the module's `install` function with `initData` as the argument(s).
Because this is a call, the module does not have any administrative permissions on the `World`.

Alternatively, the owner of the root namespace can install modules using [`World.installRootModule(address moduleAddress, bytes memory initData)`](https://github.com/latticexyz/mud/blob/main/packages/world/src/World.sol#L90-L119).
In this case, the `World` uses delegatecall and module has all the permissions of a `System` in the root namespace.

## Writing modules

The common use for a module is to add functionality to a `World`.
In most cases we expect that a module would:

1. Create a [namespace](./namespaces-access-control.mdx) for the new functionality.
1. Create the [tables](./.tables) and [`System`s](./systems) for the new functionality.
1. Create any access permissions required (beyond the default, which is that a `System` has access to its own namespace).
1. Either assign the ownership of the new namespace to an entity that would administer it (a user, a multisig, etc.) or burn it by assigning the namespace ownership to `address(0)`.

Root modules run with the same permission as `System`s in the root namespace, so they do not need to create a namespace for themselves.
They can just create their own tables and `System`s as needed.

## Sample modules

MUD comes with several modules you can use or inspect to help you write your own:

- [`KeysInTable`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/keysintable) - automatically tracks the keys in a table to make them enumerable.
- [`KeysWithValue`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/keyswithvalue) - automatically tracks a reverse mapping for a table that maps a value hash to a list of keys with this value.
- [`Puppet`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/puppet) - installs the PuppetDelegationControl to allow creating Puppet contracts, eg used by the ERC20 and ERC721 modules.
- [`ERC20`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/erc20-puppet) - installs an ERC20 token into a namespace in a World.
- [`ERC721`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/erc721-puppet) - installs an ERC721 token into a namespace in a World.
- [`UniqueEntity`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/uniqueentity) - add methods to get a unique entity ID.
- [`Standard Delegations`](https://github.com/latticexyz/mud/tree/main/packages/world-modules/src/modules/std-delegations) - add delegations limited by time or number of calls.