diff --git a/packages/world-modules/gas-report.json b/packages/world-modules/gas-report.json index b32a14beb2..e78201bf81 100644 --- a/packages/world-modules/gas-report.json +++ b/packages/world-modules/gas-report.json @@ -9,13 +9,13 @@ "file": "test/ERC20.t.sol", "test": "testBurn", "name": "burn", - "gasUsed": 75963 + "gasUsed": 75987 }, { "file": "test/ERC20.t.sol", "test": "testMint", "name": "mint", - "gasUsed": 161802 + "gasUsed": 161826 }, { "file": "test/ERC20.t.sol", diff --git a/packages/world-modules/mud.config.ts b/packages/world-modules/mud.config.ts index ad842a06f4..e41e2202c7 100644 --- a/packages/world-modules/mud.config.ts +++ b/packages/world-modules/mud.config.ts @@ -131,11 +131,18 @@ export default mudConfig({ }, tableIdArgument: true, }, - Metadata: { + TotalSupply: { directory: "modules/erc20/tables", keySchema: {}, valueSchema: { totalSupply: "uint256", + }, + tableIdArgument: true, + }, + Metadata: { + directory: "modules/erc20/tables", + keySchema: {}, + valueSchema: { decimals: "uint8", name: "string", symbol: "string", diff --git a/packages/world-modules/package.json b/packages/world-modules/package.json index f3962221a2..c541c54e46 100644 --- a/packages/world-modules/package.json +++ b/packages/world-modules/package.json @@ -35,6 +35,7 @@ "@latticexyz/schema-type": "workspace:*", "@latticexyz/store": "workspace:*", "@latticexyz/world": "workspace:*", + "@openzeppelin/contracts": "^5.0.0", "zod": "^3.21.4" }, "devDependencies": { diff --git a/packages/world-modules/remappings.txt b/packages/world-modules/remappings.txt index c4d992480e..b0119bb8fe 100644 --- a/packages/world-modules/remappings.txt +++ b/packages/world-modules/remappings.txt @@ -1,3 +1,4 @@ ds-test/=node_modules/ds-test/src/ forge-std/=node_modules/forge-std/src/ @latticexyz/=node_modules/@latticexyz/ +@openzeppelin/=node_modules/@openzeppelin/ \ No newline at end of file diff --git a/packages/world-modules/src/index.sol b/packages/world-modules/src/index.sol index fe1a16d868..f2cfbb4665 100644 --- a/packages/world-modules/src/index.sol +++ b/packages/world-modules/src/index.sol @@ -12,5 +12,6 @@ import { TimeboundDelegations, TimeboundDelegationsTableId } from "./modules/std import { PuppetRegistry } from "./modules/puppet/tables/PuppetRegistry.sol"; import { Balances } from "./modules/erc20/tables/Balances.sol"; import { Allowances } from "./modules/erc20/tables/Allowances.sol"; +import { TotalSupply } from "./modules/erc20/tables/TotalSupply.sol"; import { Metadata, MetadataData } from "./modules/erc20/tables/Metadata.sol"; import { ERC20Registry } from "./modules/erc20/tables/ERC20Registry.sol"; diff --git a/packages/world-modules/src/modules/erc20-puppet/ERC20System.sol b/packages/world-modules/src/modules/erc20-puppet/ERC20System.sol index 76322c96ea..cda16d97f4 100644 --- a/packages/world-modules/src/modules/erc20-puppet/ERC20System.sol +++ b/packages/world-modules/src/modules/erc20-puppet/ERC20System.sol @@ -12,17 +12,16 @@ import { ALLOWANCES_NAME, BALANCES_NAME, METADATA_NAME } from "./constants.sol"; import { AccessControlLib } from "../../utils/AccessControlLib.sol"; import { PuppetMaster } from "../puppet/PuppetMaster.sol"; -import { IERC20Errors } from "./IERC20Errors.sol"; import { IERC20Mintable } from "./IERC20Mintable.sol"; -import { IERC20Events } from "./IERC20Events.sol"; import { Allowances } from "./tables/Allowances.sol"; import { Balances } from "./tables/Balances.sol"; +import { TotalSupply } from "./tables/TotalSupply.sol"; import { Metadata } from "./tables/Metadata.sol"; -import { _allowancesTableId, _balancesTableId, _metadataTableId, _toBytes32 } from "./utils.sol"; +import { _allowancesTableId, _balancesTableId, _totalSupplyTableId, _metadataTableId, _toBytes32 } from "./utils.sol"; -contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster { +contract ERC20System is System, IERC20Mintable, PuppetMaster { using WorldResourceIdInstance for ResourceId; /** @@ -61,7 +60,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster { * @dev Returns the value of tokens in existence. */ function totalSupply() external view returns (uint256) { - return Metadata.getTotalSupply(_metadataTableId(_namespace())); + return TotalSupply.get(_totalSupplyTableId(_namespace())); } /** @@ -203,12 +202,12 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster { */ function _update(address from, address to, uint256 value) internal virtual { bytes14 namespace = _namespace(); - ResourceId metadataTableId = _metadataTableId(namespace); + ResourceId totalSupplyTableId = _totalSupplyTableId(namespace); ResourceId balanceTableId = _balancesTableId(namespace); if (from == address(0)) { // Overflow check required: The rest of the code assumes that totalSupply never overflows - Metadata.setTotalSupply(metadataTableId, Metadata.getTotalSupply(metadataTableId) + value); + TotalSupply.set(totalSupplyTableId, TotalSupply.get(totalSupplyTableId) + value); } else { uint256 fromBalance = Balances.get(balanceTableId, from); if (fromBalance < value) { @@ -223,7 +222,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster { if (to == address(0)) { unchecked { // Overflow not possible: value <= totalSupply or value <= fromBalance <= totalSupply. - Metadata.setTotalSupply(metadataTableId, Metadata.getTotalSupply(metadataTableId) - value); + TotalSupply.set(totalSupplyTableId, TotalSupply.get(totalSupplyTableId) - value); } } else { unchecked { @@ -233,7 +232,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster { } // Emit Transfer event on puppet - puppet().log(IERC20Events.Transfer.selector, _toBytes32(from), _toBytes32(to), abi.encode(value)); + puppet().log(Transfer.selector, _toBytes32(from), _toBytes32(to), abi.encode(value)); } /** @@ -254,7 +253,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster { Allowances.set(_allowancesTableId(_namespace()), owner, spender, value); // Emit Approval event on puppet - puppet().log(IERC20Events.Approval.selector, _toBytes32(owner), _toBytes32(spender), abi.encode(value)); + puppet().log(Approval.selector, _toBytes32(owner), _toBytes32(spender), abi.encode(value)); } /** diff --git a/packages/world-modules/src/modules/erc20-puppet/IERC20.sol b/packages/world-modules/src/modules/erc20-puppet/IERC20.sol deleted file mode 100644 index 8d10c323da..0000000000 --- a/packages/world-modules/src/modules/erc20-puppet/IERC20.sol +++ /dev/null @@ -1,93 +0,0 @@ -// SPDX-License-Identifier: MIT -// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol) - -pragma solidity >=0.8.21; - -import { IERC20Events } from "./IERC20Events.sol"; - -/** - * @dev Interface of the ERC20 standard as defined in the EIP. - */ -interface IERC20 is IERC20Events { - /** - * @dev Returns the name of the token. - */ - function name() external view returns (string memory); - - /** - * @dev Returns the symbol of the token, usually a shorter version of the - * name. - */ - function symbol() external view returns (string memory); - - /** - * @dev Returns the number of decimals used to get its user representation. - * For example, if `decimals` equals `2`, a balance of `505` tokens should - * be displayed to a user as `5.05` (`505 / 10 ** 2`). - * - * Tokens usually opt for a value of 18, imitating the relationship between - * Ether and Wei. This is the default value returned by this function, unless - * it's overridden. - * - * NOTE: This information is only used for _display_ purposes: it in - * no way affects any of the arithmetic of the contract, including - * {IERC20-balanceOf} and {IERC20-transfer}. - */ - function decimals() external view returns (uint8); - - /** - * @dev Returns the value of tokens in existence. - */ - function totalSupply() external view returns (uint256); - - /** - * @dev Returns the value of tokens owned by `account`. - */ - function balanceOf(address account) external view returns (uint256); - - /** - * @dev Moves a `value` amount of tokens from the caller's account to `to`. - * - * Returns a boolean value indicating whether the operation succeeded. - * - * Emits a {Transfer} event. - */ - function transfer(address to, uint256 value) external returns (bool); - - /** - * @dev Returns the remaining number of tokens that `spender` will be - * allowed to spend on behalf of `owner` through {transferFrom}. This is - * zero by default. - * - * This value changes when {approve} or {transferFrom} are called. - */ - function allowance(address owner, address spender) external view returns (uint256); - - /** - * @dev Sets a `value` amount of tokens as the allowance of `spender` over the - * caller's tokens. - * - * Returns a boolean value indicating whether the operation succeeded. - * - * IMPORTANT: Beware that changing an allowance with this method brings the risk - * that someone may use both the old and the new allowance by unfortunate - * transaction ordering. One possible solution to mitigate this race - * condition is to first reduce the spender's allowance to 0 and set the - * desired value afterwards: - * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 - * - * Emits an {Approval} event. - */ - function approve(address spender, uint256 value) external returns (bool); - - /** - * @dev Moves a `value` amount of tokens from `from` to `to` using the - * allowance mechanism. `value` is then deducted from the caller's - * allowance. - * - * Returns a boolean value indicating whether the operation succeeded. - * - * Emits a {Transfer} event. - */ - function transferFrom(address from, address to, uint256 value) external returns (bool); -} diff --git a/packages/world-modules/src/modules/erc20-puppet/IERC20Errors.sol b/packages/world-modules/src/modules/erc20-puppet/IERC20Errors.sol deleted file mode 100644 index 1126664f2f..0000000000 --- a/packages/world-modules/src/modules/erc20-puppet/IERC20Errors.sol +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-License-Identifier: MIT -// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/draft-IERC6093.sol) -pragma solidity >=0.8.21; - -/** - * @dev Standard ERC20 Errors - * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC20 tokens. - */ -interface IERC20Errors { - /** - * @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers. - * @param sender Address whose tokens are being transferred. - * @param balance Current balance for the interacting account. - * @param needed Minimum amount required to perform a transfer. - */ - error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed); - - /** - * @dev Indicates a failure with the token `sender`. Used in transfers. - * @param sender Address whose tokens are being transferred. - */ - error ERC20InvalidSender(address sender); - - /** - * @dev Indicates a failure with the token `receiver`. Used in transfers. - * @param receiver Address to which tokens are being transferred. - */ - error ERC20InvalidReceiver(address receiver); - - /** - * @dev Indicates a failure with the `spender`’s `allowance`. Used in transfers. - * @param spender Address that may be allowed to operate on tokens without being their owner. - * @param allowance Amount of tokens a `spender` is allowed to operate with. - * @param needed Minimum amount required to perform a transfer. - */ - error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed); - - /** - * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals. - * @param approver Address initiating an approval operation. - */ - error ERC20InvalidApprover(address approver); - - /** - * @dev Indicates a failure with the `spender` to be approved. Used in approvals. - * @param spender Address that may be allowed to operate on tokens without being their owner. - */ - error ERC20InvalidSpender(address spender); -} diff --git a/packages/world-modules/src/modules/erc20-puppet/IERC20Mintable.sol b/packages/world-modules/src/modules/erc20-puppet/IERC20Mintable.sol index 3e5ece4d28..4af56ff6ac 100644 --- a/packages/world-modules/src/modules/erc20-puppet/IERC20Mintable.sol +++ b/packages/world-modules/src/modules/erc20-puppet/IERC20Mintable.sol @@ -3,12 +3,14 @@ pragma solidity >=0.8.21; -import { IERC20 } from "./IERC20.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; +import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol"; /** * @dev Extending the ERC20 standard with permissioned mint and burn functions. */ -interface IERC20Mintable is IERC20 { +interface IERC20Mintable is IERC20, IERC20Metadata, IERC20Errors { /** * @dev Creates a `value` amount of tokens and assigns them to `account`, by transferring it from address(0). * diff --git a/packages/world-modules/src/modules/erc20-puppet/constants.sol b/packages/world-modules/src/modules/erc20-puppet/constants.sol index 6c8e1f58f2..f65d8aa8f3 100644 --- a/packages/world-modules/src/modules/erc20-puppet/constants.sol +++ b/packages/world-modules/src/modules/erc20-puppet/constants.sol @@ -12,6 +12,7 @@ ResourceId constant MODULE_NAMESPACE_ID = ResourceId.wrap( bytes16 constant ALLOWANCES_NAME = "Allowances"; bytes16 constant BALANCES_NAME = "Balances"; +bytes16 constant TOTAL_SUPPLY_NAME = "TotalSupply"; bytes16 constant METADATA_NAME = "Metadata"; bytes16 constant ERC20_SYSTEM_NAME = "ERC20System"; diff --git a/packages/world-modules/src/modules/erc20-puppet/tables/Metadata.sol b/packages/world-modules/src/modules/erc20-puppet/tables/Metadata.sol index bee52c41d2..d24ef388df 100644 --- a/packages/world-modules/src/modules/erc20-puppet/tables/Metadata.sol +++ b/packages/world-modules/src/modules/erc20-puppet/tables/Metadata.sol @@ -21,11 +21,10 @@ import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; FieldLayout constant _fieldLayout = FieldLayout.wrap( - 0x0021020220010000000000000000000000000000000000000000000000000000 + 0x0001010201000000000000000000000000000000000000000000000000000000 ); struct MetadataData { - uint256 totalSupply; uint8 decimals; string name; string symbol; @@ -55,11 +54,10 @@ library Metadata { * @return _valueSchema The value schema for the table. */ function getValueSchema() internal pure returns (Schema) { - SchemaType[] memory _valueSchema = new SchemaType[](4); - _valueSchema[0] = SchemaType.UINT256; - _valueSchema[1] = SchemaType.UINT8; + SchemaType[] memory _valueSchema = new SchemaType[](3); + _valueSchema[0] = SchemaType.UINT8; + _valueSchema[1] = SchemaType.STRING; _valueSchema[2] = SchemaType.STRING; - _valueSchema[3] = SchemaType.STRING; return SchemaLib.encode(_valueSchema); } @@ -77,11 +75,10 @@ library Metadata { * @return fieldNames An array of strings with the names of value fields. */ function getFieldNames() internal pure returns (string[] memory fieldNames) { - fieldNames = new string[](4); - fieldNames[0] = "totalSupply"; - fieldNames[1] = "decimals"; - fieldNames[2] = "name"; - fieldNames[3] = "symbol"; + fieldNames = new string[](3); + fieldNames[0] = "decimals"; + fieldNames[1] = "name"; + fieldNames[2] = "symbol"; } /** @@ -98,51 +95,13 @@ library Metadata { StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** - * @notice Get totalSupply. - */ - function getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get totalSupply. - */ - function _getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Set totalSupply. - */ - function setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); - } - - /** - * @notice Set totalSupply. - */ - function _setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); - } - /** * @notice Get decimals. */ function getDecimals(ResourceId _tableId) internal view returns (uint8 decimals) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint8(bytes1(_blob))); } @@ -152,7 +111,7 @@ library Metadata { function _getDecimals(ResourceId _tableId) internal view returns (uint8 decimals) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint8(bytes1(_blob))); } @@ -162,7 +121,7 @@ library Metadata { function setDecimals(ResourceId _tableId, uint8 decimals) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((decimals)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout); } /** @@ -171,7 +130,7 @@ library Metadata { function _setDecimals(ResourceId _tableId, uint8 decimals) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((decimals)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout); } /** @@ -501,14 +460,8 @@ library Metadata { /** * @notice Set the full data using individual values. */ - function set( - ResourceId _tableId, - uint256 totalSupply, - uint8 decimals, - string memory name, - string memory symbol - ) internal { - bytes memory _staticData = encodeStatic(totalSupply, decimals); + function set(ResourceId _tableId, uint8 decimals, string memory name, string memory symbol) internal { + bytes memory _staticData = encodeStatic(decimals); PackedCounter _encodedLengths = encodeLengths(name, symbol); bytes memory _dynamicData = encodeDynamic(name, symbol); @@ -521,14 +474,8 @@ library Metadata { /** * @notice Set the full data using individual values. */ - function _set( - ResourceId _tableId, - uint256 totalSupply, - uint8 decimals, - string memory name, - string memory symbol - ) internal { - bytes memory _staticData = encodeStatic(totalSupply, decimals); + function _set(ResourceId _tableId, uint8 decimals, string memory name, string memory symbol) internal { + bytes memory _staticData = encodeStatic(decimals); PackedCounter _encodedLengths = encodeLengths(name, symbol); bytes memory _dynamicData = encodeDynamic(name, symbol); @@ -542,7 +489,7 @@ library Metadata { * @notice Set the full data using the data struct. */ function set(ResourceId _tableId, MetadataData memory _table) internal { - bytes memory _staticData = encodeStatic(_table.totalSupply, _table.decimals); + bytes memory _staticData = encodeStatic(_table.decimals); PackedCounter _encodedLengths = encodeLengths(_table.name, _table.symbol); bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol); @@ -556,7 +503,7 @@ library Metadata { * @notice Set the full data using the data struct. */ function _set(ResourceId _tableId, MetadataData memory _table) internal { - bytes memory _staticData = encodeStatic(_table.totalSupply, _table.decimals); + bytes memory _staticData = encodeStatic(_table.decimals); PackedCounter _encodedLengths = encodeLengths(_table.name, _table.symbol); bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol); @@ -569,10 +516,8 @@ library Metadata { /** * @notice Decode the tightly packed blob of static data using this table's field layout. */ - function decodeStatic(bytes memory _blob) internal pure returns (uint256 totalSupply, uint8 decimals) { - totalSupply = (uint256(Bytes.slice32(_blob, 0))); - - decimals = (uint8(Bytes.slice1(_blob, 32))); + function decodeStatic(bytes memory _blob) internal pure returns (uint8 decimals) { + decimals = (uint8(Bytes.slice1(_blob, 0))); } /** @@ -607,7 +552,7 @@ library Metadata { PackedCounter _encodedLengths, bytes memory _dynamicData ) internal pure returns (MetadataData memory _table) { - (_table.totalSupply, _table.decimals) = decodeStatic(_staticData); + (_table.decimals) = decodeStatic(_staticData); (_table.name, _table.symbol) = decodeDynamic(_encodedLengths, _dynamicData); } @@ -634,8 +579,8 @@ library Metadata { * @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 totalSupply, uint8 decimals) internal pure returns (bytes memory) { - return abi.encodePacked(totalSupply, decimals); + function encodeStatic(uint8 decimals) internal pure returns (bytes memory) { + return abi.encodePacked(decimals); } /** @@ -667,12 +612,11 @@ library Metadata { * @return The dyanmic (variable length) data, encoded into a sequence of bytes. */ function encode( - uint256 totalSupply, uint8 decimals, string memory name, string memory symbol ) internal pure returns (bytes memory, PackedCounter, bytes memory) { - bytes memory _staticData = encodeStatic(totalSupply, decimals); + bytes memory _staticData = encodeStatic(decimals); PackedCounter _encodedLengths = encodeLengths(name, symbol); bytes memory _dynamicData = encodeDynamic(name, symbol); diff --git a/packages/world-modules/src/modules/erc20-puppet/tables/TotalSupply.sol b/packages/world-modules/src/modules/erc20-puppet/tables/TotalSupply.sol new file mode 100644 index 0000000000..876013c395 --- /dev/null +++ b/packages/world-modules/src/modules/erc20-puppet/tables/TotalSupply.sol @@ -0,0 +1,213 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.21; + +/* Autogenerated file. Do not edit manually. */ + +// Import schema type +import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; + +// 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, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.sol"; +import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; +import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; + +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + +library TotalSupply { + /** + * @notice Get the table values' field layout. + * @return _fieldLayout The field layout for the table. + */ + function getFieldLayout() internal pure returns (FieldLayout) { + return _fieldLayout; + } + + /** + * @notice Get the table's key schema. + * @return _keySchema The key schema for the table. + */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _keySchema = new SchemaType[](0); + + return SchemaLib.encode(_keySchema); + } + + /** + * @notice Get the table's value schema. + * @return _valueSchema The value schema for the table. + */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _valueSchema = new SchemaType[](1); + _valueSchema[0] = SchemaType.UINT256; + + return SchemaLib.encode(_valueSchema); + } + + /** + * @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[](1); + fieldNames[0] = "totalSupply"; + } + + /** + * @notice Register the table with its config. + */ + function register(ResourceId _tableId) internal { + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + + /** + * @notice Register the table with its config. + */ + function _register(ResourceId _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + + /** + * @notice Get totalSupply. + */ + function getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Get totalSupply. + */ + function _getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Get totalSupply. + */ + function get(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Get totalSupply. + */ + function _get(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Set totalSupply. + */ + function setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Set totalSupply. + */ + function _setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Set totalSupply. + */ + function set(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Set totalSupply. + */ + function _set(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Delete all data for given keys. + */ + function deleteRecord(ResourceId _tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreSwitch.deleteRecord(_tableId, _keyTuple); + } + + /** + * @notice Delete all data for given keys. + */ + function _deleteRecord(ResourceId _tableId) 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(uint256 totalSupply) internal pure returns (bytes memory) { + return abi.encodePacked(totalSupply); + } + + /** + * @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 dyanmic (variable length) data, encoded into a sequence of bytes. + */ + function encode(uint256 totalSupply) internal pure returns (bytes memory, PackedCounter, bytes memory) { + bytes memory _staticData = encodeStatic(totalSupply); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + 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-modules/src/modules/erc20-puppet/utils.sol b/packages/world-modules/src/modules/erc20-puppet/utils.sol index 87906b3c32..a65696ac84 100644 --- a/packages/world-modules/src/modules/erc20-puppet/utils.sol +++ b/packages/world-modules/src/modules/erc20-puppet/utils.sol @@ -7,7 +7,7 @@ import { RESOURCE_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; import { WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol"; import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol"; -import { ALLOWANCES_NAME, BALANCES_NAME, METADATA_NAME, ERC20_SYSTEM_NAME } from "./constants.sol"; +import { ALLOWANCES_NAME, BALANCES_NAME, TOTAL_SUPPLY_NAME, METADATA_NAME, ERC20_SYSTEM_NAME } from "./constants.sol"; function _allowancesTableId(bytes14 namespace) pure returns (ResourceId) { return WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: ALLOWANCES_NAME }); @@ -17,6 +17,10 @@ function _balancesTableId(bytes14 namespace) pure returns (ResourceId) { return WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: BALANCES_NAME }); } +function _totalSupplyTableId(bytes14 namespace) pure returns (ResourceId) { + return WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: TOTAL_SUPPLY_NAME }); +} + function _metadataTableId(bytes14 namespace) pure returns (ResourceId) { return WorldResourceIdLib.encode({ typeId: RESOURCE_TABLE, namespace: namespace, name: METADATA_NAME }); } diff --git a/packages/world-modules/src/modules/erc20/tables/Metadata.sol b/packages/world-modules/src/modules/erc20/tables/Metadata.sol index bee52c41d2..d24ef388df 100644 --- a/packages/world-modules/src/modules/erc20/tables/Metadata.sol +++ b/packages/world-modules/src/modules/erc20/tables/Metadata.sol @@ -21,11 +21,10 @@ import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; FieldLayout constant _fieldLayout = FieldLayout.wrap( - 0x0021020220010000000000000000000000000000000000000000000000000000 + 0x0001010201000000000000000000000000000000000000000000000000000000 ); struct MetadataData { - uint256 totalSupply; uint8 decimals; string name; string symbol; @@ -55,11 +54,10 @@ library Metadata { * @return _valueSchema The value schema for the table. */ function getValueSchema() internal pure returns (Schema) { - SchemaType[] memory _valueSchema = new SchemaType[](4); - _valueSchema[0] = SchemaType.UINT256; - _valueSchema[1] = SchemaType.UINT8; + SchemaType[] memory _valueSchema = new SchemaType[](3); + _valueSchema[0] = SchemaType.UINT8; + _valueSchema[1] = SchemaType.STRING; _valueSchema[2] = SchemaType.STRING; - _valueSchema[3] = SchemaType.STRING; return SchemaLib.encode(_valueSchema); } @@ -77,11 +75,10 @@ library Metadata { * @return fieldNames An array of strings with the names of value fields. */ function getFieldNames() internal pure returns (string[] memory fieldNames) { - fieldNames = new string[](4); - fieldNames[0] = "totalSupply"; - fieldNames[1] = "decimals"; - fieldNames[2] = "name"; - fieldNames[3] = "symbol"; + fieldNames = new string[](3); + fieldNames[0] = "decimals"; + fieldNames[1] = "name"; + fieldNames[2] = "symbol"; } /** @@ -98,51 +95,13 @@ library Metadata { StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** - * @notice Get totalSupply. - */ - function getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Get totalSupply. - */ - function _getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { - bytes32[] memory _keyTuple = new bytes32[](0); - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** - * @notice Set totalSupply. - */ - function setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); - } - - /** - * @notice Set totalSupply. - */ - function _setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { - bytes32[] memory _keyTuple = new bytes32[](0); - - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); - } - /** * @notice Get decimals. */ function getDecimals(ResourceId _tableId) internal view returns (uint8 decimals) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint8(bytes1(_blob))); } @@ -152,7 +111,7 @@ library Metadata { function _getDecimals(ResourceId _tableId) internal view returns (uint8 decimals) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); return (uint8(bytes1(_blob))); } @@ -162,7 +121,7 @@ library Metadata { function setDecimals(ResourceId _tableId, uint8 decimals) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((decimals)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout); } /** @@ -171,7 +130,7 @@ library Metadata { function _setDecimals(ResourceId _tableId, uint8 decimals) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((decimals)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout); } /** @@ -501,14 +460,8 @@ library Metadata { /** * @notice Set the full data using individual values. */ - function set( - ResourceId _tableId, - uint256 totalSupply, - uint8 decimals, - string memory name, - string memory symbol - ) internal { - bytes memory _staticData = encodeStatic(totalSupply, decimals); + function set(ResourceId _tableId, uint8 decimals, string memory name, string memory symbol) internal { + bytes memory _staticData = encodeStatic(decimals); PackedCounter _encodedLengths = encodeLengths(name, symbol); bytes memory _dynamicData = encodeDynamic(name, symbol); @@ -521,14 +474,8 @@ library Metadata { /** * @notice Set the full data using individual values. */ - function _set( - ResourceId _tableId, - uint256 totalSupply, - uint8 decimals, - string memory name, - string memory symbol - ) internal { - bytes memory _staticData = encodeStatic(totalSupply, decimals); + function _set(ResourceId _tableId, uint8 decimals, string memory name, string memory symbol) internal { + bytes memory _staticData = encodeStatic(decimals); PackedCounter _encodedLengths = encodeLengths(name, symbol); bytes memory _dynamicData = encodeDynamic(name, symbol); @@ -542,7 +489,7 @@ library Metadata { * @notice Set the full data using the data struct. */ function set(ResourceId _tableId, MetadataData memory _table) internal { - bytes memory _staticData = encodeStatic(_table.totalSupply, _table.decimals); + bytes memory _staticData = encodeStatic(_table.decimals); PackedCounter _encodedLengths = encodeLengths(_table.name, _table.symbol); bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol); @@ -556,7 +503,7 @@ library Metadata { * @notice Set the full data using the data struct. */ function _set(ResourceId _tableId, MetadataData memory _table) internal { - bytes memory _staticData = encodeStatic(_table.totalSupply, _table.decimals); + bytes memory _staticData = encodeStatic(_table.decimals); PackedCounter _encodedLengths = encodeLengths(_table.name, _table.symbol); bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol); @@ -569,10 +516,8 @@ library Metadata { /** * @notice Decode the tightly packed blob of static data using this table's field layout. */ - function decodeStatic(bytes memory _blob) internal pure returns (uint256 totalSupply, uint8 decimals) { - totalSupply = (uint256(Bytes.slice32(_blob, 0))); - - decimals = (uint8(Bytes.slice1(_blob, 32))); + function decodeStatic(bytes memory _blob) internal pure returns (uint8 decimals) { + decimals = (uint8(Bytes.slice1(_blob, 0))); } /** @@ -607,7 +552,7 @@ library Metadata { PackedCounter _encodedLengths, bytes memory _dynamicData ) internal pure returns (MetadataData memory _table) { - (_table.totalSupply, _table.decimals) = decodeStatic(_staticData); + (_table.decimals) = decodeStatic(_staticData); (_table.name, _table.symbol) = decodeDynamic(_encodedLengths, _dynamicData); } @@ -634,8 +579,8 @@ library Metadata { * @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 totalSupply, uint8 decimals) internal pure returns (bytes memory) { - return abi.encodePacked(totalSupply, decimals); + function encodeStatic(uint8 decimals) internal pure returns (bytes memory) { + return abi.encodePacked(decimals); } /** @@ -667,12 +612,11 @@ library Metadata { * @return The dyanmic (variable length) data, encoded into a sequence of bytes. */ function encode( - uint256 totalSupply, uint8 decimals, string memory name, string memory symbol ) internal pure returns (bytes memory, PackedCounter, bytes memory) { - bytes memory _staticData = encodeStatic(totalSupply, decimals); + bytes memory _staticData = encodeStatic(decimals); PackedCounter _encodedLengths = encodeLengths(name, symbol); bytes memory _dynamicData = encodeDynamic(name, symbol); diff --git a/packages/world-modules/src/modules/erc20/tables/TotalSupply.sol b/packages/world-modules/src/modules/erc20/tables/TotalSupply.sol new file mode 100644 index 0000000000..876013c395 --- /dev/null +++ b/packages/world-modules/src/modules/erc20/tables/TotalSupply.sol @@ -0,0 +1,213 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.21; + +/* Autogenerated file. Do not edit manually. */ + +// Import schema type +import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; + +// 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, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.sol"; +import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; +import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; +import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol"; + +FieldLayout constant _fieldLayout = FieldLayout.wrap( + 0x0020010020000000000000000000000000000000000000000000000000000000 +); + +library TotalSupply { + /** + * @notice Get the table values' field layout. + * @return _fieldLayout The field layout for the table. + */ + function getFieldLayout() internal pure returns (FieldLayout) { + return _fieldLayout; + } + + /** + * @notice Get the table's key schema. + * @return _keySchema The key schema for the table. + */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _keySchema = new SchemaType[](0); + + return SchemaLib.encode(_keySchema); + } + + /** + * @notice Get the table's value schema. + * @return _valueSchema The value schema for the table. + */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _valueSchema = new SchemaType[](1); + _valueSchema[0] = SchemaType.UINT256; + + return SchemaLib.encode(_valueSchema); + } + + /** + * @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[](1); + fieldNames[0] = "totalSupply"; + } + + /** + * @notice Register the table with its config. + */ + function register(ResourceId _tableId) internal { + StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + + /** + * @notice Register the table with its config. + */ + function _register(ResourceId _tableId) internal { + StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); + } + + /** + * @notice Get totalSupply. + */ + function getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Get totalSupply. + */ + function _getTotalSupply(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Get totalSupply. + */ + function get(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Get totalSupply. + */ + function _get(ResourceId _tableId) internal view returns (uint256 totalSupply) { + bytes32[] memory _keyTuple = new bytes32[](0); + + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); + return (uint256(bytes32(_blob))); + } + + /** + * @notice Set totalSupply. + */ + function setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Set totalSupply. + */ + function _setTotalSupply(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Set totalSupply. + */ + function set(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Set totalSupply. + */ + function _set(ResourceId _tableId, uint256 totalSupply) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((totalSupply)), _fieldLayout); + } + + /** + * @notice Delete all data for given keys. + */ + function deleteRecord(ResourceId _tableId) internal { + bytes32[] memory _keyTuple = new bytes32[](0); + + StoreSwitch.deleteRecord(_tableId, _keyTuple); + } + + /** + * @notice Delete all data for given keys. + */ + function _deleteRecord(ResourceId _tableId) 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(uint256 totalSupply) internal pure returns (bytes memory) { + return abi.encodePacked(totalSupply); + } + + /** + * @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 dyanmic (variable length) data, encoded into a sequence of bytes. + */ + function encode(uint256 totalSupply) internal pure returns (bytes memory, PackedCounter, bytes memory) { + bytes memory _staticData = encodeStatic(totalSupply); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + 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-modules/test/ERC20.t.sol b/packages/world-modules/test/ERC20.t.sol index dbee06f3d8..d8ab693e0e 100644 --- a/packages/world-modules/test/ERC20.t.sol +++ b/packages/world-modules/test/ERC20.t.sol @@ -13,15 +13,17 @@ import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld. import { NamespaceOwner } from "@latticexyz/world/src/codegen/tables/NamespaceOwner.sol"; import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol"; +import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol"; + import { ERC20Module } from "../src/modules/erc20-puppet/ERC20Module.sol"; import { MetadataData } from "../src/modules/erc20-puppet/tables/Metadata.sol"; import { ERC20Registry } from "../src/modules/erc20-puppet/tables/ERC20Registry.sol"; import { ERC20_REGISTRY_TABLE_ID } from "../src/modules/erc20-puppet/constants.sol"; -import { IERC20Events } from "../src/modules/erc20-puppet/IERC20Events.sol"; import { IERC20Mintable } from "../src/modules/erc20-puppet/IERC20Mintable.sol"; -import { IERC20Errors } from "../src/modules/erc20-puppet/IERC20Errors.sol"; import { registerERC20 } from "../src/modules/erc20-puppet/registerERC20.sol"; +import { IERC20Events } from "./IERC20Events.sol"; + contract ERC20Test is Test, GasReporter, IERC20Events, IERC20Errors { IBaseWorld world; ERC20Module erc20Module; @@ -33,11 +35,7 @@ contract ERC20Test is Test, GasReporter, IERC20Events, IERC20Errors { StoreSwitch.setStoreAddress(address(world)); // Register a new ERC20 token - token = registerERC20( - world, - "myERC20", - MetadataData({ totalSupply: 0, decimals: 18, name: "Token", symbol: "TKN" }) - ); + token = registerERC20(world, "myERC20", MetadataData({ decimals: 18, name: "Token", symbol: "TKN" })); } function testSetUp() public { @@ -50,7 +48,7 @@ contract ERC20Test is Test, GasReporter, IERC20Events, IERC20Errors { IERC20Mintable anotherToken = registerERC20( world, "anotherERC20", - MetadataData({ totalSupply: 0, decimals: 18, name: "Token", symbol: "TKN" }) + MetadataData({ decimals: 18, name: "Token", symbol: "TKN" }) ); assertTrue(address(anotherToken) != address(0)); assertTrue(address(anotherToken) != address(token)); diff --git a/packages/world-modules/src/modules/erc20-puppet/IERC20Events.sol b/packages/world-modules/test/IERC20Events.sol similarity index 100% rename from packages/world-modules/src/modules/erc20-puppet/IERC20Events.sol rename to packages/world-modules/test/IERC20Events.sol diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8942de00f0..ce50566dc9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1074,6 +1074,9 @@ importers: '@latticexyz/world': specifier: workspace:* version: link:../world + '@openzeppelin/contracts': + specifier: ^5.0.0 + version: 5.0.0 zod: specifier: ^3.21.4 version: 3.21.4 @@ -2896,6 +2899,10 @@ packages: hardhat: 2.10.2(typescript@5.1.6) dev: true + /@openzeppelin/contracts@5.0.0: + resolution: {integrity: sha512-bv2sdS6LKqVVMLI5+zqnNrNU/CA+6z6CmwFXm/MzmOPBRSO5reEJN7z0Gbzvs0/bv/MZZXNklubpwy3v2+azsw==} + dev: false + /@protobufjs/aspromise@1.1.2: resolution: {integrity: sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==}