@latticexyz/common 2.0.0-next.10 → 2.0.0-next.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chains.js +1 -1
- package/dist/chains.js.map +1 -1
- package/dist/chunk-7WIPV3R3.js +2 -0
- package/dist/chunk-7WIPV3R3.js.map +1 -0
- package/dist/codegen.js +13 -13
- package/dist/codegen.js.map +1 -1
- package/dist/foundry.js +2 -2
- package/dist/foundry.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +4 -8
- package/src/chains/latticeTestnet.ts +0 -4
- package/src/chains/mudFoundry.ts +2 -1
- package/src/codegen/debug.ts +3 -0
- package/src/codegen/utils/formatAndWrite.ts +8 -7
- package/src/codegen/utils/loadUserTypesFile.ts +2 -1
- package/src/common.ts +4 -2
- package/src/createNonceManager.ts +30 -26
- package/src/deprecated/createContract.ts +4 -0
- package/src/deprecated/hexToResourceId.ts +4 -0
- package/src/deprecated/resourceIdToHex.ts +4 -0
- package/src/foundry/index.ts +5 -2
- package/src/getContract.ts +115 -0
- package/src/getNonceManager.ts +21 -0
- package/src/getNonceManagerId.ts +16 -0
- package/src/hexToResource.test.ts +11 -0
- package/src/{hexToResourceId.ts → hexToResource.ts} +5 -5
- package/src/index.ts +14 -3
- package/src/{resourceIdToHex.test.ts → resourceToHex.test.ts} +15 -11
- package/src/{resourceIdToHex.ts → resourceToHex.ts} +5 -5
- package/src/sendTransaction.ts +89 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uniqueBy.ts +7 -0
- package/src/writeContract.ts +92 -0
- package/dist/deprecated.js +0 -2
- package/dist/deprecated.js.map +0 -1
- package/src/createContract.ts +0 -173
- package/src/deprecated/TableId.test.ts +0 -33
- package/src/deprecated/TableId.ts +0 -41
- package/src/deprecated/getTableIds.ts +0 -6
- package/src/deprecated/index.ts +0 -2
- package/src/hexToResourceId.test.ts +0 -11
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createBurnerAccount.ts","../src/createContract.ts","../src/createNonceManager.ts","../src/debug.ts","../src/getBurnerPrivateKey.ts","../src/hexToResourceId.ts","../src/resourceTypes.ts","../src/resourceIdToHex.ts","../src/readHex.ts","../src/spliceHex.ts","../src/transportObserver.ts"],"sourcesContent":["import { Account, Hex } from \"viem\";\nimport { privateKeyToAccount } from \"viem/accounts\";\n\nexport function createBurnerAccount(privateKey: Hex): Account {\n const account = privateKeyToAccount(privateKey);\n // We may override account features here\n return {\n ...account,\n };\n}\n","import {\n Abi,\n Account,\n Address,\n Chain,\n GetContractParameters,\n GetContractReturnType,\n Hex,\n PublicClient,\n SimulateContractParameters,\n Transport,\n WalletClient,\n WriteContractParameters,\n getContract,\n} from \"viem\";\nimport pRetry from \"p-retry\";\nimport { createNonceManager } from \"./createNonceManager\";\nimport { debug as parentDebug } from \"./debug\";\nimport { UnionOmit } from \"./type-utils/common\";\n\nconst debug = parentDebug.extend(\"createContract\");\n\n// copied from viem because this isn't exported\n// TODO: import from viem?\nfunction getFunctionParameters(values: [args?: readonly unknown[], options?: object]): {\n args: readonly unknown[];\n options: object;\n} {\n const hasArgs = values.length && Array.isArray(values[0]);\n const args = hasArgs ? values[0]! : [];\n const options = (hasArgs ? values[1] : values[0]) ?? {};\n return { args, options };\n}\n\nexport type ContractWrite = {\n id: string;\n request: WriteContractParameters;\n result: Promise<Hex>;\n};\n\nexport type CreateContractOptions<\n TTransport extends Transport,\n TAddress extends Address,\n TAbi extends Abi,\n TChain extends Chain,\n TAccount extends Account,\n TPublicClient extends PublicClient<TTransport, TChain>,\n TWalletClient extends WalletClient<TTransport, TChain, TAccount>\n> = Required<GetContractParameters<TTransport, TChain, TAccount, TAbi, TPublicClient, TWalletClient, TAddress>> & {\n onWrite?: (write: ContractWrite) => void;\n};\n\nexport function createContract<\n TTransport extends Transport,\n TAddress extends Address,\n TAbi extends Abi,\n TChain extends Chain,\n TAccount extends Account,\n TPublicClient extends PublicClient<TTransport, TChain>,\n TWalletClient extends WalletClient<TTransport, TChain, TAccount>\n>({\n abi,\n address,\n publicClient,\n walletClient,\n onWrite,\n}: CreateContractOptions<\n TTransport,\n TAddress,\n TAbi,\n TChain,\n TAccount,\n TPublicClient,\n TWalletClient\n>): GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress> {\n const contract = getContract<TTransport, TAddress, TAbi, TChain, TAccount, TPublicClient, TWalletClient>({\n abi,\n address,\n publicClient,\n walletClient,\n }) as unknown as GetContractReturnType<Abi, PublicClient, WalletClient>;\n\n if (contract.write) {\n let nextWriteId = 0;\n const nonceManager = createNonceManager({\n publicClient: publicClient as PublicClient,\n address: walletClient.account.address,\n });\n\n // Replace write calls with our own proxy. Implemented ~the same as viem, but adds better handling of nonces (via queue + retries).\n contract.write = new Proxy(\n {},\n {\n get(_, functionName: string): GetContractReturnType<Abi, PublicClient, WalletClient>[\"write\"][string] {\n async function prepareWrite(\n options: WriteContractParameters\n ): Promise<WriteContractParameters<TAbi, typeof functionName, TChain, TAccount>> {\n if (options.gas) {\n debug(\"gas provided, skipping simulate\", functionName, options);\n return options as unknown as WriteContractParameters<TAbi, typeof functionName, TChain, TAccount>;\n }\n\n debug(\"simulating write\", functionName, options);\n const { request } = await publicClient.simulateContract({\n ...options,\n account: options.account ?? walletClient.account,\n } as unknown as SimulateContractParameters<TAbi, typeof functionName, TChain>);\n\n return request as unknown as WriteContractParameters<TAbi, typeof functionName, TChain, TAccount>;\n }\n\n async function write(options: WriteContractParameters): Promise<Hex> {\n const preparedWrite = await prepareWrite(options);\n\n return await pRetry(\n async () => {\n if (!nonceManager.hasNonce()) {\n await nonceManager.resetNonce();\n }\n\n const nonce = nonceManager.nextNonce();\n debug(\"calling write function with nonce\", nonce, preparedWrite);\n return await walletClient.writeContract({\n nonce,\n ...preparedWrite,\n });\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // On nonce errors, reset the nonce and retry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error);\n await nonceManager.resetNonce();\n return;\n }\n // TODO: prepareWrite again if there are gas errors?\n throw error;\n },\n }\n );\n }\n\n return (...parameters) => {\n const id = `${walletClient.chain.id}:${walletClient.account.address}:${nextWriteId++}`;\n const { args, options } = <\n {\n args: unknown[];\n options: UnionOmit<WriteContractParameters, \"address\" | \"abi\" | \"functionName\" | \"args\">;\n }\n >getFunctionParameters(parameters as any);\n\n const request = {\n address,\n abi,\n functionName,\n args,\n ...options,\n };\n\n const result = write(request);\n\n onWrite?.({ id, request, result });\n\n return result;\n };\n },\n }\n );\n }\n\n return contract as unknown as GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress>;\n}\n","import {\n BaseError,\n BlockTag,\n Hex,\n NonceTooHighError,\n NonceTooLowError,\n PublicClient,\n TransactionExecutionError,\n} from \"viem\";\nimport { debug as parentDebug } from \"./debug\";\n\nconst debug = parentDebug.extend(\"createNonceManager\");\n\ntype CreateNonceManagerOptions = {\n publicClient: PublicClient;\n address: Hex;\n blockTag?: BlockTag;\n};\n\ntype CreateNonceManagerResult = {\n hasNonce: () => boolean;\n nextNonce: () => number;\n resetNonce: () => Promise<void>;\n shouldResetNonce: (error: unknown) => boolean;\n};\n\nexport function createNonceManager({\n publicClient,\n address,\n blockTag,\n}: CreateNonceManagerOptions): CreateNonceManagerResult {\n const nonceRef = { current: -1 };\n const channel =\n typeof BroadcastChannel !== \"undefined\"\n ? // TODO: fetch chain ID or require it via types?\n new BroadcastChannel(`mud:createNonceManager:${publicClient.chain?.id}:${address}`)\n : null;\n\n if (channel) {\n channel.addEventListener(\"message\", (event) => {\n const nonce = JSON.parse(event.data);\n debug(\"got nonce from broadcast channel\", nonce);\n nonceRef.current = nonce;\n });\n }\n\n function hasNonce(): boolean {\n return nonceRef.current >= 0;\n }\n\n function nextNonce(): number {\n if (!hasNonce()) throw new Error(\"call resetNonce before using nextNonce\");\n const nonce = nonceRef.current++;\n channel?.postMessage(JSON.stringify(nonceRef.current));\n return nonce;\n }\n\n async function resetNonce(): Promise<void> {\n const nonce = await publicClient.getTransactionCount({ address, blockTag });\n nonceRef.current = nonce;\n channel?.postMessage(JSON.stringify(nonceRef.current));\n debug(\"reset nonce to\", nonceRef.current);\n }\n\n function shouldResetNonce(error: unknown): boolean {\n return (\n error instanceof BaseError &&\n error.walk((e) => e instanceof NonceTooLowError || e instanceof NonceTooHighError) != null\n );\n }\n\n return {\n hasNonce,\n nextNonce,\n resetNonce,\n shouldResetNonce,\n };\n}\n","import createDebug from \"debug\";\n\nexport const debug = createDebug(\"mud:common\");\n","import { generatePrivateKey, privateKeyToAccount } from \"viem/accounts\";\nimport { isHex, Hex } from \"viem\";\n\nfunction assertPrivateKey(privateKey: string, cacheKey: string): asserts privateKey is Hex {\n if (!isHex(privateKey)) {\n console.error(\"Private key found in cache is not valid hex\", { privateKey, cacheKey });\n throw new Error(`Private key found in cache (${cacheKey}) is not valid hex`);\n }\n // ensure we can extract address from private key\n // this should throw on bad private keys\n privateKeyToAccount(privateKey);\n}\n\nexport function getBurnerPrivateKey(cacheKey = \"mud:burnerWallet\"): Hex {\n const cachedPrivateKey = localStorage.getItem(cacheKey);\n\n if (cachedPrivateKey != null) {\n assertPrivateKey(cachedPrivateKey, cacheKey);\n return cachedPrivateKey;\n }\n\n const privateKey = generatePrivateKey();\n console.log(\"New burner wallet created:\", privateKeyToAccount(privateKey));\n localStorage.setItem(cacheKey, privateKey);\n return privateKey;\n}\n","import { Hex, hexToString, sliceHex } from \"viem\";\nimport { ResourceId } from \"./common\";\nimport { ResourceType, resourceTypes } from \"./resourceTypes\";\nimport { resourceTypeIds } from \"./resourceIdToHex\";\nimport { ReverseMap } from \"./type-utils/common\";\n\nconst resourceTypeIdToType = Object.fromEntries(\n Object.entries(resourceTypeIds).map(([key, value]) => [value, key])\n) as ReverseMap<typeof resourceTypeIds>;\n\nfunction getResourceType(resourceTypeId: string): ResourceType | undefined {\n // TODO: replace Partial with `noUncheckedIndexedAccess`\n const type = (resourceTypeIdToType as Partial<Record<string, ResourceType>>)[resourceTypeId];\n if (resourceTypes.includes(type as ResourceType)) {\n return type;\n }\n}\n\nexport function hexToResourceId(hex: Hex): ResourceId {\n const resourceTypeId = hexToString(sliceHex(hex, 0, 2)).replace(/\\0+$/, \"\");\n const type = getResourceType(resourceTypeId);\n const namespace = hexToString(sliceHex(hex, 2, 16)).replace(/\\0+$/, \"\");\n const name = hexToString(sliceHex(hex, 16, 32)).replace(/\\0+$/, \"\");\n\n if (!type) {\n throw new Error(`Unknown resource type: ${resourceTypeId}`);\n }\n\n return { type, namespace, name };\n}\n","export const resourceTypes = [\"table\", \"offchainTable\", \"namespace\", \"module\", \"system\"] as const;\n\nexport type ResourceType = (typeof resourceTypes)[number];\n","import { Hex, stringToHex, concatHex } from \"viem\";\nimport { ResourceId } from \"./common\";\nimport { ResourceType } from \"./resourceTypes\";\n\n/** @internal */\nexport const resourceTypeIds = {\n // keep these in sync with storeResourceTypes.sol\n table: \"tb\",\n offchainTable: \"ot\",\n // keep these in sync with worldResourceTypes.sol\n namespace: \"ns\",\n module: \"md\",\n system: \"sy\",\n} as const satisfies Record<ResourceType, string>;\n\nexport function resourceIdToHex(resourceId: ResourceId): Hex {\n const typeId = resourceTypeIds[resourceId.type];\n return concatHex([\n stringToHex(typeId, { size: 2 }),\n stringToHex(resourceId.namespace.slice(0, 14), { size: 14 }),\n stringToHex(resourceId.name.slice(0, 16), { size: 16 }),\n ]);\n}\n","import { Hex } from \"viem\";\n\n/**\n * Get the hex value at start/end positions. This will always return a valid hex string.\n *\n * If `start` is out of range, this returns `\"0x\"`.\n *\n * If `end` is specified and out of range, the result is right zero-padded to the desired length (`end - start`).\n */\nexport function readHex(data: Hex, start: number, end?: number): Hex {\n return `0x${data\n .replace(/^0x/, \"\")\n .slice(start * 2, end != null ? end * 2 : undefined)\n .padEnd(((end ?? start) - start) * 2, \"0\")}`;\n}\n","import { Hex, concatHex } from \"viem\";\nimport { readHex } from \"./readHex\";\n\nexport function spliceHex(data: Hex, start: number, deleteCount = 0, newData: Hex = \"0x\"): Hex {\n return concatHex([readHex(data, 0, start), newData, readHex(data, start + deleteCount)]);\n}\n","import { Hex, Transport, keccak256 } from \"viem\";\nimport { debug as parentDebug } from \"./debug\";\n\nconst debug = parentDebug.extend(\"transportObserver\");\n\nexport function transportObserver<TTransport extends Transport>(transport: TTransport): TTransport {\n return ((opts) => {\n const result = transport(opts);\n const request: typeof result.request = async (req) => {\n if (req.method === \"eth_sendRawTransaction\" && req.params instanceof Array) {\n const txs = req.params.map((data: Hex) => keccak256(data));\n debug(\"saw txs\", txs);\n // TODO: pass these tx hashes into dev tools\n }\n // TODO: add support for `eth_sendTransaction`\n return result.request(req);\n };\n return {\n ...result,\n request,\n };\n }) as TTransport;\n}\n"],"mappings":"AACA,OAAS,uBAAAA,MAA2B,gBAE7B,SAASC,EAAoBC,EAA0B,CAG5D,MAAO,CACL,GAHcF,EAAoBE,CAAU,CAI9C,CACF,CCTA,OAaE,eAAAC,MACK,OACP,OAAOC,MAAY,UCfnB,OACE,aAAAC,EAGA,qBAAAC,EACA,oBAAAC,MAGK,OCRP,OAAOC,MAAiB,QAEjB,IAAMC,EAAQD,EAAY,YAAY,EDS7C,IAAME,EAAQA,EAAY,OAAO,oBAAoB,EAe9C,SAASC,EAAmB,CACjC,aAAAC,EACA,QAAAC,EACA,SAAAC,CACF,EAAwD,CACtD,IAAMC,EAAW,CAAE,QAAS,EAAG,EACzBC,EACJ,OAAO,iBAAqB,IAExB,IAAI,iBAAiB,0BAA0BJ,EAAa,OAAO,MAAMC,GAAS,EAClF,KAEFG,GACFA,EAAQ,iBAAiB,UAAYC,GAAU,CAC7C,IAAMC,EAAQ,KAAK,MAAMD,EAAM,IAAI,EACnCP,EAAM,mCAAoCQ,CAAK,EAC/CH,EAAS,QAAUG,CACrB,CAAC,EAGH,SAASC,GAAoB,CAC3B,OAAOJ,EAAS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yToAccount","createBurnerAccount","privateKey","getContract","pRetry","BaseError","NonceTooHighError","NonceTooLowError","createDebug","debug","debug","createNonceManager","publicClient","address","blockTag","nonceRef","channel","event","nonce","hasNonce","nextNonce","resetNonce","shouldResetNonce","error","BaseError","e","NonceTooLowError","NonceTooHighError","debug","getFunctionParameters","values","hasArgs","args","options","createContract","abi","address","publicClient","walletClient","onWrite","contract","getContract","nextWriteId","nonceManager","createNonceManager","_","functionName","prepareWrite","request","write","preparedWrite","pRetry","nonce","error","parameters","id","result","generatePrivateKey","privateKeyToAccount","isHex","assertPrivateKey","privateKey","cacheKey","getBurnerPrivateKey","cachedPrivateKey","hexToString","sliceHex","resourceTypes","stringToHex","concatHex","resourceTypeIds","resourceIdToHex","resourceId","typeId","resourceTypeIdToType","resourceTypeIds","key","value","getResourceType","resourceTypeId","type","resourceTypes","hexToResourceId","hex","hexToString","sliceHex","namespace","name","readHex","data","start","end","concatHex","spliceHex","data","start","deleteCount","newData","concatHex","readHex","keccak256","debug","transportObserver","transport","opts","result","req","txs","data","keccak256"]}
|
|
1
|
+
{"version":3,"sources":["../src/createBurnerAccount.ts","../src/createNonceManager.ts","../src/getNonceManagerId.ts","../src/getContract.ts","../src/writeContract.ts","../src/getNonceManager.ts","../src/getBurnerPrivateKey.ts","../src/hexToResource.ts","../src/resourceTypes.ts","../src/resourceToHex.ts","../src/readHex.ts","../src/sendTransaction.ts","../src/spliceHex.ts","../src/transportObserver.ts","../src/deprecated/createContract.ts","../src/deprecated/resourceIdToHex.ts","../src/deprecated/hexToResourceId.ts"],"sourcesContent":["import { Account, Hex } from \"viem\";\nimport { privateKeyToAccount } from \"viem/accounts\";\n\nexport function createBurnerAccount(privateKey: Hex): Account {\n const account = privateKeyToAccount(privateKey);\n // We may override account features here\n return {\n ...account,\n };\n}\n","import { BaseError, BlockTag, Client, Hex, NonceTooHighError, NonceTooLowError } from \"viem\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManagerId } from \"./getNonceManagerId\";\nimport { getTransactionCount } from \"viem/actions\";\nimport PQueue from \"p-queue\";\n\nconst debug = parentDebug.extend(\"createNonceManager\");\n\nexport type CreateNonceManagerOptions = {\n client: Client;\n address: Hex;\n blockTag?: BlockTag;\n broadcastChannelName?: string;\n};\n\nexport type CreateNonceManagerResult = {\n hasNonce: () => boolean;\n nextNonce: () => number;\n resetNonce: () => Promise<void>;\n shouldResetNonce: (error: unknown) => boolean;\n mempoolQueue: PQueue;\n};\n\nexport function createNonceManager({\n client,\n address, // TODO: rename to account?\n blockTag = \"pending\",\n broadcastChannelName,\n}: CreateNonceManagerOptions): CreateNonceManagerResult {\n const nonceRef = { current: -1 };\n let channel: BroadcastChannel | null = null;\n\n if (typeof BroadcastChannel !== \"undefined\") {\n const channelName = broadcastChannelName\n ? Promise.resolve(broadcastChannelName)\n : getNonceManagerId({ client, address, blockTag });\n channelName.then((name) => {\n channel = new BroadcastChannel(name);\n // TODO: emit some sort of \"connected\" event so other channels can broadcast current nonce\n channel.addEventListener(\"message\", (event) => {\n const nonce = JSON.parse(event.data);\n debug(\"got nonce from broadcast channel\", nonce);\n nonceRef.current = nonce;\n });\n });\n }\n\n function hasNonce(): boolean {\n return nonceRef.current >= 0;\n }\n\n function nextNonce(): number {\n if (!hasNonce()) throw new Error(\"call resetNonce before using nextNonce\");\n const nonce = nonceRef.current++;\n channel?.postMessage(JSON.stringify(nonceRef.current));\n return nonce;\n }\n\n async function resetNonce(): Promise<void> {\n const nonce = await getTransactionCount(client, { address, blockTag });\n nonceRef.current = nonce;\n channel?.postMessage(JSON.stringify(nonceRef.current));\n debug(\"reset nonce to\", nonceRef.current);\n }\n\n function shouldResetNonce(error: unknown): boolean {\n return (\n error instanceof BaseError &&\n error.walk((e) => e instanceof NonceTooLowError || e instanceof NonceTooHighError) != null\n );\n }\n\n const mempoolQueue = new PQueue({ concurrency: 1 });\n\n return {\n hasNonce,\n nextNonce,\n resetNonce,\n shouldResetNonce,\n mempoolQueue,\n };\n}\n","import { BlockTag, Client, Hex, getAddress } from \"viem\";\nimport { getChainId } from \"viem/actions\";\n\nexport async function getNonceManagerId({\n client,\n address,\n blockTag,\n}: {\n client: Client;\n address: Hex;\n blockTag: BlockTag;\n}): Promise<string> {\n // TODO: improve this so we don't have to call getChainId every time\n const chainId = client.chain?.id ?? (await getChainId(client));\n return `mud:createNonceManager:${chainId}:${getAddress(address)}:${blockTag}`;\n}\n","import {\n Abi,\n Account,\n Address,\n Chain,\n GetContractParameters,\n GetContractReturnType,\n Hex,\n PublicClient,\n Transport,\n WalletClient,\n WriteContractParameters,\n getContract as viem_getContract,\n} from \"viem\";\nimport { UnionOmit } from \"./type-utils/common\";\nimport { writeContract } from \"./writeContract\";\n\n// copied from viem because this isn't exported\n// TODO: import from viem?\nfunction getFunctionParameters(values: [args?: readonly unknown[], options?: object]): {\n args: readonly unknown[];\n options: object;\n} {\n const hasArgs = values.length && Array.isArray(values[0]);\n const args = hasArgs ? values[0]! : [];\n const options = (hasArgs ? values[1] : values[0]) ?? {};\n return { args, options };\n}\n\nexport type ContractWrite = {\n id: string;\n request: WriteContractParameters;\n result: Promise<Hex>;\n};\n\nexport type GetContractOptions<\n TTransport extends Transport,\n TAddress extends Address,\n TAbi extends Abi,\n TChain extends Chain,\n TAccount extends Account,\n TPublicClient extends PublicClient<TTransport, TChain>,\n TWalletClient extends WalletClient<TTransport, TChain, TAccount>\n> = Required<GetContractParameters<TTransport, TChain, TAccount, TAbi, TPublicClient, TWalletClient, TAddress>> & {\n onWrite?: (write: ContractWrite) => void;\n};\n\n// TODO: migrate away from this approach once we can hook into viem: https://github.com/wagmi-dev/viem/discussions/1230\n\nexport function getContract<\n TTransport extends Transport,\n TAddress extends Address,\n TAbi extends Abi,\n TChain extends Chain,\n TAccount extends Account,\n TPublicClient extends PublicClient<TTransport, TChain>,\n TWalletClient extends WalletClient<TTransport, TChain, TAccount>\n>({\n abi,\n address,\n publicClient,\n walletClient,\n onWrite,\n}: GetContractOptions<\n TTransport,\n TAddress,\n TAbi,\n TChain,\n TAccount,\n TPublicClient,\n TWalletClient\n>): GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress> {\n const contract = viem_getContract<TTransport, TAddress, TAbi, TChain, TAccount, TPublicClient, TWalletClient>({\n abi,\n address,\n publicClient,\n walletClient,\n }) as unknown as GetContractReturnType<Abi, PublicClient, WalletClient>;\n\n if (contract.write) {\n // Replace write calls with our own. Implemented ~the same as viem, but adds better handling of nonces (via queue + retries).\n let nextWriteId = 0;\n contract.write = new Proxy(\n {},\n {\n get(_, functionName: string) {\n return (\n ...parameters: [\n args?: readonly unknown[],\n options?: UnionOmit<WriteContractParameters, \"abi\" | \"address\" | \"functionName\" | \"args\">\n ]\n ) => {\n const { args, options } = getFunctionParameters(parameters);\n const request = {\n abi,\n address,\n functionName,\n args,\n ...options,\n onWrite,\n } as unknown as WriteContractParameters<TAbi, typeof functionName, TChain, TAccount>;\n const result = writeContract(walletClient, request);\n\n const id = `${walletClient.chain.id}:${walletClient.account.address}:${nextWriteId++}`;\n onWrite?.({ id, request: request as WriteContractParameters, result });\n\n return result;\n };\n },\n }\n );\n }\n\n return contract as unknown as GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress>;\n}\n","import {\n Abi,\n Account,\n Chain,\n Client,\n SimulateContractParameters,\n Transport,\n WriteContractParameters,\n WriteContractReturnType,\n} from \"viem\";\nimport { simulateContract, writeContract as viem_writeContract } from \"viem/actions\";\nimport pRetry from \"p-retry\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManager } from \"./getNonceManager\";\nimport { parseAccount } from \"viem/accounts\";\n\nconst debug = parentDebug.extend(\"writeContract\");\n\n// TODO: migrate away from this approach once we can hook into viem's nonce management: https://github.com/wagmi-dev/viem/discussions/1230\n\nexport async function writeContract<\n TChain extends Chain | undefined,\n TAccount extends Account | undefined,\n TAbi extends Abi | readonly unknown[],\n TFunctionName extends string,\n TChainOverride extends Chain | undefined\n>(\n client: Client<Transport, TChain, TAccount>,\n request: WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>\n): Promise<WriteContractReturnType> {\n const rawAccount = request.account ?? client.account;\n if (!rawAccount) {\n // TODO: replace with viem AccountNotFoundError once its exported\n throw new Error(\"No account provided\");\n }\n const account = parseAccount(rawAccount);\n\n const nonceManager = await getNonceManager({\n client,\n address: account.address,\n blockTag: \"pending\",\n });\n\n async function prepareWrite(): Promise<\n WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>\n > {\n if (request.gas) {\n debug(\"gas provided, skipping simulate\", request.functionName, request.address);\n return request;\n }\n\n debug(\"simulating\", request.functionName, \"at\", request.address);\n const result = await simulateContract<TChain, TAbi, TFunctionName, TChainOverride>(client, {\n ...request,\n blockTag: \"pending\",\n account,\n } as unknown as SimulateContractParameters<TAbi, TFunctionName, TChain, TChainOverride>);\n\n return result.request as unknown as WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>;\n }\n\n const preparedWrite = await prepareWrite();\n\n return nonceManager.mempoolQueue.add(\n () =>\n pRetry(\n async () => {\n if (!nonceManager.hasNonce()) {\n await nonceManager.resetNonce();\n }\n\n const nonce = nonceManager.nextNonce();\n debug(\"calling\", preparedWrite.functionName, \"with nonce\", nonce, \"at\", preparedWrite.address);\n return await viem_writeContract(client, { nonce, ...preparedWrite } as typeof preparedWrite);\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // On nonce errors, reset the nonce and retry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error.message);\n await nonceManager.resetNonce();\n return;\n }\n // TODO: prepareWrite again if there are gas errors?\n throw error;\n },\n }\n ),\n { throwOnTimeout: true }\n );\n}\n","import { CreateNonceManagerOptions, CreateNonceManagerResult, createNonceManager } from \"./createNonceManager\";\nimport { getNonceManagerId } from \"./getNonceManagerId\";\n\nconst nonceManagers = new Map<string, CreateNonceManagerResult>();\n\nexport async function getNonceManager({\n client,\n address, // TODO: rename to account?\n blockTag = \"pending\",\n}: CreateNonceManagerOptions): Promise<CreateNonceManagerResult> {\n const id = await getNonceManagerId({ client, address, blockTag });\n\n const existingNonceManager = nonceManagers.get(id);\n if (existingNonceManager) {\n return existingNonceManager;\n }\n\n const nonceManager = createNonceManager({ client, address, blockTag });\n nonceManagers.set(id, nonceManager);\n return nonceManager;\n}\n","import { generatePrivateKey, privateKeyToAccount } from \"viem/accounts\";\nimport { isHex, Hex } from \"viem\";\n\nfunction assertPrivateKey(privateKey: string, cacheKey: string): asserts privateKey is Hex {\n if (!isHex(privateKey)) {\n console.error(\"Private key found in cache is not valid hex\", { privateKey, cacheKey });\n throw new Error(`Private key found in cache (${cacheKey}) is not valid hex`);\n }\n // ensure we can extract address from private key\n // this should throw on bad private keys\n privateKeyToAccount(privateKey);\n}\n\nexport function getBurnerPrivateKey(cacheKey = \"mud:burnerWallet\"): Hex {\n const cachedPrivateKey = localStorage.getItem(cacheKey);\n\n if (cachedPrivateKey != null) {\n assertPrivateKey(cachedPrivateKey, cacheKey);\n return cachedPrivateKey;\n }\n\n const privateKey = generatePrivateKey();\n console.log(\"New burner wallet created:\", privateKeyToAccount(privateKey));\n localStorage.setItem(cacheKey, privateKey);\n return privateKey;\n}\n","import { Hex, hexToString, sliceHex } from \"viem\";\nimport { Resource } from \"./common\";\nimport { ResourceType, resourceTypes } from \"./resourceTypes\";\nimport { resourceTypeIds } from \"./resourceToHex\";\nimport { ReverseMap } from \"./type-utils/common\";\n\nconst resourceTypeIdToType = Object.fromEntries(\n Object.entries(resourceTypeIds).map(([key, value]) => [value, key])\n) as ReverseMap<typeof resourceTypeIds>;\n\nfunction getResourceType(resourceTypeId: string): ResourceType | undefined {\n // TODO: replace Partial with `noUncheckedIndexedAccess`\n const type = (resourceTypeIdToType as Partial<Record<string, ResourceType>>)[resourceTypeId];\n if (resourceTypes.includes(type as ResourceType)) {\n return type;\n }\n}\n\nexport function hexToResource(hex: Hex): Resource {\n const resourceTypeId = hexToString(sliceHex(hex, 0, 2)).replace(/\\0+$/, \"\");\n const type = getResourceType(resourceTypeId);\n const namespace = hexToString(sliceHex(hex, 2, 16)).replace(/\\0+$/, \"\");\n const name = hexToString(sliceHex(hex, 16, 32)).replace(/\\0+$/, \"\");\n\n if (!type) {\n throw new Error(`Unknown type (${resourceTypeId}) for resource (${resourceTypeId}:${namespace}:${name})`);\n }\n\n return { resourceId: hex, type, namespace, name };\n}\n","export const resourceTypes = [\"table\", \"offchainTable\", \"namespace\", \"module\", \"system\"] as const;\n\nexport type ResourceType = (typeof resourceTypes)[number];\n","import { Hex, stringToHex, concatHex } from \"viem\";\nimport { Resource } from \"./common\";\nimport { ResourceType } from \"./resourceTypes\";\n\n/** @internal */\nexport const resourceTypeIds = {\n // keep these in sync with storeResourceTypes.sol\n table: \"tb\",\n offchainTable: \"ot\",\n // keep these in sync with worldResourceTypes.sol\n namespace: \"ns\",\n module: \"md\",\n system: \"sy\",\n} as const satisfies Record<ResourceType, string>;\n\nexport function resourceToHex(resource: Omit<Resource, \"resourceId\">): Hex {\n const typeId = resourceTypeIds[resource.type];\n return concatHex([\n stringToHex(typeId, { size: 2 }),\n stringToHex(resource.namespace.slice(0, 14), { size: 14 }),\n stringToHex(resource.name.slice(0, 16), { size: 16 }),\n ]);\n}\n","import { Hex } from \"viem\";\n\n/**\n * Get the hex value at start/end positions. This will always return a valid hex string.\n *\n * If `start` is out of range, this returns `\"0x\"`.\n *\n * If `end` is specified and out of range, the result is right zero-padded to the desired length (`end - start`).\n */\nexport function readHex(data: Hex, start: number, end?: number): Hex {\n return `0x${data\n .replace(/^0x/, \"\")\n .slice(start * 2, end != null ? end * 2 : undefined)\n .padEnd(((end ?? start) - start) * 2, \"0\")}`;\n}\n","import {\n Account,\n CallParameters,\n Chain,\n Client,\n SendTransactionParameters,\n Transport,\n WriteContractReturnType,\n} from \"viem\";\nimport { call, sendTransaction as viem_sendTransaction } from \"viem/actions\";\nimport pRetry from \"p-retry\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManager } from \"./getNonceManager\";\nimport { parseAccount } from \"viem/accounts\";\n\nconst debug = parentDebug.extend(\"sendTransaction\");\n\n// TODO: migrate away from this approach once we can hook into viem's nonce management: https://github.com/wagmi-dev/viem/discussions/1230\n\nexport async function sendTransaction<\n TChain extends Chain | undefined,\n TAccount extends Account | undefined,\n TChainOverride extends Chain | undefined\n>(\n client: Client<Transport, TChain, TAccount>,\n request: SendTransactionParameters<TChain, TAccount, TChainOverride>\n): Promise<WriteContractReturnType> {\n const rawAccount = request.account ?? client.account;\n if (!rawAccount) {\n // TODO: replace with viem AccountNotFoundError once its exported\n throw new Error(\"No account provided\");\n }\n const account = parseAccount(rawAccount);\n\n const nonceManager = await getNonceManager({\n client,\n address: account.address,\n blockTag: \"pending\",\n });\n\n async function prepare(): Promise<SendTransactionParameters<TChain, TAccount, TChainOverride>> {\n if (request.gas) {\n debug(\"gas provided, skipping simulate\", request.to);\n return request;\n }\n\n debug(\"simulating tx to\", request.to);\n await call(client, {\n ...request,\n blockTag: \"pending\",\n account,\n } as CallParameters<TChain>);\n\n // TODO: estimate gas\n\n return request;\n }\n\n const preparedRequest = await prepare();\n\n return await nonceManager.mempoolQueue.add(\n () =>\n pRetry(\n async () => {\n if (!nonceManager.hasNonce()) {\n await nonceManager.resetNonce();\n }\n\n const nonce = nonceManager.nextNonce();\n debug(\"sending tx with nonce\", nonce, \"to\", preparedRequest.to);\n return await viem_sendTransaction(client, { nonce, ...preparedRequest });\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // On nonce errors, reset the nonce and retry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error.message);\n await nonceManager.resetNonce();\n return;\n }\n // TODO: prepare again if there are gas errors?\n throw error;\n },\n }\n ),\n { throwOnTimeout: true }\n );\n}\n","import { Hex, concatHex } from \"viem\";\nimport { readHex } from \"./readHex\";\n\nexport function spliceHex(data: Hex, start: number, deleteCount = 0, newData: Hex = \"0x\"): Hex {\n return concatHex([readHex(data, 0, start), newData, readHex(data, start + deleteCount)]);\n}\n","import { Hex, Transport, keccak256 } from \"viem\";\nimport { debug as parentDebug } from \"./debug\";\n\nconst debug = parentDebug.extend(\"transportObserver\");\n\nexport function transportObserver<TTransport extends Transport>(transport: TTransport): TTransport {\n return ((opts) => {\n const result = transport(opts);\n const request: typeof result.request = async (req) => {\n if (req.method === \"eth_sendRawTransaction\" && req.params instanceof Array) {\n const txs = req.params.map((data: Hex) => keccak256(data));\n debug(\"saw txs\", txs);\n // TODO: pass these tx hashes into dev tools\n }\n // TODO: add support for `eth_sendTransaction`\n return result.request(req);\n };\n return {\n ...result,\n request,\n };\n }) as TTransport;\n}\n","import { getContract } from \"../getContract\";\n\n/** @deprecated use `getContract` instead */\nexport const createContract = getContract;\n","import { resourceToHex } from \"../resourceToHex\";\n\n/** @deprecated use `resourceToHex` instead */\nexport const resourceIdToHex = resourceToHex;\n","import { hexToResource } from \"../hexToResource\";\n\n/** @deprecated use `hexToResource` instead */\nexport const hexToResourceId = hexToResource;\n"],"mappings":"wCACA,OAAS,uBAAAA,MAA2B,gBAE7B,SAASC,GAAoBC,EAA0B,CAG5D,MAAO,CACL,GAHcF,EAAoBE,CAAU,CAI9C,CACF,CCTA,OAAS,aAAAC,EAAkC,qBAAAC,EAAmB,oBAAAC,MAAwB,OCAtF,OAAgC,cAAAC,MAAkB,OAClD,OAAS,cAAAC,MAAkB,eAE3B,eAAsBC,EAAkB,CACtC,OAAAC,EACA,QAAAC,EACA,SAAAC,CACF,EAIoB,CAGlB,MAAO,0BADSF,EAAO,OAAO,IAAO,MAAMF,EAAWE,CAAM,KAChBH,EAAWI,CAAO,KAAKC,GACrE,CDZA,OAAS,uBAAAC,MAA2B,eACpC,OAAOC,MAAY,UAEnB,IAAMC,EAAQA,EAAY,OAAO,oBAAoB,EAiB9C,SAASC,EAAmB,CACjC,OAAAC,EACA,QAAAC,EACA,SAAAC,EAAW,UACX,qBAAAC,CACF,EAAwD,CACtD,IAAMC,EAAW,CAAE,QAAS,EAAG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AO,MAAMF,EAAa,aAAa,IACrC,IACEK,GACE,SAAY,CACLL,EAAa,SAAS,GACzB,MAAMA,EAAa,WAAW,EAGhC,IAAMM,EAAQN,EAAa,UAAU,EACrC,OAAAN,EAAM,wBAAyBY,EAAO,KAAMF,EAAgB,EAAE,EACvD,MAAMG,GAAqBX,EAAQ,CAAE,MAAAU,EAAO,GAAGF,CAAgB,CAAC,CACzE,EACA,CACE,QAAS,EACT,gBAAiB,MAAOI,GAAU,CAEhC,GAAIR,EAAa,iBAAiBQ,CAAK,EAAG,CACxCd,EAAM,4BAA6Bc,EAAM,OAAO,EAChD,MAAMR,EAAa,WAAW,EAC9B,OAGF,MAAMQ,CACR,CACF,CACF,EACF,CAAE,eAAgB,EAAK,CACzB,CACF,CCxFA,OAAc,aAAAC,OAAiB,OAGxB,SAASC,GAAUC,EAAWC,EAAeC,EAAc,EAAGC,EAAe,KAAW,CAC7F,OAAOC,GAAU,CAACC,EAAQL,EAAM,EAAGC,CAAK,EAAGE,EAASE,EAAQL,EAAMC,EAAQC,CAAW,CAAC,CAAC,CACzF,CCLA,OAAyB,aAAAI,OAAiB,OAG1C,IAAMC,GAAQA,EAAY,OAAO,mBAAmB,EAE7C,SAASC,GAAgDC,EAAmC,CACjG,OAASC,GAAS,CAChB,IAAMC,EAASF,EAAUC,CAAI,EAU7B,MAAO,CACL,GAAGC,EACH,QAXqC,MAAOC,GAAQ,CACpD,GAAIA,EAAI,SAAW,0BAA4BA,EAAI,kBAAkB,MAAO,CAC1E,IAAMC,EAAMD,EAAI,OAAO,IAAKE,GAAcC,GAAUD,CAAI,CAAC,EACzDP,GAAM,UAAWM,CAAG,EAItB,OAAOF,EAAO,QAAQC,CAAG,CAC3B,CAIA,CACF,CACF,CCnBO,IAAMI,GAAiBC,ECAvB,IAAMC,GAAkBC,ECAxB,IAAMC,GAAkBC","names":["privateKeyToAccount","createBurnerAccount","privateKey","BaseError","NonceTooHighError","NonceTooLowError","getAddress","getChainId","getNonceManagerId","client","address","blockTag","getTransactionCount","PQueue","debug","createNonceManager","client","address","blockTag","broadcastChannelName","nonceRef","channel","getNonceManagerId","name","event","nonce","hasNonce","nextNonce","resetNonce","shouldResetNonce","error","BaseError","e","NonceTooLowError","NonceTooHighError","mempoolQueue","viem_getContract","simulateContract","viem_writeContract","pRetry","nonceManagers","getNonceManager","client","address","blockTag","id","getNonceManagerId","existingNonceManager","nonceManager","createNonceManager","parseAccount","debug","writeContract","client","request","rawAccount","account","nonceManager","getNonceManager","prepareWrite","simulateContract","preparedWrite","pRetry","nonce","viem_writeContract","error","getFunctionParameters","values","hasArgs","args","options","getContract","abi","address","publicClient","walletClient","onWrite","contract","viem_getContract","nextWriteId","_","functionName","parameters","request","result","writeContract","id","generatePrivateKey","privateKeyToAccount","isHex","assertPrivateKey","privateKey","cacheKey","getBurnerPrivateKey","cachedPrivateKey","hexToString","sliceHex","resourceTypes","stringToHex","concatHex","resourceTypeIds","resourceToHex","resource","typeId","resourceTypeIdToType","resourceTypeIds","key","value","getResourceType","resourceTypeId","type","resourceTypes","hexToResource","hex","hexToString","sliceHex","namespace","name","readHex","data","start","end","call","viem_sendTransaction","pRetry","parseAccount","debug","sendTransaction","client","request","rawAccount","account","nonceManager","getNonceManager","prepare","call","preparedRequest","pRetry","nonce","viem_sendTransaction","error","concatHex","spliceHex","data","start","deleteCount","newData","concatHex","readHex","keccak256","debug","transportObserver","transport","opts","result","req","txs","data","keccak256","createContract","getContract","resourceIdToHex","resourceToHex","hexToResourceId","hexToResource"]}
|
package/dist/utils.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function o(r,e){throw new Error(e??`Unexpected value: ${r}`)}function a(...r){return r.reduce((e,t)=>t>e?t:e)}function s(...r){return r.reduce((e,t)=>t<e?t:e)}function f(r,e){return r<e?-1:r>e?1:0}function*x(r,e){for(let t=0;t<r.length;t+=e)yield r.slice(t,t+e)}function c(r,...e){return(...t)=>r(...e,...t)}function y(r){return r}function v(r){return r!==void 0}function g(r){return r!==null}async function w(r){let e=[];for await(let t of r)e.push(t);return e}function I(r,e){let t=new Map;for(let n of r)t.set(e(n),n);return Array.from(t.values())}function k(r){return new Promise(e=>setTimeout(()=>e(),r))}function R(){return new Promise(r=>{requestIdleCallback(()=>r())})}export{o as assertExhaustive,a as bigIntMax,s as bigIntMin,f as bigIntSort,x as chunk,c as curry,y as identity,v as isDefined,g as isNotNull,w as iteratorToArray,I as uniqueBy,k as wait,R as waitForIdle};
|
|
2
2
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/assertExhaustive.ts","../src/utils/bigIntMax.ts","../src/utils/bigIntMin.ts","../src/utils/bigIntSort.ts","../src/utils/chunk.ts","../src/utils/curry.ts","../src/utils/identity.ts","../src/utils/isDefined.ts","../src/utils/isNotNull.ts","../src/utils/iteratorToArray.ts","../src/utils/wait.ts","../src/utils/waitForIdle.ts"],"sourcesContent":["export function assertExhaustive(value: never, message?: string): never {\n throw new Error(message ?? `Unexpected value: ${value}`);\n}\n","export function bigIntMax(...args: bigint[]): bigint {\n return args.reduce((m, e) => (e > m ? e : m));\n}\n","export function bigIntMin(...args: bigint[]): bigint {\n return args.reduce((m, e) => (e < m ? e : m));\n}\n","export function bigIntSort(a: bigint, b: bigint): -1 | 0 | 1 {\n return a < b ? -1 : a > b ? 1 : 0;\n}\n","export function* chunk<T>(arr: T[], n: number): Generator<T[], void> {\n for (let i = 0; i < arr.length; i += n) {\n yield arr.slice(i, i + n);\n }\n}\n","export function curry<F extends (...params: [...P, ...any[]]) => any, P extends any[]>(\n func: F,\n ...partialParams: P\n): CurryParams<F, P> {\n return ((...args: any[]) => func(...partialParams, ...args)) as CurryParams<F, P>;\n}\n\ntype CurryParams<F extends (...params: [...PartialParams, ...any[]]) => any, PartialParams extends any[]> = F extends (\n ...params: [...PartialParams, ...infer RemainingParams]\n) => infer Result\n ? (...params: RemainingParams) => Result\n : never;\n","export function identity<T>(value: T): T {\n return value;\n}\n","export function isDefined<T>(argument: T | undefined): argument is T {\n return argument !== undefined;\n}\n","export function isNotNull<T>(argument: T | null): argument is T {\n return argument !== null;\n}\n","export async function iteratorToArray<T>(iterator: AsyncIterable<T>): Promise<T[]> {\n const items: T[] = [];\n for await (const item of iterator) {\n items.push(item);\n }\n return items;\n}\n","export function wait(ms: number): Promise<void> {\n return new Promise<void>((resolve) => setTimeout(() => resolve(), ms));\n}\n","export function waitForIdle(): Promise<void> {\n return new Promise<void>((resolve) => {\n requestIdleCallback(() => resolve());\n });\n}\n"],"mappings":"AAAO,SAASA,EAAiBC,EAAcC,EAAyB,CACtE,MAAM,IAAI,MAAMA,GAAW,qBAAqBD,GAAO,CACzD,CCFO,SAASE,KAAaC,EAAwB,CACnD,OAAOA,EAAK,OAAO,CAACC,EAAGC,IAAOA,EAAID,EAAIC,EAAID,CAAE,CAC9C,CCFO,SAASE,KAAaC,EAAwB,CACnD,OAAOA,EAAK,OAAO,CAACC,EAAGC,IAAOA,EAAID,EAAIC,EAAID,CAAE,CAC9C,CCFO,SAASE,EAAWC,EAAWC,EAAuB,CAC3D,OAAOD,EAAIC,EAAI,GAAKD,EAAIC,EAAI,EAAI,CAClC,CCFO,SAAUC,EAASC,EAAUC,EAAiC,CACnE,QAASC,EAAI,EAAGA,EAAIF,EAAI,OAAQE,GAAKD,EACnC,MAAMD,EAAI,MAAME,EAAGA,EAAID,CAAC,CAE5B,CCJO,SAASE,EACdC,KACGC,EACgB,CACnB,MAAQ,IAAIC,IAAgBF,EAAK,GAAGC,EAAe,GAAGC,CAAI,CAC5D,CCLO,SAASC,EAAYC,EAAa,CACvC,OAAOA,CACT,CCFO,SAASC,EAAaC,EAAwC,CACnE,OAAOA,IAAa,MACtB,CCFO,SAASC,EAAaC,EAAmC,CAC9D,OAAOA,IAAa,IACtB,CCFA,eAAsBC,EAAmBC,EAA0C,CACjF,IAAMC,EAAa,CAAC,EACpB,cAAiBC,KAAQF,EACvBC,EAAM,KAAKC,CAAI,EAEjB,OAAOD,CACT,CCNO,SAASE,EAAKC,EAA2B,CAC9C,OAAO,IAAI,QAAeC,GAAY,WAAW,IAAMA,EAAQ,EAAGD,CAAE,CAAC,CACvE,CCFO,SAASE,GAA6B,CAC3C,OAAO,IAAI,QAAeC,GAAY,CACpC,oBAAoB,IAAMA,EAAQ,CAAC,CACrC,CAAC,CACH","names":["assertExhaustive","value","message","bigIntMax","args","m","e","bigIntMin","args","m","e","bigIntSort","a","b","chunk","arr","n","i","curry","func","partialParams","args","identity","value","isDefined","argument","isNotNull","argument","iteratorToArray","iterator","items","item","wait","ms","resolve","waitForIdle","resolve"]}
|
|
1
|
+
{"version":3,"sources":["../src/utils/assertExhaustive.ts","../src/utils/bigIntMax.ts","../src/utils/bigIntMin.ts","../src/utils/bigIntSort.ts","../src/utils/chunk.ts","../src/utils/curry.ts","../src/utils/identity.ts","../src/utils/isDefined.ts","../src/utils/isNotNull.ts","../src/utils/iteratorToArray.ts","../src/utils/uniqueBy.ts","../src/utils/wait.ts","../src/utils/waitForIdle.ts"],"sourcesContent":["export function assertExhaustive(value: never, message?: string): never {\n throw new Error(message ?? `Unexpected value: ${value}`);\n}\n","export function bigIntMax(...args: bigint[]): bigint {\n return args.reduce((m, e) => (e > m ? e : m));\n}\n","export function bigIntMin(...args: bigint[]): bigint {\n return args.reduce((m, e) => (e < m ? e : m));\n}\n","export function bigIntSort(a: bigint, b: bigint): -1 | 0 | 1 {\n return a < b ? -1 : a > b ? 1 : 0;\n}\n","export function* chunk<T>(arr: T[], n: number): Generator<T[], void> {\n for (let i = 0; i < arr.length; i += n) {\n yield arr.slice(i, i + n);\n }\n}\n","export function curry<F extends (...params: [...P, ...any[]]) => any, P extends any[]>(\n func: F,\n ...partialParams: P\n): CurryParams<F, P> {\n return ((...args: any[]) => func(...partialParams, ...args)) as CurryParams<F, P>;\n}\n\ntype CurryParams<F extends (...params: [...PartialParams, ...any[]]) => any, PartialParams extends any[]> = F extends (\n ...params: [...PartialParams, ...infer RemainingParams]\n) => infer Result\n ? (...params: RemainingParams) => Result\n : never;\n","export function identity<T>(value: T): T {\n return value;\n}\n","export function isDefined<T>(argument: T | undefined): argument is T {\n return argument !== undefined;\n}\n","export function isNotNull<T>(argument: T | null): argument is T {\n return argument !== null;\n}\n","export async function iteratorToArray<T>(iterator: AsyncIterable<T>): Promise<T[]> {\n const items: T[] = [];\n for await (const item of iterator) {\n items.push(item);\n }\n return items;\n}\n","export function uniqueBy<value, key>(values: readonly value[], getKey: (value: value) => key): readonly value[] {\n const map = new Map<key, value>();\n for (const value of values) {\n map.set(getKey(value), value);\n }\n return Array.from(map.values());\n}\n","export function wait(ms: number): Promise<void> {\n return new Promise<void>((resolve) => setTimeout(() => resolve(), ms));\n}\n","export function waitForIdle(): Promise<void> {\n return new Promise<void>((resolve) => {\n requestIdleCallback(() => resolve());\n });\n}\n"],"mappings":"AAAO,SAASA,EAAiBC,EAAcC,EAAyB,CACtE,MAAM,IAAI,MAAMA,GAAW,qBAAqBD,GAAO,CACzD,CCFO,SAASE,KAAaC,EAAwB,CACnD,OAAOA,EAAK,OAAO,CAACC,EAAGC,IAAOA,EAAID,EAAIC,EAAID,CAAE,CAC9C,CCFO,SAASE,KAAaC,EAAwB,CACnD,OAAOA,EAAK,OAAO,CAACC,EAAGC,IAAOA,EAAID,EAAIC,EAAID,CAAE,CAC9C,CCFO,SAASE,EAAWC,EAAWC,EAAuB,CAC3D,OAAOD,EAAIC,EAAI,GAAKD,EAAIC,EAAI,EAAI,CAClC,CCFO,SAAUC,EAASC,EAAUC,EAAiC,CACnE,QAASC,EAAI,EAAGA,EAAIF,EAAI,OAAQE,GAAKD,EACnC,MAAMD,EAAI,MAAME,EAAGA,EAAID,CAAC,CAE5B,CCJO,SAASE,EACdC,KACGC,EACgB,CACnB,MAAQ,IAAIC,IAAgBF,EAAK,GAAGC,EAAe,GAAGC,CAAI,CAC5D,CCLO,SAASC,EAAYC,EAAa,CACvC,OAAOA,CACT,CCFO,SAASC,EAAaC,EAAwC,CACnE,OAAOA,IAAa,MACtB,CCFO,SAASC,EAAaC,EAAmC,CAC9D,OAAOA,IAAa,IACtB,CCFA,eAAsBC,EAAmBC,EAA0C,CACjF,IAAMC,EAAa,CAAC,EACpB,cAAiBC,KAAQF,EACvBC,EAAM,KAAKC,CAAI,EAEjB,OAAOD,CACT,CCNO,SAASE,EAAqBC,EAA0BC,EAAiD,CAC9G,IAAMC,EAAM,IAAI,IAChB,QAAWC,KAASH,EAClBE,EAAI,IAAID,EAAOE,CAAK,EAAGA,CAAK,EAE9B,OAAO,MAAM,KAAKD,EAAI,OAAO,CAAC,CAChC,CCNO,SAASE,EAAKC,EAA2B,CAC9C,OAAO,IAAI,QAAeC,GAAY,WAAW,IAAMA,EAAQ,EAAGD,CAAE,CAAC,CACvE,CCFO,SAASE,GAA6B,CAC3C,OAAO,IAAI,QAAeC,GAAY,CACpC,oBAAoB,IAAMA,EAAQ,CAAC,CACrC,CAAC,CACH","names":["assertExhaustive","value","message","bigIntMax","args","m","e","bigIntMin","args","m","e","bigIntSort","a","b","chunk","arr","n","i","curry","func","partialParams","args","identity","value","isDefined","argument","isNotNull","argument","iteratorToArray","iterator","items","item","uniqueBy","values","getKey","map","value","wait","ms","resolve","waitForIdle","resolve"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latticexyz/common",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.12",
|
|
4
4
|
"description": "Common low level logic shared between packages",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
".": "./dist/index.js",
|
|
14
14
|
"./chains": "./dist/chains.js",
|
|
15
15
|
"./codegen": "./dist/codegen.js",
|
|
16
|
-
"./deprecated": "./dist/deprecated.js",
|
|
17
16
|
"./errors": "./dist/errors.js",
|
|
18
17
|
"./foundry": "./dist/foundry.js",
|
|
19
18
|
"./type-utils": "./dist/type-utils.js",
|
|
@@ -30,9 +29,6 @@
|
|
|
30
29
|
"codegen": [
|
|
31
30
|
"./src/codegen/index.ts"
|
|
32
31
|
],
|
|
33
|
-
"deprecated": [
|
|
34
|
-
"./src/deprecated/index.ts"
|
|
35
|
-
],
|
|
36
32
|
"errors": [
|
|
37
33
|
"./src/errors/index.ts"
|
|
38
34
|
],
|
|
@@ -49,15 +45,15 @@
|
|
|
49
45
|
},
|
|
50
46
|
"dependencies": {
|
|
51
47
|
"@solidity-parser/parser": "^0.16.0",
|
|
52
|
-
"abitype": "0.9.3",
|
|
53
48
|
"chalk": "^5.2.0",
|
|
54
49
|
"debug": "^4.3.4",
|
|
55
50
|
"execa": "^7.0.0",
|
|
51
|
+
"p-queue": "^7.4.1",
|
|
56
52
|
"p-retry": "^5.1.2",
|
|
57
53
|
"prettier": "^2.8.4",
|
|
58
54
|
"prettier-plugin-solidity": "^1.1.2",
|
|
59
|
-
"viem": "1.
|
|
60
|
-
"@latticexyz/schema-type": "2.0.0-next.
|
|
55
|
+
"viem": "1.14.0",
|
|
56
|
+
"@latticexyz/schema-type": "2.0.0-next.12"
|
|
61
57
|
},
|
|
62
58
|
"devDependencies": {
|
|
63
59
|
"@types/debug": "^4.1.7",
|
package/src/chains/mudFoundry.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { MUDChain } from "./types";
|
|
|
4
4
|
export const mudFoundry = {
|
|
5
5
|
...foundry,
|
|
6
6
|
fees: {
|
|
7
|
-
|
|
7
|
+
// This is intentionally defined as a function as a workaround for https://github.com/wagmi-dev/viem/pull/1280
|
|
8
|
+
defaultPriorityFee: () => 0n,
|
|
8
9
|
},
|
|
9
10
|
} as const satisfies MUDChain;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
2
2
|
import { dirname } from "path";
|
|
3
3
|
import { formatSolidity, formatTypescript } from "./format";
|
|
4
|
+
import { debug } from "../debug";
|
|
4
5
|
|
|
5
6
|
export async function formatAndWriteSolidity(output: string, fullOutputPath: string, logPrefix: string): Promise<void> {
|
|
6
7
|
const formattedOutput = await formatSolidity(output);
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
await mkdir(dirname(fullOutputPath), { recursive: true });
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
await writeFile(fullOutputPath, formattedOutput);
|
|
12
|
+
debug(`${logPrefix}: ${fullOutputPath}`);
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export async function formatAndWriteTypescript(
|
|
@@ -18,8 +19,8 @@ export async function formatAndWriteTypescript(
|
|
|
18
19
|
): Promise<void> {
|
|
19
20
|
const formattedOutput = await formatTypescript(output);
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
await mkdir(dirname(fullOutputPath), { recursive: true });
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
await writeFile(fullOutputPath, formattedOutput);
|
|
25
|
+
debug(`${logPrefix}: ${fullOutputPath}`);
|
|
25
26
|
}
|
|
@@ -2,10 +2,11 @@ import { readFileSync } from "fs";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { SolidityUserDefinedType, extractUserTypes } from "./extractUserTypes";
|
|
4
4
|
import { MUDError } from "../../errors";
|
|
5
|
+
import { SchemaAbiType } from "@latticexyz/schema-type";
|
|
5
6
|
|
|
6
7
|
export type UserType = {
|
|
7
8
|
filePath: string;
|
|
8
|
-
internalType:
|
|
9
|
+
internalType: SchemaAbiType;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
export function loadAndExtractUserTypes(
|
package/src/common.ts
CHANGED
|
@@ -1,46 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BaseError,
|
|
3
|
-
BlockTag,
|
|
4
|
-
Hex,
|
|
5
|
-
NonceTooHighError,
|
|
6
|
-
NonceTooLowError,
|
|
7
|
-
PublicClient,
|
|
8
|
-
TransactionExecutionError,
|
|
9
|
-
} from "viem";
|
|
1
|
+
import { BaseError, BlockTag, Client, Hex, NonceTooHighError, NonceTooLowError } from "viem";
|
|
10
2
|
import { debug as parentDebug } from "./debug";
|
|
3
|
+
import { getNonceManagerId } from "./getNonceManagerId";
|
|
4
|
+
import { getTransactionCount } from "viem/actions";
|
|
5
|
+
import PQueue from "p-queue";
|
|
11
6
|
|
|
12
7
|
const debug = parentDebug.extend("createNonceManager");
|
|
13
8
|
|
|
14
|
-
type CreateNonceManagerOptions = {
|
|
15
|
-
|
|
9
|
+
export type CreateNonceManagerOptions = {
|
|
10
|
+
client: Client;
|
|
16
11
|
address: Hex;
|
|
17
12
|
blockTag?: BlockTag;
|
|
13
|
+
broadcastChannelName?: string;
|
|
18
14
|
};
|
|
19
15
|
|
|
20
|
-
type CreateNonceManagerResult = {
|
|
16
|
+
export type CreateNonceManagerResult = {
|
|
21
17
|
hasNonce: () => boolean;
|
|
22
18
|
nextNonce: () => number;
|
|
23
19
|
resetNonce: () => Promise<void>;
|
|
24
20
|
shouldResetNonce: (error: unknown) => boolean;
|
|
21
|
+
mempoolQueue: PQueue;
|
|
25
22
|
};
|
|
26
23
|
|
|
27
24
|
export function createNonceManager({
|
|
28
|
-
|
|
29
|
-
address,
|
|
30
|
-
blockTag,
|
|
25
|
+
client,
|
|
26
|
+
address, // TODO: rename to account?
|
|
27
|
+
blockTag = "pending",
|
|
28
|
+
broadcastChannelName,
|
|
31
29
|
}: CreateNonceManagerOptions): CreateNonceManagerResult {
|
|
32
30
|
const nonceRef = { current: -1 };
|
|
33
|
-
|
|
34
|
-
typeof BroadcastChannel !== "undefined"
|
|
35
|
-
? // TODO: fetch chain ID or require it via types?
|
|
36
|
-
new BroadcastChannel(`mud:createNonceManager:${publicClient.chain?.id}:${address}`)
|
|
37
|
-
: null;
|
|
31
|
+
let channel: BroadcastChannel | null = null;
|
|
38
32
|
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
if (typeof BroadcastChannel !== "undefined") {
|
|
34
|
+
const channelName = broadcastChannelName
|
|
35
|
+
? Promise.resolve(broadcastChannelName)
|
|
36
|
+
: getNonceManagerId({ client, address, blockTag });
|
|
37
|
+
channelName.then((name) => {
|
|
38
|
+
channel = new BroadcastChannel(name);
|
|
39
|
+
// TODO: emit some sort of "connected" event so other channels can broadcast current nonce
|
|
40
|
+
channel.addEventListener("message", (event) => {
|
|
41
|
+
const nonce = JSON.parse(event.data);
|
|
42
|
+
debug("got nonce from broadcast channel", nonce);
|
|
43
|
+
nonceRef.current = nonce;
|
|
44
|
+
});
|
|
44
45
|
});
|
|
45
46
|
}
|
|
46
47
|
|
|
@@ -56,7 +57,7 @@ export function createNonceManager({
|
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
async function resetNonce(): Promise<void> {
|
|
59
|
-
const nonce = await
|
|
60
|
+
const nonce = await getTransactionCount(client, { address, blockTag });
|
|
60
61
|
nonceRef.current = nonce;
|
|
61
62
|
channel?.postMessage(JSON.stringify(nonceRef.current));
|
|
62
63
|
debug("reset nonce to", nonceRef.current);
|
|
@@ -69,10 +70,13 @@ export function createNonceManager({
|
|
|
69
70
|
);
|
|
70
71
|
}
|
|
71
72
|
|
|
73
|
+
const mempoolQueue = new PQueue({ concurrency: 1 });
|
|
74
|
+
|
|
72
75
|
return {
|
|
73
76
|
hasNonce,
|
|
74
77
|
nextNonce,
|
|
75
78
|
resetNonce,
|
|
76
79
|
shouldResetNonce,
|
|
80
|
+
mempoolQueue,
|
|
77
81
|
};
|
|
78
82
|
}
|
package/src/foundry/index.ts
CHANGED
|
@@ -84,9 +84,12 @@ export async function getRemappings(profile?: string): Promise<[string, string][
|
|
|
84
84
|
* @param args The arguments to pass to forge
|
|
85
85
|
* @param options { profile?: The foundry profile to use; silent?: If true, nothing will be logged to the console }
|
|
86
86
|
*/
|
|
87
|
-
export async function forge(
|
|
87
|
+
export async function forge(
|
|
88
|
+
args: string[],
|
|
89
|
+
options?: { profile?: string; silent?: boolean; env?: NodeJS.ProcessEnv }
|
|
90
|
+
): Promise<void> {
|
|
88
91
|
const execOptions: Options<string> = {
|
|
89
|
-
env: { FOUNDRY_PROFILE: options?.profile },
|
|
92
|
+
env: { FOUNDRY_PROFILE: options?.profile, ...options?.env },
|
|
90
93
|
stdout: "inherit",
|
|
91
94
|
stderr: "pipe",
|
|
92
95
|
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Abi,
|
|
3
|
+
Account,
|
|
4
|
+
Address,
|
|
5
|
+
Chain,
|
|
6
|
+
GetContractParameters,
|
|
7
|
+
GetContractReturnType,
|
|
8
|
+
Hex,
|
|
9
|
+
PublicClient,
|
|
10
|
+
Transport,
|
|
11
|
+
WalletClient,
|
|
12
|
+
WriteContractParameters,
|
|
13
|
+
getContract as viem_getContract,
|
|
14
|
+
} from "viem";
|
|
15
|
+
import { UnionOmit } from "./type-utils/common";
|
|
16
|
+
import { writeContract } from "./writeContract";
|
|
17
|
+
|
|
18
|
+
// copied from viem because this isn't exported
|
|
19
|
+
// TODO: import from viem?
|
|
20
|
+
function getFunctionParameters(values: [args?: readonly unknown[], options?: object]): {
|
|
21
|
+
args: readonly unknown[];
|
|
22
|
+
options: object;
|
|
23
|
+
} {
|
|
24
|
+
const hasArgs = values.length && Array.isArray(values[0]);
|
|
25
|
+
const args = hasArgs ? values[0]! : [];
|
|
26
|
+
const options = (hasArgs ? values[1] : values[0]) ?? {};
|
|
27
|
+
return { args, options };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type ContractWrite = {
|
|
31
|
+
id: string;
|
|
32
|
+
request: WriteContractParameters;
|
|
33
|
+
result: Promise<Hex>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type GetContractOptions<
|
|
37
|
+
TTransport extends Transport,
|
|
38
|
+
TAddress extends Address,
|
|
39
|
+
TAbi extends Abi,
|
|
40
|
+
TChain extends Chain,
|
|
41
|
+
TAccount extends Account,
|
|
42
|
+
TPublicClient extends PublicClient<TTransport, TChain>,
|
|
43
|
+
TWalletClient extends WalletClient<TTransport, TChain, TAccount>
|
|
44
|
+
> = Required<GetContractParameters<TTransport, TChain, TAccount, TAbi, TPublicClient, TWalletClient, TAddress>> & {
|
|
45
|
+
onWrite?: (write: ContractWrite) => void;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// TODO: migrate away from this approach once we can hook into viem: https://github.com/wagmi-dev/viem/discussions/1230
|
|
49
|
+
|
|
50
|
+
export function getContract<
|
|
51
|
+
TTransport extends Transport,
|
|
52
|
+
TAddress extends Address,
|
|
53
|
+
TAbi extends Abi,
|
|
54
|
+
TChain extends Chain,
|
|
55
|
+
TAccount extends Account,
|
|
56
|
+
TPublicClient extends PublicClient<TTransport, TChain>,
|
|
57
|
+
TWalletClient extends WalletClient<TTransport, TChain, TAccount>
|
|
58
|
+
>({
|
|
59
|
+
abi,
|
|
60
|
+
address,
|
|
61
|
+
publicClient,
|
|
62
|
+
walletClient,
|
|
63
|
+
onWrite,
|
|
64
|
+
}: GetContractOptions<
|
|
65
|
+
TTransport,
|
|
66
|
+
TAddress,
|
|
67
|
+
TAbi,
|
|
68
|
+
TChain,
|
|
69
|
+
TAccount,
|
|
70
|
+
TPublicClient,
|
|
71
|
+
TWalletClient
|
|
72
|
+
>): GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress> {
|
|
73
|
+
const contract = viem_getContract<TTransport, TAddress, TAbi, TChain, TAccount, TPublicClient, TWalletClient>({
|
|
74
|
+
abi,
|
|
75
|
+
address,
|
|
76
|
+
publicClient,
|
|
77
|
+
walletClient,
|
|
78
|
+
}) as unknown as GetContractReturnType<Abi, PublicClient, WalletClient>;
|
|
79
|
+
|
|
80
|
+
if (contract.write) {
|
|
81
|
+
// Replace write calls with our own. Implemented ~the same as viem, but adds better handling of nonces (via queue + retries).
|
|
82
|
+
let nextWriteId = 0;
|
|
83
|
+
contract.write = new Proxy(
|
|
84
|
+
{},
|
|
85
|
+
{
|
|
86
|
+
get(_, functionName: string) {
|
|
87
|
+
return (
|
|
88
|
+
...parameters: [
|
|
89
|
+
args?: readonly unknown[],
|
|
90
|
+
options?: UnionOmit<WriteContractParameters, "abi" | "address" | "functionName" | "args">
|
|
91
|
+
]
|
|
92
|
+
) => {
|
|
93
|
+
const { args, options } = getFunctionParameters(parameters);
|
|
94
|
+
const request = {
|
|
95
|
+
abi,
|
|
96
|
+
address,
|
|
97
|
+
functionName,
|
|
98
|
+
args,
|
|
99
|
+
...options,
|
|
100
|
+
onWrite,
|
|
101
|
+
} as unknown as WriteContractParameters<TAbi, typeof functionName, TChain, TAccount>;
|
|
102
|
+
const result = writeContract(walletClient, request);
|
|
103
|
+
|
|
104
|
+
const id = `${walletClient.chain.id}:${walletClient.account.address}:${nextWriteId++}`;
|
|
105
|
+
onWrite?.({ id, request: request as WriteContractParameters, result });
|
|
106
|
+
|
|
107
|
+
return result;
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return contract as unknown as GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress>;
|
|
115
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CreateNonceManagerOptions, CreateNonceManagerResult, createNonceManager } from "./createNonceManager";
|
|
2
|
+
import { getNonceManagerId } from "./getNonceManagerId";
|
|
3
|
+
|
|
4
|
+
const nonceManagers = new Map<string, CreateNonceManagerResult>();
|
|
5
|
+
|
|
6
|
+
export async function getNonceManager({
|
|
7
|
+
client,
|
|
8
|
+
address, // TODO: rename to account?
|
|
9
|
+
blockTag = "pending",
|
|
10
|
+
}: CreateNonceManagerOptions): Promise<CreateNonceManagerResult> {
|
|
11
|
+
const id = await getNonceManagerId({ client, address, blockTag });
|
|
12
|
+
|
|
13
|
+
const existingNonceManager = nonceManagers.get(id);
|
|
14
|
+
if (existingNonceManager) {
|
|
15
|
+
return existingNonceManager;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const nonceManager = createNonceManager({ client, address, blockTag });
|
|
19
|
+
nonceManagers.set(id, nonceManager);
|
|
20
|
+
return nonceManager;
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BlockTag, Client, Hex, getAddress } from "viem";
|
|
2
|
+
import { getChainId } from "viem/actions";
|
|
3
|
+
|
|
4
|
+
export async function getNonceManagerId({
|
|
5
|
+
client,
|
|
6
|
+
address,
|
|
7
|
+
blockTag,
|
|
8
|
+
}: {
|
|
9
|
+
client: Client;
|
|
10
|
+
address: Hex;
|
|
11
|
+
blockTag: BlockTag;
|
|
12
|
+
}): Promise<string> {
|
|
13
|
+
// TODO: improve this so we don't have to call getChainId every time
|
|
14
|
+
const chainId = client.chain?.id ?? (await getChainId(client));
|
|
15
|
+
return `mud:createNonceManager:${chainId}:${getAddress(address)}:${blockTag}`;
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { hexToResource } from "./hexToResource";
|
|
3
|
+
|
|
4
|
+
describe("hexToResource", () => {
|
|
5
|
+
it("can convert from hex string", () => {
|
|
6
|
+
const resource = hexToResource("0x74626e616d65737061636500000000006e616d65000000000000000000000000");
|
|
7
|
+
expect(resource.type).toMatchInlineSnapshot('"table"');
|
|
8
|
+
expect(resource.namespace).toMatchInlineSnapshot('"namespace"');
|
|
9
|
+
expect(resource.name).toMatchInlineSnapshot('"name"');
|
|
10
|
+
});
|
|
11
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Hex, hexToString, sliceHex } from "viem";
|
|
2
|
-
import {
|
|
2
|
+
import { Resource } from "./common";
|
|
3
3
|
import { ResourceType, resourceTypes } from "./resourceTypes";
|
|
4
|
-
import { resourceTypeIds } from "./
|
|
4
|
+
import { resourceTypeIds } from "./resourceToHex";
|
|
5
5
|
import { ReverseMap } from "./type-utils/common";
|
|
6
6
|
|
|
7
7
|
const resourceTypeIdToType = Object.fromEntries(
|
|
@@ -16,15 +16,15 @@ function getResourceType(resourceTypeId: string): ResourceType | undefined {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export function
|
|
19
|
+
export function hexToResource(hex: Hex): Resource {
|
|
20
20
|
const resourceTypeId = hexToString(sliceHex(hex, 0, 2)).replace(/\0+$/, "");
|
|
21
21
|
const type = getResourceType(resourceTypeId);
|
|
22
22
|
const namespace = hexToString(sliceHex(hex, 2, 16)).replace(/\0+$/, "");
|
|
23
23
|
const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, "");
|
|
24
24
|
|
|
25
25
|
if (!type) {
|
|
26
|
-
throw new Error(`Unknown
|
|
26
|
+
throw new Error(`Unknown type (${resourceTypeId}) for resource (${resourceTypeId}:${namespace}:${name})`);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
return { type, namespace, name };
|
|
29
|
+
return { resourceId: hex, type, namespace, name };
|
|
30
30
|
}
|