@latticexyz/common 3.0.0-main-560fd6a0c → 3.0.0-main-8f6fa588d

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.
@@ -0,0 +1,2 @@
1
+ var m=["table","offchainTable","namespace","system"];import{stringToHex as s,concatHex as u}from"viem";var o={table:"tb",offchainTable:"ot",namespace:"ns",system:"sy"};function x(e){let r=o[e.type];if(e.namespace.length>14)throw new Error(`Namespaces must fit into \`bytes14\`, but "${e.namespace}" is too long.`);return u([s(r,{size:2}),s(e.namespace,{size:14}),s(e.name.slice(0,16),{size:16})])}var y="";function i({namespace:e,name:r}){return e===y?r:`${e}__${r}`}import{hexToString as t,sliceHex as n}from"viem";var f=Object.fromEntries(Object.entries(o).map(([e,r])=>[r,e]));function T(e){let r=f[e];if(m.includes(r))return r}function _(e){let r=t(n(e,0,2)).replace(/\0+$/,""),c=T(r),a=t(n(e,2,16)).replace(/\0+$/,""),p=t(n(e,16,32)).replace(/\0+$/,"");if(!c)throw new Error(`Unknown type (${r}) for resource (${i({namespace:a,name:p})})`);return{resourceId:e,type:c,namespace:a,name:p}}export{m as a,o as b,x as c,i as d,_ as e};
2
+ //# sourceMappingURL=chunk-DDEUATTE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resourceTypes.ts","../src/resourceToHex.ts","../src/resourceToLabel.ts","../src/hexToResource.ts"],"sourcesContent":["export const resourceTypes = [\"table\", \"offchainTable\", \"namespace\", \"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 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 // Because namespaces are tied to access control, it's not safe to automatically truncate. Instead, we'll throw an error.\n if (resource.namespace.length > 14) {\n throw new Error(`Namespaces must fit into \\`bytes14\\`, but \"${resource.namespace}\" is too long.`);\n }\n return concatHex([\n stringToHex(typeId, { size: 2 }),\n stringToHex(resource.namespace, { size: 14 }),\n stringToHex(resource.name.slice(0, 16), { size: 16 }),\n ]);\n}\n","const rootNamespace = \"\";\n\nexport type ResourceLabel<\n namespace extends string = string,\n name extends string = string,\n> = namespace extends typeof rootNamespace ? name : `${namespace}__${name}`;\n\nexport function resourceToLabel<namespace extends string, name extends string>({\n namespace,\n name,\n}: {\n readonly namespace: namespace;\n readonly name: name;\n}): ResourceLabel<namespace, name> {\n return (namespace === rootNamespace ? name : `${namespace}__${name}`) as ResourceLabel<namespace, name>;\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\";\nimport { resourceToLabel } from \"./resourceToLabel\";\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 (${resourceToLabel({ namespace, name })})`);\n }\n\n return { resourceId: hex, type, namespace, name };\n}\n"],"mappings":"AAAO,IAAMA,EAAgB,CAAC,QAAS,gBAAiB,YAAa,QAAQ,ECA7E,OAAc,eAAAC,EAAa,aAAAC,MAAiB,OAKrC,IAAMC,EAAkB,CAE7B,MAAO,KACP,cAAe,KAEf,UAAW,KACX,OAAQ,IACV,EAEO,SAASC,EAAcC,EAA6C,CACzE,IAAMC,EAASH,EAAgBE,EAAS,IAAI,EAE5C,GAAIA,EAAS,UAAU,OAAS,GAC9B,MAAM,IAAI,MAAM,8CAA8CA,EAAS,yBAAyB,EAElG,OAAOH,EAAU,CACfD,EAAYK,EAAQ,CAAE,KAAM,CAAE,CAAC,EAC/BL,EAAYI,EAAS,UAAW,CAAE,KAAM,EAAG,CAAC,EAC5CJ,EAAYI,EAAS,KAAK,MAAM,EAAG,EAAE,EAAG,CAAE,KAAM,EAAG,CAAC,CACtD,CAAC,CACH,CCzBA,IAAME,EAAgB,GAOf,SAASC,EAA+D,CAC7E,UAAAC,EACA,KAAAC,CACF,EAGmC,CACjC,OAAQD,IAAcF,EAAgBG,EAAO,GAAGD,MAAcC,GAChE,CCfA,OAAc,eAAAC,EAAa,YAAAC,MAAgB,OAO3C,IAAMC,EAAuB,OAAO,YAClC,OAAO,QAAQC,CAAe,EAAE,IAAI,CAAC,CAACC,EAAKC,CAAK,IAAM,CAACA,EAAOD,CAAG,CAAC,CACpE,EAEA,SAASE,EAAgBC,EAAkD,CAEzE,IAAMC,EAAQN,EAA+DK,CAAc,EAC3F,GAAIE,EAAc,SAASD,CAAoB,EAC7C,OAAOA,CAEX,CAEO,SAASE,EAAcC,EAAoB,CAChD,IAAMJ,EAAiBK,EAAYC,EAASF,EAAK,EAAG,CAAC,CAAC,EAAE,QAAQ,OAAQ,EAAE,EACpEH,EAAOF,EAAgBC,CAAc,EACrCO,EAAYF,EAAYC,EAASF,EAAK,EAAG,EAAE,CAAC,EAAE,QAAQ,OAAQ,EAAE,EAChEI,EAAOH,EAAYC,EAASF,EAAK,GAAI,EAAE,CAAC,EAAE,QAAQ,OAAQ,EAAE,EAElE,GAAI,CAACH,EACH,MAAM,IAAI,MAAM,iBAAiBD,oBAAiCS,EAAgB,CAAE,UAAAF,EAAW,KAAAC,CAAK,CAAC,IAAI,EAG3G,MAAO,CAAE,WAAYJ,EAAK,KAAAH,EAAM,UAAAM,EAAW,KAAAC,CAAK,CAClD","names":["resourceTypes","stringToHex","concatHex","resourceTypeIds","resourceToHex","resource","typeId","rootNamespace","resourceToLabel","namespace","name","hexToString","sliceHex","resourceTypeIdToType","resourceTypeIds","key","value","getResourceType","resourceTypeId","type","resourceTypes","hexToResource","hex","hexToString","sliceHex","namespace","name","resourceToLabel"]}
package/dist/codegen.js CHANGED
@@ -1,4 +1,4 @@
1
- import{c as N,e as Y}from"./chunk-JYZJVKLH.js";import{a as m}from"./chunk-TCWGPC6G.js";import{a as p}from"./chunk-UPQEB2HW.js";import J from"path";import{parse as _,visit as d}from"@solidity-parser/parser";function ie(t,r){let n=_(t),i=W(_(t),r),s=[],o=[],a=[];if(!i)throw new p(`Contract not found: ${r}`);return d(i,{FunctionDefinition({name:y,visibility:T,parameters:c,stateMutability:R,returnParameters:g,isConstructor:F,isFallback:M,isReceiveEther:k}){try{if(F||M||k)return;if(T==="default")throw new p("Visibility is not specified");if(T==="external"||T==="public"){o.push({name:y===null?"":y,parameters:c.map(l),stateMutability:R||"",returnParameters:g===null?[]:g.map(l)});for(let{typeName:I}of c.concat(g??[])){let L=f(I);s=s.concat(b(n,L))}}}catch(I){throw I instanceof p&&(I.message=`Function "${y}" in contract "${r}": ${I.message}`),I}},CustomErrorDefinition({name:y,parameters:T}){a.push({name:y,parameters:T.map(l)});for(let c of T){let R=f(c.typeName);s=s.concat(b(n,R))}}}),{functions:o,errors:a,symbolImports:s}}function W(t,r){let n;return d(t,{ContractDefinition(i){i.name===r&&(n=i)}}),n}function l({name:t,typeName:r,storageLocation:n}){let i="",{name:s,stateMutability:o}=h(r);return i+=s,o!==null&&(i+=` ${o}`),n!==null&&(i+=` ${n}`),t!==null&&(i+=` ${t}`),i}function h(t){if(t===null)return{name:"",stateMutability:null};if(t.type==="ElementaryTypeName")return{name:t.name,stateMutability:t.stateMutability};if(t.type==="UserDefinedTypeName")return{name:t.namePath,stateMutability:null};if(t.type==="ArrayTypeName"){let r="";t.length?.type==="NumberLiteral"?r=t.length.number:t.length?.type==="Identifier"&&(r=t.length.name);let{name:n,stateMutability:i}=h(t.baseTypeName);return{name:`${n}[${r}]`,stateMutability:i}}else throw new p(`Invalid typeName.type ${t.type}`)}function f(t){if(t?.type==="UserDefinedTypeName")return[t.namePath.split(".")[0]];if(t?.type==="ArrayTypeName"){let r=f(t.baseTypeName);if(t.length?.type==="Identifier"){let n=t.length.name;r.push(n.split(".")[0])}return r}else return[]}function b(t,r){let n=[];for(let i of r){let s;if(d(t,{ImportDirective({path:o,symbolAliases:a}){if(a)for(let y of a){let T=y[1]||y[0];if(i===T){s={symbol:y[0],path:o};return}}}}),s)n.push(s);else throw new p(`Symbol "${i}" has no explicit import`)}return n}import{parse as j,visit as O}from"@solidity-parser/parser";function U(t,r,n){let i=j(t),s=n.at(0)===".",o={};return O(i,{TypeDefinition({name:a,definition:y},T){if(y.name.includes("fixed"))throw new p("Fixed point numbers are not supported by MUD");if(r.includes(a)){if(a in o)throw new p(`File has multiple user types with the same name: ${a}`);T?.type==="ContractDefinition"?o[a]={typeId:`${T.name}.${a}`,internalTypeId:y.name,importSymbol:T.name,fromPath:n,isRelativePath:s}:o[a]={typeId:a,internalTypeId:y.name,importSymbol:a,fromPath:n,isRelativePath:s}}}}),o}import B from"prettier";import H from"prettier-plugin-solidity";async function E(t,r){let n;r&&(n=await B.resolveConfig(r));try{return B.format(t,{plugins:[H],parser:"solidity-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...n})}catch(i){let s;return i instanceof Error?s=i.message:s=i,console.log(`Error during output formatting: ${s}`),t}}async function x(t){return B.format(t,{parser:"typescript"})}import u from"node:fs/promises";import $ from"node:path";var A=m.extend("codegen"),K=m.extend("codegen");A.log=console.debug.bind(console);K.log=console.error.bind(console);async function le(t,r,n){let i=await E(t);await u.mkdir($.dirname(r),{recursive:!0}),await u.writeFile(r,i),A(`${n}: ${r}`)}async function fe(t,r,n){let i=await x(t);await u.mkdir($.dirname(r),{recursive:!0}),await u.writeFile(r,i),A(`${n}: ${r}`)}import{readFileSync as w}from"fs";import V from"path";function be(t,r,n){let i={};for(let[o,{filePath:a}]of Object.entries(t))a in i||(i[a]=[]),i[a].push(o);let s={};for(let[o,a]of Object.entries(i)){let{filePath:y,data:T}=z(r,o,n),c=U(T,a,y);for(let[R,g]of Object.entries(c))if(g.internalTypeId!==t[R].internalType)throw new p(`User type "${R}" has internal type "${g.internalTypeId}" but config specifies "${t[R].internalType}"`);s={...s,...c}}return s}function z(t,r,n){if(r.at(0)===".")return{filePath:"./"+V.relative(t,r),data:w(r,"utf8")};{let i=r;for(let[s,o]of n)if(i.includes(s)){i=i.replace(s,o);break}return{filePath:r,data:w(i,"utf8")}}}function S(t){return t.replace(/\\/g,"/")}var v=`// SPDX-License-Identifier: MIT
1
+ import{c as N,e as Y}from"./chunk-DDEUATTE.js";import{a as m}from"./chunk-TCWGPC6G.js";import{a as p}from"./chunk-UPQEB2HW.js";import J from"path";import{parse as _,visit as d}from"@solidity-parser/parser";function ie(t,r){let n=_(t),i=W(_(t),r),s=[],o=[],a=[];if(!i)throw new p(`Contract not found: ${r}`);return d(i,{FunctionDefinition({name:y,visibility:T,parameters:c,stateMutability:R,returnParameters:g,isConstructor:F,isFallback:M,isReceiveEther:k}){try{if(F||M||k)return;if(T==="default")throw new p("Visibility is not specified");if(T==="external"||T==="public"){o.push({name:y===null?"":y,parameters:c.map(l),stateMutability:R||"",returnParameters:g===null?[]:g.map(l)});for(let{typeName:I}of c.concat(g??[])){let L=f(I);s=s.concat(b(n,L))}}}catch(I){throw I instanceof p&&(I.message=`Function "${y}" in contract "${r}": ${I.message}`),I}},CustomErrorDefinition({name:y,parameters:T}){a.push({name:y,parameters:T.map(l)});for(let c of T){let R=f(c.typeName);s=s.concat(b(n,R))}}}),{functions:o,errors:a,symbolImports:s}}function W(t,r){let n;return d(t,{ContractDefinition(i){i.name===r&&(n=i)}}),n}function l({name:t,typeName:r,storageLocation:n}){let i="",{name:s,stateMutability:o}=h(r);return i+=s,o!==null&&(i+=` ${o}`),n!==null&&(i+=` ${n}`),t!==null&&(i+=` ${t}`),i}function h(t){if(t===null)return{name:"",stateMutability:null};if(t.type==="ElementaryTypeName")return{name:t.name,stateMutability:t.stateMutability};if(t.type==="UserDefinedTypeName")return{name:t.namePath,stateMutability:null};if(t.type==="ArrayTypeName"){let r="";t.length?.type==="NumberLiteral"?r=t.length.number:t.length?.type==="Identifier"&&(r=t.length.name);let{name:n,stateMutability:i}=h(t.baseTypeName);return{name:`${n}[${r}]`,stateMutability:i}}else throw new p(`Invalid typeName.type ${t.type}`)}function f(t){if(t?.type==="UserDefinedTypeName")return[t.namePath.split(".")[0]];if(t?.type==="ArrayTypeName"){let r=f(t.baseTypeName);if(t.length?.type==="Identifier"){let n=t.length.name;r.push(n.split(".")[0])}return r}else return[]}function b(t,r){let n=[];for(let i of r){let s;if(d(t,{ImportDirective({path:o,symbolAliases:a}){if(a)for(let y of a){let T=y[1]||y[0];if(i===T){s={symbol:y[0],path:o};return}}}}),s)n.push(s);else throw new p(`Symbol "${i}" has no explicit import`)}return n}import{parse as j,visit as O}from"@solidity-parser/parser";function U(t,r,n){let i=j(t),s=n.at(0)===".",o={};return O(i,{TypeDefinition({name:a,definition:y},T){if(y.name.includes("fixed"))throw new p("Fixed point numbers are not supported by MUD");if(r.includes(a)){if(a in o)throw new p(`File has multiple user types with the same name: ${a}`);T?.type==="ContractDefinition"?o[a]={typeId:`${T.name}.${a}`,internalTypeId:y.name,importSymbol:T.name,fromPath:n,isRelativePath:s}:o[a]={typeId:a,internalTypeId:y.name,importSymbol:a,fromPath:n,isRelativePath:s}}}}),o}import B from"prettier";import H from"prettier-plugin-solidity";async function E(t,r){let n;r&&(n=await B.resolveConfig(r));try{return B.format(t,{plugins:[H],parser:"solidity-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...n})}catch(i){let s;return i instanceof Error?s=i.message:s=i,console.log(`Error during output formatting: ${s}`),t}}async function x(t){return B.format(t,{parser:"typescript"})}import u from"node:fs/promises";import $ from"node:path";var A=m.extend("codegen"),K=m.extend("codegen");A.log=console.debug.bind(console);K.log=console.error.bind(console);async function le(t,r,n){let i=await E(t);await u.mkdir($.dirname(r),{recursive:!0}),await u.writeFile(r,i),A(`${n}: ${r}`)}async function fe(t,r,n){let i=await x(t);await u.mkdir($.dirname(r),{recursive:!0}),await u.writeFile(r,i),A(`${n}: ${r}`)}import{readFileSync as w}from"fs";import V from"path";function be(t,r,n){let i={};for(let[o,{filePath:a}]of Object.entries(t))a in i||(i[a]=[]),i[a].push(o);let s={};for(let[o,a]of Object.entries(i)){let{filePath:y,data:T}=z(r,o,n),c=U(T,a,y);for(let[R,g]of Object.entries(c))if(g.internalTypeId!==t[R].internalType)throw new p(`User type "${R}" has internal type "${g.internalTypeId}" but config specifies "${t[R].internalType}"`);s={...s,...c}}return s}function z(t,r,n){if(r.at(0)===".")return{filePath:"./"+V.relative(t,r),data:w(r,"utf8")};{let i=r;for(let[s,o]of n)if(i.includes(s)){i=i.replace(s,o);break}return{filePath:r,data:w(i,"utf8")}}}function S(t){return t.replace(/\\/g,"/")}var v=`// SPDX-License-Identifier: MIT
2
2
  pragma solidity >=0.8.24;
3
3
 
4
4
  /* Autogenerated file. Do not edit manually. */`;function G(t,r){return C("",t,r)}function X(t){let r=t.filter(n=>n!==void 0&&n!=="");return C(",",r,n=>n)}function Le({staticResourceData:t,keyTuple:r}){let n=t?"":"ResourceId _tableId",i=X(r.map(({name:o,typeWithLocation:a})=>`${a} ${o}`)),s=`
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import PQueue from 'p-queue';
3
3
  import { g as getContract } from './getContract-93922960.js';
4
4
  export { C as ContractWrite, G as GetContractOptions } from './getContract-93922960.js';
5
5
 
6
- declare const resourceTypes: readonly ["table", "offchainTable", "namespace", "module", "system"];
6
+ declare const resourceTypes: readonly ["table", "offchainTable", "namespace", "system"];
7
7
  type ResourceType = (typeof resourceTypes)[number];
8
8
 
9
9
  type Resource = {
@@ -81,7 +81,6 @@ declare const resourceTypeIds: {
81
81
  readonly table: "tb";
82
82
  readonly offchainTable: "ot";
83
83
  readonly namespace: "ns";
84
- readonly module: "md";
85
84
  readonly system: "sy";
86
85
  };
87
86
  declare function resourceToHex(resource: Omit<Resource, "resourceId">): Hex;
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{a as N,b as $,c as q,d as l,e as j}from"./chunk-3WH5F6SJ.js";import{a as B,b as D,c as x,d as _,e as m}from"./chunk-JYZJVKLH.js";import{a as p}from"./chunk-TCWGPC6G.js";import y from"debug";var T=y("mud:benchmark");T.log=console.info.bind(console);function J(e){let t=T.extend(e),r=performance.now();return n=>{let o=(performance.now()-r)/1e3;t("%s: +%ds",n,o),r=performance.now()}}import{privateKeyToAccount as w}from"viem/accounts";function Q(e){return{...w(e)}}import{getContract as P}from"viem";function H(e){let t=e.length&&Array.isArray(e[0]),r=t?e[0]:[],n=(t?e[1]:e[0])??{};return{args:r,options:n}}function d({abi:e,address:t,client:{public:r,wallet:n},onWrite:o}){let s=P({abi:e,address:t,client:{public:r,wallet:n}});if(s.write){let i=0;s.write=new Proxy({},{get(K,b){return(...A)=>{let{args:g,options:h}=H(A),c={abi:e,address:t,functionName:b,args:g,...h,onWrite:o},u=l(n,c,{publicClient:r}),k=`${n.chain.id}:${n.account.address}:${i++}`;return o?.({id:k,request:c,result:u}),u}}})}return s}import{generatePrivateKey as W,privateKeyToAccount as f}from"viem/accounts";import{isHex as v}from"viem";function O(e,t){if(!v(e))throw console.error("Private key found in cache is not valid hex",{privateKey:e,cacheKey:t}),new Error(`Private key found in cache (${t}) is not valid hex`);f(e)}function Te(e="mud:burnerWallet"){let t=localStorage.getItem(e);if(t!=null)return O(t,e),t;let r=W();return console.log("New burner wallet created:",f(r)),localStorage.setItem(e,r),r}var C=class extends Map{maxSize;constructor(t){super(),this.maxSize=t}set(t,r){return super.set(t,r),this.maxSize&&this.size>this.maxSize&&this.delete(this.keys().next().value),this}};function a(e,t,r){return`0x${e.replace(/^0x/,"").slice(t*2,r!=null?r*2:void 0).padEnd(((r??t)-t)*2,"0")}`}function be(e){return"ok"in e}function E(e){return"error"in e}function Ae(e){if(E(e))throw e.error;return e.ok}import{concatHex as R}from"viem";function we(e,t,r=0,n="0x"){return R([a(e,0,t),n,a(e,t+r)])}import{keccak256 as S}from"viem";var G=p.extend("transportObserver");function Ee(e){return t=>{let r=e(t);return{...r,request:async o=>{if(o.method==="eth_sendRawTransaction"&&o.params instanceof Array){let s=o.params.map(i=>S(i));G("saw txs",s)}return r.request(o)}}}}var I=d;var z=x;var F=m;export{C as LruMap,J as createBenchmark,Q as createBurnerAccount,I as createContract,$ as createNonceManager,Te as getBurnerPrivateKey,d as getContract,q as getNonceManager,N as getNonceManagerId,m as hexToResource,F as hexToResourceId,E as isError,be as isOk,a as readHex,z as resourceIdToHex,x as resourceToHex,_ as resourceToLabel,D as resourceTypeIds,B as resourceTypes,j as sendTransaction,we as spliceHex,Ee as transportObserver,Ae as unwrap,l as writeContract};
1
+ import{a as N,b as $,c as q,d as l,e as j}from"./chunk-3WH5F6SJ.js";import{a as B,b as D,c as x,d as _,e as m}from"./chunk-DDEUATTE.js";import{a as p}from"./chunk-TCWGPC6G.js";import y from"debug";var T=y("mud:benchmark");T.log=console.info.bind(console);function J(e){let t=T.extend(e),r=performance.now();return n=>{let o=(performance.now()-r)/1e3;t("%s: +%ds",n,o),r=performance.now()}}import{privateKeyToAccount as w}from"viem/accounts";function Q(e){return{...w(e)}}import{getContract as P}from"viem";function H(e){let t=e.length&&Array.isArray(e[0]),r=t?e[0]:[],n=(t?e[1]:e[0])??{};return{args:r,options:n}}function d({abi:e,address:t,client:{public:r,wallet:n},onWrite:o}){let s=P({abi:e,address:t,client:{public:r,wallet:n}});if(s.write){let i=0;s.write=new Proxy({},{get(K,b){return(...A)=>{let{args:g,options:h}=H(A),c={abi:e,address:t,functionName:b,args:g,...h,onWrite:o},u=l(n,c,{publicClient:r}),k=`${n.chain.id}:${n.account.address}:${i++}`;return o?.({id:k,request:c,result:u}),u}}})}return s}import{generatePrivateKey as W,privateKeyToAccount as f}from"viem/accounts";import{isHex as v}from"viem";function O(e,t){if(!v(e))throw console.error("Private key found in cache is not valid hex",{privateKey:e,cacheKey:t}),new Error(`Private key found in cache (${t}) is not valid hex`);f(e)}function Te(e="mud:burnerWallet"){let t=localStorage.getItem(e);if(t!=null)return O(t,e),t;let r=W();return console.log("New burner wallet created:",f(r)),localStorage.setItem(e,r),r}var C=class extends Map{maxSize;constructor(t){super(),this.maxSize=t}set(t,r){return super.set(t,r),this.maxSize&&this.size>this.maxSize&&this.delete(this.keys().next().value),this}};function a(e,t,r){return`0x${e.replace(/^0x/,"").slice(t*2,r!=null?r*2:void 0).padEnd(((r??t)-t)*2,"0")}`}function be(e){return"ok"in e}function E(e){return"error"in e}function Ae(e){if(E(e))throw e.error;return e.ok}import{concatHex as R}from"viem";function we(e,t,r=0,n="0x"){return R([a(e,0,t),n,a(e,t+r)])}import{keccak256 as S}from"viem";var G=p.extend("transportObserver");function Ee(e){return t=>{let r=e(t);return{...r,request:async o=>{if(o.method==="eth_sendRawTransaction"&&o.params instanceof Array){let s=o.params.map(i=>S(i));G("saw txs",s)}return r.request(o)}}}}var I=d;var z=x;var F=m;export{C as LruMap,J as createBenchmark,Q as createBurnerAccount,I as createContract,$ as createNonceManager,Te as getBurnerPrivateKey,d as getContract,q as getNonceManager,N as getNonceManagerId,m as hexToResource,F as hexToResourceId,E as isError,be as isOk,a as readHex,z as resourceIdToHex,x as resourceToHex,_ as resourceToLabel,D as resourceTypeIds,B as resourceTypes,j as sendTransaction,we as spliceHex,Ee as transportObserver,Ae as unwrap,l as writeContract};
2
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@latticexyz/common",
3
- "version": "3.0.0-main-560fd6a0c",
3
+ "version": "3.0.0-main-8f6fa588d",
4
4
  "description": "Common low level logic shared between packages",
5
5
  "repository": {
6
6
  "type": "git",
@@ -65,7 +65,7 @@
65
65
  "prettier": "3.2.5",
66
66
  "prettier-plugin-solidity": "1.3.1",
67
67
  "viem": "2.9.20",
68
- "@latticexyz/schema-type": "3.0.0-main-560fd6a0c"
68
+ "@latticexyz/schema-type": "3.0.0-main-8f6fa588d"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@types/debug": "^4.1.7",
@@ -1,2 +0,0 @@
1
- var m=["table","offchainTable","namespace","module","system"];import{stringToHex as s,concatHex as u}from"viem";var o={table:"tb",offchainTable:"ot",namespace:"ns",module:"md",system:"sy"};function x(e){let r=o[e.type];if(e.namespace.length>14)throw new Error(`Namespaces must fit into \`bytes14\`, but "${e.namespace}" is too long.`);return u([s(r,{size:2}),s(e.namespace,{size:14}),s(e.name.slice(0,16),{size:16})])}var y="";function i({namespace:e,name:r}){return e===y?r:`${e}__${r}`}import{hexToString as t,sliceHex as n}from"viem";var f=Object.fromEntries(Object.entries(o).map(([e,r])=>[r,e]));function T(e){let r=f[e];if(m.includes(r))return r}function _(e){let r=t(n(e,0,2)).replace(/\0+$/,""),c=T(r),a=t(n(e,2,16)).replace(/\0+$/,""),p=t(n(e,16,32)).replace(/\0+$/,"");if(!c)throw new Error(`Unknown type (${r}) for resource (${i({namespace:a,name:p})})`);return{resourceId:e,type:c,namespace:a,name:p}}export{m as a,o as b,x as c,i as d,_ as e};
2
- //# sourceMappingURL=chunk-JYZJVKLH.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/resourceTypes.ts","../src/resourceToHex.ts","../src/resourceToLabel.ts","../src/hexToResource.ts"],"sourcesContent":["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 // Because namespaces are tied to access control, it's not safe to automatically truncate. Instead, we'll throw an error.\n if (resource.namespace.length > 14) {\n throw new Error(`Namespaces must fit into \\`bytes14\\`, but \"${resource.namespace}\" is too long.`);\n }\n return concatHex([\n stringToHex(typeId, { size: 2 }),\n stringToHex(resource.namespace, { size: 14 }),\n stringToHex(resource.name.slice(0, 16), { size: 16 }),\n ]);\n}\n","const rootNamespace = \"\";\n\nexport type ResourceLabel<\n namespace extends string = string,\n name extends string = string,\n> = namespace extends typeof rootNamespace ? name : `${namespace}__${name}`;\n\nexport function resourceToLabel<namespace extends string, name extends string>({\n namespace,\n name,\n}: {\n readonly namespace: namespace;\n readonly name: name;\n}): ResourceLabel<namespace, name> {\n return (namespace === rootNamespace ? name : `${namespace}__${name}`) as ResourceLabel<namespace, name>;\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\";\nimport { resourceToLabel } from \"./resourceToLabel\";\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 (${resourceToLabel({ namespace, name })})`);\n }\n\n return { resourceId: hex, type, namespace, name };\n}\n"],"mappings":"AAAO,IAAMA,EAAgB,CAAC,QAAS,gBAAiB,YAAa,SAAU,QAAQ,ECAvF,OAAc,eAAAC,EAAa,aAAAC,MAAiB,OAKrC,IAAMC,EAAkB,CAE7B,MAAO,KACP,cAAe,KAEf,UAAW,KACX,OAAQ,KACR,OAAQ,IACV,EAEO,SAASC,EAAcC,EAA6C,CACzE,IAAMC,EAASH,EAAgBE,EAAS,IAAI,EAE5C,GAAIA,EAAS,UAAU,OAAS,GAC9B,MAAM,IAAI,MAAM,8CAA8CA,EAAS,yBAAyB,EAElG,OAAOH,EAAU,CACfD,EAAYK,EAAQ,CAAE,KAAM,CAAE,CAAC,EAC/BL,EAAYI,EAAS,UAAW,CAAE,KAAM,EAAG,CAAC,EAC5CJ,EAAYI,EAAS,KAAK,MAAM,EAAG,EAAE,EAAG,CAAE,KAAM,EAAG,CAAC,CACtD,CAAC,CACH,CC1BA,IAAME,EAAgB,GAOf,SAASC,EAA+D,CAC7E,UAAAC,EACA,KAAAC,CACF,EAGmC,CACjC,OAAQD,IAAcF,EAAgBG,EAAO,GAAGD,MAAcC,GAChE,CCfA,OAAc,eAAAC,EAAa,YAAAC,MAAgB,OAO3C,IAAMC,EAAuB,OAAO,YAClC,OAAO,QAAQC,CAAe,EAAE,IAAI,CAAC,CAACC,EAAKC,CAAK,IAAM,CAACA,EAAOD,CAAG,CAAC,CACpE,EAEA,SAASE,EAAgBC,EAAkD,CAEzE,IAAMC,EAAQN,EAA+DK,CAAc,EAC3F,GAAIE,EAAc,SAASD,CAAoB,EAC7C,OAAOA,CAEX,CAEO,SAASE,EAAcC,EAAoB,CAChD,IAAMJ,EAAiBK,EAAYC,EAASF,EAAK,EAAG,CAAC,CAAC,EAAE,QAAQ,OAAQ,EAAE,EACpEH,EAAOF,EAAgBC,CAAc,EACrCO,EAAYF,EAAYC,EAASF,EAAK,EAAG,EAAE,CAAC,EAAE,QAAQ,OAAQ,EAAE,EAChEI,EAAOH,EAAYC,EAASF,EAAK,GAAI,EAAE,CAAC,EAAE,QAAQ,OAAQ,EAAE,EAElE,GAAI,CAACH,EACH,MAAM,IAAI,MAAM,iBAAiBD,oBAAiCS,EAAgB,CAAE,UAAAF,EAAW,KAAAC,CAAK,CAAC,IAAI,EAG3G,MAAO,CAAE,WAAYJ,EAAK,KAAAH,EAAM,UAAAM,EAAW,KAAAC,CAAK,CAClD","names":["resourceTypes","stringToHex","concatHex","resourceTypeIds","resourceToHex","resource","typeId","rootNamespace","resourceToLabel","namespace","name","hexToString","sliceHex","resourceTypeIdToType","resourceTypeIds","key","value","getResourceType","resourceTypeId","type","resourceTypes","hexToResource","hex","hexToString","sliceHex","namespace","name","resourceToLabel"]}