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: rename table to tableId #1484

Merged
merged 20 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
25 changes: 25 additions & 0 deletions .changeset/cold-years-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"@latticexyz/cli": major
"@latticexyz/protocol-parser": major
"@latticexyz/store-sync": major
"@latticexyz/store": major
"create-mud": minor
---

Renamed all occurrences of `schema` where it is used as "value schema" to `valueSchema` to clearly distinguish it from "key schema".
The only breaking change for users is the change from `schema` to `valueSchema` in `mud.config.ts`.

```diff
// mud.config.ts
export default mudConfig({
tables: {
CounterTable: {
keySchema: {},
- schema: {
+ valueSchema: {
value: "uint32",
},
},
}
}
```
38 changes: 38 additions & 0 deletions .changeset/metal-hounds-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
"@latticexyz/block-logs-stream": patch
"@latticexyz/store-sync": patch
"@latticexyz/store": patch
---

Renamed all occurrences of `table` where it is used as "table ID" to `tableId`.
This is only a breaking change for consumers who manually decode `Store` events, but not for consumers who use the MUD libraries.

```diff
event StoreSetRecord(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key,
bytes data
);

event StoreSetField(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key,
uint8 fieldIndex,
bytes data
);

event StoreDeleteRecord(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key
);

event StoreEphemeralRecord(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key,
bytes data
);
```
2 changes: 1 addition & 1 deletion docs/pages/client-side.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const config = mudConfig({
tables: {
NameComponent: "string",
PlayerComponent: "bool",
PositionComponent: { schema: { x: "int32", y: "int32" } },
PositionComponent: { valueSchema: { x: "int32", y: "int32" } },
},
});
```
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/ecs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default mudConfig({
tables: {
PlayerComponent: "bool",
PositionComponent: {
schema: { x: "int32", y: "int32" },
valueSchema: { x: "int32", y: "int32" },
},
NameComponent: "string",
DamageComponent: uint256,
Expand Down
9 changes: 4 additions & 5 deletions docs/pages/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default mudConfig({
tables: {
Counter: {
keySchema: {},
schema: "uint32",
valueSchema: "uint32",
},
},
});
Expand Down Expand Up @@ -117,7 +117,7 @@ contract IncrementSystem is System {
}
```

The increment system is able to import `Counter` (from its autogenerated table schema) and operate on it by increasing its value by one.
The increment system is able to import `Counter` (from its autogenerated table library) and operate on it by increasing its value by one.

Each system can contain any number of methods, though this system only has the single method—`increment`. These methods can then be called in the client to execute them in a transaction.

Expand All @@ -127,7 +127,6 @@ The client package will vary depending on which template used (vanilla, react, p

- You can adjust `createClientComponents.ts` to either override contract components or add client-only components.
- If you are using chains other than foundry/anvil and lattice testnet, you can add them in `getNetworkConfig.ts`
- `createSystemCalls` represents how the client talks to the system contracts via our `worldSend` helper
- `setup.ts`

Beyond these files you can concern yourself simply with building out the frontend of the client.
Expand All @@ -136,13 +135,13 @@ Beyond these files you can concern yourself simply with building out the fronten

Now that you’re familiar with the basic structure of the client package, let’s go over how you can call on systems from the contracts package.

The starter project comes with `IncrementSystem.sol`—you can see it being called in `index.ts` (or `index.tsx` in the react template).
The starter project comes with `IncrementSystem.sol`—you can see it being called in `index.ts` (or `App.tsx` in the react template).

```tsx
// Just for demonstration purposes: we create a global function that can be
// called to invoke the Increment system contract via the world. (See IncrementSystem.sol.)
(window as any).increment = async () => {
const tx = await worldSend("increment", []);
const tx = await worldContract.write.increment();

console.log("increment tx", tx);
console.log("increment result", await tx.wait());
Expand Down
34 changes: 17 additions & 17 deletions docs/pages/store/advanced-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default mudConfig({
tables: {
CounterSingleton: {
keySchema: {},
schema: "uint256",
valueSchema: "uint256",
},
},
});
Expand Down Expand Up @@ -48,7 +48,7 @@ import { mudConfig } from "@latticexyz/store/register";
export default mudConfig({
tables: {
TradeExecuted: {
schema: {
valueSchema: {
amount: "uint32",
receiver: "bytes32",
},
Expand Down Expand Up @@ -79,28 +79,28 @@ It is possible to register hooks on tables, allowing additional logic to be exec
This is an example of a Mirror hook which mirrors a table into another one:

```solidity
uint256 constant indexerTableId = uint256(keccak256("indexer.table"));
bytes32 constant indexerTableId = bytes32("indexer.table");

contract MirrorSubscriber is IStoreHook {
uint256 _table;
bytes32 _tableId;

constructor(uint256 table, Schema schema, Schema keySchema) {
IStore(msg.sender).registerSchema(indexerTableId, schema, keySchema);
_table = table;
constructor(bytes32 tableId, Schema keySchema, Schema valueSchema) {
IStore(msg.sender).registerSchema(indexerTableId, valueSchema, keySchema);
_tableId = tableId;
}

function onSetRecord(uint256 table, bytes32[] memory key, bytes memory data) public {
if (_table != table) revert("invalid table");
function onSetRecord(bytes32 tableId, bytes32[] memory key, bytes memory data) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.setRecord(indexerTableId, key, data);
}

function onSetField(uint256 table, bytes32[] memory key, uint8 schemaIndex, bytes memory data) public {
if (_table != table) revert("invalid table");
function onSetField(bytes32 tableId, bytes32[] memory key, uint8 schemaIndex, bytes memory data) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.setField(indexerTableId, key, schemaIndex, data);
}

function onDeleteRecord(uint256 table, bytes32[] memory key) public {
if (_table != table) revert("invalid table");
function onDeleteRecord(bytes32 tableId, bytes32[] memory key) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.deleteRecord(indexerTableId, key);
}
}
Expand All @@ -109,11 +109,11 @@ contract MirrorSubscriber is IStoreHook {
Registering the hook can be done using the low-level Store API:

```solidity
uint256 table = keccak256("table");
Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
bytes32 tableId = bytes32("table");
Schema valueSchema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
Schema keySchema = SchemaLib.encode(SchemaType.UINT256);
MirrorSubscriber subscriber = new MirrorSubscriber(table, schema, keySchema);
StoreCore.registerStoreHook(table, subscriber);
MirrorSubscriber subscriber = new MirrorSubscriber(tableId, keySchema, valueSchema);
StoreCore.registerStoreHook(tableId, subscriber);
```

### Accessing Store from a `CALL` or `DELEGATECALL` transparently
Expand Down
12 changes: 6 additions & 6 deletions docs/pages/store/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { mudConfig } from "@latticexyz/store/register";
export default mudConfig({
tables: {
MyTable: {
schema: {
valueSchema: {
value: "uint32",
},
},
Expand All @@ -62,7 +62,7 @@ The table configuration can have these properties:

**`fileSelector` (optional)** _only used with the World framework_: a `string`: where to create the table in the namespace.

**`tableIdArgument` (optional)**: `bool`: whether to create getter and setter functions with the table ID as an argument, this is used to generate a single library to operate on multiple tables with the same schema and key structure.
**`tableIdArgument` (optional)**: `bool`: whether to create getter and setter functions with the table ID as an argument, this is used to generate a single library to operate on multiple tables with the same key and value structure.

**`storeArgument` (optional)**: `bool`: whether to create getter and setter functions with the store address as an argument, this is used to generate a single library to operate on the same table in multiple stores. This adds new functions to the library, doubling the amount of functions created (each getter/setter will comes in a pair of “with `storeArgument`” and “without `storeArgument`”)

Expand All @@ -86,7 +86,7 @@ Example:
```tsx
tables: {
MyTableWithTwoKeys: {
schema: {
valueSchema: {
value1: "uint32",
value2: "uint32",
},
Expand All @@ -98,14 +98,14 @@ tables: {
}
```

**`schema` (required)**: an object with keys being the column name, and value being types from `SchemaType`
**`valueSchema` (required)**: an object with keys being the column name, and value being types from `SchemaType`

Example:

```tsx
tables: {
MyTableWithFourValues: {
schema: {
valueSchema: {
x: "uint32",
y: "uint32",
stringField: "string",
Expand Down Expand Up @@ -135,7 +135,7 @@ Example:
```tsx
tables: {
MySingletonTable: {
schema: {
valueSchema: {
value1: "uint32",
value2: "uint32",
},
Expand Down
8 changes: 4 additions & 4 deletions docs/pages/store/reading-and-writing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This section assumes the existence of “MyTable” as described with the config
// definition of MyTable
tables: {
MyTable: {
schema: {
valueSchema: {
foo: "uint256",
bar: "bool",
fooArray: "uint256[]", // Store supports dynamic arrays
Expand Down Expand Up @@ -106,7 +106,7 @@ This section assumes the existence of “MyTable” as described with the config
// definition of MyTable
tables: {
MyTable: {
schema: {
valueSchema: {
foo: "uint256",
bar: "bool",
},
Expand Down Expand Up @@ -147,8 +147,8 @@ uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
// Retrieve a record
Schema schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
bytes memory loadedData = StoreCore.getRecord(tableId, key, schema);
Schema valueSchema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
bytes memory loadedData = StoreCore.getRecord(tableId, key, valueSchema);
uint256 foo = (uint256(Bytes.slice4(loadedData, 0)));
uint256 bar = (uint256(Bytes.slice4(loadedData, 32)));
```
Expand Down
19 changes: 13 additions & 6 deletions docs/pages/store/using-without-world.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { mudConfig } from "@latticexyz/store/register";
export default mudConfig({
tables: {
MyTable: {
schema: {
valueSchema: {
field1: "uint256",
field2: "uint256",
},
Expand Down Expand Up @@ -78,18 +78,25 @@ contract Contract is Store {
```solidity
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 schema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
uint256 table = uint256(keccak256("MyTable"));
StoreCore.registerSchema(table, schema, keySchema);
// Setting metadata is optional. It helps off-chain actors name columns
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";
StoreSwitch.setMetadata(table, "MyTable", fieldNames);

StoreCore.registerTable(tableId, fieldLayout, keySchema, valueSchema, keyNames, fieldNames);
}
}
```
Loading
Loading