Using Store without World

Using Store without World

Installation

Install @latticexyz/store via npm, pnpm, or Yarn, and add the correct remapping into your Forge config.

If you store your contracts in packages/contract, this would look like this:

remappings.txt
@latticexyz/=./node_modules/@latticexyz/

If you want to use code-generation for libraries, install the MUD cli: pnpm install -D mud and the store config package: pnpm install -D @latticexyz/store

Import Store and extend your contract with the Store contract:

import { Store } from "@latticexyz/store/src/Store.sol";
 
contract Contract is Store {
  // ...
}

Inheriting Store will bootstrap the internal tables in the contract’s constructor. You can initialize default tables in your contract constructor if needed.

Initializing tables in the constructor with code-generated librarie

create a MUD config at the root of your project named mud.config.ts

mud.config.ts
import { mudConfig } from "@latticexyz/store/register";
 
export default mudConfig({
  tables: {
    MyTable: {
      schema: {
        field1: "uint256",
        field2: "uint256",
      },
    },
  },
});
 
export default config;

Then run pnpm mud tablegen in the folder where your MUD config is located. It will output a single library: MyTable.sol at ./src/tables/MyTable.sol

import { Store } from "@latticexyz/store/src/Store.sol";
import { MyTable } from "./codegen/tables/MyTable.sol";
 
contract Contract is Store {
  constructor() {
    MyTable.registerSchema();
    // Setting metadata is optional. It helps off-chain actors name columns
    MyTable.setMetadata();
  }
}

Initializing tables in the constructor with the low-level API

import { Store } from "@latticexyz/store/src/Store.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
 
contract Contract is Store {
  constructor() {
    Schema keySchema = SchemaLib.encode(SchemaType.UINT256);
    Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
    bytes32 tableId = uint256(keccak256("MyTable"));
    StoreCore.registerSchema(table, schema, keySchema);
    // Setting metadata is optional. It helps off-chain actors name columns
    string[] memory fieldNames = new string[](2);
    fieldNames[0] = "field1";
    fieldNames[1] = "field2";
    StoreSwitch.setMetadata(table, "MyTable", fieldNames);
  }
}