From 87235a21b28fc831b5fb7a1546835ef08bd51655 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Fri, 29 Sep 2023 12:49:54 +0000 Subject: [PATCH] fix(cli): fix table IDs for module install (#1663) --- .changeset/cyan-timers-tan.md | 5 +++++ packages/cli/package.json | 1 + packages/cli/src/utils/deploy.ts | 1 - packages/cli/src/utils/tables/getTableIds.ts | 23 +++++++++----------- packages/cli/src/utils/utils/toBytes16.ts | 16 -------------- pnpm-lock.yaml | 7 ++++-- 6 files changed, 21 insertions(+), 32 deletions(-) create mode 100644 .changeset/cyan-timers-tan.md delete mode 100644 packages/cli/src/utils/utils/toBytes16.ts diff --git a/.changeset/cyan-timers-tan.md b/.changeset/cyan-timers-tan.md new file mode 100644 index 0000000000..27c2dd5fa9 --- /dev/null +++ b/.changeset/cyan-timers-tan.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/cli": patch +--- + +Fix table IDs for module install step of deploy diff --git a/packages/cli/package.json b/packages/cli/package.json index 5bffe9e741..f6eed87f8b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -57,6 +57,7 @@ "path": "^0.12.7", "throttle-debounce": "^5.0.0", "typescript": "5.1.6", + "viem": "1.6.0", "yargs": "^17.7.1", "zod": "^3.21.4", "zod-validation-error": "^1.3.0" diff --git a/packages/cli/src/utils/deploy.ts b/packages/cli/src/utils/deploy.ts index 68f725b8ad..b901ae7d2b 100644 --- a/packages/cli/src/utils/deploy.ts +++ b/packages/cli/src/utils/deploy.ts @@ -21,7 +21,6 @@ import { fastTxExecute } from "./utils/fastTxExecute"; import { getContractData } from "./utils/getContractData"; import { postDeploy } from "./utils/postDeploy"; import { setInternalFeePerGas } from "./utils/setInternalFeePerGas"; -import { toBytes16 } from "./utils/toBytes16"; import { ContractCode } from "./utils/types"; import { resourceIdToHex } from "@latticexyz/common"; diff --git a/packages/cli/src/utils/tables/getTableIds.ts b/packages/cli/src/utils/tables/getTableIds.ts index 798fb8d381..fe771cc852 100644 --- a/packages/cli/src/utils/tables/getTableIds.ts +++ b/packages/cli/src/utils/tables/getTableIds.ts @@ -1,21 +1,18 @@ import { StoreConfig } from "@latticexyz/store"; import { TableIds } from "./types"; -import { toBytes16 } from "../utils/toBytes16"; +import { resourceIdToHex } from "@latticexyz/common"; +import { hexToBytes } from "viem"; export function getTableIds(storeConfig: StoreConfig): TableIds { const tableIds: TableIds = {}; - for (const [tableName, { name }] of Object.entries(storeConfig.tables)) { - tableIds[tableName] = toResourceSelector(storeConfig.namespace, name); + for (const [tableName, { name, offchainOnly }] of Object.entries(storeConfig.tables)) { + tableIds[tableName] = hexToBytes( + resourceIdToHex({ + type: offchainOnly ? "offchainTable" : "table", + namespace: storeConfig.namespace, + name, + }) + ); } return tableIds; } - -// (see https://github.com/latticexyz/mud/issues/499) -function toResourceSelector(namespace: string, file: string): Uint8Array { - const namespaceBytes = toBytes16(namespace); - const fileBytes = toBytes16(file); - const result = new Uint8Array(32); - result.set(namespaceBytes); - result.set(fileBytes, 16); - return result; -} diff --git a/packages/cli/src/utils/utils/toBytes16.ts b/packages/cli/src/utils/utils/toBytes16.ts deleted file mode 100644 index 414fb351ca..0000000000 --- a/packages/cli/src/utils/utils/toBytes16.ts +++ /dev/null @@ -1,16 +0,0 @@ -// TODO: use stringToBytes16 from utils as soon as utils are usable inside cli -// (see https://github.com/latticexyz/mud/issues/499) -export function toBytes16(input: string) { - if (input.length > 16) throw new Error("String does not fit into 16 bytes"); - - const result = new Uint8Array(16); - // Set ascii bytes - for (let i = 0; i < input.length; i++) { - result[i] = input.charCodeAt(i); - } - // Set the remaining bytes to 0 - for (let i = input.length; i < 16; i++) { - result[i] = 0; - } - return result; -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cbefc05f82..b0eda8ebc1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -196,6 +196,9 @@ importers: typescript: specifier: 5.1.6 version: 5.1.6 + viem: + specifier: 1.6.0 + version: 1.6.0(typescript@5.1.6)(zod@3.21.4) yargs: specifier: ^17.7.1 version: 17.7.1 @@ -2969,7 +2972,7 @@ packages: resolution: {integrity: sha512-bcKpo1oj54hGholplGLpqPHRbIsnbixFtc06nwuNM5/dwSXOq/AAYoIBRsBmnZJSdfeNW5rnff7NTAz3ZCqR9Q==} dependencies: '@noble/curves': 1.0.0 - '@noble/hashes': 1.3.0 + '@noble/hashes': 1.3.1 '@scure/base': 1.1.1 dev: false @@ -2983,7 +2986,7 @@ packages: /@scure/bip39@1.2.0: resolution: {integrity: sha512-SX/uKq52cuxm4YFXWFaVByaSHJh2w3BnokVSeUJVCv6K7WulT9u2BuNRBhuFl8vAuYnzx9bEu9WgpcNYTrYieg==} dependencies: - '@noble/hashes': 1.3.0 + '@noble/hashes': 1.3.1 '@scure/base': 1.1.1 dev: false