From 708b49c50e05f7b67b596e72ebfcbd76e1ff6280 Mon Sep 17 00:00:00 2001 From: Boffee Date: Sat, 16 Sep 2023 17:06:33 -0500 Subject: [PATCH] feat(common,store,world): add internal table functions and use them for internal mud logic (#1513) Co-authored-by: alvrs --- .changeset/thin-terms-lay.md | 25 + .../contracts/src/codegen/tables/Multi.sol | 95 ++++ .../contracts/src/codegen/tables/Number.sol | 30 ++ .../src/codegen/tables/NumberList.sol | 76 +++ .../contracts/src/codegen/tables/Vector.sol | 74 +++ .../src/codegen/tables/CounterTable.sol | 27 ++ .../src/codegen/tables/Inventory.sol | 36 ++ .../src/codegen/tables/MessageTable.sol | 16 + .../src/codegen/tables/Dynamics1.sol | 428 +++++++++++++++++ .../src/codegen/tables/Dynamics2.sol | 252 ++++++++++ .../src/codegen/tables/Ephemeral.sol | 18 + .../src/codegen/tables/Singleton.sol | 239 ++++++++++ .../contracts/src/codegen/tables/Statics.sol | 237 ++++++++++ .../src/codegen/render-solidity/common.ts | 8 +- packages/store/gas-report.json | 102 ++-- packages/store/src/Hook.sol | 4 +- packages/store/src/StoreCore.sol | 22 +- .../store/src/codegen/tables/Callbacks.sol | 91 ++++ packages/store/src/codegen/tables/Hooks.sol | 91 ++++ .../store/src/codegen/tables/KeyEncoding.sol | 52 +++ packages/store/src/codegen/tables/Mixed.sol | 216 +++++++++ .../store/src/codegen/tables/StoreHooks.sol | 91 ++++ packages/store/src/codegen/tables/Tables.sol | 247 ++++++++++ packages/store/src/codegen/tables/Vector2.sol | 74 +++ packages/store/ts/codegen/ephemeral.ts | 28 +- packages/store/ts/codegen/field.ts | 28 +- packages/store/ts/codegen/record.ts | 40 +- packages/store/ts/codegen/renderTable.ts | 14 +- packages/world/gas-report.json | 116 ++--- packages/world/src/AccessControl.sol | 6 +- packages/world/src/SystemCall.sol | 8 +- packages/world/src/World.sol | 14 +- .../world/src/modules/core/CoreModule.sol | 6 +- .../AccessManagementSystem.sol | 10 +- .../implementations/BalanceTransferSystem.sol | 10 +- .../ModuleInstallationSystem.sol | 2 +- .../StoreRegistrationSystem.sol | 8 +- .../WorldRegistrationSystem.sol | 38 +- .../src/modules/core/tables/Balances.sol | 30 ++ .../modules/core/tables/FunctionSelectors.sol | 71 +++ .../src/modules/core/tables/ResourceType.sol | 30 ++ .../src/modules/core/tables/SystemHooks.sol | 91 ++++ .../modules/core/tables/SystemRegistry.sol | 30 ++ .../world/src/modules/core/tables/Systems.sol | 69 +++ .../keysintable/tables/KeysInTable.sol | 436 ++++++++++++++++++ .../keysintable/tables/UsedKeysIndex.sol | 76 +++ .../keyswithvalue/tables/KeysWithValue.sol | 91 ++++ .../tables/CallboundDelegations.sol | 55 +++ .../tables/TimeboundDelegations.sol | 33 ++ .../uniqueentity/tables/UniqueEntity.sol | 27 ++ packages/world/src/tables/Delegations.sol | 33 ++ .../world/src/tables/InstalledModules.sol | 33 ++ packages/world/src/tables/NamespaceOwner.sol | 30 ++ packages/world/src/tables/ResourceAccess.sol | 33 ++ packages/world/test/tables/AddressArray.sol | 91 ++++ packages/world/test/tables/Bool.sol | 27 ++ .../contracts/src/codegen/tables/Counter.sol | 27 ++ .../contracts/src/codegen/tables/Counter.sol | 27 ++ .../contracts/src/codegen/tables/Position.sol | 91 ++++ .../contracts/src/codegen/tables/Counter.sol | 27 ++ 60 files changed, 4108 insertions(+), 229 deletions(-) create mode 100644 .changeset/thin-terms-lay.md diff --git a/.changeset/thin-terms-lay.md b/.changeset/thin-terms-lay.md new file mode 100644 index 0000000000..31999bcaa5 --- /dev/null +++ b/.changeset/thin-terms-lay.md @@ -0,0 +1,25 @@ +--- +"@latticexyz/cli": patch +"@latticexyz/common": minor +"@latticexyz/store": minor +"@latticexyz/world": patch +--- + +Generated table libraries now have a set of functions prefixed with `_` that always use their own storage for read/write. +This saves gas for use cases where the functionality to dynamically determine which `Store` to use for read/write is not needed, e.g. root systems in a `World`, or when using `Store` without `World`. + +We decided to continue to always generate a set of functions that dynamically decide which `Store` to use, so that the generated table libraries can still be imported by non-root systems. + +```solidity + +library Counter { + // Dynamically determine which store to write to based on the context + function set(uint32 value) internal; + + // Always write to own storage + function _set(uint32 value) internal; + + // ... equivalent functions for all other Store methods +} + +``` diff --git a/e2e/packages/contracts/src/codegen/tables/Multi.sol b/e2e/packages/contracts/src/codegen/tables/Multi.sol index 8b97411563..fd30c691ef 100644 --- a/e2e/packages/contracts/src/codegen/tables/Multi.sol +++ b/e2e/packages/contracts/src/codegen/tables/Multi.sol @@ -77,6 +77,11 @@ library Multi { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -94,6 +99,18 @@ library Multi { return (int256(uint256(bytes32(_blob)))); } + /** Get num */ + function _getNum(uint32 a, bool b, uint256 c, int120 d) internal view returns (int256 num) { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (int256(uint256(bytes32(_blob)))); + } + /** Get num (using the specified store) */ function getNum(IStore _store, uint32 a, bool b, uint256 c, int120 d) internal view returns (int256 num) { bytes32[] memory _keyTuple = new bytes32[](4); @@ -117,6 +134,17 @@ library Multi { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), _fieldLayout); } + /** Set num */ + function _setNum(uint32 a, bool b, uint256 c, int120 d, int256 num) internal { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((num)), _fieldLayout); + } + /** Set num (using the specified store) */ function setNum(IStore _store, uint32 a, bool b, uint256 c, int120 d, int256 num) internal { bytes32[] memory _keyTuple = new bytes32[](4); @@ -140,6 +168,18 @@ library Multi { return (_toBool(uint8(bytes1(_blob)))); } + /** Get value */ + function _getValue(uint32 a, bool b, uint256 c, int120 d) internal view returns (bool value) { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get value (using the specified store) */ function getValue(IStore _store, uint32 a, bool b, uint256 c, int120 d) internal view returns (bool value) { bytes32[] memory _keyTuple = new bytes32[](4); @@ -163,6 +203,17 @@ library Multi { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _setValue(uint32 a, bool b, uint256 c, int120 d, bool value) internal { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function setValue(IStore _store, uint32 a, bool b, uint256 c, int120 d, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](4); @@ -186,6 +237,18 @@ library Multi { return decode(_blob); } + /** Get the full data */ + function _get(uint32 a, bool b, uint256 c, int120 d) internal view returns (MultiData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, uint32 a, bool b, uint256 c, int120 d) internal view returns (MultiData memory _table) { bytes32[] memory _keyTuple = new bytes32[](4); @@ -214,6 +277,22 @@ library Multi { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(uint32 a, bool b, uint256 c, int120 d, int256 num, bool value) internal { + bytes memory _staticData = encodeStatic(num, value); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, uint32 a, bool b, uint256 c, int120 d, int256 num, bool value) internal { bytes memory _staticData = encodeStatic(num, value); @@ -235,6 +314,11 @@ library Multi { set(a, b, c, d, _table.num, _table.value); } + /** Set the full data using the data struct */ + function _set(uint32 a, bool b, uint256 c, int120 d, MultiData memory _table) internal { + set(a, b, c, d, _table.num, _table.value); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, uint32 a, bool b, uint256 c, int120 d, MultiData memory _table) internal { set(_store, a, b, c, d, _table.num, _table.value); @@ -284,6 +368,17 @@ library Multi { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(uint32 a, bool b, uint256 c, int120 d) internal { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(a)); + _keyTuple[1] = _boolToBytes32(b); + _keyTuple[2] = bytes32(uint256(c)); + _keyTuple[3] = bytes32(uint256(int256(d))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, uint32 a, bool b, uint256 c, int120 d) internal { bytes32[] memory _keyTuple = new bytes32[](4); diff --git a/e2e/packages/contracts/src/codegen/tables/Number.sol b/e2e/packages/contracts/src/codegen/tables/Number.sol index f5c62ae689..77dcf598c3 100644 --- a/e2e/packages/contracts/src/codegen/tables/Number.sol +++ b/e2e/packages/contracts/src/codegen/tables/Number.sol @@ -64,6 +64,11 @@ library Number { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library Number { return (uint32(bytes4(_blob))); } + /** Get value */ + function _get(uint32 key) internal view returns (uint32 value) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get value (using the specified store) */ function get(IStore _store, uint32 key) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library Number { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(uint32 key, uint32 value) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, uint32 key, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -134,6 +156,14 @@ library Number { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(uint32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, uint32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/e2e/packages/contracts/src/codegen/tables/NumberList.sol b/e2e/packages/contracts/src/codegen/tables/NumberList.sol index 8f203ff4d0..2703550074 100644 --- a/e2e/packages/contracts/src/codegen/tables/NumberList.sol +++ b/e2e/packages/contracts/src/codegen/tables/NumberList.sol @@ -62,6 +62,11 @@ library NumberList { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,14 @@ library NumberList { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } + /** Get value */ + function _get() internal view returns (uint32[] memory value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); + } + /** Get value (using the specified store) */ function get(IStore _store) internal view returns (uint32[] memory value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -90,6 +103,13 @@ library NumberList { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } + /** Set value */ + function _set(uint32[] memory value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, uint32[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -107,6 +127,16 @@ library NumberList { } } + /** Get the length of value */ + function _length() internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](0); + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 4; + } + } + /** Get the length of value (using the specified store) */ function length(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -137,6 +167,19 @@ library NumberList { } } + /** + * Get an item of value + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(uint256 _index) internal view returns (uint32) { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 4, (_index + 1) * 4); + return (uint32(bytes4(_blob))); + } + } + /** * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -157,6 +200,13 @@ library NumberList { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to value */ + function _push(uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to value (using the specified store) */ function push(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -171,6 +221,13 @@ library NumberList { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 4, _fieldLayout); } + /** Pop an element from value */ + function _pop() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.popFromField(_tableId, _keyTuple, 0, 4, _fieldLayout); + } + /** Pop an element from value (using the specified store) */ function pop(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -190,6 +247,18 @@ library NumberList { } } + /** + * Update an element of value at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(uint256 _index, uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -238,6 +307,13 @@ library NumberList { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/e2e/packages/contracts/src/codegen/tables/Vector.sol b/e2e/packages/contracts/src/codegen/tables/Vector.sol index a872229034..380f222702 100644 --- a/e2e/packages/contracts/src/codegen/tables/Vector.sol +++ b/e2e/packages/contracts/src/codegen/tables/Vector.sol @@ -71,6 +71,11 @@ library Vector { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -85,6 +90,15 @@ library Vector { return (int32(uint32(bytes4(_blob)))); } + /** Get x */ + function _getX(uint32 key) internal view returns (int32 x) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (int32(uint32(bytes4(_blob)))); + } + /** Get x (using the specified store) */ function getX(IStore _store, uint32 key) internal view returns (int32 x) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -102,6 +116,14 @@ library Vector { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } + /** Set x */ + function _setX(uint32 key, int32 x) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); + } + /** Set x (using the specified store) */ function setX(IStore _store, uint32 key, int32 x) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -119,6 +141,15 @@ library Vector { return (int32(uint32(bytes4(_blob)))); } + /** Get y */ + function _getY(uint32 key) internal view returns (int32 y) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (int32(uint32(bytes4(_blob)))); + } + /** Get y (using the specified store) */ function getY(IStore _store, uint32 key) internal view returns (int32 y) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -136,6 +167,14 @@ library Vector { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } + /** Set y */ + function _setY(uint32 key, int32 y) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); + } + /** Set y (using the specified store) */ function setY(IStore _store, uint32 key, int32 y) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -153,6 +192,15 @@ library Vector { return decode(_blob); } + /** Get the full data */ + function _get(uint32 key) internal view returns (VectorData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, uint32 key) internal view returns (VectorData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -175,6 +223,19 @@ library Vector { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(uint32 key, int32 x, int32 y) internal { + bytes memory _staticData = encodeStatic(x, y); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, uint32 key, int32 x, int32 y) internal { bytes memory _staticData = encodeStatic(x, y); @@ -193,6 +254,11 @@ library Vector { set(key, _table.x, _table.y); } + /** Set the full data using the data struct */ + function _set(uint32 key, VectorData memory _table) internal { + set(key, _table.x, _table.y); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, uint32 key, VectorData memory _table) internal { set(_store, key, _table.x, _table.y); @@ -236,6 +302,14 @@ library Vector { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(uint32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(key)); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, uint32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol index ec404deade..8cff969735 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/CounterTable.sol @@ -62,6 +62,11 @@ library CounterTable { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,14 @@ library CounterTable { return (uint32(bytes4(_blob))); } + /** Get value */ + function _get() internal view returns (uint32 value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get value (using the specified store) */ function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -90,6 +103,13 @@ library CounterTable { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(uint32 value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -126,6 +146,13 @@ library CounterTable { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol b/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol index 40b4913ac1..8344c02274 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/Inventory.sol @@ -68,6 +68,11 @@ library Inventory { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -84,6 +89,17 @@ library Inventory { return (uint32(bytes4(_blob))); } + /** Get amount */ + function _get(address owner, uint32 item, uint32 itemVariant) internal view returns (uint32 amount) { + bytes32[] memory _keyTuple = new bytes32[](3); + _keyTuple[0] = bytes32(uint256(uint160(owner))); + _keyTuple[1] = bytes32(uint256(item)); + _keyTuple[2] = bytes32(uint256(itemVariant)); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get amount (using the specified store) */ function get(IStore _store, address owner, uint32 item, uint32 itemVariant) internal view returns (uint32 amount) { bytes32[] memory _keyTuple = new bytes32[](3); @@ -105,6 +121,16 @@ library Inventory { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), _fieldLayout); } + /** Set amount */ + function _set(address owner, uint32 item, uint32 itemVariant, uint32 amount) internal { + bytes32[] memory _keyTuple = new bytes32[](3); + _keyTuple[0] = bytes32(uint256(uint160(owner))); + _keyTuple[1] = bytes32(uint256(item)); + _keyTuple[2] = bytes32(uint256(itemVariant)); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((amount)), _fieldLayout); + } + /** Set amount (using the specified store) */ function set(IStore _store, address owner, uint32 item, uint32 itemVariant, uint32 amount) internal { bytes32[] memory _keyTuple = new bytes32[](3); @@ -150,6 +176,16 @@ library Inventory { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(address owner, uint32 item, uint32 itemVariant) internal { + bytes32[] memory _keyTuple = new bytes32[](3); + _keyTuple[0] = bytes32(uint256(uint160(owner))); + _keyTuple[1] = bytes32(uint256(item)); + _keyTuple[2] = bytes32(uint256(itemVariant)); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, address owner, uint32 item, uint32 itemVariant) internal { bytes32[] memory _keyTuple = new bytes32[](3); diff --git a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol index 6e58cfc3df..9bcba034c6 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol @@ -62,6 +62,11 @@ library MessageTable { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,17 @@ library MessageTable { StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Emit the ephemeral event using individual values */ + function _emitEphemeral(string memory value) internal { + bytes memory _staticData; + PackedCounter _encodedLengths = encodeLengths(value); + bytes memory _dynamicData = encodeDynamic(value); + + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Emit the ephemeral event using individual values (using the specified store) */ function emitEphemeral(IStore _store, string memory value) internal { bytes memory _staticData; diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol index b4b6f609cb..1b7dee5110 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics1.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics1.sol @@ -80,6 +80,11 @@ library Dynamics1 { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -94,6 +99,15 @@ library Dynamics1 { return toStaticArray_bytes32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get staticB32 */ + function _getStaticB32(bytes32 key) internal view returns (bytes32[1] memory staticB32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return toStaticArray_bytes32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get staticB32 (using the specified store) */ function getStaticB32(IStore _store, bytes32 key) internal view returns (bytes32[1] memory staticB32) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -117,6 +131,14 @@ library Dynamics1 { ); } + /** Set staticB32 */ + function _setStaticB32(bytes32 key, bytes32[1] memory staticB32) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode(fromStaticArray_bytes32_1(staticB32)), _fieldLayout); + } + /** Set staticB32 (using the specified store) */ function setStaticB32(IStore _store, bytes32 key, bytes32[1] memory staticB32) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -136,6 +158,17 @@ library Dynamics1 { } } + /** Get the length of staticB32 */ + function _lengthStaticB32(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of staticB32 (using the specified store) */ function lengthStaticB32(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -168,6 +201,27 @@ library Dynamics1 { } } + /** + * Get an item of staticB32 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemStaticB32(bytes32 key, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of staticB32 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -190,6 +244,14 @@ library Dynamics1 { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to staticB32 */ + function _pushStaticB32(bytes32 key, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to staticB32 (using the specified store) */ function pushStaticB32(IStore _store, bytes32 key, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -206,6 +268,14 @@ library Dynamics1 { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } + /** Pop an element from staticB32 */ + function _popStaticB32(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); + } + /** Pop an element from staticB32 (using the specified store) */ function popStaticB32(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -227,6 +297,19 @@ library Dynamics1 { } } + /** + * Update an element of staticB32 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateStaticB32(bytes32 key, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of staticB32 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -249,6 +332,15 @@ library Dynamics1 { return toStaticArray_int32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_int32()); } + /** Get staticI32 */ + function _getStaticI32(bytes32 key) internal view returns (int32[2] memory staticI32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); + return toStaticArray_int32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_int32()); + } + /** Get staticI32 (using the specified store) */ function getStaticI32(IStore _store, bytes32 key) internal view returns (int32[2] memory staticI32) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -266,6 +358,14 @@ library Dynamics1 { StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), _fieldLayout); } + /** Set staticI32 */ + function _setStaticI32(bytes32 key, int32[2] memory staticI32) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_int32_2(staticI32)), _fieldLayout); + } + /** Set staticI32 (using the specified store) */ function setStaticI32(IStore _store, bytes32 key, int32[2] memory staticI32) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -285,6 +385,17 @@ library Dynamics1 { } } + /** Get the length of staticI32 */ + function _lengthStaticI32(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); + unchecked { + return _byteLength / 4; + } + } + /** Get the length of staticI32 (using the specified store) */ function lengthStaticI32(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -317,6 +428,20 @@ library Dynamics1 { } } + /** + * Get an item of staticI32 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemStaticI32(bytes32 key, uint256 _index) internal view returns (int32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 4, (_index + 1) * 4); + return (int32(uint32(bytes4(_blob)))); + } + } + /** * Get an item of staticI32 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -339,6 +464,14 @@ library Dynamics1 { StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to staticI32 */ + function _pushStaticI32(bytes32 key, int32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to staticI32 (using the specified store) */ function pushStaticI32(IStore _store, bytes32 key, int32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -355,6 +488,14 @@ library Dynamics1 { StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); } + /** Pop an element from staticI32 */ + function _popStaticI32(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); + } + /** Pop an element from staticI32 (using the specified store) */ function popStaticI32(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -376,6 +517,19 @@ library Dynamics1 { } } + /** + * Update an element of staticI32 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateStaticI32(bytes32 key, uint256 _index, int32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of staticI32 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -398,6 +552,15 @@ library Dynamics1 { return toStaticArray_uint128_3(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint128()); } + /** Get staticU128 */ + function _getStaticU128(bytes32 key) internal view returns (uint128[3] memory staticU128) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 2); + return toStaticArray_uint128_3(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint128()); + } + /** Get staticU128 (using the specified store) */ function getStaticU128(IStore _store, bytes32 key) internal view returns (uint128[3] memory staticU128) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -421,6 +584,14 @@ library Dynamics1 { ); } + /** Set staticU128 */ + function _setStaticU128(bytes32 key, uint128[3] memory staticU128) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint128_3(staticU128)), _fieldLayout); + } + /** Set staticU128 (using the specified store) */ function setStaticU128(IStore _store, bytes32 key, uint128[3] memory staticU128) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -440,6 +611,17 @@ library Dynamics1 { } } + /** Get the length of staticU128 */ + function _lengthStaticU128(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); + unchecked { + return _byteLength / 16; + } + } + /** Get the length of staticU128 (using the specified store) */ function lengthStaticU128(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -472,6 +654,27 @@ library Dynamics1 { } } + /** + * Get an item of staticU128 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemStaticU128(bytes32 key, uint256 _index) internal view returns (uint128) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 2, + _fieldLayout, + _index * 16, + (_index + 1) * 16 + ); + return (uint128(bytes16(_blob))); + } + } + /** * Get an item of staticU128 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -494,6 +697,14 @@ library Dynamics1 { StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to staticU128 */ + function _pushStaticU128(bytes32 key, uint128 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to staticU128 (using the specified store) */ function pushStaticU128(IStore _store, bytes32 key, uint128 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -510,6 +721,14 @@ library Dynamics1 { StoreSwitch.popFromField(_tableId, _keyTuple, 2, 16, _fieldLayout); } + /** Pop an element from staticU128 */ + function _popStaticU128(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 2, 16, _fieldLayout); + } + /** Pop an element from staticU128 (using the specified store) */ function popStaticU128(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -531,6 +750,19 @@ library Dynamics1 { } } + /** + * Update an element of staticU128 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateStaticU128(bytes32 key, uint256 _index, uint128 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 2, _index * 16, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of staticU128 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -553,6 +785,15 @@ library Dynamics1 { return toStaticArray_address_4(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } + /** Get staticAddrs */ + function _getStaticAddrs(bytes32 key) internal view returns (address[4] memory staticAddrs) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 3); + return toStaticArray_address_4(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); + } + /** Get staticAddrs (using the specified store) */ function getStaticAddrs(IStore _store, bytes32 key) internal view returns (address[4] memory staticAddrs) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -576,6 +817,20 @@ library Dynamics1 { ); } + /** Set staticAddrs */ + function _setStaticAddrs(bytes32 key, address[4] memory staticAddrs) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField( + _tableId, + _keyTuple, + 3, + EncodeArray.encode(fromStaticArray_address_4(staticAddrs)), + _fieldLayout + ); + } + /** Set staticAddrs (using the specified store) */ function setStaticAddrs(IStore _store, bytes32 key, address[4] memory staticAddrs) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -595,6 +850,17 @@ library Dynamics1 { } } + /** Get the length of staticAddrs */ + function _lengthStaticAddrs(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); + unchecked { + return _byteLength / 20; + } + } + /** Get the length of staticAddrs (using the specified store) */ function lengthStaticAddrs(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -627,6 +893,27 @@ library Dynamics1 { } } + /** + * Get an item of staticAddrs + * (unchecked, returns invalid data if index overflows) + */ + function _getItemStaticAddrs(bytes32 key, uint256 _index) internal view returns (address) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 3, + _fieldLayout, + _index * 20, + (_index + 1) * 20 + ); + return (address(bytes20(_blob))); + } + } + /** * Get an item of staticAddrs (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -649,6 +936,14 @@ library Dynamics1 { StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to staticAddrs */ + function _pushStaticAddrs(bytes32 key, address _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to staticAddrs (using the specified store) */ function pushStaticAddrs(IStore _store, bytes32 key, address _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -665,6 +960,14 @@ library Dynamics1 { StoreSwitch.popFromField(_tableId, _keyTuple, 3, 20, _fieldLayout); } + /** Pop an element from staticAddrs */ + function _popStaticAddrs(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 3, 20, _fieldLayout); + } + /** Pop an element from staticAddrs (using the specified store) */ function popStaticAddrs(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -686,6 +989,19 @@ library Dynamics1 { } } + /** + * Update an element of staticAddrs at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateStaticAddrs(bytes32 key, uint256 _index, address _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 3, _index * 20, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of staticAddrs (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -708,6 +1024,15 @@ library Dynamics1 { return toStaticArray_bool_5(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bool()); } + /** Get staticBools */ + function _getStaticBools(bytes32 key) internal view returns (bool[5] memory staticBools) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 4); + return toStaticArray_bool_5(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bool()); + } + /** Get staticBools (using the specified store) */ function getStaticBools(IStore _store, bytes32 key) internal view returns (bool[5] memory staticBools) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -725,6 +1050,14 @@ library Dynamics1 { StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), _fieldLayout); } + /** Set staticBools */ + function _setStaticBools(bytes32 key, bool[5] memory staticBools) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 4, EncodeArray.encode(fromStaticArray_bool_5(staticBools)), _fieldLayout); + } + /** Set staticBools (using the specified store) */ function setStaticBools(IStore _store, bytes32 key, bool[5] memory staticBools) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -744,6 +1077,17 @@ library Dynamics1 { } } + /** Get the length of staticBools */ + function _lengthStaticBools(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); + unchecked { + return _byteLength / 1; + } + } + /** Get the length of staticBools (using the specified store) */ function lengthStaticBools(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -776,6 +1120,20 @@ library Dynamics1 { } } + /** + * Get an item of staticBools + * (unchecked, returns invalid data if index overflows) + */ + function _getItemStaticBools(bytes32 key, uint256 _index) internal view returns (bool) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 4, _fieldLayout, _index * 1, (_index + 1) * 1); + return (_toBool(uint8(bytes1(_blob)))); + } + } + /** * Get an item of staticBools (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -798,6 +1156,14 @@ library Dynamics1 { StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to staticBools */ + function _pushStaticBools(bytes32 key, bool _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to staticBools (using the specified store) */ function pushStaticBools(IStore _store, bytes32 key, bool _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -814,6 +1180,14 @@ library Dynamics1 { StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); } + /** Pop an element from staticBools */ + function _popStaticBools(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); + } + /** Pop an element from staticBools (using the specified store) */ function popStaticBools(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -835,6 +1209,19 @@ library Dynamics1 { } } + /** + * Update an element of staticBools at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateStaticBools(bytes32 key, uint256 _index, bool _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 4, _index * 1, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of staticBools (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -857,6 +1244,15 @@ library Dynamics1 { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 key) internal view returns (Dynamics1Data memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (Dynamics1Data memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -885,6 +1281,25 @@ library Dynamics1 { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set( + bytes32 key, + bytes32[1] memory staticB32, + int32[2] memory staticI32, + uint128[3] memory staticU128, + address[4] memory staticAddrs, + bool[5] memory staticBools + ) internal { + bytes memory _staticData; + PackedCounter _encodedLengths = encodeLengths(staticB32, staticI32, staticU128, staticAddrs, staticBools); + bytes memory _dynamicData = encodeDynamic(staticB32, staticI32, staticU128, staticAddrs, staticBools); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set( IStore _store, @@ -910,6 +1325,11 @@ library Dynamics1 { set(key, _table.staticB32, _table.staticI32, _table.staticU128, _table.staticAddrs, _table.staticBools); } + /** Set the full data using the data struct */ + function _set(bytes32 key, Dynamics1Data memory _table) internal { + set(key, _table.staticB32, _table.staticI32, _table.staticU128, _table.staticAddrs, _table.staticBools); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 key, Dynamics1Data memory _table) internal { set(_store, key, _table.staticB32, _table.staticI32, _table.staticU128, _table.staticAddrs, _table.staticBools); @@ -1028,6 +1448,14 @@ library Dynamics1 { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/cli/contracts/src/codegen/tables/Dynamics2.sol b/packages/cli/contracts/src/codegen/tables/Dynamics2.sol index 59d39a2106..77371f92a7 100644 --- a/packages/cli/contracts/src/codegen/tables/Dynamics2.sol +++ b/packages/cli/contracts/src/codegen/tables/Dynamics2.sol @@ -74,6 +74,11 @@ library Dynamics2 { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -88,6 +93,15 @@ library Dynamics2 { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint64()); } + /** Get u64 */ + function _getU64(bytes32 key) internal view returns (uint64[] memory u64) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint64()); + } + /** Get u64 (using the specified store) */ function getU64(IStore _store, bytes32 key) internal view returns (uint64[] memory u64) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -105,6 +119,14 @@ library Dynamics2 { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), _fieldLayout); } + /** Set u64 */ + function _setU64(bytes32 key, uint64[] memory u64) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((u64)), _fieldLayout); + } + /** Set u64 (using the specified store) */ function setU64(IStore _store, bytes32 key, uint64[] memory u64) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -124,6 +146,17 @@ library Dynamics2 { } } + /** Get the length of u64 */ + function _lengthU64(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 8; + } + } + /** Get the length of u64 (using the specified store) */ function lengthU64(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -156,6 +189,20 @@ library Dynamics2 { } } + /** + * Get an item of u64 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemU64(bytes32 key, uint256 _index) internal view returns (uint64) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 0, _fieldLayout, _index * 8, (_index + 1) * 8); + return (uint64(bytes8(_blob))); + } + } + /** * Get an item of u64 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -178,6 +225,14 @@ library Dynamics2 { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to u64 */ + function _pushU64(bytes32 key, uint64 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to u64 (using the specified store) */ function pushU64(IStore _store, bytes32 key, uint64 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -194,6 +249,14 @@ library Dynamics2 { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 8, _fieldLayout); } + /** Pop an element from u64 */ + function _popU64(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 8, _fieldLayout); + } + /** Pop an element from u64 (using the specified store) */ function popU64(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -215,6 +278,19 @@ library Dynamics2 { } } + /** + * Update an element of u64 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateU64(bytes32 key, uint256 _index, uint64 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 8, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of u64 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -237,6 +313,15 @@ library Dynamics2 { return (string(_blob)); } + /** Get str */ + function _getStr(bytes32 key) internal view returns (string memory str) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); + return (string(_blob)); + } + /** Get str (using the specified store) */ function getStr(IStore _store, bytes32 key) internal view returns (string memory str) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -254,6 +339,14 @@ library Dynamics2 { StoreSwitch.setField(_tableId, _keyTuple, 1, bytes((str)), _fieldLayout); } + /** Set str */ + function _setStr(bytes32 key, string memory str) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 1, bytes((str)), _fieldLayout); + } + /** Set str (using the specified store) */ function setStr(IStore _store, bytes32 key, string memory str) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -273,6 +366,17 @@ library Dynamics2 { } } + /** Get the length of str */ + function _lengthStr(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); + unchecked { + return _byteLength / 1; + } + } + /** Get the length of str (using the specified store) */ function lengthStr(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -305,6 +409,20 @@ library Dynamics2 { } } + /** + * Get an item of str + * (unchecked, returns invalid data if index overflows) + */ + function _getItemStr(bytes32 key, uint256 _index) internal view returns (string memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 1, (_index + 1) * 1); + return (string(_blob)); + } + } + /** * Get an item of str (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -327,6 +445,14 @@ library Dynamics2 { StoreSwitch.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), _fieldLayout); } + /** Push a slice to str */ + function _pushStr(bytes32 key, string memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 1, bytes((_slice)), _fieldLayout); + } + /** Push a slice to str (using the specified store) */ function pushStr(IStore _store, bytes32 key, string memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -343,6 +469,14 @@ library Dynamics2 { StoreSwitch.popFromField(_tableId, _keyTuple, 1, 1, _fieldLayout); } + /** Pop a slice from str */ + function _popStr(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 1, 1, _fieldLayout); + } + /** Pop a slice from str (using the specified store) */ function popStr(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -364,6 +498,19 @@ library Dynamics2 { } } + /** + * Update a slice of str at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateStr(bytes32 key, uint256 _index, string memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 1, _index * 1, bytes((_slice)), _fieldLayout); + } + } + /** * Update a slice of str (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -386,6 +533,15 @@ library Dynamics2 { return (bytes(_blob)); } + /** Get b */ + function _getB(bytes32 key) internal view returns (bytes memory b) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 2); + return (bytes(_blob)); + } + /** Get b (using the specified store) */ function getB(IStore _store, bytes32 key) internal view returns (bytes memory b) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -403,6 +559,14 @@ library Dynamics2 { StoreSwitch.setField(_tableId, _keyTuple, 2, bytes((b)), _fieldLayout); } + /** Set b */ + function _setB(bytes32 key, bytes memory b) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 2, bytes((b)), _fieldLayout); + } + /** Set b (using the specified store) */ function setB(IStore _store, bytes32 key, bytes memory b) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -422,6 +586,17 @@ library Dynamics2 { } } + /** Get the length of b */ + function _lengthB(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); + unchecked { + return _byteLength / 1; + } + } + /** Get the length of b (using the specified store) */ function lengthB(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -454,6 +629,20 @@ library Dynamics2 { } } + /** + * Get an item of b + * (unchecked, returns invalid data if index overflows) + */ + function _getItemB(bytes32 key, uint256 _index) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 1, (_index + 1) * 1); + return (bytes(_blob)); + } + } + /** * Get an item of b (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -476,6 +665,14 @@ library Dynamics2 { StoreSwitch.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), _fieldLayout); } + /** Push a slice to b */ + function _pushB(bytes32 key, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 2, bytes((_slice)), _fieldLayout); + } + /** Push a slice to b (using the specified store) */ function pushB(IStore _store, bytes32 key, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -492,6 +689,14 @@ library Dynamics2 { StoreSwitch.popFromField(_tableId, _keyTuple, 2, 1, _fieldLayout); } + /** Pop a slice from b */ + function _popB(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 2, 1, _fieldLayout); + } + /** Pop a slice from b (using the specified store) */ function popB(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -513,6 +718,19 @@ library Dynamics2 { } } + /** + * Update a slice of b at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateB(bytes32 key, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 2, _index * 1, bytes((_slice)), _fieldLayout); + } + } + /** * Update a slice of b (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -535,6 +753,15 @@ library Dynamics2 { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 key) internal view returns (Dynamics2Data memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (Dynamics2Data memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -556,6 +783,18 @@ library Dynamics2 { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes32 key, uint64[] memory u64, string memory str, bytes memory b) internal { + bytes memory _staticData; + PackedCounter _encodedLengths = encodeLengths(u64, str, b); + bytes memory _dynamicData = encodeDynamic(u64, str, b); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes32 key, uint64[] memory u64, string memory str, bytes memory b) internal { bytes memory _staticData; @@ -573,6 +812,11 @@ library Dynamics2 { set(key, _table.u64, _table.str, _table.b); } + /** Set the full data using the data struct */ + function _set(bytes32 key, Dynamics2Data memory _table) internal { + set(key, _table.u64, _table.str, _table.b); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 key, Dynamics2Data memory _table) internal { set(_store, key, _table.u64, _table.str, _table.b); @@ -652,6 +896,14 @@ library Dynamics2 { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/cli/contracts/src/codegen/tables/Ephemeral.sol b/packages/cli/contracts/src/codegen/tables/Ephemeral.sol index 31b8000e6b..04e78e2503 100644 --- a/packages/cli/contracts/src/codegen/tables/Ephemeral.sol +++ b/packages/cli/contracts/src/codegen/tables/Ephemeral.sol @@ -64,6 +64,11 @@ library Ephemeral { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -82,6 +87,19 @@ library Ephemeral { StoreSwitch.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Emit the ephemeral event using individual values */ + function _emitEphemeral(bytes32 key, uint256 value) internal { + bytes memory _staticData = encodeStatic(value); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.emitEphemeralRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Emit the ephemeral event using individual values (using the specified store) */ function emitEphemeral(IStore _store, bytes32 key, uint256 value) internal { bytes memory _staticData = encodeStatic(value); diff --git a/packages/cli/contracts/src/codegen/tables/Singleton.sol b/packages/cli/contracts/src/codegen/tables/Singleton.sol index 90e1319b41..3db7f72067 100644 --- a/packages/cli/contracts/src/codegen/tables/Singleton.sol +++ b/packages/cli/contracts/src/codegen/tables/Singleton.sol @@ -68,6 +68,11 @@ library Singleton { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -81,6 +86,14 @@ library Singleton { return (int256(uint256(bytes32(_blob)))); } + /** Get v1 */ + function _getV1() internal view returns (int256 v1) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (int256(uint256(bytes32(_blob)))); + } + /** Get v1 (using the specified store) */ function getV1(IStore _store) internal view returns (int256 v1) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -96,6 +109,13 @@ library Singleton { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); } + /** Set v1 */ + function _setV1(int256 v1) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); + } + /** Set v1 (using the specified store) */ function setV1(IStore _store, int256 v1) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -111,6 +131,14 @@ library Singleton { return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } + /** Get v2 */ + function _getV2() internal view returns (uint32[2] memory v2) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); + } + /** Get v2 (using the specified store) */ function getV2(IStore _store) internal view returns (uint32[2] memory v2) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -126,6 +154,13 @@ library Singleton { StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), _fieldLayout); } + /** Set v2 */ + function _setV2(uint32[2] memory v2) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)), _fieldLayout); + } + /** Set v2 (using the specified store) */ function setV2(IStore _store, uint32[2] memory v2) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -143,6 +178,16 @@ library Singleton { } } + /** Get the length of v2 */ + function _lengthV2() internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](0); + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); + unchecked { + return _byteLength / 4; + } + } + /** Get the length of v2 (using the specified store) */ function lengthV2(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -173,6 +218,19 @@ library Singleton { } } + /** + * Get an item of v2 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemV2(uint256 _index) internal view returns (uint32) { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 1, _fieldLayout, _index * 4, (_index + 1) * 4); + return (uint32(bytes4(_blob))); + } + } + /** * Get an item of v2 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -193,6 +251,13 @@ library Singleton { StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to v2 */ + function _pushV2(uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to v2 (using the specified store) */ function pushV2(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -207,6 +272,13 @@ library Singleton { StoreSwitch.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); } + /** Pop an element from v2 */ + function _popV2() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.popFromField(_tableId, _keyTuple, 1, 4, _fieldLayout); + } + /** Pop an element from v2 (using the specified store) */ function popV2(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -226,6 +298,18 @@ library Singleton { } } + /** + * Update an element of v2 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateV2(uint256 _index, uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 1, _index * 4, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of v2 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -246,6 +330,14 @@ library Singleton { return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } + /** Get v3 */ + function _getV3() internal view returns (uint32[2] memory v3) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); + return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); + } + /** Get v3 (using the specified store) */ function getV3(IStore _store) internal view returns (uint32[2] memory v3) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -261,6 +353,13 @@ library Singleton { StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), _fieldLayout); } + /** Set v3 */ + function _setV3(uint32[2] memory v3) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)), _fieldLayout); + } + /** Set v3 (using the specified store) */ function setV3(IStore _store, uint32[2] memory v3) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -278,6 +377,16 @@ library Singleton { } } + /** Get the length of v3 */ + function _lengthV3() internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](0); + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); + unchecked { + return _byteLength / 4; + } + } + /** Get the length of v3 (using the specified store) */ function lengthV3(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -308,6 +417,19 @@ library Singleton { } } + /** + * Get an item of v3 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemV3(uint256 _index) internal view returns (uint32) { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 4, (_index + 1) * 4); + return (uint32(bytes4(_blob))); + } + } + /** * Get an item of v3 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -328,6 +450,13 @@ library Singleton { StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to v3 */ + function _pushV3(uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to v3 (using the specified store) */ function pushV3(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -342,6 +471,13 @@ library Singleton { StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); } + /** Pop an element from v3 */ + function _popV3() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); + } + /** Pop an element from v3 (using the specified store) */ function popV3(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -361,6 +497,18 @@ library Singleton { } } + /** + * Update an element of v3 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateV3(uint256 _index, uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of v3 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -381,6 +529,14 @@ library Singleton { return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } + /** Get v4 */ + function _getV4() internal view returns (uint32[1] memory v4) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 2); + return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); + } + /** Get v4 (using the specified store) */ function getV4(IStore _store) internal view returns (uint32[1] memory v4) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -396,6 +552,13 @@ library Singleton { StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), _fieldLayout); } + /** Set v4 */ + function _setV4(uint32[1] memory v4) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)), _fieldLayout); + } + /** Set v4 (using the specified store) */ function setV4(IStore _store, uint32[1] memory v4) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -413,6 +576,16 @@ library Singleton { } } + /** Get the length of v4 */ + function _lengthV4() internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](0); + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); + unchecked { + return _byteLength / 4; + } + } + /** Get the length of v4 (using the specified store) */ function lengthV4(IStore _store) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -443,6 +616,19 @@ library Singleton { } } + /** + * Get an item of v4 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemV4(uint256 _index) internal view returns (uint32) { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 4, (_index + 1) * 4); + return (uint32(bytes4(_blob))); + } + } + /** * Get an item of v4 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -463,6 +649,13 @@ library Singleton { StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to v4 */ + function _pushV4(uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to v4 (using the specified store) */ function pushV4(IStore _store, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -477,6 +670,13 @@ library Singleton { StoreSwitch.popFromField(_tableId, _keyTuple, 3, 4, _fieldLayout); } + /** Pop an element from v4 */ + function _popV4() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.popFromField(_tableId, _keyTuple, 3, 4, _fieldLayout); + } + /** Pop an element from v4 (using the specified store) */ function popV4(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -496,6 +696,18 @@ library Singleton { } } + /** + * Update an element of v4 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateV4(uint256 _index, uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 3, _index * 4, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of v4 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -516,6 +728,14 @@ library Singleton { return decode(_blob); } + /** Get the full data */ + function _get() internal view returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get( IStore _store @@ -538,6 +758,18 @@ library Singleton { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) internal { + bytes memory _staticData = encodeStatic(v1); + + PackedCounter _encodedLengths = encodeLengths(v2, v3, v4); + bytes memory _dynamicData = encodeDynamic(v2, v3, v4); + + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) internal { bytes memory _staticData = encodeStatic(v1); @@ -646,6 +878,13 @@ library Singleton { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/packages/cli/contracts/src/codegen/tables/Statics.sol b/packages/cli/contracts/src/codegen/tables/Statics.sol index 0f0376d429..0c74e1b419 100644 --- a/packages/cli/contracts/src/codegen/tables/Statics.sol +++ b/packages/cli/contracts/src/codegen/tables/Statics.sol @@ -96,6 +96,11 @@ library Statics { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -115,6 +120,20 @@ library Statics { return (uint256(bytes32(_blob))); } + /** Get v1 */ + function _getV1(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal view returns (uint256 v1) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + /** Get v1 (using the specified store) */ function getV1( IStore _store, @@ -150,6 +169,19 @@ library Statics { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); } + /** Set v1 */ + function _setV1(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, uint256 v1) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((v1)), _fieldLayout); + } + /** Set v1 (using the specified store) */ function setV1(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, uint256 v1) internal { bytes32[] memory _keyTuple = new bytes32[](6); @@ -177,6 +209,20 @@ library Statics { return (int32(uint32(bytes4(_blob)))); } + /** Get v2 */ + function _getV2(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal view returns (int32 v2) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (int32(uint32(bytes4(_blob)))); + } + /** Get v2 (using the specified store) */ function getV2( IStore _store, @@ -212,6 +258,19 @@ library Statics { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), _fieldLayout); } + /** Set v2 */ + function _setV2(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, int32 v2) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((v2)), _fieldLayout); + } + /** Set v2 (using the specified store) */ function setV2(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, int32 v2) internal { bytes32[] memory _keyTuple = new bytes32[](6); @@ -239,6 +298,20 @@ library Statics { return (bytes16(_blob)); } + /** Get v3 */ + function _getV3(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal view returns (bytes16 v3) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); + return (bytes16(_blob)); + } + /** Get v3 (using the specified store) */ function getV3( IStore _store, @@ -274,6 +347,19 @@ library Statics { StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), _fieldLayout); } + /** Set v3 */ + function _setV3(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, bytes16 v3) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 2, abi.encodePacked((v3)), _fieldLayout); + } + /** Set v3 (using the specified store) */ function setV3(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, bytes16 v3) internal { bytes32[] memory _keyTuple = new bytes32[](6); @@ -301,6 +387,20 @@ library Statics { return (address(bytes20(_blob))); } + /** Get v4 */ + function _getV4(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal view returns (address v4) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 3, _fieldLayout); + return (address(bytes20(_blob))); + } + /** Get v4 (using the specified store) */ function getV4( IStore _store, @@ -336,6 +436,19 @@ library Statics { StoreSwitch.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), _fieldLayout); } + /** Set v4 */ + function _setV4(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, address v4) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 3, abi.encodePacked((v4)), _fieldLayout); + } + /** Set v4 (using the specified store) */ function setV4(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, address v4) internal { bytes32[] memory _keyTuple = new bytes32[](6); @@ -363,6 +476,20 @@ library Statics { return (_toBool(uint8(bytes1(_blob)))); } + /** Get v5 */ + function _getV5(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal view returns (bool v5) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 4, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get v5 (using the specified store) */ function getV5( IStore _store, @@ -398,6 +525,19 @@ library Statics { StoreSwitch.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), _fieldLayout); } + /** Set v5 */ + function _setV5(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, bool v5) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 4, abi.encodePacked((v5)), _fieldLayout); + } + /** Set v5 (using the specified store) */ function setV5(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, bool v5) internal { bytes32[] memory _keyTuple = new bytes32[](6); @@ -425,6 +565,20 @@ library Statics { return Enum1(uint8(bytes1(_blob))); } + /** Get v6 */ + function _getV6(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal view returns (Enum1 v6) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 5, _fieldLayout); + return Enum1(uint8(bytes1(_blob))); + } + /** Get v6 (using the specified store) */ function getV6( IStore _store, @@ -460,6 +614,19 @@ library Statics { StoreSwitch.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), _fieldLayout); } + /** Set v6 */ + function _setV6(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, Enum1 v6) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 5, abi.encodePacked(uint8(v6)), _fieldLayout); + } + /** Set v6 (using the specified store) */ function setV6(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, Enum1 v6) internal { bytes32[] memory _keyTuple = new bytes32[](6); @@ -494,6 +661,27 @@ library Statics { return decode(_blob); } + /** Get the full data */ + function _get( + uint256 k1, + int32 k2, + bytes16 k3, + address k4, + bool k5, + Enum2 k6 + ) internal view returns (StaticsData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get( IStore _store, @@ -547,6 +735,37 @@ library Statics { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set( + uint256 k1, + int32 k2, + bytes16 k3, + address k4, + bool k5, + Enum2 k6, + uint256 v1, + int32 v2, + bytes16 v3, + address v4, + bool v5, + Enum1 v6 + ) internal { + bytes memory _staticData = encodeStatic(v1, v2, v3, v4, v5, v6); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set( IStore _store, @@ -584,6 +803,11 @@ library Statics { set(k1, k2, k3, k4, k5, k6, _table.v1, _table.v2, _table.v3, _table.v4, _table.v5, _table.v6); } + /** Set the full data using the data struct */ + function _set(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6, StaticsData memory _table) internal { + set(k1, k2, k3, k4, k5, k6, _table.v1, _table.v2, _table.v3, _table.v4, _table.v5, _table.v6); + } + /** Set the full data using the data struct (using the specified store) */ function set( IStore _store, @@ -675,6 +899,19 @@ library Statics { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, Enum2 k6) internal { bytes32[] memory _keyTuple = new bytes32[](6); diff --git a/packages/common/src/codegen/render-solidity/common.ts b/packages/common/src/codegen/render-solidity/common.ts index a853ff8513..4f20e8d965 100644 --- a/packages/common/src/codegen/render-solidity/common.ts +++ b/packages/common/src/codegen/render-solidity/common.ts @@ -131,14 +131,16 @@ export function renderWithStore( _typedStore: string | undefined, _store: string, _commentSuffix: string, - _untypedStore: string | undefined + _untypedStore: string | undefined, + _methodPrefix: string ) => string ): string { let result = ""; - result += callback(undefined, "StoreSwitch", "", undefined); + result += callback(undefined, "StoreSwitch", "", undefined, ""); + result += callback(undefined, "StoreCore", "", undefined, "_"); if (storeArgument) { - result += "\n" + callback("IStore _store", "_store", " (using the specified store)", "_store"); + result += "\n" + callback("IStore _store", "_store", " (using the specified store)", "_store", ""); } return result; diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index c1ac85ebb5..9f301b4105 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -351,7 +351,7 @@ "file": "test/KeyEncoding.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "register KeyEncoding table", - "gasUsed": 688555 + "gasUsed": 687695 }, { "file": "test/Mixed.t.sol", @@ -363,13 +363,13 @@ "file": "test/Mixed.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "register Mixed table", - "gasUsed": 550389 + "gasUsed": 549513 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "set record in Mixed", - "gasUsed": 104102 + "gasUsed": 103812 }, { "file": "test/Mixed.t.sol", @@ -609,25 +609,25 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (cold, 1 slot, 1 uint32 item)", - "gasUsed": 20433 + "gasUsed": 20143 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (warm, 1 slot, 1 uint32 item)", - "gasUsed": 14445 + "gasUsed": 14155 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (cold, 2 slots, 10 uint32 items)", - "gasUsed": 22629 + "gasUsed": 22339 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (warm, 2 slots, 10 uint32 items)", - "gasUsed": 14641 + "gasUsed": 14351 }, { "file": "test/StoreCoreGas.t.sol", @@ -663,109 +663,109 @@ "file": "test/StoreCoreGas.t.sol", "test": "testDeleteData", "name": "delete record (complex data, 3 slots)", - "gasUsed": 6882 + "gasUsed": 6592 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHasFieldLayout", "name": "Check for existence of table (existent)", - "gasUsed": 1555 + "gasUsed": 1260 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHasFieldLayout", "name": "check for existence of table (non-existent)", - "gasUsed": 3556 + "gasUsed": 3261 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "register subscriber", - "gasUsed": 58604 + "gasUsed": 60314 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set record on table with subscriber", - "gasUsed": 71502 + "gasUsed": 70846 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set static field on table with subscriber", - "gasUsed": 21245 + "gasUsed": 20625 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "delete record on table with subscriber", - "gasUsed": 16797 + "gasUsed": 16217 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "register subscriber", - "gasUsed": 58604 + "gasUsed": 60314 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) record on table with subscriber", - "gasUsed": 164598 + "gasUsed": 163968 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) field on table with subscriber", - "gasUsed": 24445 + "gasUsed": 23825 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "delete (dynamic) record on table with subscriber", - "gasUsed": 17782 + "gasUsed": 17202 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (1 slot, 1 uint32 item)", - "gasUsed": 12189 + "gasUsed": 11899 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (2 slots, 10 uint32 items)", - "gasUsed": 34943 + "gasUsed": 34653 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: register table", - "gasUsed": 612540 + "gasUsed": 609664 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: get field layout (warm)", - "gasUsed": 1567 + "gasUsed": 1272 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: get value schema (warm)", - "gasUsed": 2077 + "gasUsed": 1782 }, { "file": "test/StoreCoreGas.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "StoreCore: get key schema (warm)", - "gasUsed": 3406 + "gasUsed": 2816 }, { "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "set complex record with dynamic data (4 slots)", - "gasUsed": 102022 + "gasUsed": 101732 }, { "file": "test/StoreCoreGas.t.sol", @@ -807,7 +807,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (1 slot)", - "gasUsed": 31744 + "gasUsed": 31454 }, { "file": "test/StoreCoreGas.t.sol", @@ -819,7 +819,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (overlap 2 slot)", - "gasUsed": 30384 + "gasUsed": 30094 }, { "file": "test/StoreCoreGas.t.sol", @@ -831,7 +831,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, first dynamic field)", - "gasUsed": 53269 + "gasUsed": 52979 }, { "file": "test/StoreCoreGas.t.sol", @@ -843,7 +843,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, second dynamic field)", - "gasUsed": 31496 + "gasUsed": 31206 }, { "file": "test/StoreCoreGas.t.sol", @@ -855,7 +855,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticData", "name": "set static record (1 slot)", - "gasUsed": 32328 + "gasUsed": 32038 }, { "file": "test/StoreCoreGas.t.sol", @@ -867,7 +867,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticDataSpanningWords", "name": "set static record (2 slots)", - "gasUsed": 54832 + "gasUsed": 54542 }, { "file": "test/StoreCoreGas.t.sol", @@ -879,13 +879,13 @@ "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "update in field (1 slot, 1 uint32 item)", - "gasUsed": 13227 + "gasUsed": 12937 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "push to field (2 slots, 6 uint64 items)", - "gasUsed": 14029 + "gasUsed": 13739 }, { "file": "test/StoreHook.t.sol", @@ -933,31 +933,31 @@ "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: set field", - "gasUsed": 56344 + "gasUsed": 56047 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: get field (warm)", - "gasUsed": 2912 + "gasUsed": 2894 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "Callbacks: push 1 element", - "gasUsed": 35254 + "gasUsed": 34957 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testOneSlot", "name": "StoreHooks: set field with one elements (cold)", - "gasUsed": 58342 + "gasUsed": 58052 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: set field (cold)", - "gasUsed": 58342 + "gasUsed": 58052 }, { "file": "test/tables/StoreHooks.t.sol", @@ -969,55 +969,55 @@ "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: push 1 element (cold)", - "gasUsed": 15341 + "gasUsed": 15051 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: pop 1 element (warm)", - "gasUsed": 11896 + "gasUsed": 11606 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: push 1 element (warm)", - "gasUsed": 13367 + "gasUsed": 13077 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: update 1 element (warm)", - "gasUsed": 34465 + "gasUsed": 34175 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: delete record (warm)", - "gasUsed": 7293 + "gasUsed": 7003 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTable", "name": "StoreHooks: set field (warm)", - "gasUsed": 30504 + "gasUsed": 30214 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testThreeSlots", "name": "StoreHooks: set field with three elements (cold)", - "gasUsed": 81033 + "gasUsed": 80743 }, { "file": "test/tables/StoreHooks.t.sol", "test": "testTwoSlots", "name": "StoreHooks: set field with two elements (cold)", - "gasUsed": 80944 + "gasUsed": 80654 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testDelete", "name": "StoreHooks: delete record (cold)", - "gasUsed": 16158 + "gasUsed": 15868 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", @@ -1041,13 +1041,13 @@ "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testPop", "name": "StoreHooks: pop 1 element (cold)", - "gasUsed": 22382 + "gasUsed": 22092 }, { "file": "test/tables/StoreHooksColdLoad.t.sol", "test": "testUpdate", "name": "StoreHooks: update 1 element (cold)", - "gasUsed": 24518 + "gasUsed": 24228 }, { "file": "test/tightcoder/DecodeSlice.t.sol", @@ -1101,13 +1101,13 @@ "file": "test/Vector2.t.sol", "test": "testRegisterAndGetFieldLayout", "name": "register Vector2 field layout", - "gasUsed": 411812 + "gasUsed": 410936 }, { "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "set Vector2 record", - "gasUsed": 33231 + "gasUsed": 32941 }, { "file": "test/Vector2.t.sol", diff --git a/packages/store/src/Hook.sol b/packages/store/src/Hook.sol index 876f7e7e52..2d7da97a6f 100644 --- a/packages/store/src/Hook.sol +++ b/packages/store/src/Hook.sol @@ -21,7 +21,7 @@ library HookLib { * Filter the given hook from the hook list at the given key in the given hook table */ function filterListByAddress(bytes32 hookTableId, bytes32 key, address hookAddressToRemove) internal { - bytes21[] memory currentHooks = Hooks.get(hookTableId, key); + bytes21[] memory currentHooks = Hooks._get(hookTableId, key); // Initialize the new hooks array with the same length because we don't know if the hook is registered yet bytes21[] memory newHooks = new bytes21[](currentHooks.length); @@ -44,7 +44,7 @@ library HookLib { } // Set the new hooks table - Hooks.set(hookTableId, key, newHooks); + Hooks._set(hookTableId, key, newHooks); } } diff --git a/packages/store/src/StoreCore.sol b/packages/store/src/StoreCore.sol index 684c834c42..004c4a5170 100644 --- a/packages/store/src/StoreCore.sol +++ b/packages/store/src/StoreCore.sol @@ -74,7 +74,7 @@ library StoreCore { * Get the field layout for the given tableId */ function getFieldLayout(bytes32 tableId) internal view returns (FieldLayout fieldLayout) { - fieldLayout = FieldLayout.wrap(Tables.getFieldLayout(tableId)); + fieldLayout = FieldLayout.wrap(Tables._getFieldLayout(tableId)); if (fieldLayout.isEmpty()) { revert IStoreErrors.StoreCore_TableNotFound(tableId, string(abi.encodePacked(tableId))); } @@ -84,7 +84,7 @@ library StoreCore { * Get the key schema for the given tableId */ function getKeySchema(bytes32 tableId) internal view returns (Schema keySchema) { - keySchema = Schema.wrap(Tables.getKeySchema(tableId)); + keySchema = Schema.wrap(Tables._getKeySchema(tableId)); // key schemas can be empty for singleton tables, so we can't depend on key schema for table check if (!hasTable(tableId)) { revert IStoreErrors.StoreCore_TableNotFound(tableId, string(abi.encodePacked(tableId))); @@ -95,7 +95,7 @@ library StoreCore { * Get the schema for the given tableId */ function getValueSchema(bytes32 tableId) internal view returns (Schema valueSchema) { - valueSchema = Schema.wrap(Tables.getValueSchema(tableId)); + valueSchema = Schema.wrap(Tables._getValueSchema(tableId)); if (valueSchema.isEmpty()) { revert IStoreErrors.StoreCore_TableNotFound(tableId, string(abi.encodePacked(tableId))); } @@ -105,7 +105,7 @@ library StoreCore { * Check if a table with the given tableId exists */ function hasTable(bytes32 tableId) internal view returns (bool) { - return Tables.getFieldLayout(tableId) != bytes32(0); + return Tables._getFieldLayout(tableId) != bytes32(0); } /** @@ -147,7 +147,7 @@ library StoreCore { } // Register the table metadata - Tables.set( + Tables._set( tableId, FieldLayout.unwrap(fieldLayout), Schema.unwrap(keySchema), @@ -204,7 +204,7 @@ library StoreCore { emit StoreSetRecord(tableId, keyTuple, staticData, encodedLengths.unwrap(), dynamicData); // Call onBeforeSetRecord hooks (before actually modifying the state, so observers have access to the previous state if needed) - bytes21[] memory hooks = StoreHooks.get(tableId); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(uint8(StoreHookType.BEFORE_SET_RECORD))) { @@ -284,7 +284,7 @@ library StoreCore { FieldLayout fieldLayout ) internal { // Call onBeforeSetField hooks (before modifying the state) - bytes21[] memory hooks = StoreHooks.get(tableId); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(uint8(StoreHookType.BEFORE_SET_FIELD))) { @@ -315,7 +315,7 @@ library StoreCore { emit StoreDeleteRecord(tableId, keyTuple); // Call onBeforeDeleteRecord hooks (before actually modifying the state, so observers have access to the previous state if needed) - bytes21[] memory hooks = StoreHooks.get(tableId); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(uint8(StoreHookType.BEFORE_DELETE_RECORD))) { @@ -363,7 +363,7 @@ library StoreCore { ); // Call onBeforeSetField hooks (before modifying the state) - bytes21[] memory hooks = StoreHooks.get(tableId); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(uint8(StoreHookType.BEFORE_SET_FIELD))) { @@ -404,7 +404,7 @@ library StoreCore { } // Call onBeforeSetField hooks (before modifying the state) - bytes21[] memory hooks = StoreHooks.get(tableId); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(uint8(StoreHookType.BEFORE_SET_FIELD))) { @@ -456,7 +456,7 @@ library StoreCore { } // Call onBeforeSetField hooks (before modifying the state) - bytes21[] memory hooks = StoreHooks.get(tableId); + bytes21[] memory hooks = StoreHooks._get(tableId); for (uint256 i; i < hooks.length; i++) { Hook hook = Hook.wrap(hooks[i]); if (hook.isEnabled(uint8(StoreHookType.BEFORE_SET_FIELD))) { diff --git a/packages/store/src/codegen/tables/Callbacks.sol b/packages/store/src/codegen/tables/Callbacks.sol index cbe5d850c5..499daf1a2a 100644 --- a/packages/store/src/codegen/tables/Callbacks.sol +++ b/packages/store/src/codegen/tables/Callbacks.sol @@ -64,6 +64,11 @@ library Callbacks { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library Callbacks { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes24()); } + /** Get value */ + function _get(bytes32 key) internal view returns (bytes24[] memory value) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes24()); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (bytes24[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library Callbacks { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 key, bytes24[] memory value) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 key, bytes24[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -114,6 +136,17 @@ library Callbacks { } } + /** Get the length of value */ + function _length(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 24; + } + } + /** Get the length of value (using the specified store) */ function length(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -146,6 +179,27 @@ library Callbacks { } } + /** + * Get an item of value + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(bytes32 key, uint256 _index) internal view returns (bytes24) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 24, + (_index + 1) * 24 + ); + return (bytes24(_blob)); + } + } + /** * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -168,6 +222,14 @@ library Callbacks { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to value */ + function _push(bytes32 key, bytes24 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to value (using the specified store) */ function push(IStore _store, bytes32 key, bytes24 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -184,6 +246,14 @@ library Callbacks { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 24, _fieldLayout); } + /** Pop an element from value */ + function _pop(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 24, _fieldLayout); + } + /** Pop an element from value (using the specified store) */ function pop(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -205,6 +275,19 @@ library Callbacks { } } + /** + * Update an element of value at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(bytes32 key, uint256 _index, bytes24 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 24, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -256,6 +339,14 @@ library Callbacks { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/store/src/codegen/tables/Hooks.sol b/packages/store/src/codegen/tables/Hooks.sol index d01e5b05e0..d5eaafb986 100644 --- a/packages/store/src/codegen/tables/Hooks.sol +++ b/packages/store/src/codegen/tables/Hooks.sol @@ -61,6 +61,11 @@ library Hooks { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register(bytes32 _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,15 @@ library Hooks { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } + /** Get value */ + function _get(bytes32 _tableId, bytes32 key) internal view returns (bytes21[] memory value) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 _tableId, bytes32 key) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -92,6 +106,14 @@ library Hooks { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 _tableId, bytes32 key, bytes21[] memory value) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, bytes32 key, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -111,6 +133,17 @@ library Hooks { } } + /** Get the length of value */ + function _length(bytes32 _tableId, bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 21; + } + } + /** Get the length of value (using the specified store) */ function length(IStore _store, bytes32 _tableId, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -143,6 +176,27 @@ library Hooks { } } + /** + * Get an item of value + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(bytes32 _tableId, bytes32 key, uint256 _index) internal view returns (bytes21) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 21, + (_index + 1) * 21 + ); + return (bytes21(_blob)); + } + } + /** * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -165,6 +219,14 @@ library Hooks { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to value */ + function _push(bytes32 _tableId, bytes32 key, bytes21 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to value (using the specified store) */ function push(IStore _store, bytes32 _tableId, bytes32 key, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -181,6 +243,14 @@ library Hooks { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } + /** Pop an element from value */ + function _pop(bytes32 _tableId, bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); + } + /** Pop an element from value (using the specified store) */ function pop(IStore _store, bytes32 _tableId, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -202,6 +272,19 @@ library Hooks { } } + /** + * Update an element of value at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(bytes32 _tableId, bytes32 key, uint256 _index, bytes21 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -253,6 +336,14 @@ library Hooks { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 _tableId, bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/store/src/codegen/tables/KeyEncoding.sol b/packages/store/src/codegen/tables/KeyEncoding.sol index 78d705bf2d..47cc468cb7 100644 --- a/packages/store/src/codegen/tables/KeyEncoding.sol +++ b/packages/store/src/codegen/tables/KeyEncoding.sol @@ -77,6 +77,11 @@ library KeyEncoding { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -103,6 +108,27 @@ library KeyEncoding { return (_toBool(uint8(bytes1(_blob)))); } + /** Get value */ + function _get( + uint256 k1, + int32 k2, + bytes16 k3, + address k4, + bool k5, + ExampleEnum k6 + ) internal view returns (bool value) { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get value (using the specified store) */ function get( IStore _store, @@ -138,6 +164,19 @@ library KeyEncoding { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, ExampleEnum k6, bool value) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set( IStore _store, @@ -208,6 +247,19 @@ library KeyEncoding { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, ExampleEnum k6) internal { + bytes32[] memory _keyTuple = new bytes32[](6); + _keyTuple[0] = bytes32(uint256(k1)); + _keyTuple[1] = bytes32(uint256(int256(k2))); + _keyTuple[2] = bytes32(k3); + _keyTuple[3] = bytes32(uint256(uint160(k4))); + _keyTuple[4] = _boolToBytes32(k5); + _keyTuple[5] = bytes32(uint256(uint8(k6))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, uint256 k1, int32 k2, bytes16 k3, address k4, bool k5, ExampleEnum k6) internal { bytes32[] memory _keyTuple = new bytes32[](6); diff --git a/packages/store/src/codegen/tables/Mixed.sol b/packages/store/src/codegen/tables/Mixed.sol index 13f6d12dfd..fbadcc31ee 100644 --- a/packages/store/src/codegen/tables/Mixed.sol +++ b/packages/store/src/codegen/tables/Mixed.sol @@ -77,6 +77,11 @@ library Mixed { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -91,6 +96,15 @@ library Mixed { return (uint32(bytes4(_blob))); } + /** Get u32 */ + function _getU32(bytes32 key) internal view returns (uint32 u32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get u32 (using the specified store) */ function getU32(IStore _store, bytes32 key) internal view returns (uint32 u32) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -108,6 +122,14 @@ library Mixed { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), _fieldLayout); } + /** Set u32 */ + function _setU32(bytes32 key, uint32 u32) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((u32)), _fieldLayout); + } + /** Set u32 (using the specified store) */ function setU32(IStore _store, bytes32 key, uint32 u32) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -125,6 +147,15 @@ library Mixed { return (uint128(bytes16(_blob))); } + /** Get u128 */ + function _getU128(bytes32 key) internal view returns (uint128 u128) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (uint128(bytes16(_blob))); + } + /** Get u128 (using the specified store) */ function getU128(IStore _store, bytes32 key) internal view returns (uint128 u128) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -142,6 +173,14 @@ library Mixed { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), _fieldLayout); } + /** Set u128 */ + function _setU128(bytes32 key, uint128 u128) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((u128)), _fieldLayout); + } + /** Set u128 (using the specified store) */ function setU128(IStore _store, bytes32 key, uint128 u128) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -159,6 +198,15 @@ library Mixed { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); } + /** Get a32 */ + function _getA32(bytes32 key) internal view returns (uint32[] memory a32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32()); + } + /** Get a32 (using the specified store) */ function getA32(IStore _store, bytes32 key) internal view returns (uint32[] memory a32) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -176,6 +224,14 @@ library Mixed { StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), _fieldLayout); } + /** Set a32 */ + function _setA32(bytes32 key, uint32[] memory a32) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 2, EncodeArray.encode((a32)), _fieldLayout); + } + /** Set a32 (using the specified store) */ function setA32(IStore _store, bytes32 key, uint32[] memory a32) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -195,6 +251,17 @@ library Mixed { } } + /** Get the length of a32 */ + function _lengthA32(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); + unchecked { + return _byteLength / 4; + } + } + /** Get the length of a32 (using the specified store) */ function lengthA32(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -227,6 +294,20 @@ library Mixed { } } + /** + * Get an item of a32 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemA32(bytes32 key, uint256 _index) internal view returns (uint32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 2, _fieldLayout, _index * 4, (_index + 1) * 4); + return (uint32(bytes4(_blob))); + } + } + /** * Get an item of a32 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -249,6 +330,14 @@ library Mixed { StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to a32 */ + function _pushA32(bytes32 key, uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to a32 (using the specified store) */ function pushA32(IStore _store, bytes32 key, uint32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -265,6 +354,14 @@ library Mixed { StoreSwitch.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); } + /** Pop an element from a32 */ + function _popA32(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 2, 4, _fieldLayout); + } + /** Pop an element from a32 (using the specified store) */ function popA32(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -286,6 +383,19 @@ library Mixed { } } + /** + * Update an element of a32 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateA32(bytes32 key, uint256 _index, uint32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 2, _index * 4, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of a32 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -308,6 +418,15 @@ library Mixed { return (string(_blob)); } + /** Get s */ + function _getS(bytes32 key) internal view returns (string memory s) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); + return (string(_blob)); + } + /** Get s (using the specified store) */ function getS(IStore _store, bytes32 key) internal view returns (string memory s) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -325,6 +444,14 @@ library Mixed { StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((s)), _fieldLayout); } + /** Set s */ + function _setS(bytes32 key, string memory s) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 3, bytes((s)), _fieldLayout); + } + /** Set s (using the specified store) */ function setS(IStore _store, bytes32 key, string memory s) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -344,6 +471,17 @@ library Mixed { } } + /** Get the length of s */ + function _lengthS(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); + unchecked { + return _byteLength / 1; + } + } + /** Get the length of s (using the specified store) */ function lengthS(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -376,6 +514,20 @@ library Mixed { } } + /** + * Get an item of s + * (unchecked, returns invalid data if index overflows) + */ + function _getItemS(bytes32 key, uint256 _index) internal view returns (string memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 1, (_index + 1) * 1); + return (string(_blob)); + } + } + /** * Get an item of s (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -398,6 +550,14 @@ library Mixed { StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); } + /** Push a slice to s */ + function _pushS(bytes32 key, string memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); + } + /** Push a slice to s (using the specified store) */ function pushS(IStore _store, bytes32 key, string memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -414,6 +574,14 @@ library Mixed { StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); } + /** Pop a slice from s */ + function _popS(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); + } + /** Pop a slice from s (using the specified store) */ function popS(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -435,6 +603,19 @@ library Mixed { } } + /** + * Update a slice of s at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateS(bytes32 key, uint256 _index, string memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), _fieldLayout); + } + } + /** * Update a slice of s (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -457,6 +638,15 @@ library Mixed { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 key) internal view returns (MixedData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (MixedData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -479,6 +669,19 @@ library Mixed { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes32 key, uint32 u32, uint128 u128, uint32[] memory a32, string memory s) internal { + bytes memory _staticData = encodeStatic(u32, u128); + + PackedCounter _encodedLengths = encodeLengths(a32, s); + bytes memory _dynamicData = encodeDynamic(a32, s); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes32 key, uint32 u32, uint128 u128, uint32[] memory a32, string memory s) internal { bytes memory _staticData = encodeStatic(u32, u128); @@ -497,6 +700,11 @@ library Mixed { set(key, _table.u32, _table.u128, _table.a32, _table.s); } + /** Set the full data using the data struct */ + function _set(bytes32 key, MixedData memory _table) internal { + set(key, _table.u32, _table.u128, _table.a32, _table.s); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 key, MixedData memory _table) internal { set(_store, key, _table.u32, _table.u128, _table.a32, _table.s); @@ -576,6 +784,14 @@ library Mixed { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/store/src/codegen/tables/StoreHooks.sol b/packages/store/src/codegen/tables/StoreHooks.sol index a2632c0b20..43ca3b5fc1 100644 --- a/packages/store/src/codegen/tables/StoreHooks.sol +++ b/packages/store/src/codegen/tables/StoreHooks.sol @@ -64,6 +64,11 @@ library StoreHooks { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library StoreHooks { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } + /** Get value */ + function _get(bytes32 key) internal view returns (bytes21[] memory value) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library StoreHooks { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 key, bytes21[] memory value) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 key, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -114,6 +136,17 @@ library StoreHooks { } } + /** Get the length of value */ + function _length(bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 21; + } + } + /** Get the length of value (using the specified store) */ function length(IStore _store, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -146,6 +179,27 @@ library StoreHooks { } } + /** + * Get an item of value + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(bytes32 key, uint256 _index) internal view returns (bytes21) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 21, + (_index + 1) * 21 + ); + return (bytes21(_blob)); + } + } + /** * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -168,6 +222,14 @@ library StoreHooks { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to value */ + function _push(bytes32 key, bytes21 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to value (using the specified store) */ function push(IStore _store, bytes32 key, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -184,6 +246,14 @@ library StoreHooks { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } + /** Pop an element from value */ + function _pop(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); + } + /** Pop an element from value (using the specified store) */ function pop(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -205,6 +275,19 @@ library StoreHooks { } } + /** + * Update an element of value at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(bytes32 key, uint256 _index, bytes21 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -256,6 +339,14 @@ library StoreHooks { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/store/src/codegen/tables/Tables.sol b/packages/store/src/codegen/tables/Tables.sol index 6204d3de73..268d19548c 100644 --- a/packages/store/src/codegen/tables/Tables.sol +++ b/packages/store/src/codegen/tables/Tables.sol @@ -80,6 +80,11 @@ library Tables { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -94,6 +99,15 @@ library Tables { return (bytes32(_blob)); } + /** Get fieldLayout */ + function _getFieldLayout(bytes32 tableId) internal view returns (bytes32 fieldLayout) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (bytes32(_blob)); + } + /** Get fieldLayout (using the specified store) */ function getFieldLayout(IStore _store, bytes32 tableId) internal view returns (bytes32 fieldLayout) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -111,6 +125,14 @@ library Tables { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); } + /** Set fieldLayout */ + function _setFieldLayout(bytes32 tableId, bytes32 fieldLayout) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((fieldLayout)), _fieldLayout); + } + /** Set fieldLayout (using the specified store) */ function setFieldLayout(IStore _store, bytes32 tableId, bytes32 fieldLayout) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -128,6 +150,15 @@ library Tables { return (bytes32(_blob)); } + /** Get keySchema */ + function _getKeySchema(bytes32 tableId) internal view returns (bytes32 keySchema) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (bytes32(_blob)); + } + /** Get keySchema (using the specified store) */ function getKeySchema(IStore _store, bytes32 tableId) internal view returns (bytes32 keySchema) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -145,6 +176,14 @@ library Tables { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); } + /** Set keySchema */ + function _setKeySchema(bytes32 tableId, bytes32 keySchema) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((keySchema)), _fieldLayout); + } + /** Set keySchema (using the specified store) */ function setKeySchema(IStore _store, bytes32 tableId, bytes32 keySchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -162,6 +201,15 @@ library Tables { return (bytes32(_blob)); } + /** Get valueSchema */ + function _getValueSchema(bytes32 tableId) internal view returns (bytes32 valueSchema) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); + return (bytes32(_blob)); + } + /** Get valueSchema (using the specified store) */ function getValueSchema(IStore _store, bytes32 tableId) internal view returns (bytes32 valueSchema) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -179,6 +227,14 @@ library Tables { StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); } + /** Set valueSchema */ + function _setValueSchema(bytes32 tableId, bytes32 valueSchema) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.setField(_tableId, _keyTuple, 2, abi.encodePacked((valueSchema)), _fieldLayout); + } + /** Set valueSchema (using the specified store) */ function setValueSchema(IStore _store, bytes32 tableId, bytes32 valueSchema) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -196,6 +252,15 @@ library Tables { return (bytes(_blob)); } + /** Get abiEncodedKeyNames */ + function _getAbiEncodedKeyNames(bytes32 tableId) internal view returns (bytes memory abiEncodedKeyNames) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (bytes(_blob)); + } + /** Get abiEncodedKeyNames (using the specified store) */ function getAbiEncodedKeyNames( IStore _store, @@ -216,6 +281,14 @@ library Tables { StoreSwitch.setField(_tableId, _keyTuple, 3, bytes((abiEncodedKeyNames)), _fieldLayout); } + /** Set abiEncodedKeyNames */ + function _setAbiEncodedKeyNames(bytes32 tableId, bytes memory abiEncodedKeyNames) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.setField(_tableId, _keyTuple, 3, bytes((abiEncodedKeyNames)), _fieldLayout); + } + /** Set abiEncodedKeyNames (using the specified store) */ function setAbiEncodedKeyNames(IStore _store, bytes32 tableId, bytes memory abiEncodedKeyNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -235,6 +308,17 @@ library Tables { } } + /** Get the length of abiEncodedKeyNames */ + function _lengthAbiEncodedKeyNames(bytes32 tableId) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); + unchecked { + return _byteLength / 1; + } + } + /** Get the length of abiEncodedKeyNames (using the specified store) */ function lengthAbiEncodedKeyNames(IStore _store, bytes32 tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -267,6 +351,20 @@ library Tables { } } + /** + * Get an item of abiEncodedKeyNames + * (unchecked, returns invalid data if index overflows) + */ + function _getItemAbiEncodedKeyNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 3, _fieldLayout, _index * 1, (_index + 1) * 1); + return (bytes(_blob)); + } + } + /** * Get an item of abiEncodedKeyNames (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -293,6 +391,14 @@ library Tables { StoreSwitch.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); } + /** Push a slice to abiEncodedKeyNames */ + function _pushAbiEncodedKeyNames(bytes32 tableId, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.pushToField(_tableId, _keyTuple, 3, bytes((_slice)), _fieldLayout); + } + /** Push a slice to abiEncodedKeyNames (using the specified store) */ function pushAbiEncodedKeyNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -309,6 +415,14 @@ library Tables { StoreSwitch.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); } + /** Pop a slice from abiEncodedKeyNames */ + function _popAbiEncodedKeyNames(bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.popFromField(_tableId, _keyTuple, 3, 1, _fieldLayout); + } + /** Pop a slice from abiEncodedKeyNames (using the specified store) */ function popAbiEncodedKeyNames(IStore _store, bytes32 tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -330,6 +444,19 @@ library Tables { } } + /** + * Update a slice of abiEncodedKeyNames at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateAbiEncodedKeyNames(bytes32 tableId, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 3, _index * 1, bytes((_slice)), _fieldLayout); + } + } + /** * Update a slice of abiEncodedKeyNames (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -352,6 +479,15 @@ library Tables { return (bytes(_blob)); } + /** Get abiEncodedFieldNames */ + function _getAbiEncodedFieldNames(bytes32 tableId) internal view returns (bytes memory abiEncodedFieldNames) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); + return (bytes(_blob)); + } + /** Get abiEncodedFieldNames (using the specified store) */ function getAbiEncodedFieldNames( IStore _store, @@ -372,6 +508,14 @@ library Tables { StoreSwitch.setField(_tableId, _keyTuple, 4, bytes((abiEncodedFieldNames)), _fieldLayout); } + /** Set abiEncodedFieldNames */ + function _setAbiEncodedFieldNames(bytes32 tableId, bytes memory abiEncodedFieldNames) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.setField(_tableId, _keyTuple, 4, bytes((abiEncodedFieldNames)), _fieldLayout); + } + /** Set abiEncodedFieldNames (using the specified store) */ function setAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory abiEncodedFieldNames) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -391,6 +535,17 @@ library Tables { } } + /** Get the length of abiEncodedFieldNames */ + function _lengthAbiEncodedFieldNames(bytes32 tableId) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); + unchecked { + return _byteLength / 1; + } + } + /** Get the length of abiEncodedFieldNames (using the specified store) */ function lengthAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -423,6 +578,20 @@ library Tables { } } + /** + * Get an item of abiEncodedFieldNames + * (unchecked, returns invalid data if index overflows) + */ + function _getItemAbiEncodedFieldNames(bytes32 tableId, uint256 _index) internal view returns (bytes memory) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice(_tableId, _keyTuple, 4, _fieldLayout, _index * 1, (_index + 1) * 1); + return (bytes(_blob)); + } + } + /** * Get an item of abiEncodedFieldNames (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -449,6 +618,14 @@ library Tables { StoreSwitch.pushToField(_tableId, _keyTuple, 4, bytes((_slice)), _fieldLayout); } + /** Push a slice to abiEncodedFieldNames */ + function _pushAbiEncodedFieldNames(bytes32 tableId, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.pushToField(_tableId, _keyTuple, 4, bytes((_slice)), _fieldLayout); + } + /** Push a slice to abiEncodedFieldNames (using the specified store) */ function pushAbiEncodedFieldNames(IStore _store, bytes32 tableId, bytes memory _slice) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -465,6 +642,14 @@ library Tables { StoreSwitch.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); } + /** Pop a slice from abiEncodedFieldNames */ + function _popAbiEncodedFieldNames(bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.popFromField(_tableId, _keyTuple, 4, 1, _fieldLayout); + } + /** Pop a slice from abiEncodedFieldNames (using the specified store) */ function popAbiEncodedFieldNames(IStore _store, bytes32 tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -486,6 +671,19 @@ library Tables { } } + /** + * Update a slice of abiEncodedFieldNames at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateAbiEncodedFieldNames(bytes32 tableId, uint256 _index, bytes memory _slice) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 4, _index * 1, bytes((_slice)), _fieldLayout); + } + } + /** * Update a slice of abiEncodedFieldNames (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -508,6 +706,15 @@ library Tables { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 tableId) internal view returns (TablesData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 tableId) internal view returns (TablesData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -537,6 +744,26 @@ library Tables { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set( + bytes32 tableId, + bytes32 fieldLayout, + bytes32 keySchema, + bytes32 valueSchema, + bytes memory abiEncodedKeyNames, + bytes memory abiEncodedFieldNames + ) internal { + bytes memory _staticData = encodeStatic(fieldLayout, keySchema, valueSchema); + + PackedCounter _encodedLengths = encodeLengths(abiEncodedKeyNames, abiEncodedFieldNames); + bytes memory _dynamicData = encodeDynamic(abiEncodedKeyNames, abiEncodedFieldNames); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set( IStore _store, @@ -570,6 +797,18 @@ library Tables { ); } + /** Set the full data using the data struct */ + function _set(bytes32 tableId, TablesData memory _table) internal { + set( + tableId, + _table.fieldLayout, + _table.keySchema, + _table.valueSchema, + _table.abiEncodedKeyNames, + _table.abiEncodedFieldNames + ); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 tableId, TablesData memory _table) internal { set( @@ -675,6 +914,14 @@ library Tables { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = tableId; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 tableId) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/store/src/codegen/tables/Vector2.sol b/packages/store/src/codegen/tables/Vector2.sol index d7694660eb..707c7a3dac 100644 --- a/packages/store/src/codegen/tables/Vector2.sol +++ b/packages/store/src/codegen/tables/Vector2.sol @@ -71,6 +71,11 @@ library Vector2 { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -85,6 +90,15 @@ library Vector2 { return (uint32(bytes4(_blob))); } + /** Get x */ + function _getX(bytes32 key) internal view returns (uint32 x) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get x (using the specified store) */ function getX(IStore _store, bytes32 key) internal view returns (uint32 x) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -102,6 +116,14 @@ library Vector2 { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } + /** Set x */ + function _setX(bytes32 key, uint32 x) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); + } + /** Set x (using the specified store) */ function setX(IStore _store, bytes32 key, uint32 x) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -119,6 +141,15 @@ library Vector2 { return (uint32(bytes4(_blob))); } + /** Get y */ + function _getY(bytes32 key) internal view returns (uint32 y) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get y (using the specified store) */ function getY(IStore _store, bytes32 key) internal view returns (uint32 y) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -136,6 +167,14 @@ library Vector2 { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } + /** Set y */ + function _setY(bytes32 key, uint32 y) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); + } + /** Set y (using the specified store) */ function setY(IStore _store, bytes32 key, uint32 y) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -153,6 +192,15 @@ library Vector2 { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 key) internal view returns (Vector2Data memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (Vector2Data memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -175,6 +223,19 @@ library Vector2 { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes32 key, uint32 x, uint32 y) internal { + bytes memory _staticData = encodeStatic(x, y); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes32 key, uint32 x, uint32 y) internal { bytes memory _staticData = encodeStatic(x, y); @@ -193,6 +254,11 @@ library Vector2 { set(key, _table.x, _table.y); } + /** Set the full data using the data struct */ + function _set(bytes32 key, Vector2Data memory _table) internal { + set(key, _table.x, _table.y); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 key, Vector2Data memory _table) internal { set(_store, key, _table.x, _table.y); @@ -236,6 +302,14 @@ library Vector2 { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/store/ts/codegen/ephemeral.ts b/packages/store/ts/codegen/ephemeral.ts index 21b850a782..df57892d8a 100644 --- a/packages/store/ts/codegen/ephemeral.ts +++ b/packages/store/ts/codegen/ephemeral.ts @@ -8,14 +8,14 @@ export function renderEphemeralMethods(options: RenderTableOptions) { let result = renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Emit the ephemeral event using individual values${_commentSuffix} */ - function emitEphemeral(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)), - ])}) internal { + function ${_methodNamePrefix}emitEphemeral(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)), + ])}) internal { ${renderRecordData(options)} ${_keyTupleDefinition} @@ -28,14 +28,14 @@ export function renderEphemeralMethods(options: RenderTableOptions) { if (structName !== undefined) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix, _untypedStore) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Emit the ephemeral event using the data struct${_commentSuffix} */ - function emitEphemeral(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - `${structName} memory _table`, - ])}) internal { + function ${_methodNamePrefix}emitEphemeral(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + `${structName} memory _table`, + ])}) internal { emitEphemeral(${renderArguments([ _untypedStore, _tableId, diff --git a/packages/store/ts/codegen/field.ts b/packages/store/ts/codegen/field.ts index e63ad43f2d..2b811b864c 100644 --- a/packages/store/ts/codegen/field.ts +++ b/packages/store/ts/codegen/field.ts @@ -19,9 +19,9 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Get ${field.name}${_commentSuffix} */ - function get${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}get${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -48,9 +48,9 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Set ${field.name}${_commentSuffix} */ - function set${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}set${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -67,9 +67,9 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Get the length of ${field.name}${_commentSuffix} */ - function length${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}length${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -85,12 +85,12 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** * Get an item of ${field.name}${_commentSuffix} * (unchecked, returns invalid data if index overflows) */ - function getItem${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}getItem${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -114,9 +114,9 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Push ${portionData.title} to ${field.name}${_commentSuffix} */ - function push${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}push${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -130,9 +130,9 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Pop ${portionData.title} from ${field.name}${_commentSuffix} */ - function pop${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}pop${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -145,12 +145,12 @@ export function renderFieldMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** * Update ${portionData.title} of ${field.name}${_commentSuffix} at \`_index\` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) */ - function update${field.methodNameSuffix}(${renderArguments([ + function ${_methodNamePrefix}update${field.methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, diff --git a/packages/store/ts/codegen/record.ts b/packages/store/ts/codegen/record.ts index 75277e463e..9b8b99bb99 100644 --- a/packages/store/ts/codegen/record.ts +++ b/packages/store/ts/codegen/record.ts @@ -14,13 +14,13 @@ export function renderRecordMethods(options: RenderTableOptions) { let result = renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Get the full data${_commentSuffix} */ - function get(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - ])}) internal view returns (${renderDecodedRecord(options)}) { + function ${_methodNamePrefix}get(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal view returns (${renderDecodedRecord(options)}) { ${_keyTupleDefinition} bytes memory _blob = ${_store}.getRecord(_tableId, _keyTuple, _fieldLayout); return decode(_blob); @@ -30,14 +30,14 @@ export function renderRecordMethods(options: RenderTableOptions) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Set the full data using individual values${_commentSuffix} */ - function set(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)), - ])}) internal { + function ${_methodNamePrefix}set(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)), + ])}) internal { ${renderRecordData(options)} ${_keyTupleDefinition} @@ -50,14 +50,14 @@ export function renderRecordMethods(options: RenderTableOptions) { if (structName !== undefined) { result += renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix, _untypedStore) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Set the full data using the data struct${_commentSuffix} */ - function set(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - `${structName} memory _table`, - ])}) internal { + function ${_methodNamePrefix}set(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + `${structName} memory _table`, + ])}) internal { set(${renderArguments([ _untypedStore, _tableId, diff --git a/packages/store/ts/codegen/renderTable.ts b/packages/store/ts/codegen/renderTable.ts index e918822585..0d4c8e4b48 100644 --- a/packages/store/ts/codegen/renderTable.ts +++ b/packages/store/ts/codegen/renderTable.ts @@ -1,4 +1,5 @@ import { + RenderDynamicField, renderArguments, renderCommonData, renderList, @@ -7,7 +8,6 @@ import { renderTypeHelpers, renderWithStore, renderedSolidityHeader, - RenderDynamicField, RenderStaticField, } from "@latticexyz/common/codegen"; import { renderEphemeralMethods } from "./ephemeral"; @@ -113,9 +113,9 @@ export function renderTable(options: RenderTableOptions) { ${renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /** Register the table with its config${_commentSuffix} */ - function register(${renderArguments([_typedStore, _typedTableId])}) internal { + function ${_methodNamePrefix}register(${renderArguments([_typedStore, _typedTableId])}) internal { ${_store}.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } ` @@ -152,9 +152,13 @@ export function renderTable(options: RenderTableOptions) { shouldRenderDelete ? renderWithStore( storeArgument, - (_typedStore, _store, _commentSuffix) => ` + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` /* Delete all data for given keys${_commentSuffix} */ - function deleteRecord(${renderArguments([_typedStore, _typedTableId, _typedKeyArgs])}) internal { + function ${_methodNamePrefix}deleteRecord(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal { ${_keyTupleDefinition} ${_store}.deleteRecord(_tableId, _keyTuple, _fieldLayout); } diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 1568db80f3..c28d505e3f 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -3,31 +3,31 @@ "file": "test/AccessControl.t.sol", "test": "testAccessControl", "name": "AccessControl: hasAccess (cold)", - "gasUsed": 9514 + "gasUsed": 6924 }, { "file": "test/AccessControl.t.sol", "test": "testAccessControl", "name": "AccessControl: hasAccess (warm, namespace only)", - "gasUsed": 1829 + "gasUsed": 1534 }, { "file": "test/AccessControl.t.sol", "test": "testAccessControl", "name": "AccessControl: hasAccess (warm)", - "gasUsed": 3534 + "gasUsed": 2944 }, { "file": "test/AccessControl.t.sol", "test": "testRequireAccess", "name": "AccessControl: requireAccess (cold)", - "gasUsed": 9556 + "gasUsed": 6966 }, { "file": "test/AccessControl.t.sol", "test": "testRequireAccess", "name": "AccessControl: requireAccess (warm)", - "gasUsed": 3559 + "gasUsed": 2969 }, { "file": "test/AccessControl.t.sol", @@ -39,67 +39,67 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1425069 + "gasUsed": 1414535 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1425069 + "gasUsed": 1414535 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "set a record on a table with keysInTableModule installed", - "gasUsed": 161043 + "gasUsed": 158665 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1425069 + "gasUsed": 1414535 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1425069 + "gasUsed": 1414535 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "change a composite record on a table with keysInTableModule installed", - "gasUsed": 22513 + "gasUsed": 21917 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 178377 + "gasUsed": 170653 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1425069 + "gasUsed": 1414535 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "change a record on a table with keysInTableModule installed", - "gasUsed": 21235 + "gasUsed": 20639 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 91846 + "gasUsed": 87686 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 661189 + "gasUsed": 653818 }, { "file": "test/KeysWithValueModule.t.sol", @@ -117,79 +117,79 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 661189 + "gasUsed": 653818 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "set a record on a table with KeysWithValueModule installed", - "gasUsed": 136579 + "gasUsed": 134201 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 661189 + "gasUsed": 653818 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "change a record on a table with KeysWithValueModule installed", - "gasUsed": 106157 + "gasUsed": 103779 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "delete a record on a table with KeysWithValueModule installed", - "gasUsed": 34216 + "gasUsed": 32729 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 661189 + "gasUsed": 653818 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "set a field on a table with KeysWithValueModule installed", - "gasUsed": 141193 + "gasUsed": 138815 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "change a field on a table with KeysWithValueModule installed", - "gasUsed": 105952 + "gasUsed": 103574 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueNotQuery", "name": "CombinedHasHasValueNotQuery", - "gasUsed": 103370 + "gasUsed": 101600 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueQuery", "name": "CombinedHasHasValueQuery", - "gasUsed": 52259 + "gasUsed": 51669 }, { "file": "test/query.t.sol", "test": "testCombinedHasNotQuery", "name": "CombinedHasNotQuery", - "gasUsed": 130426 + "gasUsed": 127476 }, { "file": "test/query.t.sol", "test": "testCombinedHasQuery", "name": "CombinedHasQuery", - "gasUsed": 83357 + "gasUsed": 80997 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueNotQuery", "name": "CombinedHasValueNotQuery", - "gasUsed": 84121 + "gasUsed": 82351 }, { "file": "test/query.t.sol", @@ -201,19 +201,19 @@ "file": "test/query.t.sol", "test": "testHasQuery", "name": "HasQuery", - "gasUsed": 18578 + "gasUsed": 17988 }, { "file": "test/query.t.sol", "test": "testHasQuery1000Keys", "name": "HasQuery with 1000 keys", - "gasUsed": 5902151 + "gasUsed": 5901561 }, { "file": "test/query.t.sol", "test": "testHasQuery100Keys", "name": "HasQuery with 100 keys", - "gasUsed": 550845 + "gasUsed": 550255 }, { "file": "test/query.t.sol", @@ -225,156 +225,156 @@ "file": "test/query.t.sol", "test": "testNotValueQuery", "name": "NotValueQuery", - "gasUsed": 45855 + "gasUsed": 45265 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromCallboundDelegation", "name": "register a callbound delegation", - "gasUsed": 116017 + "gasUsed": 113879 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromCallboundDelegation", "name": "call a system via a callbound delegation", - "gasUsed": 35016 + "gasUsed": 33481 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromTimeboundDelegation", "name": "register a timebound delegation", - "gasUsed": 110371 + "gasUsed": 108316 }, { "file": "test/StandardDelegationsModule.t.sol", "test": "testCallFromTimeboundDelegation", "name": "call a system via a timebound delegation", - "gasUsed": 27946 + "gasUsed": 26692 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 693352 + "gasUsed": 677906 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "get a unique entity nonce (non-root module)", - "gasUsed": 52577 + "gasUsed": 51083 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 680858 + "gasUsed": 667966 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "get a unique entity nonce (root module)", - "gasUsed": 52577 + "gasUsed": 51083 }, { "file": "test/World.t.sol", "test": "testCall", "name": "call a system via the World", - "gasUsed": 13196 + "gasUsed": 12288 }, { "file": "test/World.t.sol", "test": "testCallFromUnlimitedDelegation", "name": "register an unlimited delegation", - "gasUsed": 49345 + "gasUsed": 50163 }, { "file": "test/World.t.sol", "test": "testCallFromUnlimitedDelegation", "name": "call a system via an unlimited delegation", - "gasUsed": 13590 + "gasUsed": 12682 }, { "file": "test/World.t.sol", "test": "testDeleteRecord", "name": "Delete record", - "gasUsed": 9343 + "gasUsed": 8747 }, { "file": "test/World.t.sol", "test": "testPushToField", "name": "Push data to the table", - "gasUsed": 88725 + "gasUsed": 88129 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a fallback system", - "gasUsed": 60882 + "gasUsed": 58788 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a root fallback system", - "gasUsed": 54136 + "gasUsed": 52106 }, { "file": "test/World.t.sol", "test": "testRegisterFunctionSelector", "name": "Register a function selector", - "gasUsed": 81476 + "gasUsed": 79382 }, { "file": "test/World.t.sol", "test": "testRegisterNamespace", "name": "Register a new namespace", - "gasUsed": 127343 + "gasUsed": 122458 }, { "file": "test/World.t.sol", "test": "testRegisterRootFunctionSelector", "name": "Register a root function selector", - "gasUsed": 76054 + "gasUsed": 74024 }, { "file": "test/World.t.sol", "test": "testRegisterTable", "name": "Register a new table in the namespace", - "gasUsed": 648531 + "gasUsed": 641185 }, { "file": "test/World.t.sol", "test": "testSetField", "name": "Write data to a table field", - "gasUsed": 37631 + "gasUsed": 37035 }, { "file": "test/World.t.sol", "test": "testSetRecord", "name": "Write data to the table", - "gasUsed": 35625 + "gasUsed": 35029 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (cold)", - "gasUsed": 27929 + "gasUsed": 25333 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (warm)", - "gasUsed": 15042 + "gasUsed": 14446 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (cold)", - "gasUsed": 31284 + "gasUsed": 28688 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (warm)", - "gasUsed": 18489 + "gasUsed": 17893 } ] diff --git a/packages/world/src/AccessControl.sol b/packages/world/src/AccessControl.sol index ca637f7c3f..d99fd415ef 100644 --- a/packages/world/src/AccessControl.sol +++ b/packages/world/src/AccessControl.sol @@ -16,8 +16,8 @@ library AccessControl { function hasAccess(bytes32 resourceSelector, address caller) internal view returns (bool) { return address(this) == caller || // First check if the World is calling itself - ResourceAccess.get(resourceSelector.getNamespace(), caller) || // Then check access based on the namespace - ResourceAccess.get(resourceSelector, caller); // If caller has no namespace access, check access on the name + ResourceAccess._get(resourceSelector.getNamespace(), caller) || // Then check access based on the namespace + ResourceAccess._get(resourceSelector, caller); // If caller has no namespace access, check access on the name } /** @@ -36,7 +36,7 @@ library AccessControl { * Reverts with AccessDenied if the check fails. */ function requireOwner(bytes32 resourceSelector, address caller) internal view { - if (NamespaceOwner.get(resourceSelector.getNamespace()) != caller) { + if (NamespaceOwner._get(resourceSelector.getNamespace()) != caller) { revert IWorldErrors.AccessDenied(resourceSelector.toString(), caller); } } diff --git a/packages/world/src/SystemCall.sol b/packages/world/src/SystemCall.sol index d5bdf7f53f..f2863b00c4 100644 --- a/packages/world/src/SystemCall.sol +++ b/packages/world/src/SystemCall.sol @@ -34,7 +34,7 @@ library SystemCall { bytes memory funcSelectorAndArgs ) internal returns (bool success, bytes memory data) { // Load the system data - (address systemAddress, bool publicAccess) = Systems.get(resourceSelector); + (address systemAddress, bool publicAccess) = Systems._get(resourceSelector); // Check if the system exists if (systemAddress == address(0)) revert IWorldErrors.ResourceNotFound(resourceSelector.toString()); @@ -45,8 +45,8 @@ library SystemCall { // If the msg.value is non-zero, update the namespace's balance if (value > 0) { bytes16 namespace = resourceSelector.getNamespace(); - uint256 currentBalance = Balances.get(namespace); - Balances.set(namespace, currentBalance + value); + uint256 currentBalance = Balances._get(namespace); + Balances._set(namespace, currentBalance + value); } // Call the system and forward any return data @@ -76,7 +76,7 @@ library SystemCall { uint256 value ) internal returns (bool success, bytes memory data) { // Get system hooks - bytes21[] memory hooks = SystemHooks.get(resourceSelector); + bytes21[] memory hooks = SystemHooks._get(resourceSelector); // Call onBeforeCallSystem hooks (before calling the system) for (uint256 i; i < hooks.length; i++) { diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index f813a96f46..bc3895dd1e 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -54,7 +54,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { } // The World can only be initialized once - if (InstalledModules.get(CORE_MODULE_NAME, keccak256("")) != address(0)) { + if (InstalledModules._get(CORE_MODULE_NAME, keccak256("")) != address(0)) { revert WorldAlreadyInitialized(); } @@ -84,7 +84,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { }); // Register the module in the InstalledModules table - InstalledModules.set(module.getName(), keccak256(args), address(module)); + InstalledModules._set(module.getName(), keccak256(args), address(module)); } /************************************************************************ @@ -229,7 +229,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { } // Check if there is an explicit authorization for this caller to perform actions on behalf of the delegator - Delegation explicitDelegation = Delegation.wrap(Delegations.get({ delegator: delegator, delegatee: msg.sender })); + Delegation explicitDelegation = Delegation.wrap(Delegations._get({ delegator: delegator, delegatee: msg.sender })); if (explicitDelegation.verify(delegator, msg.sender, resourceSelector, funcSelectorAndArgs)) { // forward the call as `delegator` @@ -237,7 +237,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { } // Check if the delegator has a fallback delegation control set - Delegation fallbackDelegation = Delegation.wrap(Delegations.get({ delegator: delegator, delegatee: address(0) })); + Delegation fallbackDelegation = Delegation.wrap(Delegations._get({ delegator: delegator, delegatee: address(0) })); if (fallbackDelegation.verify(delegator, msg.sender, resourceSelector, funcSelectorAndArgs)) { // forward the call with `from` as `msgSender` return SystemCall.callWithHooksOrRevert(delegator, resourceSelector, funcSelectorAndArgs, msg.value); @@ -256,15 +256,15 @@ contract World is StoreRead, IStoreData, IWorldKernel { * ETH sent to the World without calldata is added to the root namespace's balance */ receive() external payable { - uint256 rootBalance = Balances.get(ROOT_NAMESPACE); - Balances.set(ROOT_NAMESPACE, rootBalance + msg.value); + uint256 rootBalance = Balances._get(ROOT_NAMESPACE); + Balances._set(ROOT_NAMESPACE, rootBalance + msg.value); } /** * Fallback function to call registered function selectors */ fallback() external payable { - (bytes32 resourceSelector, bytes4 systemFunctionSelector) = FunctionSelectors.get(msg.sig); + (bytes32 resourceSelector, bytes4 systemFunctionSelector) = FunctionSelectors._get(msg.sig); if (resourceSelector == 0) revert FunctionSelectorNotFound(msg.sig); diff --git a/packages/world/src/modules/core/CoreModule.sol b/packages/world/src/modules/core/CoreModule.sol index aaf34ff19e..a9712b4576 100644 --- a/packages/world/src/modules/core/CoreModule.sol +++ b/packages/world/src/modules/core/CoreModule.sol @@ -76,9 +76,9 @@ contract CoreModule is Module { SystemRegistry.register(); ResourceType.register(); - NamespaceOwner.set(ROOT_NAMESPACE, _msgSender()); - ResourceAccess.set(ROOT_NAMESPACE, _msgSender(), true); - ResourceType.set(ROOT_NAMESPACE, Resource.NAMESPACE); + NamespaceOwner._set(ROOT_NAMESPACE, _msgSender()); + ResourceAccess._set(ROOT_NAMESPACE, _msgSender(), true); + ResourceType._set(ROOT_NAMESPACE, Resource.NAMESPACE); } /** diff --git a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol index c29d693ac0..cff47dd2db 100644 --- a/packages/world/src/modules/core/implementations/AccessManagementSystem.sol +++ b/packages/world/src/modules/core/implementations/AccessManagementSystem.sol @@ -22,7 +22,7 @@ contract AccessManagementSystem is System { AccessControl.requireOwner(resourceSelector, _msgSender()); // Grant access to the given resource - ResourceAccess.set(resourceSelector, grantee, true); + ResourceAccess._set(resourceSelector, grantee, true); } /** @@ -34,7 +34,7 @@ contract AccessManagementSystem is System { AccessControl.requireOwner(resourceSelector, _msgSender()); // Revoke access from the given resource - ResourceAccess.deleteRecord(resourceSelector, grantee); + ResourceAccess._deleteRecord(resourceSelector, grantee); } /** @@ -47,12 +47,12 @@ contract AccessManagementSystem is System { AccessControl.requireOwner(namespace, _msgSender()); // Set namespace new owner - NamespaceOwner.set(namespace, newOwner); + NamespaceOwner._set(namespace, newOwner); // Revoke access from old owner - ResourceAccess.deleteRecord(namespace, _msgSender()); + ResourceAccess._deleteRecord(namespace, _msgSender()); // Grant access to new owner - ResourceAccess.set(namespace, newOwner, true); + ResourceAccess._set(namespace, newOwner, true); } } diff --git a/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol b/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol index d57cc82a6c..9f0fdde1cd 100644 --- a/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol +++ b/packages/world/src/modules/core/implementations/BalanceTransferSystem.sol @@ -20,14 +20,14 @@ contract BalanceTransferSystem is System, IWorldErrors { AccessControl.requireAccess(fromNamespace, _msgSender()); // Get current namespace balance - uint256 balance = Balances.get(fromNamespace); + uint256 balance = Balances._get(fromNamespace); // Require the balance balance to be greater or equal to the amount to transfer if (amount > balance) revert InsufficientBalance(balance, amount); // Update the balances - Balances.set(fromNamespace, balance - amount); - Balances.set(toNamespace, Balances.get(toNamespace) + amount); + Balances._set(fromNamespace, balance - amount); + Balances._set(toNamespace, Balances._get(toNamespace) + amount); } /** @@ -38,13 +38,13 @@ contract BalanceTransferSystem is System, IWorldErrors { AccessControl.requireAccess(fromNamespace, _msgSender()); // Get current namespace balance - uint256 balance = Balances.get(fromNamespace); + uint256 balance = Balances._get(fromNamespace); // Require the balance balance to be greater or equal to the amount to transfer if (amount > balance) revert InsufficientBalance(balance, amount); // Update the balances - Balances.set(fromNamespace, balance - amount); + Balances._set(fromNamespace, balance - amount); // Transfer the balance to the given address, revert on failure (bool success, bytes memory data) = payable(toAddress).call{ value: amount }(""); diff --git a/packages/world/src/modules/core/implementations/ModuleInstallationSystem.sol b/packages/world/src/modules/core/implementations/ModuleInstallationSystem.sol index ebd21ed856..80a004975a 100644 --- a/packages/world/src/modules/core/implementations/ModuleInstallationSystem.sol +++ b/packages/world/src/modules/core/implementations/ModuleInstallationSystem.sol @@ -28,6 +28,6 @@ contract ModuleInstallationSystem is System { }); // Register the module in the InstalledModules table - InstalledModules.set(module.getName(), keccak256(args), address(module)); + InstalledModules._set(module.getName(), keccak256(args), address(module)); } } diff --git a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol index 00a5d3017c..eacfff6087 100644 --- a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol @@ -49,10 +49,10 @@ contract StoreRegistrationSystem is System, IWorldErrors { // If the namespace doesn't exist yet, register it bytes16 namespace = resourceSelector.getNamespace(); - if (ResourceType.get(namespace) == Resource.NONE) { + if (ResourceType._get(namespace) == Resource.NONE) { // We can't call IBaseWorld(this).registerNamespace directly because it would be handled like // an external call, so msg.sender would be the address of the World contract - (address systemAddress, ) = Systems.get(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME)); + (address systemAddress, ) = Systems._get(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME)); WorldContextProvider.delegatecallWithContextOrRevert({ msgSender: _msgSender(), msgValue: 0, @@ -65,12 +65,12 @@ contract StoreRegistrationSystem is System, IWorldErrors { } // Require no resource to exist at this selector yet - if (ResourceType.get(resourceSelector) != Resource.NONE) { + if (ResourceType._get(resourceSelector) != Resource.NONE) { revert ResourceExists(resourceSelector.toString()); } // Store the table resource type - ResourceType.set(resourceSelector, Resource.TABLE); + ResourceType._set(resourceSelector, Resource.TABLE); // Register the table StoreCore.registerTable(resourceSelector, fieldLayout, keySchema, valueSchema, keyNames, fieldNames); diff --git a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol index 2d018ae0fb..3502e5bd6c 100644 --- a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol @@ -39,16 +39,16 @@ contract WorldRegistrationSystem is System, IWorldErrors { bytes32 resourceSelector = ResourceSelector.from(namespace); // Require namespace to not exist yet - if (ResourceType.get(namespace) != Resource.NONE) revert ResourceExists(resourceSelector.toString()); + if (ResourceType._get(namespace) != Resource.NONE) revert ResourceExists(resourceSelector.toString()); // Register namespace resource - ResourceType.set(namespace, Resource.NAMESPACE); + ResourceType._set(namespace, Resource.NAMESPACE); // Register caller as the namespace owner - NamespaceOwner.set(namespace, _msgSender()); + NamespaceOwner._set(namespace, _msgSender()); // Give caller access to the new namespace - ResourceAccess.set(resourceSelector, _msgSender(), true); + ResourceAccess._set(resourceSelector, _msgSender(), true); } /** @@ -97,7 +97,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { if (resourceSelector.getName() == ROOT_NAME) revert InvalidSelector(resourceSelector.toString()); // Require this system to not be registered at a different resource selector yet - bytes32 existingResourceSelector = SystemRegistry.get(address(system)); + bytes32 existingResourceSelector = SystemRegistry._get(address(system)); if (existingResourceSelector != 0 && existingResourceSelector != resourceSelector) { revert SystemExists(address(system)); } @@ -105,38 +105,38 @@ contract WorldRegistrationSystem is System, IWorldErrors { // If the namespace doesn't exist yet, register it // otherwise require caller to own the namespace bytes16 namespace = resourceSelector.getNamespace(); - if (ResourceType.get(namespace) == Resource.NONE) registerNamespace(namespace); + if (ResourceType._get(namespace) == Resource.NONE) registerNamespace(namespace); else AccessControl.requireOwner(namespace, _msgSender()); // Require no resource other than a system to exist at this selector yet - Resource resourceType = ResourceType.get(resourceSelector); + Resource resourceType = ResourceType._get(resourceSelector); if (resourceType != Resource.NONE && resourceType != Resource.SYSTEM) { revert ResourceExists(resourceSelector.toString()); } // Check if a system already exists at this resource selector - address existingSystem = Systems.getSystem(resourceSelector); + address existingSystem = Systems._getSystem(resourceSelector); // If there is an existing system with this resource selector, remove it if (existingSystem != address(0)) { // Remove the existing system from the system registry - SystemRegistry.deleteRecord(existingSystem); + SystemRegistry._deleteRecord(existingSystem); // Remove the existing system's access to its namespace - ResourceAccess.deleteRecord(namespace, existingSystem); + ResourceAccess._deleteRecord(namespace, existingSystem); } else { // Otherwise, this is a new system, so register its resource type - ResourceType.set(resourceSelector, Resource.SYSTEM); + ResourceType._set(resourceSelector, Resource.SYSTEM); } // Systems = mapping from resourceSelector to system address and publicAccess - Systems.set(resourceSelector, address(system), publicAccess); + Systems._set(resourceSelector, address(system), publicAccess); // SystemRegistry = mapping from system address to resourceSelector - SystemRegistry.set(address(system), resourceSelector); + SystemRegistry._set(address(system), resourceSelector); // Grant the system access to its namespace - ResourceAccess.set(namespace, address(system), true); + ResourceAccess._set(namespace, address(system), true); } /** @@ -162,7 +162,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { ); // Require the function selector to be globally unique - bytes32 existingResourceSelector = FunctionSelectors.getResourceSelector(worldFunctionSelector); + bytes32 existingResourceSelector = FunctionSelectors._getResourceSelector(worldFunctionSelector); if (existingResourceSelector != 0) revert FunctionSelectorExists(worldFunctionSelector); @@ -171,7 +171,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { bytes4 systemFunctionSelector = systemFunctionSignature.length == 0 ? bytes4(0) // Save gas by storing 0x0 for empty function signatures (= fallback function) : bytes4(keccak256(systemFunctionSignature)); - FunctionSelectors.set(worldFunctionSelector, resourceSelector, systemFunctionSelector); + FunctionSelectors._set(worldFunctionSelector, resourceSelector, systemFunctionSelector); } /** @@ -190,12 +190,12 @@ contract WorldRegistrationSystem is System, IWorldErrors { AccessControl.requireOwner(ROOT_NAMESPACE, _msgSender()); // Require the function selector to be globally unique - bytes32 existingResourceSelector = FunctionSelectors.getResourceSelector(worldFunctionSelector); + bytes32 existingResourceSelector = FunctionSelectors._getResourceSelector(worldFunctionSelector); if (existingResourceSelector != 0) revert FunctionSelectorExists(worldFunctionSelector); // Register the function selector - FunctionSelectors.set(worldFunctionSelector, resourceSelector, systemFunctionSelector); + FunctionSelectors._set(worldFunctionSelector, resourceSelector, systemFunctionSelector); return worldFunctionSelector; } @@ -214,7 +214,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { // If the delegation is not unlimited... if (delegationControlId != UNLIMITED_DELEGATION && initFuncSelectorAndArgs.length > 0) { // Require the delegationControl contract to implement the IDelegationControl interface - (address delegationControl, ) = Systems.get(delegationControlId); + (address delegationControl, ) = Systems._get(delegationControlId); requireInterface(delegationControl, DELEGATION_CONTROL_INTERFACE_ID); // Call the delegation control contract's init function diff --git a/packages/world/src/modules/core/tables/Balances.sol b/packages/world/src/modules/core/tables/Balances.sol index 280b77ae71..e87221fb49 100644 --- a/packages/world/src/modules/core/tables/Balances.sol +++ b/packages/world/src/modules/core/tables/Balances.sol @@ -64,6 +64,11 @@ library Balances { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library Balances { return (uint256(bytes32(_blob))); } + /** Get balance */ + function _get(bytes16 namespace) internal view returns (uint256 balance) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(namespace); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + /** Get balance (using the specified store) */ function get(IStore _store, bytes16 namespace) internal view returns (uint256 balance) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library Balances { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); } + /** Set balance */ + function _set(bytes16 namespace, uint256 balance) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(namespace); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); + } + /** Set balance (using the specified store) */ function set(IStore _store, bytes16 namespace, uint256 balance) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -134,6 +156,14 @@ library Balances { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes16 namespace) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(namespace); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes16 namespace) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/core/tables/FunctionSelectors.sol b/packages/world/src/modules/core/tables/FunctionSelectors.sol index 28ee846849..036335865a 100644 --- a/packages/world/src/modules/core/tables/FunctionSelectors.sol +++ b/packages/world/src/modules/core/tables/FunctionSelectors.sol @@ -66,6 +66,11 @@ library FunctionSelectors { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -80,6 +85,15 @@ library FunctionSelectors { return (bytes32(_blob)); } + /** Get resourceSelector */ + function _getResourceSelector(bytes4 functionSelector) internal view returns (bytes32 resourceSelector) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (bytes32(_blob)); + } + /** Get resourceSelector (using the specified store) */ function getResourceSelector( IStore _store, @@ -100,6 +114,14 @@ library FunctionSelectors { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); } + /** Set resourceSelector */ + function _setResourceSelector(bytes4 functionSelector, bytes32 resourceSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); + } + /** Set resourceSelector (using the specified store) */ function setResourceSelector(IStore _store, bytes4 functionSelector, bytes32 resourceSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -117,6 +139,15 @@ library FunctionSelectors { return (bytes4(_blob)); } + /** Get systemFunctionSelector */ + function _getSystemFunctionSelector(bytes4 functionSelector) internal view returns (bytes4 systemFunctionSelector) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (bytes4(_blob)); + } + /** Get systemFunctionSelector (using the specified store) */ function getSystemFunctionSelector( IStore _store, @@ -137,6 +168,14 @@ library FunctionSelectors { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((systemFunctionSelector)), _fieldLayout); } + /** Set systemFunctionSelector */ + function _setSystemFunctionSelector(bytes4 functionSelector, bytes4 systemFunctionSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((systemFunctionSelector)), _fieldLayout); + } + /** Set systemFunctionSelector (using the specified store) */ function setSystemFunctionSelector(IStore _store, bytes4 functionSelector, bytes4 systemFunctionSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -156,6 +195,17 @@ library FunctionSelectors { return decode(_blob); } + /** Get the full data */ + function _get( + bytes4 functionSelector + ) internal view returns (bytes32 resourceSelector, bytes4 systemFunctionSelector) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get( IStore _store, @@ -181,6 +231,19 @@ library FunctionSelectors { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes4 functionSelector, bytes32 resourceSelector, bytes4 systemFunctionSelector) internal { + bytes memory _staticData = encodeStatic(resourceSelector, systemFunctionSelector); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set( IStore _store, @@ -237,6 +300,14 @@ library FunctionSelectors { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes4 functionSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(functionSelector); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes4 functionSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/core/tables/ResourceType.sol b/packages/world/src/modules/core/tables/ResourceType.sol index 4dd272567b..0fb6a1c293 100644 --- a/packages/world/src/modules/core/tables/ResourceType.sol +++ b/packages/world/src/modules/core/tables/ResourceType.sol @@ -67,6 +67,11 @@ library ResourceType { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -81,6 +86,15 @@ library ResourceType { return Resource(uint8(bytes1(_blob))); } + /** Get resourceType */ + function _get(bytes32 resourceSelector) internal view returns (Resource resourceType) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return Resource(uint8(bytes1(_blob))); + } + /** Get resourceType (using the specified store) */ function get(IStore _store, bytes32 resourceSelector) internal view returns (Resource resourceType) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -98,6 +112,14 @@ library ResourceType { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), _fieldLayout); } + /** Set resourceType */ + function _set(bytes32 resourceSelector, Resource resourceType) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), _fieldLayout); + } + /** Set resourceType (using the specified store) */ function set(IStore _store, bytes32 resourceSelector, Resource resourceType) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -137,6 +159,14 @@ library ResourceType { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 resourceSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 resourceSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/core/tables/SystemHooks.sol b/packages/world/src/modules/core/tables/SystemHooks.sol index 1314088f74..a322e22ced 100644 --- a/packages/world/src/modules/core/tables/SystemHooks.sol +++ b/packages/world/src/modules/core/tables/SystemHooks.sol @@ -64,6 +64,11 @@ library SystemHooks { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library SystemHooks { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); } + /** Get value */ + function _get(bytes32 resourceSelector) internal view returns (bytes21[] memory value) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes21()); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 resourceSelector) internal view returns (bytes21[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library SystemHooks { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 resourceSelector, bytes21[] memory value) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 resourceSelector, bytes21[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -114,6 +136,17 @@ library SystemHooks { } } + /** Get the length of value */ + function _length(bytes32 resourceSelector) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 21; + } + } + /** Get the length of value (using the specified store) */ function length(IStore _store, bytes32 resourceSelector) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -146,6 +179,27 @@ library SystemHooks { } } + /** + * Get an item of value + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(bytes32 resourceSelector, uint256 _index) internal view returns (bytes21) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 21, + (_index + 1) * 21 + ); + return (bytes21(_blob)); + } + } + /** * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -168,6 +222,14 @@ library SystemHooks { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to value */ + function _push(bytes32 resourceSelector, bytes21 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to value (using the specified store) */ function push(IStore _store, bytes32 resourceSelector, bytes21 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -184,6 +246,14 @@ library SystemHooks { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); } + /** Pop an element from value */ + function _pop(bytes32 resourceSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 21, _fieldLayout); + } + /** Pop an element from value (using the specified store) */ function pop(IStore _store, bytes32 resourceSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -205,6 +275,19 @@ library SystemHooks { } } + /** + * Update an element of value at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(bytes32 resourceSelector, uint256 _index, bytes21 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 21, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -256,6 +339,14 @@ library SystemHooks { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 resourceSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 resourceSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/core/tables/SystemRegistry.sol b/packages/world/src/modules/core/tables/SystemRegistry.sol index e27e2fdd95..42f7311644 100644 --- a/packages/world/src/modules/core/tables/SystemRegistry.sol +++ b/packages/world/src/modules/core/tables/SystemRegistry.sol @@ -64,6 +64,11 @@ library SystemRegistry { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library SystemRegistry { return (bytes32(_blob)); } + /** Get resourceSelector */ + function _get(address system) internal view returns (bytes32 resourceSelector) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(system))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (bytes32(_blob)); + } + /** Get resourceSelector (using the specified store) */ function get(IStore _store, address system) internal view returns (bytes32 resourceSelector) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library SystemRegistry { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); } + /** Set resourceSelector */ + function _set(address system, bytes32 resourceSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(system))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), _fieldLayout); + } + /** Set resourceSelector (using the specified store) */ function set(IStore _store, address system, bytes32 resourceSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -134,6 +156,14 @@ library SystemRegistry { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(address system) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(system))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, address system) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/core/tables/Systems.sol b/packages/world/src/modules/core/tables/Systems.sol index edffc1d222..f60421fce5 100644 --- a/packages/world/src/modules/core/tables/Systems.sol +++ b/packages/world/src/modules/core/tables/Systems.sol @@ -66,6 +66,11 @@ library Systems { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -80,6 +85,15 @@ library Systems { return (address(bytes20(_blob))); } + /** Get system */ + function _getSystem(bytes32 resourceSelector) internal view returns (address system) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (address(bytes20(_blob))); + } + /** Get system (using the specified store) */ function getSystem(IStore _store, bytes32 resourceSelector) internal view returns (address system) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -97,6 +111,14 @@ library Systems { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); } + /** Set system */ + function _setSystem(bytes32 resourceSelector, address system) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), _fieldLayout); + } + /** Set system (using the specified store) */ function setSystem(IStore _store, bytes32 resourceSelector, address system) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -114,6 +136,15 @@ library Systems { return (_toBool(uint8(bytes1(_blob)))); } + /** Get publicAccess */ + function _getPublicAccess(bytes32 resourceSelector) internal view returns (bool publicAccess) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get publicAccess (using the specified store) */ function getPublicAccess(IStore _store, bytes32 resourceSelector) internal view returns (bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -131,6 +162,14 @@ library Systems { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); } + /** Set publicAccess */ + function _setPublicAccess(bytes32 resourceSelector, bool publicAccess) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), _fieldLayout); + } + /** Set publicAccess (using the specified store) */ function setPublicAccess(IStore _store, bytes32 resourceSelector, bool publicAccess) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -148,6 +187,15 @@ library Systems { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 resourceSelector) internal view returns (address system, bool publicAccess) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 resourceSelector) internal view returns (address system, bool publicAccess) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -170,6 +218,19 @@ library Systems { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes32 resourceSelector, address system, bool publicAccess) internal { + bytes memory _staticData = encodeStatic(system, publicAccess); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes32 resourceSelector, address system, bool publicAccess) internal { bytes memory _staticData = encodeStatic(system, publicAccess); @@ -221,6 +282,14 @@ library Systems { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 resourceSelector) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = resourceSelector; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 resourceSelector) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/keysintable/tables/KeysInTable.sol b/packages/world/src/modules/keysintable/tables/KeysInTable.sol index c18a7915d5..5ba1921b92 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -80,6 +80,11 @@ library KeysInTable { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -94,6 +99,15 @@ library KeysInTable { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get keys0 */ + function _getKeys0(bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get keys0 (using the specified store) */ function getKeys0(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys0) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -111,6 +125,14 @@ library KeysInTable { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), _fieldLayout); } + /** Set keys0 */ + function _setKeys0(bytes32 sourceTable, bytes32[] memory keys0) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), _fieldLayout); + } + /** Set keys0 (using the specified store) */ function setKeys0(IStore _store, bytes32 sourceTable, bytes32[] memory keys0) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -130,6 +152,17 @@ library KeysInTable { } } + /** Get the length of keys0 */ + function _lengthKeys0(bytes32 sourceTable) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of keys0 (using the specified store) */ function lengthKeys0(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -162,6 +195,27 @@ library KeysInTable { } } + /** + * Get an item of keys0 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemKeys0(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of keys0 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -184,6 +238,14 @@ library KeysInTable { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to keys0 */ + function _pushKeys0(bytes32 sourceTable, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to keys0 (using the specified store) */ function pushKeys0(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -200,6 +262,14 @@ library KeysInTable { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } + /** Pop an element from keys0 */ + function _popKeys0(bytes32 sourceTable) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); + } + /** Pop an element from keys0 (using the specified store) */ function popKeys0(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -221,6 +291,19 @@ library KeysInTable { } } + /** + * Update an element of keys0 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateKeys0(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of keys0 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -243,6 +326,15 @@ library KeysInTable { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get keys1 */ + function _getKeys1(bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get keys1 (using the specified store) */ function getKeys1(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys1) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -260,6 +352,14 @@ library KeysInTable { StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), _fieldLayout); } + /** Set keys1 */ + function _setKeys1(bytes32 sourceTable, bytes32[] memory keys1) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), _fieldLayout); + } + /** Set keys1 (using the specified store) */ function setKeys1(IStore _store, bytes32 sourceTable, bytes32[] memory keys1) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -279,6 +379,17 @@ library KeysInTable { } } + /** Get the length of keys1 */ + function _lengthKeys1(bytes32 sourceTable) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 1, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of keys1 (using the specified store) */ function lengthKeys1(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -311,6 +422,27 @@ library KeysInTable { } } + /** + * Get an item of keys1 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemKeys1(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 1, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of keys1 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -333,6 +465,14 @@ library KeysInTable { StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to keys1 */ + function _pushKeys1(bytes32 sourceTable, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to keys1 (using the specified store) */ function pushKeys1(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -349,6 +489,14 @@ library KeysInTable { StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, _fieldLayout); } + /** Pop an element from keys1 */ + function _popKeys1(bytes32 sourceTable) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.popFromField(_tableId, _keyTuple, 1, 32, _fieldLayout); + } + /** Pop an element from keys1 (using the specified store) */ function popKeys1(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -370,6 +518,19 @@ library KeysInTable { } } + /** + * Update an element of keys1 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateKeys1(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of keys1 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -392,6 +553,15 @@ library KeysInTable { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get keys2 */ + function _getKeys2(bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 2); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get keys2 (using the specified store) */ function getKeys2(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys2) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -409,6 +579,14 @@ library KeysInTable { StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), _fieldLayout); } + /** Set keys2 */ + function _setKeys2(bytes32 sourceTable, bytes32[] memory keys2) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), _fieldLayout); + } + /** Set keys2 (using the specified store) */ function setKeys2(IStore _store, bytes32 sourceTable, bytes32[] memory keys2) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -428,6 +606,17 @@ library KeysInTable { } } + /** Get the length of keys2 */ + function _lengthKeys2(bytes32 sourceTable) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 2, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of keys2 (using the specified store) */ function lengthKeys2(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -460,6 +649,27 @@ library KeysInTable { } } + /** + * Get an item of keys2 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemKeys2(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 2, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of keys2 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -482,6 +692,14 @@ library KeysInTable { StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to keys2 */ + function _pushKeys2(bytes32 sourceTable, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to keys2 (using the specified store) */ function pushKeys2(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -498,6 +716,14 @@ library KeysInTable { StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, _fieldLayout); } + /** Pop an element from keys2 */ + function _popKeys2(bytes32 sourceTable) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.popFromField(_tableId, _keyTuple, 2, 32, _fieldLayout); + } + /** Pop an element from keys2 (using the specified store) */ function popKeys2(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -519,6 +745,19 @@ library KeysInTable { } } + /** + * Update an element of keys2 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateKeys2(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of keys2 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -541,6 +780,15 @@ library KeysInTable { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get keys3 */ + function _getKeys3(bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 3); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get keys3 (using the specified store) */ function getKeys3(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys3) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -558,6 +806,14 @@ library KeysInTable { StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), _fieldLayout); } + /** Set keys3 */ + function _setKeys3(bytes32 sourceTable, bytes32[] memory keys3) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), _fieldLayout); + } + /** Set keys3 (using the specified store) */ function setKeys3(IStore _store, bytes32 sourceTable, bytes32[] memory keys3) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -577,6 +833,17 @@ library KeysInTable { } } + /** Get the length of keys3 */ + function _lengthKeys3(bytes32 sourceTable) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 3, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of keys3 (using the specified store) */ function lengthKeys3(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -609,6 +876,27 @@ library KeysInTable { } } + /** + * Get an item of keys3 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemKeys3(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 3, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of keys3 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -631,6 +919,14 @@ library KeysInTable { StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to keys3 */ + function _pushKeys3(bytes32 sourceTable, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to keys3 (using the specified store) */ function pushKeys3(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -647,6 +943,14 @@ library KeysInTable { StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, _fieldLayout); } + /** Pop an element from keys3 */ + function _popKeys3(bytes32 sourceTable) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.popFromField(_tableId, _keyTuple, 3, 32, _fieldLayout); + } + /** Pop an element from keys3 (using the specified store) */ function popKeys3(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -668,6 +972,19 @@ library KeysInTable { } } + /** + * Update an element of keys3 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateKeys3(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of keys3 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -690,6 +1007,15 @@ library KeysInTable { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get keys4 */ + function _getKeys4(bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 4); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get keys4 (using the specified store) */ function getKeys4(IStore _store, bytes32 sourceTable) internal view returns (bytes32[] memory keys4) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -707,6 +1033,14 @@ library KeysInTable { StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), _fieldLayout); } + /** Set keys4 */ + function _setKeys4(bytes32 sourceTable, bytes32[] memory keys4) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), _fieldLayout); + } + /** Set keys4 (using the specified store) */ function setKeys4(IStore _store, bytes32 sourceTable, bytes32[] memory keys4) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -726,6 +1060,17 @@ library KeysInTable { } } + /** Get the length of keys4 */ + function _lengthKeys4(bytes32 sourceTable) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 4, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of keys4 (using the specified store) */ function lengthKeys4(IStore _store, bytes32 sourceTable) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -758,6 +1103,27 @@ library KeysInTable { } } + /** + * Get an item of keys4 + * (unchecked, returns invalid data if index overflows) + */ + function _getItemKeys4(bytes32 sourceTable, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 4, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of keys4 (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -780,6 +1146,14 @@ library KeysInTable { StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to keys4 */ + function _pushKeys4(bytes32 sourceTable, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to keys4 (using the specified store) */ function pushKeys4(IStore _store, bytes32 sourceTable, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -796,6 +1170,14 @@ library KeysInTable { StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, _fieldLayout); } + /** Pop an element from keys4 */ + function _popKeys4(bytes32 sourceTable) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.popFromField(_tableId, _keyTuple, 4, 32, _fieldLayout); + } + /** Pop an element from keys4 (using the specified store) */ function popKeys4(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -817,6 +1199,19 @@ library KeysInTable { } } + /** + * Update an element of keys4 at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _updateKeys4(bytes32 sourceTable, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of keys4 (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -839,6 +1234,15 @@ library KeysInTable { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 sourceTable) internal view returns (KeysInTableData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -867,6 +1271,25 @@ library KeysInTable { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set( + bytes32 sourceTable, + bytes32[] memory keys0, + bytes32[] memory keys1, + bytes32[] memory keys2, + bytes32[] memory keys3, + bytes32[] memory keys4 + ) internal { + bytes memory _staticData; + PackedCounter _encodedLengths = encodeLengths(keys0, keys1, keys2, keys3, keys4); + bytes memory _dynamicData = encodeDynamic(keys0, keys1, keys2, keys3, keys4); + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set( IStore _store, @@ -892,6 +1315,11 @@ library KeysInTable { set(sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); } + /** Set the full data using the data struct */ + function _set(bytes32 sourceTable, KeysInTableData memory _table) internal { + set(sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 sourceTable, KeysInTableData memory _table) internal { set(_store, sourceTable, _table.keys0, _table.keys1, _table.keys2, _table.keys3, _table.keys4); @@ -1010,6 +1438,14 @@ library KeysInTable { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 sourceTable) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = sourceTable; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 sourceTable) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index a0ab8dc9fd..c50753a03b 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -68,6 +68,11 @@ library UsedKeysIndex { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -83,6 +88,16 @@ library UsedKeysIndex { return (_toBool(uint8(bytes1(_blob)))); } + /** Get has */ + function _getHas(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get has (using the specified store) */ function getHas(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has) { bytes32[] memory _keyTuple = new bytes32[](2); @@ -102,6 +117,15 @@ library UsedKeysIndex { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); } + /** Set has */ + function _setHas(bytes32 sourceTable, bytes32 keysHash, bool has) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), _fieldLayout); + } + /** Set has (using the specified store) */ function setHas(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has) internal { bytes32[] memory _keyTuple = new bytes32[](2); @@ -121,6 +145,16 @@ library UsedKeysIndex { return (uint40(bytes5(_blob))); } + /** Get index */ + function _getIndex(bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (uint40(bytes5(_blob))); + } + /** Get index (using the specified store) */ function getIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); @@ -140,6 +174,15 @@ library UsedKeysIndex { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); } + /** Set index */ + function _setIndex(bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), _fieldLayout); + } + /** Set index (using the specified store) */ function setIndex(IStore _store, bytes32 sourceTable, bytes32 keysHash, uint40 index) internal { bytes32[] memory _keyTuple = new bytes32[](2); @@ -159,6 +202,16 @@ library UsedKeysIndex { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal view returns (bool has, uint40 index) { bytes32[] memory _keyTuple = new bytes32[](2); @@ -183,6 +236,20 @@ library UsedKeysIndex { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { + bytes memory _staticData = encodeStatic(has, index); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes32 sourceTable, bytes32 keysHash, bool has, uint40 index) internal { bytes memory _staticData = encodeStatic(has, index); @@ -237,6 +304,15 @@ library UsedKeysIndex { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 sourceTable, bytes32 keysHash) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = sourceTable; + _keyTuple[1] = keysHash; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 sourceTable, bytes32 keysHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); diff --git a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol index 9255f04f3f..76db05f36f 100644 --- a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol +++ b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol @@ -61,6 +61,11 @@ library KeysWithValue { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register(bytes32 _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,15 @@ library KeysWithValue { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } + /** Get keysWithValue */ + function _get(bytes32 _tableId, bytes32 valueHash) internal view returns (bytes32[] memory keysWithValue) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); + } + /** Get keysWithValue (using the specified store) */ function get( IStore _store, @@ -96,6 +110,14 @@ library KeysWithValue { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), _fieldLayout); } + /** Set keysWithValue */ + function _set(bytes32 _tableId, bytes32 valueHash, bytes32[] memory keysWithValue) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), _fieldLayout); + } + /** Set keysWithValue (using the specified store) */ function set(IStore _store, bytes32 _tableId, bytes32 valueHash, bytes32[] memory keysWithValue) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -115,6 +137,17 @@ library KeysWithValue { } } + /** Get the length of keysWithValue */ + function _length(bytes32 _tableId, bytes32 valueHash) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 32; + } + } + /** Get the length of keysWithValue (using the specified store) */ function length(IStore _store, bytes32 _tableId, bytes32 valueHash) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -147,6 +180,27 @@ library KeysWithValue { } } + /** + * Get an item of keysWithValue + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(bytes32 _tableId, bytes32 valueHash, uint256 _index) internal view returns (bytes32) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 32, + (_index + 1) * 32 + ); + return (bytes32(_blob)); + } + } + /** * Get an item of keysWithValue (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -169,6 +223,14 @@ library KeysWithValue { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to keysWithValue */ + function _push(bytes32 _tableId, bytes32 valueHash, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to keysWithValue (using the specified store) */ function push(IStore _store, bytes32 _tableId, bytes32 valueHash, bytes32 _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -185,6 +247,14 @@ library KeysWithValue { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); } + /** Pop an element from keysWithValue */ + function _pop(bytes32 _tableId, bytes32 valueHash) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 32, _fieldLayout); + } + /** Pop an element from keysWithValue (using the specified store) */ function pop(IStore _store, bytes32 _tableId, bytes32 valueHash) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -206,6 +276,19 @@ library KeysWithValue { } } + /** + * Update an element of keysWithValue at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(bytes32 _tableId, bytes32 valueHash, uint256 _index, bytes32 _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of keysWithValue (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -257,6 +340,14 @@ library KeysWithValue { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 _tableId, bytes32 valueHash) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = valueHash; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId, bytes32 valueHash) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol b/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol index 54d2f15682..f36c9e45b5 100644 --- a/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol +++ b/packages/world/src/modules/std-delegations/tables/CallboundDelegations.sol @@ -70,6 +70,11 @@ library CallboundDelegations { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -92,6 +97,23 @@ library CallboundDelegations { return (uint256(bytes32(_blob))); } + /** Get availableCalls */ + function _get( + address delegator, + address delegatee, + bytes32 resourceSelector, + bytes32 funcSelectorAndArgsHash + ) internal view returns (uint256 availableCalls) { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + _keyTuple[2] = resourceSelector; + _keyTuple[3] = funcSelectorAndArgsHash; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + /** Get availableCalls (using the specified store) */ function get( IStore _store, @@ -127,6 +149,23 @@ library CallboundDelegations { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); } + /** Set availableCalls */ + function _set( + address delegator, + address delegatee, + bytes32 resourceSelector, + bytes32 funcSelectorAndArgsHash, + uint256 availableCalls + ) internal { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + _keyTuple[2] = resourceSelector; + _keyTuple[3] = funcSelectorAndArgsHash; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((availableCalls)), _fieldLayout); + } + /** Set availableCalls (using the specified store) */ function set( IStore _store, @@ -192,6 +231,22 @@ library CallboundDelegations { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord( + address delegator, + address delegatee, + bytes32 resourceSelector, + bytes32 funcSelectorAndArgsHash + ) internal { + bytes32[] memory _keyTuple = new bytes32[](4); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + _keyTuple[2] = resourceSelector; + _keyTuple[3] = funcSelectorAndArgsHash; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord( IStore _store, diff --git a/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol b/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol index f751ad7f14..30a11520f3 100644 --- a/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol +++ b/packages/world/src/modules/std-delegations/tables/TimeboundDelegations.sol @@ -66,6 +66,11 @@ library TimeboundDelegations { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -81,6 +86,16 @@ library TimeboundDelegations { return (uint256(bytes32(_blob))); } + /** Get maxTimestamp */ + function _get(address delegator, address delegatee) internal view returns (uint256 maxTimestamp) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + /** Get maxTimestamp (using the specified store) */ function get(IStore _store, address delegator, address delegatee) internal view returns (uint256 maxTimestamp) { bytes32[] memory _keyTuple = new bytes32[](2); @@ -100,6 +115,15 @@ library TimeboundDelegations { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((maxTimestamp)), _fieldLayout); } + /** Set maxTimestamp */ + function _set(address delegator, address delegatee, uint256 maxTimestamp) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((maxTimestamp)), _fieldLayout); + } + /** Set maxTimestamp (using the specified store) */ function set(IStore _store, address delegator, address delegatee, uint256 maxTimestamp) internal { bytes32[] memory _keyTuple = new bytes32[](2); @@ -142,6 +166,15 @@ library TimeboundDelegations { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(address delegator, address delegatee) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, address delegator, address delegatee) internal { bytes32[] memory _keyTuple = new bytes32[](2); diff --git a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol index a47b78e150..7c0fc191e7 100644 --- a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol +++ b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol @@ -59,6 +59,11 @@ library UniqueEntity { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register(bytes32 _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -72,6 +77,14 @@ library UniqueEntity { return (uint256(bytes32(_blob))); } + /** Get value */ + function _get(bytes32 _tableId) internal view returns (uint256 value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 _tableId) internal view returns (uint256 value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -87,6 +100,13 @@ library UniqueEntity { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 _tableId, uint256 value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -123,6 +143,13 @@ library UniqueEntity { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 _tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/packages/world/src/tables/Delegations.sol b/packages/world/src/tables/Delegations.sol index 2e0378a220..4226caa189 100644 --- a/packages/world/src/tables/Delegations.sol +++ b/packages/world/src/tables/Delegations.sol @@ -66,6 +66,11 @@ library Delegations { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -81,6 +86,16 @@ library Delegations { return (bytes32(_blob)); } + /** Get delegationControlId */ + function _get(address delegator, address delegatee) internal view returns (bytes32 delegationControlId) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (bytes32(_blob)); + } + /** Get delegationControlId (using the specified store) */ function get( IStore _store, @@ -104,6 +119,15 @@ library Delegations { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); } + /** Set delegationControlId */ + function _set(address delegator, address delegatee, bytes32 delegationControlId) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((delegationControlId)), _fieldLayout); + } + /** Set delegationControlId (using the specified store) */ function set(IStore _store, address delegator, address delegatee, bytes32 delegationControlId) internal { bytes32[] memory _keyTuple = new bytes32[](2); @@ -146,6 +170,15 @@ library Delegations { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(address delegator, address delegatee) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(uint256(uint160(delegator))); + _keyTuple[1] = bytes32(uint256(uint160(delegatee))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, address delegator, address delegatee) internal { bytes32[] memory _keyTuple = new bytes32[](2); diff --git a/packages/world/src/tables/InstalledModules.sol b/packages/world/src/tables/InstalledModules.sol index b89b19bb88..530489f7a8 100644 --- a/packages/world/src/tables/InstalledModules.sol +++ b/packages/world/src/tables/InstalledModules.sol @@ -66,6 +66,11 @@ library InstalledModules { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -81,6 +86,16 @@ library InstalledModules { return (address(bytes20(_blob))); } + /** Get moduleAddress */ + function _get(bytes16 moduleName, bytes32 argumentsHash) internal view returns (address moduleAddress) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(moduleName); + _keyTuple[1] = argumentsHash; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (address(bytes20(_blob))); + } + /** Get moduleAddress (using the specified store) */ function get(IStore _store, bytes16 moduleName, bytes32 argumentsHash) internal view returns (address moduleAddress) { bytes32[] memory _keyTuple = new bytes32[](2); @@ -100,6 +115,15 @@ library InstalledModules { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), _fieldLayout); } + /** Set moduleAddress */ + function _set(bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(moduleName); + _keyTuple[1] = argumentsHash; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), _fieldLayout); + } + /** Set moduleAddress (using the specified store) */ function set(IStore _store, bytes16 moduleName, bytes32 argumentsHash, address moduleAddress) internal { bytes32[] memory _keyTuple = new bytes32[](2); @@ -142,6 +166,15 @@ library InstalledModules { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes16 moduleName, bytes32 argumentsHash) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = bytes32(moduleName); + _keyTuple[1] = argumentsHash; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes16 moduleName, bytes32 argumentsHash) internal { bytes32[] memory _keyTuple = new bytes32[](2); diff --git a/packages/world/src/tables/NamespaceOwner.sol b/packages/world/src/tables/NamespaceOwner.sol index 10f34300f2..5b7946f7a9 100644 --- a/packages/world/src/tables/NamespaceOwner.sol +++ b/packages/world/src/tables/NamespaceOwner.sol @@ -64,6 +64,11 @@ library NamespaceOwner { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -78,6 +83,15 @@ library NamespaceOwner { return (address(bytes20(_blob))); } + /** Get owner */ + function _get(bytes16 namespace) internal view returns (address owner) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(namespace); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (address(bytes20(_blob))); + } + /** Get owner (using the specified store) */ function get(IStore _store, bytes16 namespace) internal view returns (address owner) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -95,6 +109,14 @@ library NamespaceOwner { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); } + /** Set owner */ + function _set(bytes16 namespace, address owner) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(namespace); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), _fieldLayout); + } + /** Set owner (using the specified store) */ function set(IStore _store, bytes16 namespace, address owner) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -134,6 +156,14 @@ library NamespaceOwner { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes16 namespace) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(namespace); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes16 namespace) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/src/tables/ResourceAccess.sol b/packages/world/src/tables/ResourceAccess.sol index 56ce7e7616..9d591d49ef 100644 --- a/packages/world/src/tables/ResourceAccess.sol +++ b/packages/world/src/tables/ResourceAccess.sol @@ -66,6 +66,11 @@ library ResourceAccess { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -81,6 +86,16 @@ library ResourceAccess { return (_toBool(uint8(bytes1(_blob)))); } + /** Get access */ + function _get(bytes32 resourceSelector, address caller) internal view returns (bool access) { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = resourceSelector; + _keyTuple[1] = bytes32(uint256(uint160(caller))); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get access (using the specified store) */ function get(IStore _store, bytes32 resourceSelector, address caller) internal view returns (bool access) { bytes32[] memory _keyTuple = new bytes32[](2); @@ -100,6 +115,15 @@ library ResourceAccess { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); } + /** Set access */ + function _set(bytes32 resourceSelector, address caller, bool access) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = resourceSelector; + _keyTuple[1] = bytes32(uint256(uint160(caller))); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), _fieldLayout); + } + /** Set access (using the specified store) */ function set(IStore _store, bytes32 resourceSelector, address caller, bool access) internal { bytes32[] memory _keyTuple = new bytes32[](2); @@ -142,6 +166,15 @@ library ResourceAccess { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 resourceSelector, address caller) internal { + bytes32[] memory _keyTuple = new bytes32[](2); + _keyTuple[0] = resourceSelector; + _keyTuple[1] = bytes32(uint256(uint160(caller))); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 resourceSelector, address caller) internal { bytes32[] memory _keyTuple = new bytes32[](2); diff --git a/packages/world/test/tables/AddressArray.sol b/packages/world/test/tables/AddressArray.sol index 32a0e2131e..e2b29648b8 100644 --- a/packages/world/test/tables/AddressArray.sol +++ b/packages/world/test/tables/AddressArray.sol @@ -61,6 +61,11 @@ library AddressArray { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register(bytes32 _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,15 @@ library AddressArray { return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } + /** Get value */ + function _get(bytes32 _tableId, bytes32 key) internal view returns (address[] memory value) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); + return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 _tableId, bytes32 key) internal view returns (address[] memory value) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -92,6 +106,14 @@ library AddressArray { StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 _tableId, bytes32 key, address[] memory value) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, bytes32 key, address[] memory value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -111,6 +133,17 @@ library AddressArray { } } + /** Get the length of value */ + function _length(bytes32 _tableId, bytes32 key) internal view returns (uint256) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + uint256 _byteLength = StoreCore.getFieldLength(_tableId, _keyTuple, 0, _fieldLayout); + unchecked { + return _byteLength / 20; + } + } + /** Get the length of value (using the specified store) */ function length(IStore _store, bytes32 _tableId, bytes32 key) internal view returns (uint256) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -143,6 +176,27 @@ library AddressArray { } } + /** + * Get an item of value + * (unchecked, returns invalid data if index overflows) + */ + function _getItem(bytes32 _tableId, bytes32 key, uint256 _index) internal view returns (address) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + bytes memory _blob = StoreCore.getFieldSlice( + _tableId, + _keyTuple, + 0, + _fieldLayout, + _index * 20, + (_index + 1) * 20 + ); + return (address(bytes20(_blob))); + } + } + /** * Get an item of value (using the specified store) * (unchecked, returns invalid data if index overflows) @@ -165,6 +219,14 @@ library AddressArray { StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); } + /** Push an element to value */ + function _push(bytes32 _tableId, bytes32 key, address _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), _fieldLayout); + } + /** Push an element to value (using the specified store) */ function push(IStore _store, bytes32 _tableId, bytes32 key, address _element) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -181,6 +243,14 @@ library AddressArray { StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, _fieldLayout); } + /** Pop an element from value */ + function _pop(bytes32 _tableId, bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.popFromField(_tableId, _keyTuple, 0, 20, _fieldLayout); + } + /** Pop an element from value (using the specified store) */ function pop(IStore _store, bytes32 _tableId, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -202,6 +272,19 @@ library AddressArray { } } + /** + * Update an element of value at `_index` + * (checked only to prevent modifying other tables; can corrupt own data if index overflows) + */ + function _update(bytes32 _tableId, bytes32 key, uint256 _index, address _element) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + unchecked { + StoreCore.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), _fieldLayout); + } + } + /** * Update an element of value (using the specified store) at `_index` * (checked only to prevent modifying other tables; can corrupt own data if index overflows) @@ -253,6 +336,14 @@ library AddressArray { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 _tableId, bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/packages/world/test/tables/Bool.sol b/packages/world/test/tables/Bool.sol index 87b9f6e209..6605ccc924 100644 --- a/packages/world/test/tables/Bool.sol +++ b/packages/world/test/tables/Bool.sol @@ -59,6 +59,11 @@ library Bool { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register(bytes32 _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store, bytes32 _tableId) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -72,6 +77,14 @@ library Bool { return (_toBool(uint8(bytes1(_blob)))); } + /** Get value */ + function _get(bytes32 _tableId) internal view returns (bool value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (_toBool(uint8(bytes1(_blob)))); + } + /** Get value (using the specified store) */ function get(IStore _store, bytes32 _tableId) internal view returns (bool value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -87,6 +100,13 @@ library Bool { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(bytes32 _tableId, bool value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -123,6 +143,13 @@ library Bool { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 _tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol index 77ca8cd102..24ba4c5d0f 100644 --- a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol @@ -62,6 +62,11 @@ library Counter { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,14 @@ library Counter { return (uint32(bytes4(_blob))); } + /** Get value */ + function _get() internal view returns (uint32 value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get value (using the specified store) */ function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -90,6 +103,13 @@ library Counter { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(uint32 value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -126,6 +146,13 @@ library Counter { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/templates/react/packages/contracts/src/codegen/tables/Counter.sol b/templates/react/packages/contracts/src/codegen/tables/Counter.sol index 77ca8cd102..24ba4c5d0f 100644 --- a/templates/react/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/react/packages/contracts/src/codegen/tables/Counter.sol @@ -62,6 +62,11 @@ library Counter { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,14 @@ library Counter { return (uint32(bytes4(_blob))); } + /** Get value */ + function _get() internal view returns (uint32 value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get value (using the specified store) */ function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -90,6 +103,13 @@ library Counter { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(uint32 value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -126,6 +146,13 @@ library Counter { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0); diff --git a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol index f757dc4c9f..1ea72f6f0b 100644 --- a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol +++ b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol @@ -74,6 +74,11 @@ library Position { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -88,6 +93,15 @@ library Position { return (int32(uint32(bytes4(_blob)))); } + /** Get x */ + function _getX(bytes32 key) internal view returns (int32 x) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (int32(uint32(bytes4(_blob)))); + } + /** Get x (using the specified store) */ function getX(IStore _store, bytes32 key) internal view returns (int32 x) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -105,6 +119,14 @@ library Position { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); } + /** Set x */ + function _setX(bytes32 key, int32 x) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout); + } + /** Set x (using the specified store) */ function setX(IStore _store, bytes32 key, int32 x) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -122,6 +144,15 @@ library Position { return (int32(uint32(bytes4(_blob)))); } + /** Get y */ + function _getY(bytes32 key) internal view returns (int32 y) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (int32(uint32(bytes4(_blob)))); + } + /** Get y (using the specified store) */ function getY(IStore _store, bytes32 key) internal view returns (int32 y) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -139,6 +170,14 @@ library Position { StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); } + /** Set y */ + function _setY(bytes32 key, int32 y) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout); + } + /** Set y (using the specified store) */ function setY(IStore _store, bytes32 key, int32 y) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -156,6 +195,15 @@ library Position { return (int32(uint32(bytes4(_blob)))); } + /** Get z */ + function _getZ(bytes32 key) internal view returns (int32 z) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); + return (int32(uint32(bytes4(_blob)))); + } + /** Get z (using the specified store) */ function getZ(IStore _store, bytes32 key) internal view returns (int32 z) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -173,6 +221,14 @@ library Position { StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), _fieldLayout); } + /** Set z */ + function _setZ(bytes32 key, int32 z) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setField(_tableId, _keyTuple, 2, abi.encodePacked((z)), _fieldLayout); + } + /** Set z (using the specified store) */ function setZ(IStore _store, bytes32 key, int32 z) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -190,6 +246,15 @@ library Position { return decode(_blob); } + /** Get the full data */ + function _get(bytes32 key) internal view returns (PositionData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + bytes memory _blob = StoreCore.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_blob); + } + /** Get the full data (using the specified store) */ function get(IStore _store, bytes32 key) internal view returns (PositionData memory _table) { bytes32[] memory _keyTuple = new bytes32[](1); @@ -212,6 +277,19 @@ library Position { StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); } + /** Set the full data using individual values */ + function _set(bytes32 key, int32 x, int32 y, int32 z) internal { + bytes memory _staticData = encodeStatic(x, y, z); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + /** Set the full data using individual values (using the specified store) */ function set(IStore _store, bytes32 key, int32 x, int32 y, int32 z) internal { bytes memory _staticData = encodeStatic(x, y, z); @@ -230,6 +308,11 @@ library Position { set(key, _table.x, _table.y, _table.z); } + /** Set the full data using the data struct */ + function _set(bytes32 key, PositionData memory _table) internal { + set(key, _table.x, _table.y, _table.z); + } + /** Set the full data using the data struct (using the specified store) */ function set(IStore _store, bytes32 key, PositionData memory _table) internal { set(_store, key, _table.x, _table.y, _table.z); @@ -275,6 +358,14 @@ library Position { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord(bytes32 key) internal { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = key; + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 key) internal { bytes32[] memory _keyTuple = new bytes32[](1); diff --git a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol index 77ca8cd102..24ba4c5d0f 100644 --- a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol @@ -62,6 +62,11 @@ library Counter { StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } + /** Register the table with its config */ + function _register() internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + /** Register the table with its config (using the specified store) */ function register(IStore _store) internal { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); @@ -75,6 +80,14 @@ library Counter { return (uint32(bytes4(_blob))); } + /** Get value */ + function _get() internal view returns (uint32 value) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint32(bytes4(_blob))); + } + /** Get value (using the specified store) */ function get(IStore _store) internal view returns (uint32 value) { bytes32[] memory _keyTuple = new bytes32[](0); @@ -90,6 +103,13 @@ library Counter { StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } + /** Set value */ + function _set(uint32 value) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); + } + /** Set value (using the specified store) */ function set(IStore _store, uint32 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); @@ -126,6 +146,13 @@ library Counter { StoreSwitch.deleteRecord(_tableId, _keyTuple, _fieldLayout); } + /* Delete all data for given keys */ + function _deleteRecord() internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); + } + /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store) internal { bytes32[] memory _keyTuple = new bytes32[](0);