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:
@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
import { mudConfig } from "@latticexyz/store/register";
export default mudConfig({
tables: {
MyTable: {
valueSchema: {
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";
import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol";
import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.sol";
contract Contract is Store {
constructor() {
bytes32 tableId = bytes32("MyTable");
FieldLayout fieldLayout = FieldLayoutLib.encode(32, 32);
Schema keySchema = SchemaLib.encode(SchemaType.UINT256);
Schema valueSchema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
string[] memory keyNames = new string[](1);
keyNames[0] = "field1";
string[] memory fieldNames = new string[](2);
fieldNames[0] = "field1";
fieldNames[1] = "field2";
StoreCore.registerTable(tableId, fieldLayout, keySchema, valueSchema, keyNames, fieldNames);
}
}