Skip to content

Latest commit

 

History

History
178 lines (143 loc) · 4.51 KB

reading-and-writing.mdx

File metadata and controls

178 lines (143 loc) · 4.51 KB

import { Aside } from "../../components/Aside";

Reading and writing to Store

There exists two ways to interact with the Store:

  1. Using the libraries generated by tablegen
  2. Directly using the low-level API

Access to Store via code-generated libraries

Accessing Store via the libraries generated with the `tablegen` tool is the recommended way to use Store.

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(keccak256("some.key"), 45, false, fooArray, barArray);

Setting a field

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

Operations on dynamic arrays

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

Retrieving a record

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

Retrieving a field

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

Deleting a record

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

Access to Store via low-level API

Accessing Store via the low-level API is not recommend, although it is safe (no storage corruption can happen).

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 key = new bytes32[](1);
key[0] = keccak256("some.key");
// Setting a record
StoreSwitch.setRecord(tableId, key, abi.encodePacked((foo, bar)));

Setting a field

uint256 tableId = uint256(keccak256("MyTable"));
uint256 foo = 45;
uint256 foo = 67;
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
// Setting foo
StoreSwitch.setField(tableId, key, 0, abi.encodePacked((foo)));
// Setting bar
StoreSwitch.setField(tableId, key, 1, abi.encodePacked((bar)));

Retrieving a record

uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
// Retrieve a record
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)));

Retrieving a field

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

Deleting a record

uint256 tableId = uint256(keccak256("MyTable"));
bytes32[] memory key = new bytes32[](1);
key[0] = keccak256("some.key");
// Deleting a record
StoreCore.deleteRecord(tableId, key);