Skip to content

Commit

Permalink
feat: rename key to keyTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 14, 2023
1 parent 48c5fdc commit 120c576
Show file tree
Hide file tree
Showing 94 changed files with 24,250 additions and 522 deletions.
12 changes: 6 additions & 6 deletions docs/pages/store/advanced-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ contract MirrorSubscriber is IStoreHook {
_tableId = tableId;
}
function onSetRecord(bytes32 tableId, bytes32[] memory key, bytes memory data) public {
function onSetRecord(bytes32 tableId, bytes32[] memory keyTuple, bytes memory data) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.setRecord(indexerTableId, key, data);
StoreSwitch.setRecord(indexerTableId, keyTuple, data);
}
function onSetField(bytes32 tableId, bytes32[] memory key, uint8 schemaIndex, bytes memory data) public {
function onSetField(bytes32 tableId, bytes32[] memory keyTuple, uint8 schemaIndex, bytes memory data) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.setField(indexerTableId, key, schemaIndex, data);
StoreSwitch.setField(indexerTableId, keyTuple, schemaIndex, data);
}
function onDeleteRecord(bytes32 tableId, bytes32[] memory key) public {
function onDeleteRecord(bytes32 tableId, bytes32[] memory keyTuple) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.deleteRecord(indexerTableId, key);
StoreSwitch.deleteRecord(indexerTableId, keyTuple);
}
}
```
Expand Down
60 changes: 30 additions & 30 deletions docs/pages/store/reading-and-writing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,56 @@ uint256[2] memory barArray;
barArray[0] = 69;
barArray[1] = 85;
fooArray[0] = 42;
// args: key, foo, bar, fooArray, staticArray
MyTable.set(keccak256("some.key"), 45, false, fooArray, barArray);
// args: keyTuple, foo, bar, fooArray, staticArray
MyTable.set(keccak256("some.keyTuple"), 45, false, fooArray, barArray);
```

**Setting a field**

```solidity
// Setting foo
MyTable.setFoo(keccak256("some.key"), 33);
MyTable.setFoo(keccak256("some.keyTuple"), 33);
// Setting bar
MyTable.setBar(keccak256("some.key"), false);
MyTable.setBar(keccak256("some.keyTuple"), false);
// Setting fooArray
uint256[] memory fooArray = new uint256[](3);
fooArray[0] = 42;
fooArray[1] = 15;
fooArray[2] = 3;
MyTable.setFooArray(keccak256("some.key"), fooArray);
MyTable.setFooArray(keccak256("some.keyTuple"), fooArray);
```

**Operations on dynamic arrays**

```solidity
MyTable.pushFooArray(keccak256("some.key"), 4242); // adds 4242 at end of fooArray
MyTable.popFooArray(keccak256("some.key")); // pop fooArray
MyTable.setItemFooArray(keccak256("some.key"), 0, 123); // set fooArray[0] to 123
MyTable.pushFooArray(keccak256("some.keyTuple"), 4242); // adds 4242 at end of fooArray
MyTable.popFooArray(keccak256("some.keyTuple")); // pop fooArray
MyTable.setItemFooArray(keccak256("some.keyTuple"), 0, 123); // set fooArray[0] to 123
```

**Retrieving a record**

```solidity
// Retrieving a record
MyTable.get(keccak256("some.key"));
MyTable.get(keccak256("some.keyTuple"));
```

**Retrieving a field**

```solidity
// Retrieve foo
MyTable.getFoo(keccak256("some.key"));
MyTable.getFoo(keccak256("some.keyTuple"));
// Retrieve bar
MyTable.getBar(keccak256("some.key"));
MyTable.getBar(keccak256("some.keyTuple"));
// Retrieve an element of fooArray
MyTable.getItemFooArray(keccak256("some.key"), 0); // get fooArray[0]
MyTable.getItemFooArray(keccak256("some.keyTuple"), 0); // get fooArray[0]
```

**Deleting a record**

```solidity
// Deleting a record
MyTable.deleteRecord(keccak256("some.key"));
MyTable.deleteRecord(keccak256("some.keyTuple"));
```

### Access to Store via low-level API
Expand Down Expand Up @@ -120,10 +120,10 @@ tables: {
uint256 tableId = uint256(keccak256("MyTable"));
uint256 foo = 10;
uint256 bar = 24;
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = keccak256("some.keyTuple");
// Setting a record
StoreSwitch.setRecord(tableId, key, abi.encodePacked((foo, bar)));
StoreSwitch.setRecord(tableId, keyTuple, abi.encodePacked((foo, bar)));
```

**Setting a field**
Expand All @@ -132,23 +132,23 @@ StoreSwitch.setRecord(tableId, key, abi.encodePacked((foo, bar)));
uint256 tableId = uint256(keccak256("MyTable"));
uint256 foo = 45;
uint256 foo = 67;
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = keccak256("some.keyTuple");
// Setting foo
StoreSwitch.setField(tableId, key, 0, abi.encodePacked((foo)));
StoreSwitch.setField(tableId, keyTuple, 0, abi.encodePacked((foo)));
// Setting bar
StoreSwitch.setField(tableId, key, 1, abi.encodePacked((bar)));
StoreSwitch.setField(tableId, keyTuple, 1, abi.encodePacked((bar)));
```

**Retrieving a record**

```solidity
uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = keccak256("some.keyTuple");
// Retrieve a record
Schema valueSchema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
bytes memory loadedData = StoreCore.getRecord(tableId, key, valueSchema);
bytes memory loadedData = StoreCore.getRecord(tableId, keyTuple, valueSchema);
uint256 foo = (uint256(Bytes.slice4(loadedData, 0)));
uint256 bar = (uint256(Bytes.slice4(loadedData, 32)));
```
Expand All @@ -157,22 +157,22 @@ uint256 bar = (uint256(Bytes.slice4(loadedData, 32)));

```solidity
uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = keccak256("some.keyTuple");
// Retrieve foo
bytes memory loadedDatafoo = StoreCore.getField(tableId, key, 0);
bytes memory loadedDatafoo = StoreCore.getField(tableId, keyTuple, 0);
int32 foo = (uint32(Bytes.slice4(loadedDatafoo, 0)));
// Retrieve bar
bytes memory loadedDatabar = StoreCore.getField(tableId, key, 1);
bytes memory loadedDatabar = StoreCore.getField(tableId, keyTuple, 1);
int32 bar = (uint32(Bytes.slice4(loadedData, 0)));
```

**Deleting a record**

```solidity
uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = keccak256("some.keyTuple");
// Deleting a record
StoreCore.deleteRecord(tableId, key);
StoreCore.deleteRecord(tableId, keyTuple);
```
6 changes: 3 additions & 3 deletions docs/pages/store/spec.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Aside } from "../../components/Aside";
When a record or a single field is edited, or when a record is deleted, Store emits a standard event with enough information for off-chain actors to reconstruct the new version of the Store with event-sourcing.

```solidity
event StoreSetRecord(uint256 tableId, bytes32[] key, bytes data);
event StoreSetField(uint256 tableId, bytes32[] key, uint8 schemaIndex, bytes data);
event StoreDeleteRecord(uint256 tableId, bytes32[] key);
event StoreSetRecord(uint256 tableId, bytes32[] keyTuple, bytes data);
event StoreSetField(uint256 tableId, bytes32[] keyTuple, uint8 schemaIndex, bytes data);
event StoreDeleteRecord(uint256 tableId, bytes32[] keyTuple);
```

Each event includes the table ID and each key encoded as `bytes32`.
Expand Down
6 changes: 3 additions & 3 deletions docs/pages/world/modules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For example, it can be used to fetch all keys in the "Position" table (if the ta
It offers two methods:

- `getKeysInTable(bytes32 tableId)` returns all keys in a table
- `hasKey(bytes32 tableId, bytes32[] memory key)` returns whether a given key is in a table (with O(1) gas)
- `hasKey(bytes32 tableId, bytes32[] memory keyTuple)` returns whether a given keyTuple is in a table (with O(1) gas)

Using `getKeysInTable` to retrieve all keys in the Player table:

Expand All @@ -62,7 +62,7 @@ import { Player, PlayerTableId } from "../codegen/tables/Player.sol";
bytes32[][] memory players = getKeysInTable(world, PlayerTableId);
```

Using `hasKey` to check if a key is in the Player table:
Using `hasKey` to check if a keyTuple is in the Player table:

```solidity
// assumes a boolean flag indicates a player:
Expand All @@ -79,7 +79,7 @@ bool memory isPlayer = hasKey(world, PlayerTableId, keyTuple);
Internally, it works by installing a [hook](https://v2.mud.dev/store/advanced-features#storage-hooks) that maintains:

- an array of all keys in the table
- an index or "**reverse mapping**" of whether a given key is in the table
- an index or "**reverse mapping**" of whether a given keyTuple is in the table

#### **`KeysWithValueModule`**

Expand Down
8 changes: 4 additions & 4 deletions packages/block-logs-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ latestBlockNumber$
publicClient,
address,
events: parseAbi([
"event StoreDeleteRecord(bytes32 tableId, bytes32[] key)",
"event StoreSetField(bytes32 tableId, bytes32[] key, uint8 schemaIndex, bytes data)",
"event StoreSetRecord(bytes32 tableId, bytes32[] key, bytes data)",
"event StoreEphemeralRecord(bytes32 tableId, bytes32[] key, bytes data)",
"event StoreDeleteRecord(bytes32 tableId, bytes32[] keyTuple)",
"event StoreSetField(bytes32 tableId, bytes32[] keyTuple, uint8 schemaIndex, bytes data)",
"event StoreSetRecord(bytes32 tableId, bytes32[] keyTuple, bytes data)",
"event StoreEphemeralRecord(bytes32 tableId, bytes32[] keyTuple, bytes data)",
]),
}),
mergeMap(({ logs }) => from(groupLogsByBlockNumber(logs)))
Expand Down
Loading

0 comments on commit 120c576

Please sign in to comment.