Store
Reference
StoreCore (internal)

StoreCore

Git Source (opens in a new tab)

This library includes implementations for all IStore methods and events related to the store actions.

Functions

initialize

Initialize the store address in StoreSwitch.

Consumers must call this function in their constructor. StoreSwitch uses the storeAddress to decide where to write data to. If StoreSwitch is called in the context of a Store contract (storeAddress == address(this)), StoreSwitch uses internal methods to write data instead of external calls.

function initialize() internal;

registerCoreTables

Register core tables in the store.

Consumers must call this function in their constructor before setting any table data to allow indexers to decode table events.

function registerCoreTables() internal;

getFieldLayout

SCHEMA

Get the field layout for the given table ID.

function getFieldLayout(ResourceId tableId) internal view returns (FieldLayout);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table for which to get the field layout.

Returns

NameTypeDescription
<none>FieldLayoutThe field layout for the given table ID.

getKeySchema

Get the key schema for the given table ID.

Reverts if the table ID is not registered.

function getKeySchema(ResourceId tableId) internal view returns (Schema keySchema);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table for which to get the key schema.

Returns

NameTypeDescription
keySchemaSchemaThe key schema for the given table ID.

getValueSchema

Get the value schema for the given table ID.

Reverts if the table ID is not registered.

function getValueSchema(ResourceId tableId) internal view returns (Schema valueSchema);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table for which to get the value schema.

Returns

NameTypeDescription
valueSchemaSchemaThe value schema for the given table ID.

registerTable

Register a new table with the given configuration.

*This method reverts if

  • The table ID is not of type RESOURCE_TABLE or RESOURCE_OFFCHAIN_TABLE.
  • The field layout is invalid.
  • The key schema is invalid.
  • The value schema is invalid.
  • The number of key names does not match the number of key schema types.
  • The number of field names does not match the number of field layout fields.*
function registerTable(
  ResourceId tableId,
  FieldLayout fieldLayout,
  Schema keySchema,
  Schema valueSchema,
  string[] memory keyNames,
  string[] memory fieldNames
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to register.
fieldLayoutFieldLayoutThe field layout of the table.
keySchemaSchemaThe key schema of the table.
valueSchemaSchemaThe value schema of the table.
keyNamesstring[]The names of the keys in the table.
fieldNamesstring[]The names of the fields in the table.

registerStoreHook

REGISTER HOOKS

Register hooks to be called when a record or field is set or deleted.

This method reverts for all resource IDs other than tables. Hooks are not supported for offchain tables.

function registerStoreHook(ResourceId tableId, IStoreHook hookAddress, uint8 enabledHooksBitmap) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to register the hook for.
hookAddressIStoreHookThe address of the hook contract to register.
enabledHooksBitmapuint8The bitmap of enabled hooks.

unregisterStoreHook

Unregister a hook from the given table ID.

function unregisterStoreHook(ResourceId tableId, IStoreHook hookAddress) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to unregister the hook from.
hookAddressIStoreHookThe address of the hook to unregister.

setRecord

SET DATA

Set a full record for the given table ID and key tuple.

Calling this method emits a Store_SetRecord event. This method internally calls another overload of setRecord by fetching the field layout for the given table ID. If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.

function setRecord(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  bytes memory staticData,
  PackedCounter encodedLengths,
  bytes memory dynamicData
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to set the record for.
keyTuplebytes32[]An array representing the composite key for the record.
staticDatabytesThe static data of the record.
encodedLengthsPackedCounterThe encoded lengths of the dynamic data of the record.
dynamicDatabytesThe dynamic data of the record.

setRecord

Set a full data record for the given table ID, key tuple, and field layout.

For onchain tables, the method emits a Store_SetRecord event, updates the data in storage, calls onBeforeSetRecord hooks before actually modifying the state, and calls onAfterSetRecord hooks after modifying the state. For offchain tables, the method returns early after emitting the event without calling hooks or modifying the state.

function setRecord(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  bytes memory staticData,
  PackedCounter encodedLengths,
  bytes memory dynamicData,
  FieldLayout fieldLayout
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to set the record for.
keyTuplebytes32[]An array representing the composite key for the record.
staticDatabytesThe static data of the record.
encodedLengthsPackedCounterThe encoded lengths of the dynamic data of the record.
dynamicDatabytesThe dynamic data of the record.
fieldLayoutFieldLayoutThe field layout for the record.

spliceStaticData

Splice the static data for the given table ID and key tuple.

This method emits a Store_SpliceStaticData event, updates the data in storage, and calls onBeforeSpliceStaticData and onAfterSpliceStaticData hooks. For offchain tables, it returns early after emitting the event.

function spliceStaticData(ResourceId tableId, bytes32[] memory keyTuple, uint48 start, bytes memory data) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to splice the static data for.
keyTuplebytes32[]An array representing the composite key for the record.
startuint48The start position in bytes for the splice operation.
databytesThe data to write to the static data of the record at the start byte.

spliceDynamicData

Splice the dynamic data for the given table ID, key tuple, and dynamic field index.

This method emits a Store_SpliceDynamicData event, updates the data in storage, and calls onBeforeSpliceDynamicData and onAfterSpliceDynamicData hooks. For offchain tables, it returns early after emitting the event.

function spliceDynamicData(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex,
  uint40 startWithinField,
  uint40 deleteCount,
  bytes memory data
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to splice the dynamic data for.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to splice. (Dynamic field index = field index - number of static fields)
startWithinFielduint40The start position within the field for the splice operation.
deleteCountuint40The number of bytes to delete in the splice operation.
databytesThe data to insert into the dynamic data of the record at the start byte.

setField

Set data for a field at the given index in a table with the given tableId, key tuple, and value field layout.

This method internally calls another overload of setField by fetching the field layout for the given table ID. If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read. This function emits a Store_SpliceStaticData or Store_SpliceDynamicData event and calls the corresponding hooks. For offchain tables, it returns early after emitting the event.

function setField(ResourceId tableId, bytes32[] memory keyTuple, uint8 fieldIndex, bytes memory data) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to set the field for.
keyTuplebytes32[]An array representing the key for the record.
fieldIndexuint8The index of the field to set.
databytesThe data to set for the field.

setField

Set data for a field at the given index in a table with the given tableId, key tuple, and value field layout.

This method internally calls to setStaticField or setDynamicField based on the field index and layout. Calling setStaticField or setDynamicField directly is recommended if the caller is aware of the field layout. This function emits a Store_SpliceStaticData or Store_SpliceDynamicData event, updates the data in storage, and calls the corresponding hooks. For offchain tables, it returns early after emitting the event.

function setField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 fieldIndex,
  bytes memory data,
  FieldLayout fieldLayout
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to set the field for.
keyTuplebytes32[]An array representing the composite key for the record.
fieldIndexuint8The index of the field to set.
databytesThe data to set for the field.
fieldLayoutFieldLayoutThe field layout for the record.

setStaticField

Set a static field for the given table ID, key tuple, field index, and field layout.

This method emits a Store_SpliceStaticData event, updates the data in storage and calls the onBeforeSpliceStaticData and onAfterSpliceStaticData hooks. For offchain tables, it returns early after emitting the event.

function setStaticField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 fieldIndex,
  bytes memory data,
  FieldLayout fieldLayout
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to set the static field for.
keyTuplebytes32[]An array representing the key for the record.
fieldIndexuint8The index of the field to set.
databytesThe data to set for the static field.
fieldLayoutFieldLayoutThe field layout for the record.

setDynamicField

Set a dynamic field for the given table ID, key tuple, and dynamic field index.

This method emits a Store_SpliceDynamicData event, updates the data in storage and calls the onBeforeSpliceDynamicaData and onAfterSpliceDynamicData hooks. For offchain tables, it returns early after emitting the event.

function setDynamicField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex,
  bytes memory data
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to set the dynamic field for.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to set. (Dynamic field index = field index - number of static fields).
databytesThe data to set for the dynamic field.

deleteRecord

Delete a record for the given table ID and key tuple.

This method internally calls another overload of deleteRecord by fetching the field layout for the given table ID. This method deletes static data and sets the dynamic data length to 0, but does not actually modify the dynamic data. It emits a Store_DeleteRecord event and emits the onBeforeDeleteRecord and onAfterDeleteRecord hooks. For offchain tables, it returns early after emitting the event.

function deleteRecord(ResourceId tableId, bytes32[] memory keyTuple) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to delete the record from.
keyTuplebytes32[]An array representing the composite key for the record.

deleteRecord

Delete a record for the given table ID and key tuple.

This method deletes static data and sets the dynamic data length to 0, but does not actually modify the dynamic data. It emits a Store_DeleteRecord event and emits the onBeforeDeleteRecord and onAfterDeleteRecord hooks. For offchain tables, it returns early after emitting the event.

function deleteRecord(ResourceId tableId, bytes32[] memory keyTuple, FieldLayout fieldLayout) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to delete the record from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldLayoutFieldLayoutThe field layout for the record.

pushToDynamicField

Push data to a field at the dynamic field index in a table with the given table ID and key tuple.

This method emits a Store_SpliceDynamicData event, updates the data in storage and calls the onBeforeSpliceDynamicData and onAfterSpliceDynamicData hooks. For offchain tables, it returns early after emitting the event.

function pushToDynamicField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex,
  bytes memory dataToPush
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to push data to the dynamic field.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to push data to.
dataToPushbytesThe data to push to the dynamic field.

popFromDynamicField

Pop data from a field at the dynamic field index in a table with the given table ID and key tuple.

This method emits a Store_SpliceDynamicData event, updates the data in storage and calls the onBeforeSpliceDynamicData and onAfterSpliceDynamicData hooks. For offchain tables, it returns early after emitting the event.

function popFromDynamicField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex,
  uint256 byteLengthToPop
) internal;

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to pop data from the dynamic field.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to pop data from.
byteLengthToPopuint256The byte length to pop from the dynamic field.

getRecord

GET DATA

Get the full record (all fields, static and dynamic data) for the given table ID and key tuple.

This function internally calls another overload of getRecord, loading the field layout from storage. If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.

function getRecord(
  ResourceId tableId,
  bytes32[] memory keyTuple
) internal view returns (bytes memory staticData, PackedCounter encodedLengths, bytes memory dynamicData);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the record from.
keyTuplebytes32[]An array representing the composite key for the record.

Returns

NameTypeDescription
staticDatabytesThe static data of the record.
encodedLengthsPackedCounterThe encoded lengths of the dynamic data of the record.
dynamicDatabytesThe dynamic data of the record.

getRecord

Get the full record (all fields, static and dynamic data) for the given table ID and key tuple, with the given field layout.

function getRecord(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  FieldLayout fieldLayout
) internal view returns (bytes memory staticData, PackedCounter encodedLengths, bytes memory dynamicData);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the record from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldLayoutFieldLayoutThe field layout for the record.

Returns

NameTypeDescription
staticDatabytesThe static data of the record.
encodedLengthsPackedCounterThe encoded lengths of the dynamic data of the record.
dynamicDatabytesThe dynamic data of the record.

getField

Get a single field from the given table ID and key tuple.

This function internally calls another overload of getField, loading the field layout from storage.

function getField(ResourceId tableId, bytes32[] memory keyTuple, uint8 fieldIndex) internal view returns (bytes memory);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the field from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldIndexuint8The index of the field to get.

Returns

NameTypeDescription
<none>bytesThe data of the field.

getField

Get a single field from the given table ID and key tuple, with the given field layout.

function getField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 fieldIndex,
  FieldLayout fieldLayout
) internal view returns (bytes memory);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the field from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldIndexuint8The index of the field to get.
fieldLayoutFieldLayoutThe field layout for the record.

Returns

NameTypeDescription
<none>bytesThe data of the field.

getStaticField

Get a single static field from the given table ID and key tuple, with the given value field layout.

The field value is left-aligned in the returned bytes32, the rest of the word is not zeroed out. Consumers are expected to truncate the returned value as needed.

function getStaticField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 fieldIndex,
  FieldLayout fieldLayout
) internal view returns (bytes32);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the static field from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldIndexuint8The index of the field to get.
fieldLayoutFieldLayoutThe field layout for the record.

Returns

NameTypeDescription
<none>bytes32The data of the static field.

getDynamicField

Get a single dynamic field from the given table ID and key tuple.

function getDynamicField(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex
) internal view returns (bytes memory);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the dynamic field from.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to get, relative to the start of the dynamic fields. (Dynamic field index = field index - number of static fields)

Returns

NameTypeDescription
<none>bytesThe data of the dynamic field.

getFieldLength

Get the byte length of a single field from the given table ID and key tuple.

This function internally calls another overload of getFieldLength, loading the field layout from storage. If the field layout is available to the caller, it is recommended to use the other overload to avoid an additional storage read.

function getFieldLength(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 fieldIndex
) internal view returns (uint256);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the field length from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldIndexuint8The index of the field to get the length for.

Returns

NameTypeDescription
<none>uint256The byte length of the field.

getFieldLength

Get the byte length of a single field from the given table ID and key tuple.

function getFieldLength(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 fieldIndex,
  FieldLayout fieldLayout
) internal view returns (uint256);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the field length from.
keyTuplebytes32[]An array representing the composite key for the record.
fieldIndexuint8The index of the field to get the length for.
fieldLayoutFieldLayoutThe field layout for the record.

Returns

NameTypeDescription
<none>uint256The byte length of the field.

getDynamicFieldLength

Get the byte length of a single dynamic field from the given table ID and key tuple.

function getDynamicFieldLength(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex
) internal view returns (uint256);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the dynamic field length from.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to get the length for, relative to the start of the dynamic fields. (Dynamic field index = field index - number of static fields)

Returns

NameTypeDescription
<none>uint256The byte length of the dynamic field.

getDynamicFieldSlice

Get a byte slice (including start, excluding end) of a single dynamic field from the given table ID and key tuple.

function getDynamicFieldSlice(
  ResourceId tableId,
  bytes32[] memory keyTuple,
  uint8 dynamicFieldIndex,
  uint256 start,
  uint256 end
) internal view returns (bytes memory);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table to get the dynamic field slice from.
keyTuplebytes32[]An array representing the composite key for the record.
dynamicFieldIndexuint8The index of the dynamic field to get the slice from, relative to the start of the dynamic fields. (Dynamic field index = field index - number of static fields)
startuint256The start index within the dynamic field for the slice operation (inclusive).
enduint256The end index within the dynamic field for the slice operation (exclusive).

Returns

NameTypeDescription
<none>bytesThe byte slice of the dynamic field.

Events

Store_SetRecord

Emitted when a new record is set in the store.

event Store_SetRecord(
    ResourceId indexed tableId, bytes32[] keyTuple, bytes staticData, PackedCounter encodedLengths, bytes dynamicData
);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table where the record is set.
keyTuplebytes32[]An array representing the composite key for the record.
staticDatabytesThe static data of the record.
encodedLengthsPackedCounterThe encoded lengths of the dynamic data of the record.
dynamicDatabytesThe dynamic data of the record.

Store_SpliceStaticData

Emitted when static data in the store is spliced.

In static data, data is always overwritten starting at the start position, so the total length of the data remains the same and no data is shifted.

event Store_SpliceStaticData(ResourceId indexed tableId, bytes32[] keyTuple, uint48 start, bytes data);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table where the data is spliced.
keyTuplebytes32[]An array representing the key for the record.
startuint48The start position in bytes for the splice operation.
databytesThe data to write to the static data of the record at the start byte.

Store_SpliceDynamicData

Emitted when dynamic data in the store is spliced.

event Store_SpliceDynamicData(
    ResourceId indexed tableId,
    bytes32[] keyTuple,
    uint48 start,
    uint40 deleteCount,
    PackedCounter encodedLengths,
    bytes data
);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table where the data is spliced.
keyTuplebytes32[]An array representing the composite key for the record.
startuint48The start position in bytes for the splice operation.
deleteCountuint40The number of bytes to delete in the splice operation.
encodedLengthsPackedCounterThe encoded lengths of the dynamic data of the record.
databytesThe data to insert into the dynamic data of the record at the start byte.

Store_DeleteRecord

Emitted when a record is deleted from the store.

event Store_DeleteRecord(ResourceId indexed tableId, bytes32[] keyTuple);

Parameters

NameTypeDescription
tableIdResourceIdThe ID of the table where the record is deleted.
keyTuplebytes32[]An array representing the composite key for the record.