Reading and writing to Store
There exists two ways to interact with the Store:
- Using the libraries generated by
tablegen
- Directly using the low-level API
Access to Store via code-generated libraries
The tablegen
tool of the MUD CLI can create libraries for each table that wraps the low-level API of Store and types the results.
This section assumes the existence of “MyTable” as described with the configuration below.
// definition of MyTable
tables: {
MyTable: {
valueSchema: {
foo: "uint256",
bar: "bool",
fooArray: "uint256[]", // Store supports dynamic arrays
barArray: "uint256[2]" // Store also supports static arrays
},
},
}
Setting a record
// Setting a record
uint256[] memory fooArray = new uint256[](1);
uint256[2] memory barArray;
barArray[0] = 69;
barArray[1] = 85;
fooArray[0] = 42;
// args: key, foo, bar, fooArray, staticArray
MyTable.set("some.key", 45, false, fooArray, barArray);
Setting a field
// Setting foo
MyTable.setFoo("some.key", 33);
// Setting bar
MyTable.setBar("some.key", false);
// Setting fooArray
uint256[] memory fooArray = new uint256[](3);
fooArray[0] = 42;
fooArray[1] = 15;
fooArray[2] = 3;
MyTable.setFooArray("some.key", fooArray);
Operations on dynamic arrays
MyTable.pushFooArray("some.key", 4242); // adds 4242 at end of fooArray
MyTable.popFooArray("some.key"); // pop fooArray
MyTable.setItemFooArray("some.key", 0, 123); // set fooArray[0] to 123
Retrieving a record
// Retrieving a record
MyTable.get("some.key");
Retrieving a field
// Retrieve foo
MyTable.getFoo("some.key");
// Retrieve bar
MyTable.getBar("some.key");
// Retrieve an element of fooArray
MyTable.getItemFooArray("some.key", 0); // get fooArray[0]
Deleting a record
// Deleting a record
MyTable.deleteRecord("some.key");
Access to Store via low-level API
Reading records returns bytes
, and writing to records require bytes
too. Store will make sure to prevent corruption when writes happen by retrieving the schemas and computing expected total length.
This section assumes the existence of “MyTable” as described with the configuration below.
// definition of MyTable
tables: {
MyTable: {
valueSchema: {
foo: "uint256",
bar: "bool",
},
},
}
Setting a record
uint256 tableId = uint256(keccak256("MyTable"));
uint256 foo = 10;
uint256 bar = 24;
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = "some.key";
// Setting a record
StoreSwitch.setRecord(tableId, keyTuple, abi.encodePacked((foo, bar)));
Setting a field
uint256 tableId = uint256(keccak256("MyTable"));
uint256 foo = 45;
uint256 foo = 67;
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = "some.key";
// Setting foo
StoreSwitch.setField(tableId, keyTuple, 0, abi.encodePacked((foo)));
// Setting bar
StoreSwitch.setField(tableId, keyTuple, 1, abi.encodePacked((bar)));
Retrieving a record
uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = "some.key";
// Retrieve a record
Schema valueSchema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
bytes memory loadedData = StoreCore.getRecord(tableId, keyTuple, valueSchema);
uint256 foo = (uint256(Bytes.slice4(loadedData, 0)));
uint256 bar = (uint256(Bytes.slice4(loadedData, 32)));
Retrieving a field
uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = "some.key";
// Retrieve foo
bytes memory loadedDatafoo = StoreCore.getField(tableId, keyTuple, 0);
int32 foo = (uint32(Bytes.slice4(loadedDatafoo, 0)));
// Retrieve bar
bytes memory loadedDatabar = StoreCore.getField(tableId, keyTuple, 1);
int32 bar = (uint32(Bytes.slice4(loadedData, 0)));
Deleting a record
uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = "some.key";
// Deleting a record
StoreCore.deleteRecord(tableId, keyTuple);