From 57a45b5e58cfc9e69b960d23f9563fcf25fee92d Mon Sep 17 00:00:00 2001 From: Tamara Ringas <69479754+TamaraRingas@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:07:04 +0200 Subject: [PATCH] Contract skeletons --- .../src/ERC20.sol | 22 +- .../src/codegen/index.sol | 8 - .../src/codegen/tables/Allowances.sol | 211 ------ .../src/codegen/tables/Balances.sol | 199 ----- .../src/codegen/tables/Token.sol | 693 ------------------ .../test/ERC20.t.sol | 181 +---- 6 files changed, 13 insertions(+), 1301 deletions(-) delete mode 100644 packages/world-module-erc20-external-store/src/codegen/index.sol delete mode 100644 packages/world-module-erc20-external-store/src/codegen/tables/Allowances.sol delete mode 100644 packages/world-module-erc20-external-store/src/codegen/tables/Balances.sol delete mode 100644 packages/world-module-erc20-external-store/src/codegen/tables/Token.sol diff --git a/packages/world-module-erc20-external-store/src/ERC20.sol b/packages/world-module-erc20-external-store/src/ERC20.sol index 3f444e2cec..4da990ca15 100644 --- a/packages/world-module-erc20-external-store/src/ERC20.sol +++ b/packages/world-module-erc20-external-store/src/ERC20.sol @@ -3,9 +3,9 @@ pragma solidity >=0.8.24; import { IERC20Errors } from "./IERC20Errors.sol"; import { IERC20Events } from "./IERC20Events.sol"; -import { Token } from "./codegen/tables/Token.sol"; -import { Balances } from "./codegen/tables/Balances.sol"; -import { Allowances } from "./codegen/tables/Allowances.sol"; +//import { Token } from "./codegen/tables/Token.sol"; +//import { Balances } from "./codegen/tables/Balances.sol"; +//import { Allowances } from "./codegen/tables/Allowances.sol"; import { Store } from "@latticexyz/store/src/Store.sol"; import { Schema } from "@latticexyz/store/src/Schema.sol"; @@ -16,19 +16,17 @@ import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol"; import { EncodedLengths } from "@latticexyz/store/src/EncodedLengths.sol"; /** - * @title ERC20 Module with own Store + * @title ERC20 Module with external Store * @author MUD (https://mud.dev) by Lattice (https://lattice.xyz) * @dev Implementation of EIP-20 that has on instance of `Store` which enables built in indexing and storage packing. */ -contract ERC20 is Store, IERC20Errors, IERC20Events { +contract ERC20 is IERC20Errors, IERC20Events { constructor(string memory _name, string memory _symbol, address _owner, uint8 _decimals) { - StoreCore.registerInternalTables(); - - Token.register(); - Balances.register(); - Allowances.register(); - - Token.set(_decimals, 0, _owner, _name, _symbol); + // StoreCore.registerInternalTables(); + // Token.register(); + // Balances.register(); + // Allowances.register(); + // Token.set(_decimals, 0, _owner, _name, _symbol); } /** diff --git a/packages/world-module-erc20-external-store/src/codegen/index.sol b/packages/world-module-erc20-external-store/src/codegen/index.sol deleted file mode 100644 index 26ea531e76..0000000000 --- a/packages/world-module-erc20-external-store/src/codegen/index.sol +++ /dev/null @@ -1,8 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.24; - -/* Autogenerated file. Do not edit manually. */ - -import { Token, TokenData } from "./tables/Token.sol"; -import { Balances } from "./tables/Balances.sol"; -import { Allowances } from "./tables/Allowances.sol"; diff --git a/packages/world-module-erc20-external-store/src/codegen/tables/Allowances.sol b/packages/world-module-erc20-external-store/src/codegen/tables/Allowances.sol deleted file mode 100644 index 463132cd2d..0000000000 --- a/packages/world-module-erc20-external-store/src/codegen/tables/Allowances.sol +++ /dev/null @@ -1,211 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.24; - -/* Autogenerated file. Do not edit manually. */ - -// Import store internals -import { IStore } from "@latticexyz/store/src/IStore.sol"; -import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; -import { StoreCore } from "@latticexyz/store/src/StoreCore.sol"; -import { Bytes } from "@latticexyz/store/src/Bytes.sol"; -import { Memory } from "@latticexyz/store/src/Memory.sol"; -import { SliceLib } from "@latticexyz/store/src/Slice.sol"; -import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; -import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol"; -import { Schema } from "@latticexyz/store/src/Schema.sol"; -import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/EncodedLengths.sol"; -import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; - -library Allowances { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "erc20-store", name: "Allowances", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x746265726332302d73746f7265000000416c6c6f77616e636573000000000000); - - FieldLayout constant _fieldLayout = - FieldLayout.wrap(0x0020010020000000000000000000000000000000000000000000000000000000); - - // Hex-encoded key schema of (address, address) - Schema constant _keySchema = Schema.wrap(0x0028020061610000000000000000000000000000000000000000000000000000); - // Hex-encoded value schema of (uint256) - Schema constant _valueSchema = Schema.wrap(0x002001001f000000000000000000000000000000000000000000000000000000); - - /** - * @notice Get the table's key field names. - * @return keyNames An array of strings with the names of key fields. - */ - function getKeyNames() internal pure returns (string[] memory keyNames) { - keyNames = new string[](2); - keyNames[0] = "account"; - keyNames[1] = "spender"; - } - - /** - * @notice Get the table's value field names. - * @return fieldNames An array of strings with the names of value fields. - */ - function getFieldNames() internal pure returns (string[] memory fieldNames) { - fieldNames = new string[](1); - fieldNames[0] = "approval"; - } - - /** - * @notice Register the table with its config. - */ - function register() internal { - StoreSwitch.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames()); - } - - /** - * @notice Register the table with its config. - */ - function _register() internal { - StoreCore.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames()); - } - - /** - * @notice Get approval. - */ - function getApproval(address account, address spender) internal view returns (uint256 approval) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get approval. - */ - function _getApproval(address account, address spender) internal view returns (uint256 approval) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get approval. - */ - function get(address account, address spender) internal view returns (uint256 approval) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get approval. - */ - function _get(address account, address spender) internal view returns (uint256 approval) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Set approval. - */ - function setApproval(address account, address spender, uint256 approval) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((approval)), _fieldLayout); - } - - /** - * @notice Set approval. - */ - function _setApproval(address account, address spender, uint256 approval) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((approval)), _fieldLayout); - } - - /** - * @notice Set approval. - */ - function set(address account, address spender, uint256 approval) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((approval)), _fieldLayout); - } - - /** - * @notice Set approval. - */ - function _set(address account, address spender, uint256 approval) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((approval)), _fieldLayout); - } - - /** - * @notice Delete all data for given keys. - */ - function deleteRecord(address account, address spender) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - StoreSwitch.deleteRecord(_tableId, _keyTuple); - } - - /** - * @notice Delete all data for given keys. - */ - function _deleteRecord(address account, address spender) internal { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); - } - - /** - * @notice Tightly pack static (fixed length) data using this table's schema. - * @return The static data, encoded into a sequence of bytes. - */ - function encodeStatic(uint256 approval) internal pure returns (bytes memory) { - return abi.encodePacked(approval); - } - - /** - * @notice Encode all of a record's fields. - * @return The static (fixed length) data, encoded into a sequence of bytes. - * @return The lengths of the dynamic fields (packed into a single bytes32 value). - * @return The dynamic (variable length) data, encoded into a sequence of bytes. - */ - function encode(uint256 approval) internal pure returns (bytes memory, EncodedLengths, bytes memory) { - bytes memory _staticData = encodeStatic(approval); - - EncodedLengths _encodedLengths; - bytes memory _dynamicData; - - return (_staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Encode keys as a bytes32 array using this table's field layout. - */ - function encodeKeyTuple(address account, address spender) internal pure returns (bytes32[] memory) { - bytes32[] memory _keyTuple = new bytes32[](2); - _keyTuple[0] = bytes32(uint256(uint160(account))); - _keyTuple[1] = bytes32(uint256(uint160(spender))); - - return _keyTuple; - } -} diff --git a/packages/world-module-erc20-external-store/src/codegen/tables/Balances.sol b/packages/world-module-erc20-external-store/src/codegen/tables/Balances.sol deleted file mode 100644 index 839a3573d6..0000000000 --- a/packages/world-module-erc20-external-store/src/codegen/tables/Balances.sol +++ /dev/null @@ -1,199 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.24; - -/* Autogenerated file. Do not edit manually. */ - -// Import store internals -import { IStore } from "@latticexyz/store/src/IStore.sol"; -import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; -import { StoreCore } from "@latticexyz/store/src/StoreCore.sol"; -import { Bytes } from "@latticexyz/store/src/Bytes.sol"; -import { Memory } from "@latticexyz/store/src/Memory.sol"; -import { SliceLib } from "@latticexyz/store/src/Slice.sol"; -import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; -import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol"; -import { Schema } from "@latticexyz/store/src/Schema.sol"; -import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/EncodedLengths.sol"; -import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; - -library Balances { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "erc20-store", name: "Balances", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x746265726332302d73746f726500000042616c616e6365730000000000000000); - - FieldLayout constant _fieldLayout = - FieldLayout.wrap(0x0020010020000000000000000000000000000000000000000000000000000000); - - // Hex-encoded key schema of (address) - Schema constant _keySchema = Schema.wrap(0x0014010061000000000000000000000000000000000000000000000000000000); - // Hex-encoded value schema of (uint256) - Schema constant _valueSchema = Schema.wrap(0x002001001f000000000000000000000000000000000000000000000000000000); - - /** - * @notice Get the table's key field names. - * @return keyNames An array of strings with the names of key fields. - */ - function getKeyNames() internal pure returns (string[] memory keyNames) { - keyNames = new string[](1); - keyNames[0] = "account"; - } - - /** - * @notice Get the table's value field names. - * @return fieldNames An array of strings with the names of value fields. - */ - function getFieldNames() internal pure returns (string[] memory fieldNames) { - fieldNames = new string[](1); - fieldNames[0] = "balance"; - } - - /** - * @notice Register the table with its config. - */ - function register() internal { - StoreSwitch.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames()); - } - - /** - * @notice Register the table with its config. - */ - function _register() internal { - StoreCore.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames()); - } - - /** - * @notice Get balance. - */ - function getBalance(address account) internal view returns (uint256 balance) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get balance. - */ - function _getBalance(address account) internal view returns (uint256 balance) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get balance. - */ - function get(address account) internal view returns (uint256 balance) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get balance. - */ - function _get(address account) internal view returns (uint256 balance) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Set balance. - */ - function setBalance(address account, uint256 balance) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); - } - - /** - * @notice Set balance. - */ - function _setBalance(address account, uint256 balance) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); - } - - /** - * @notice Set balance. - */ - function set(address account, uint256 balance) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); - } - - /** - * @notice Set balance. - */ - function _set(address account, uint256 balance) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((balance)), _fieldLayout); - } - - /** - * @notice Delete all data for given keys. - */ - function deleteRecord(address account) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - StoreSwitch.deleteRecord(_tableId, _keyTuple); - } - - /** - * @notice Delete all data for given keys. - */ - function _deleteRecord(address account) internal { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); - } - - /** - * @notice Tightly pack static (fixed length) data using this table's schema. - * @return The static data, encoded into a sequence of bytes. - */ - function encodeStatic(uint256 balance) internal pure returns (bytes memory) { - return abi.encodePacked(balance); - } - - /** - * @notice Encode all of a record's fields. - * @return The static (fixed length) data, encoded into a sequence of bytes. - * @return The lengths of the dynamic fields (packed into a single bytes32 value). - * @return The dynamic (variable length) data, encoded into a sequence of bytes. - */ - function encode(uint256 balance) internal pure returns (bytes memory, EncodedLengths, bytes memory) { - bytes memory _staticData = encodeStatic(balance); - - EncodedLengths _encodedLengths; - bytes memory _dynamicData; - - return (_staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Encode keys as a bytes32 array using this table's field layout. - */ - function encodeKeyTuple(address account) internal pure returns (bytes32[] memory) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = bytes32(uint256(uint160(account))); - - return _keyTuple; - } -} diff --git a/packages/world-module-erc20-external-store/src/codegen/tables/Token.sol b/packages/world-module-erc20-external-store/src/codegen/tables/Token.sol deleted file mode 100644 index 6ea7921229..0000000000 --- a/packages/world-module-erc20-external-store/src/codegen/tables/Token.sol +++ /dev/null @@ -1,693 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.24; - -/* Autogenerated file. Do not edit manually. */ - -// Import store internals -import { IStore } from "@latticexyz/store/src/IStore.sol"; -import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; -import { StoreCore } from "@latticexyz/store/src/StoreCore.sol"; -import { Bytes } from "@latticexyz/store/src/Bytes.sol"; -import { Memory } from "@latticexyz/store/src/Memory.sol"; -import { SliceLib } from "@latticexyz/store/src/Slice.sol"; -import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; -import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol"; -import { Schema } from "@latticexyz/store/src/Schema.sol"; -import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/EncodedLengths.sol"; -import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; - -struct TokenData { - uint8 decimals; - uint256 totalSupply; - address owner; - string name; - string symbol; -} - -library Token { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "erc20-store", name: "Token", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x746265726332302d73746f7265000000546f6b656e0000000000000000000000); - - FieldLayout constant _fieldLayout = - FieldLayout.wrap(0x0035030201201400000000000000000000000000000000000000000000000000); - - // Hex-encoded key schema of () - Schema constant _keySchema = Schema.wrap(0x0000000000000000000000000000000000000000000000000000000000000000); - // Hex-encoded value schema of (uint8, uint256, address, string, string) - Schema constant _valueSchema = Schema.wrap(0x00350302001f61c5c50000000000000000000000000000000000000000000000); - - /** - * @notice Get the table's key field names. - * @return keyNames An array of strings with the names of key fields. - */ - function getKeyNames() internal pure returns (string[] memory keyNames) { - keyNames = new string[](0); - } - - /** - * @notice Get the table's value field names. - * @return fieldNames An array of strings with the names of value fields. - */ - function getFieldNames() internal pure returns (string[] memory fieldNames) { - fieldNames = new string[](5); - fieldNames[0] = "decimals"; - fieldNames[1] = "totalSupply"; - fieldNames[2] = "owner"; - fieldNames[3] = "name"; - fieldNames[4] = "symbol"; - } - - /** - * @notice Register the table with its config. - */ - function register() internal { - StoreSwitch.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames()); - } - - /** - * @notice Register the table with its config. - */ - function _register() internal { - StoreCore.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames()); - } - - /** - * @notice Get decimals. - */ - function getDecimals() internal view returns (uint8 decimals) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint8(bytes1(_blob))); - } - - /** - * @notice Get decimals. - */ - function _getDecimals() internal view returns (uint8 decimals) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint8(bytes1(_blob))); - } - - /** - * @notice Set decimals. - */ - function setDecimals(uint8 decimals) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout); - } - - /** - * @notice Set decimals. - */ - function _setDecimals(uint8 decimals) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout); - } - - /** - * @notice Get totalSupply. - */ - function getTotalSupply() internal view returns (uint256 totalSupply) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get totalSupply. - */ - function _getTotalSupply() internal view returns (uint256 totalSupply) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Set totalSupply. - */ - function setTotalSupply(uint256 totalSupply) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((totalSupply)), _fieldLayout); - } - - /** - * @notice Set totalSupply. - */ - function _setTotalSupply(uint256 totalSupply) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((totalSupply)), _fieldLayout); - } - - /** - * @notice Get owner. - */ - function getOwner() internal view returns (address owner) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); - return (address(bytes20(_blob))); - } - - /** - * @notice Get owner. - */ - function _getOwner() internal view returns (address owner) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 2, _fieldLayout); - return (address(bytes20(_blob))); - } - - /** - * @notice Set owner. - */ - function setOwner(address owner) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked((owner)), _fieldLayout); - } - - /** - * @notice Set owner. - */ - function _setOwner(address owner) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setStaticField(_tableId, _keyTuple, 2, abi.encodePacked((owner)), _fieldLayout); - } - - /** - * @notice Get name. - */ - function getName() internal view returns (string memory name) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0); - return (string(_blob)); - } - - /** - * @notice Get name. - */ - function _getName() internal view returns (string memory name) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0); - return (string(_blob)); - } - - /** - * @notice Set name. - */ - function setName(string memory name) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, bytes((name))); - } - - /** - * @notice Set name. - */ - function _setName(string memory name) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setDynamicField(_tableId, _keyTuple, 0, bytes((name))); - } - - /** - * @notice Get the length of name. - */ - function lengthName() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 1; - } - } - - /** - * @notice Get the length of name. - */ - function _lengthName() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0); - unchecked { - return _byteLength / 1; - } - } - - /** - * @notice Get an item of name. - * @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array. - */ - function getItemName(uint256 _index) internal view returns (string memory) { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1); - return (string(_blob)); - } - } - - /** - * @notice Get an item of name. - * @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array. - */ - function _getItemName(uint256 _index) internal view returns (string memory) { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1); - return (string(_blob)); - } - } - - /** - * @notice Push a slice to name. - */ - function pushName(string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice))); - } - - /** - * @notice Push a slice to name. - */ - function _pushName(string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice))); - } - - /** - * @notice Pop a slice from name. - */ - function popName() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 1); - } - - /** - * @notice Pop a slice from name. - */ - function _popName() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 1); - } - - /** - * @notice Update a slice of name at `_index`. - */ - function updateName(uint256 _index, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _encoded = bytes((_slice)); - StoreSwitch.spliceDynamicData(_tableId, _keyTuple, 0, uint40(_index * 1), uint40(_encoded.length), _encoded); - } - } - - /** - * @notice Update a slice of name at `_index`. - */ - function _updateName(uint256 _index, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _encoded = bytes((_slice)); - StoreCore.spliceDynamicData(_tableId, _keyTuple, 0, uint40(_index * 1), uint40(_encoded.length), _encoded); - } - } - - /** - * @notice Get symbol. - */ - function getSymbol() internal view returns (string memory symbol) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 1); - return (string(_blob)); - } - - /** - * @notice Get symbol. - */ - function _getSymbol() internal view returns (string memory symbol) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1); - return (string(_blob)); - } - - /** - * @notice Set symbol. - */ - function setSymbol(string memory symbol) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setDynamicField(_tableId, _keyTuple, 1, bytes((symbol))); - } - - /** - * @notice Set symbol. - */ - function _setSymbol(string memory symbol) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setDynamicField(_tableId, _keyTuple, 1, bytes((symbol))); - } - - /** - * @notice Get the length of symbol. - */ - function lengthSymbol() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 1); - unchecked { - return _byteLength / 1; - } - } - - /** - * @notice Get the length of symbol. - */ - function _lengthSymbol() internal view returns (uint256) { - bytes32[] memory _keyTuple = new bytes32[](0); - - uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 1); - unchecked { - return _byteLength / 1; - } - } - - /** - * @notice Get an item of symbol. - * @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array. - */ - function getItemSymbol(uint256 _index) internal view returns (string memory) { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1); - return (string(_blob)); - } - } - - /** - * @notice Get an item of symbol. - * @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array. - */ - function _getItemSymbol(uint256 _index) internal view returns (string memory) { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1); - return (string(_blob)); - } - } - - /** - * @notice Push a slice to symbol. - */ - function pushSymbol(string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice))); - } - - /** - * @notice Push a slice to symbol. - */ - function _pushSymbol(string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice))); - } - - /** - * @notice Pop a slice from symbol. - */ - function popSymbol() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 1, 1); - } - - /** - * @notice Pop a slice from symbol. - */ - function _popSymbol() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, 1); - } - - /** - * @notice Update a slice of symbol at `_index`. - */ - function updateSymbol(uint256 _index, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _encoded = bytes((_slice)); - StoreSwitch.spliceDynamicData(_tableId, _keyTuple, 1, uint40(_index * 1), uint40(_encoded.length), _encoded); - } - } - - /** - * @notice Update a slice of symbol at `_index`. - */ - function _updateSymbol(uint256 _index, string memory _slice) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - unchecked { - bytes memory _encoded = bytes((_slice)); - StoreCore.spliceDynamicData(_tableId, _keyTuple, 1, uint40(_index * 1), uint40(_encoded.length), _encoded); - } - } - - /** - * @notice Get the full data. - */ - function get() internal view returns (TokenData memory _table) { - bytes32[] memory _keyTuple = new bytes32[](0); - - (bytes memory _staticData, EncodedLengths _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Get the full data. - */ - function _get() internal view returns (TokenData memory _table) { - bytes32[] memory _keyTuple = new bytes32[](0); - - (bytes memory _staticData, EncodedLengths _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Set the full data using individual values. - */ - function set(uint8 decimals, uint256 totalSupply, address owner, string memory name, string memory symbol) internal { - bytes memory _staticData = encodeStatic(decimals, totalSupply, owner); - - EncodedLengths _encodedLengths = encodeLengths(name, symbol); - bytes memory _dynamicData = encodeDynamic(name, symbol); - - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Set the full data using individual values. - */ - function _set(uint8 decimals, uint256 totalSupply, address owner, string memory name, string memory symbol) internal { - bytes memory _staticData = encodeStatic(decimals, totalSupply, owner); - - EncodedLengths _encodedLengths = encodeLengths(name, symbol); - bytes memory _dynamicData = encodeDynamic(name, symbol); - - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); - } - - /** - * @notice Set the full data using the data struct. - */ - function set(TokenData memory _table) internal { - bytes memory _staticData = encodeStatic(_table.decimals, _table.totalSupply, _table.owner); - - EncodedLengths _encodedLengths = encodeLengths(_table.name, _table.symbol); - bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol); - - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Set the full data using the data struct. - */ - function _set(TokenData memory _table) internal { - bytes memory _staticData = encodeStatic(_table.decimals, _table.totalSupply, _table.owner); - - EncodedLengths _encodedLengths = encodeLengths(_table.name, _table.symbol); - bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol); - - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); - } - - /** - * @notice Decode the tightly packed blob of static data using this table's field layout. - */ - function decodeStatic(bytes memory _blob) internal pure returns (uint8 decimals, uint256 totalSupply, address owner) { - decimals = (uint8(Bytes.getBytes1(_blob, 0))); - - totalSupply = (uint256(Bytes.getBytes32(_blob, 1))); - - owner = (address(Bytes.getBytes20(_blob, 33))); - } - - /** - * @notice Decode the tightly packed blob of dynamic data using the encoded lengths. - */ - function decodeDynamic( - EncodedLengths _encodedLengths, - bytes memory _blob - ) internal pure returns (string memory name, string memory symbol) { - uint256 _start; - uint256 _end; - unchecked { - _end = _encodedLengths.atIndex(0); - } - name = (string(SliceLib.getSubslice(_blob, _start, _end).toBytes())); - - _start = _end; - unchecked { - _end += _encodedLengths.atIndex(1); - } - symbol = (string(SliceLib.getSubslice(_blob, _start, _end).toBytes())); - } - - /** - * @notice Decode the tightly packed blobs using this table's field layout. - * @param _staticData Tightly packed static fields. - * @param _encodedLengths Encoded lengths of dynamic fields. - * @param _dynamicData Tightly packed dynamic fields. - */ - function decode( - bytes memory _staticData, - EncodedLengths _encodedLengths, - bytes memory _dynamicData - ) internal pure returns (TokenData memory _table) { - (_table.decimals, _table.totalSupply, _table.owner) = decodeStatic(_staticData); - - (_table.name, _table.symbol) = decodeDynamic(_encodedLengths, _dynamicData); - } - - /** - * @notice Delete all data for given keys. - */ - function deleteRecord() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.deleteRecord(_tableId, _keyTuple); - } - - /** - * @notice Delete all data for given keys. - */ - function _deleteRecord() internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout); - } - - /** - * @notice Tightly pack static (fixed length) data using this table's schema. - * @return The static data, encoded into a sequence of bytes. - */ - function encodeStatic(uint8 decimals, uint256 totalSupply, address owner) internal pure returns (bytes memory) { - return abi.encodePacked(decimals, totalSupply, owner); - } - - /** - * @notice Tightly pack dynamic data lengths using this table's schema. - * @return _encodedLengths The lengths of the dynamic fields (packed into a single bytes32 value). - */ - function encodeLengths( - string memory name, - string memory symbol - ) internal pure returns (EncodedLengths _encodedLengths) { - // Lengths are effectively checked during copy by 2**40 bytes exceeding gas limits - unchecked { - _encodedLengths = EncodedLengthsLib.pack(bytes(name).length, bytes(symbol).length); - } - } - - /** - * @notice Tightly pack dynamic (variable length) data using this table's schema. - * @return The dynamic data, encoded into a sequence of bytes. - */ - function encodeDynamic(string memory name, string memory symbol) internal pure returns (bytes memory) { - return abi.encodePacked(bytes((name)), bytes((symbol))); - } - - /** - * @notice Encode all of a record's fields. - * @return The static (fixed length) data, encoded into a sequence of bytes. - * @return The lengths of the dynamic fields (packed into a single bytes32 value). - * @return The dynamic (variable length) data, encoded into a sequence of bytes. - */ - function encode( - uint8 decimals, - uint256 totalSupply, - address owner, - string memory name, - string memory symbol - ) internal pure returns (bytes memory, EncodedLengths, bytes memory) { - bytes memory _staticData = encodeStatic(decimals, totalSupply, owner); - - EncodedLengths _encodedLengths = encodeLengths(name, symbol); - bytes memory _dynamicData = encodeDynamic(name, symbol); - - return (_staticData, _encodedLengths, _dynamicData); - } - - /** - * @notice Encode keys as a bytes32 array using this table's field layout. - */ - function encodeKeyTuple() internal pure returns (bytes32[] memory) { - bytes32[] memory _keyTuple = new bytes32[](0); - - return _keyTuple; - } -} diff --git a/packages/world-module-erc20-external-store/test/ERC20.t.sol b/packages/world-module-erc20-external-store/test/ERC20.t.sol index 5f8481362e..9b1ce07267 100644 --- a/packages/world-module-erc20-external-store/test/ERC20.t.sol +++ b/packages/world-module-erc20-external-store/test/ERC20.t.sol @@ -6,184 +6,9 @@ import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; import { ERC20 } from "../src/ERC20.sol"; -import { Token } from "../src/codegen/tables/Token.sol"; -import { Balances } from "../src/codegen/tables/Balances.sol"; -import { Allowances } from "../src/codegen/tables/Allowances.sol"; - +// import { Token } from "../src/codegen/tables/Token.sol"; +// import { Balances } from "../src/codegen/tables/Balances.sol"; +// import { Allowances } from "../src/codegen/tables/Allowances.sol"; contract tokenTest is Test, GasReporter { ERC20 token; - - address alice = address(0x123); - address charlie = address(0x456); - - function setUp() public { - startGasReport("token constructor"); - token = new ERC20("token", "MUD", address(this), 18); - endGasReport(); - StoreSwitch.setStoreAddress(address(token)); - } - - function testTokenSetUp() public { - assertTrue(address(token) != address(0)); - - assertEq(Token.getDecimals(), 18); - assertEq(Token.getTotalSupply(), 0); - assertEq(Token.getName(), "token"); - assertEq(Token.getSymbol(), "MUD"); - } - - function testTokenMint() public { - startGasReport("token mint"); - token.mint(address(this), 1000); - endGasReport(); - - assertEq(Balances.getBalance(address(this)), 1000); - assertEq(Token.getTotalSupply(), 1000); - } - - function testTokenMintInvalidCaller() public { - vm.expectRevert(); - vm.prank(alice); - startGasReport("token mint invalid caller"); - token.mint(address(alice), 1000); - endGasReport(); - } - - function testTokenMintToZeroAddress() public { - vm.expectRevert(); - startGasReport("token mint to zero address"); - token.mint(address(0), 1000); - endGasReport(); - } - - function testFuzzTokenMint(uint256 mintAmount) public { - startGasReport("fuzz token mint"); - token.mint(address(this), mintAmount); - endGasReport(); - - assertEq(Balances.getBalance(address(this)), mintAmount); - assertEq(Token.getTotalSupply(), mintAmount); - } - - function testTokenBurn() public { - token.mint(address(this), 1000); - startGasReport("token burn"); - token.burn(address(this), 500); - endGasReport(); - - assertEq(Balances.getBalance(address(this)), 500); - assertEq(Token.getTotalSupply(), 500); - } - - function testTokenBurnInvalidCaller() public { - vm.expectRevert(); - vm.prank(alice); - startGasReport("token burn invalid caller"); - token.burn(address(alice), 1000); - endGasReport(); - } - - function testTokenBurnZeroAddress() public { - vm.expectRevert(); - startGasReport("token burn zero address"); - token.burn(address(0), 1000); - endGasReport(); - } - - function testFuzzTokenBurn(uint256 burnAmount) public { - burnAmount = bound(burnAmount, 1, 1000000000); - token.mint(address(this), 1000000000); - - startGasReport("fuzz token burn"); - token.burn(address(this), burnAmount); - endGasReport(); - - assertEq(Balances.getBalance(address(this)), 1000000000 - burnAmount); - assertEq(Token.getTotalSupply(), 1000000000 - burnAmount); - } - - function testTokenTransfer() public { - token.mint(address(this), 1000); - - startGasReport("token transfer"); - token.transfer(alice, 500); - endGasReport(); - - assertEq(Balances.getBalance(address(this)), 500); - assertEq(Balances.getBalance(alice), 500); - } - - function testTokenTransferInsufficientBalance() public { - token.mint(address(this), 1000); - vm.expectRevert(); - - startGasReport("token transfer insufficient balance"); - token.transfer(alice, 1001); - endGasReport(); - } - - function testFuzzTransfer(uint256 transferAmount) public { - transferAmount = bound(transferAmount, 1, 1000000); - token.mint(address(this), 1000000); - - startGasReport("fuzz transfer"); - token.transfer(alice, transferAmount); - endGasReport(); - - assertEq(Balances.getBalance(address(this)), 1000000 - transferAmount); - assertEq(Balances.getBalance(alice), transferAmount); - } - - function testApprove() public { - startGasReport("token approve"); - token.approve(charlie, 1000); - endGasReport(); - - assertEq(Allowances.getApproval(address(this), charlie), 1000); - } - - function testTransferFrom() public { - token.mint(alice, 1000); - - vm.prank(alice); - token.approve(address(this), 500); - - startGasReport("token transferFrom"); - token.transferFrom(alice, charlie, 500); - endGasReport(); - - assertEq(Balances.getBalance(alice), 500); - assertEq(Balances.getBalance(charlie), 500); - assertEq(Allowances.getApproval(address(this), alice), 0); - } - - function testTransferFromInsufficientAllowance() public { - token.mint(address(this), 1000); - vm.expectRevert(); - - startGasReport("token transferFrom insufficient allowance"); - token.transferFrom(charlie, alice, 1000); - endGasReport(); - } - - function testFuzzTransferFrom(uint256 transferAmount) public { - transferAmount = bound(transferAmount, 1, 1000000); - token.mint(alice, 1000000); - - vm.prank(alice); - token.approve(address(this), transferAmount); - - startGasReport("fuzz transferFrom"); - token.transferFrom(alice, charlie, transferAmount); - endGasReport(); - - assertEq(Balances.getBalance(alice), 1000000 - transferAmount); - assertEq(Balances.getBalance(charlie), transferAmount); - assertEq(Allowances.getApproval(address(this), alice), 0); - } - - function testBalanceOf() public { - token.mint(address(this), 1000); - assertEq(token.balanceOf(address(this)), 1000); - } }