@latticexyz/cli 2.0.13-main-5ff9f2aa8 → 3.0.0-main-e85dc5349

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,39 @@
1
+ import{a as U}from"./chunk-QXUPZVZL.js";import Us from"@latticexyz/gas-report";import Ks from"@latticexyz/abi-ts";import{loadConfig as Yo,resolveConfigPath as Go}from"@latticexyz/config/node";import{tablegen as Vo}from"@latticexyz/store/codegen";import{worldgen as Uo}from"@latticexyz/world/node";import{forge as Ko,getRemappings as Jo}from"@latticexyz/common/foundry";import{execa as qo}from"execa";async function K({rootDir:e,config:o,foundryProfile:n=process.env.FOUNDRY_PROFILE}){let r=await Jo(n);await Promise.all([Vo({rootDir:e,config:o,remappings:r}),Uo({rootDir:e,config:o})]),await Ko(["build"],{profile:n}),await qo("mud",["abi-ts"],{stdio:"inherit"})}import Qo from"node:path";var Zo={command:"build",describe:"Build contracts and generate MUD artifacts (table libraries, world interface, ABI)",builder(e){return e.options({configPath:{type:"string",desc:"Path to the MUD config file"},profile:{type:"string",desc:"The foundry profile to use"}})},async handler(e){let o=await Go(e.configPath),n=await Yo(o);await K({rootDir:Qo.dirname(o),config:n,foundryProfile:e.profile}),process.exit(0)}},xe=Zo;import{rmSync as Xo}from"fs";import{homedir as et}from"os";import ot from"path";import{execa as tt}from"execa";var rt={command:"devnode",describe:"Start a local Ethereum node for development",builder(e){return e.options({blocktime:{type:"number",default:1,decs:"Interval in which new blocks are produced"}})},async handler({blocktime:e}){console.log("Clearing devnode history");let o=et();Xo(ot.join(o,".foundry","anvil","tmp"),{recursive:!0,force:!0});let n=["-b",String(e),"--block-base-fee-per-gas","0"];console.log(`Running: anvil ${n.join(" ")}`);let r=tt("anvil",n,{stdio:["inherit","inherit","inherit"]});process.on("SIGINT",()=>{console.log(`
2
+ gracefully shutting down from SIGINT (Crtl-C)`),r.kill(),process.exit()}),await r}},Se=rt;var nt={command:"hello <name>",describe:"Greet <name> with Hello",builder(e){return e.options({upper:{type:"boolean"}}).positional("name",{type:"string",demandOption:!0})},handler({name:e}){let o=`Gm, ${e}!`;console.log(o),process.exit(0)}},Ce=nt;import{loadConfig as st,resolveConfigPath as at}from"@latticexyz/config/node";import{tablegen as it}from"@latticexyz/store/codegen";import{getRemappings as dt}from"@latticexyz/common/foundry";import ct from"node:path";var lt={command:"tablegen",describe:"Autogenerate MUD Store table libraries based on the config file",builder(e){return e.options({configPath:{type:"string",desc:"Path to the MUD config file"}})},async handler(e){let o=await at(e.configPath),n=await st(o),r=await dt();await it({rootDir:ct.dirname(o),config:n,remappings:r}),process.exit(0)}},De=lt;import ce from"node:path";import{existsSync as yn,mkdirSync as un,readFileSync as gn,writeFileSync as ue}from"node:fs";import{getBalance as gt,sendRawTransaction as bt,sendTransaction as ve,waitForTransactionReceipt as Te}from"viem/actions";var v={gasPrice:1e11,gasLimit:1e5,signerAddress:"3fab184622dc19b6109349b94811493bf2a45362",transaction:"f8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222",address:"4e59b44847b379578588920ca78fbf26c0b4956c",creationCode:"604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3"};import Ae from"debug";var J=Ae("mud:cli"),pt=Ae("mud:cli");J.log=console.debug.bind(console);pt.log=console.error.bind(console);var m=J.extend("deploy"),ft=J.extend("deploy");m.log=console.debug.bind(console);ft.log=console.error.bind(console);import{sliceHex as yt}from"viem";import{getBytecode as ut}from"viem/actions";var q=`0x${v.address}`;async function Y(e){let o=await ut(e,{address:q});if(o)return m("found CREATE2 deployer at",q),o!==yt(`0x${v.creationCode}`,14)&&console.warn(`
3
+ \u26A0\uFE0F Bytecode for deployer at ${q} did not match the expected CREATE2 bytecode. You may have unexpected results.
4
+ `),q}var le=`0x${v.address}`;async function ke(e){let o=await Y(e);if(o!==void 0)return o;let n=BigInt(v.gasLimit)*BigInt(v.gasPrice),r=await gt(e,{address:`0x${v.signerAddress}`}),t=n-r;if(t>0){m("sending gas for CREATE2 deployer to signer at",v.signerAddress);let a=await ve(e,{chain:e.chain??null,to:`0x${v.signerAddress}`,value:t}),l=await Te(e,{hash:a});if(l.status!=="success")throw console.error("failed to send gas to deployer signer",l),new Error("failed to send gas to deployer signer")}m("deploying CREATE2 deployer at",le);let s=await bt(e,{serializedTransaction:`0x${v.transaction}`}).catch(a=>{if(String(a).includes("only replay-protected (EIP-155) transactions allowed over RPC"))return console.warn(`
5
+ \u26A0\uFE0F Your chain or RPC does not allow for non EIP-155 signed transactions, so your deploys will not be determinstic and contract addresses may change between deploys.
6
+
7
+ We recommend running your chain's node with \`--rpc.allow-unprotected-txs\` to enable determinstic deployments.
8
+ `),m("deploying CREATE2 deployer"),ve(e,{chain:e.chain??null,data:`0x${v.creationCode}`});throw a}),i=await Te(e,{hash:s});if(!i.contractAddress)throw new Error("Deploy receipt did not have contract address, was the deployer not deployed?");return i.contractAddress!==le&&console.warn(`
9
+ \u26A0\uFE0F CREATE2 deployer created at ${i.contractAddress} does not match the CREATE2 determinstic deployer we expected (${le})`),i.contractAddress}import{waitForTransactionReceipt as Kt}from"viem/actions";import{waitForTransactionReceipt as It}from"viem/actions";import{concatHex as At,getCreate2Address as vt}from"viem";import{getBytecode as Tt}from"viem/actions";import{padHex as ht}from"viem";import wt from"@latticexyz/store/mud.config";import xt from"@latticexyz/world/mud.config";import St from"@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json"assert{type:"json"};import{helloStoreEvent as Ct}from"@latticexyz/store";import{helloWorldEvent as Dt}from"@latticexyz/world";var S=ht("0x",{size:32}),F=parseInt("6000",16),W=wt.tables,A=xt.tables,G=[Ct,Dt],C=St,Pe=["2.0.0","2.0.1","2.0.2"],Oe=["2.0.0","2.0.1","2.0.2"];import{sendTransaction as kt}from"@latticexyz/common";import Pt from"p-retry";import{wait as Ot}from"@latticexyz/common/utils";async function Ie({client:e,deployerAddress:o,bytecode:n,deployedBytecodeSize:r,label:t="contract"}){if(n.includes("__$"))throw new Error(`Found unlinked public library in ${t} bytecode`);let s=vt({from:o,salt:S,bytecode:n});return await Tt(e,{address:s,blockTag:"pending"})?(m("found",t,"at",s),[]):(r>F?console.warn(`
10
+ Bytecode for ${t} (${r} bytes) is over the contract size limit (${F} bytes). Run \`forge build --sizes\` for more info.
11
+ `):r>F*.95&&console.warn(`
12
+ Bytecode for ${t} (${r} bytes) is almost over the contract size limit (${F} bytes). Run \`forge build --sizes\` for more info.
13
+ `),m("deploying",t,"at",s),[await Pt(()=>kt(e,{chain:e.chain??null,to:o,data:At([S,n])}),{retries:3,onFailedAttempt:async a=>{let l=a.attemptNumber*500;m(`failed to deploy ${t}, retrying in ${l}ms...`),await Ot(l)}})])}import{uniqueBy as Mt}from"@latticexyz/common/utils";async function k({client:e,deployerAddress:o,contracts:n}){let r=Mt(n,s=>s.bytecode),t=(await Promise.all(r.map(s=>Ie({client:e,deployerAddress:o,...s})))).flat();if(t.length){m("waiting for contracts");for(let s of t)await It(e,{hash:s})}return t}import He from"@latticexyz/world/out/WorldFactory.sol/WorldFactory.json"assert{type:"json"};import Rt from"@latticexyz/world/out/WorldFactory.sol/WorldFactory.abi.json"assert{type:"json"};import{getCreate2Address as jt,encodeDeployData as Ht,size as $t}from"viem";import Me from"@latticexyz/world/out/AccessManagementSystem.sol/AccessManagementSystem.json"assert{type:"json"};import Be from"@latticexyz/world/out/BalanceTransferSystem.sol/BalanceTransferSystem.json"assert{type:"json"};import We from"@latticexyz/world/out/BatchCallSystem.sol/BatchCallSystem.json"assert{type:"json"};import Re from"@latticexyz/world/out/RegistrationSystem.sol/RegistrationSystem.json"assert{type:"json"};import je from"@latticexyz/world/out/InitModule.sol/InitModule.json"assert{type:"json"};import Bt from"@latticexyz/world/out/InitModule.sol/InitModule.abi.json"assert{type:"json"};import{getCreate2Address as z,encodeDeployData as Wt,size as N}from"viem";function Q(e){let o=N(Me.deployedBytecode.object),n=Me.bytecode.object,r=z({from:e,bytecode:n,salt:S}),t=N(Be.deployedBytecode.object),s=Be.bytecode.object,i=z({from:e,bytecode:s,salt:S}),a=N(We.deployedBytecode.object),l=We.bytecode.object,d=z({from:e,bytecode:l,salt:S}),c=N(Re.deployedBytecode.object),p=Re.bytecode.object,u=z({from:e,bytecode:p,salt:S}),h=N(je.deployedBytecode.object),w=Wt({bytecode:je.bytecode.object,abi:Bt,args:[r,i,d,u]}),g=z({from:e,bytecode:w,salt:S});return{AccessManagementSystem:{bytecode:n,deployedBytecodeSize:o,label:"access management system",address:r},BalanceTransferSystem:{bytecode:s,deployedBytecodeSize:t,label:"balance transfer system",address:i},BatchCallSystem:{bytecode:l,deployedBytecodeSize:a,label:"batch call system",address:d},RegistrationSystem:{bytecode:p,deployedBytecodeSize:c,label:"core registration system",address:u},InitModule:{bytecode:w,deployedBytecodeSize:h,label:"core module",address:g}}}function Z(e){let o=Q(e),n=$t(He.deployedBytecode.object),r=Ht({bytecode:He.bytecode.object,abi:Rt,args:[o.InitModule.address]}),t=jt({from:e,bytecode:r,salt:S});return{...o,WorldFactory:{bytecode:r,deployedBytecodeSize:n,label:"world factory",address:t}}}import $e from"@latticexyz/world/out/WorldProxyFactory.sol/WorldProxyFactory.json"assert{type:"json"};import Et from"@latticexyz/world/out/WorldProxyFactory.sol/WorldProxyFactory.abi.json"assert{type:"json"};import{getCreate2Address as Ft,encodeDeployData as zt,size as Nt}from"viem";function X(e){let o=Q(e),n=Nt($e.deployedBytecode.object),r=zt({bytecode:$e.bytecode.object,abi:Et,args:[o.InitModule.address]}),t=Ft({from:e,bytecode:r,salt:S});return{...o,WorldProxyFactory:{bytecode:r,deployedBytecodeSize:n,label:"world proxy factory",address:t}}}async function ee(e,o,n){if(n){let t=X(o);return await k({client:e,deployerAddress:o,contracts:Object.values(t)}),t.WorldProxyFactory.address}let r=Z(o);return await k({client:e,deployerAddress:o,contracts:Object.values(r)}),r.WorldFactory.address}import Jt from"@latticexyz/world/out/WorldFactory.sol/WorldFactory.abi.json"assert{type:"json"};import{writeContract as qt}from"@latticexyz/common";import{AbiEventSignatureNotFoundError as _t,decodeEventLog as Lt,hexToString as Ee,parseAbi as Vt}from"viem";import{isDefined as Ut}from"@latticexyz/common/utils";function oe(e){let o=e.map(i=>{try{return{...i,...Lt({strict:!0,abi:Vt(G),topics:i.topics,data:i.data})}}catch(a){if(a instanceof _t)return;throw a}}).filter(Ut),{address:n,deployBlock:r,worldVersion:t,storeVersion:s}=o.reduce((i,a)=>({...i,address:a.address,deployBlock:a.blockNumber,...a.eventName==="HelloWorld"?{worldVersion:Ee(a.args.worldVersion).replace(/\0+$/,"")}:null,...a.eventName==="HelloStore"?{storeVersion:Ee(a.args.storeVersion).replace(/\0+$/,"")}:null}),{});if(n==null)throw new Error("could not find world address");if(r==null)throw new Error("could not find world deploy block number");if(t==null)throw new Error("could not find world version");if(s==null)throw new Error("could not find store version");return{address:n,deployBlock:r,worldVersion:t,storeVersion:s}}async function Fe(e,o,n,r){let t=await ee(e,o,r);m("deploying world");let s=await qt(e,{chain:e.chain??null,address:t,abi:Jt,functionName:"deployWorld",args:[n]});m("waiting for world deploy");let i=await Kt(e,{hash:s});if(i.status!=="success")throw console.error("world deploy failed",i),new Error("world deploy failed");let a=oe(i.logs);return m("deployed world to",a.address,"at block",a.deployBlock),{...a,stateBlock:a.deployBlock}}import{resourceToLabel as me,writeContract as rr}from"@latticexyz/common";import{valueSchemaToFieldLayoutHex as nr,keySchemaToHex as sr,valueSchemaToHex as ar,getSchemaTypes as Ue,getValueSchema as ir,getKeySchema as dr}from"@latticexyz/protocol-parser/internal";import{parseAbiItem as Yt,decodeAbiParameters as ze,parseAbiParameters as Ne}from"viem";import{hexToResource as Gt}from"@latticexyz/common";import{storeSetRecordEvent as Qt}from"@latticexyz/store";import{getLogs as Zt}from"viem/actions";import{decodeKey as Xt,decodeValueArgs as er,getKeySchema as or,getSchemaTypes as _e,getValueSchema as tr,hexToSchema as Le}from"@latticexyz/protocol-parser/internal";async function Ve({client:e,worldDeploy:o}){m("looking up tables for",o.address);let r=(await Zt(e,{strict:!0,fromBlock:o.deployBlock,toBlock:o.stateBlock,address:o.address,event:Yt(Qt),args:{tableId:W.store__Tables.tableId}})).map(t=>{let{tableId:s}=Xt(_e(or(W.store__Tables)),t.args.keyTuple),{type:i,namespace:a,name:l}=Gt(s),d=er(_e(tr(W.store__Tables)),t.args),c=Le(d.keySchema),p=Le(d.valueSchema),u=ze(Ne("string[]"),d.abiEncodedKeyNames)[0],h=ze(Ne("string[]"),d.abiEncodedFieldNames)[0],w=[...p.staticFields,...p.dynamicFields],g=Object.fromEntries(c.staticFields.map((y,M)=>[u[M],{type:y,internalType:y}])),x=Object.fromEntries(w.map((y,M)=>[h[M],{type:y,internalType:y}]));return{type:i,namespace:a,name:l,tableId:s,schema:{...g,...x},key:Object.keys(g)}});return m("found",r.length,"tables for",o.address),r}import cr from"p-retry";import{wait as lr}from"@latticexyz/common/utils";async function Ke({client:e,worldDeploy:o,tables:n}){let t=(await Ve({client:e,worldDeploy:o})).map(a=>a.tableId),s=n.filter(a=>t.includes(a.tableId));s.length&&m("existing tables",s.map(me).join(", "));let i=n.filter(a=>!t.includes(a.tableId));return i.length?(m("registering tables",i.map(me).join(", ")),await Promise.all(i.map(a=>{let l=Ue(dr(a)),d=Ue(ir(a));return cr(()=>rr(e,{chain:e.chain??null,address:o.address,abi:C,functionName:"registerTable",args:[a.tableId,nr(d),sr(l),ar(d),Object.keys(l),Object.keys(d)]}),{retries:3,onFailedAttempt:async c=>{let p=c.attemptNumber*500;m(`failed to register table ${me(a)}, retrying in ${p}ms...`),await lr(p)}})}))):[]}import{getAddress as O}from"viem";import{writeContract as pe,resourceToLabel as _}from"@latticexyz/common";import{parseAbiItem as mr,HttpRequestError as pr}from"viem";import{getLogs as fr}from"viem/actions";import{storeSpliceStaticDataEvent as yr}from"@latticexyz/store";import ur from"p-retry";async function te({client:e,worldDeploy:o}){m("looking up resource IDs for",o.address);let r=(await ur(()=>fr(e,{strict:!0,address:o.address,fromBlock:o.deployBlock,toBlock:o.stateBlock,event:mr(yr),args:{tableId:W.store__ResourceIds.tableId}}),{retries:3,onFailedAttempt:async t=>{if(!(t instanceof pr&&t.status===400&&t.message.includes("block is out of range")))throw t}})).map(t=>t.args.keyTuple[0]);return m("found",r.length,"resource IDs for",o.address),r}import{hexToResource as Pr,resourceToLabel as Or}from"@latticexyz/common";import{decodeValueArgs as gr,encodeKey as br,getKeySchema as hr,getSchemaTypes as Je,getValueSchema as wr}from"@latticexyz/protocol-parser/internal";import{readContract as xr}from"viem/actions";async function R({client:e,worldDeploy:o,table:n,key:r}){let[t,s,i]=await xr(e,{blockNumber:o.stateBlock,address:o.address,abi:C,functionName:"getRecord",args:[n.tableId,br(Je(hr(n)),r)]});return gr(Je(wr(n)),{staticData:t,encodedLengths:s,dynamicData:i})}import{parseAbiItem as qe}from"viem";import{storeSetRecordEvent as Ye}from"@latticexyz/store";import{getLogs as Ge}from"viem/actions";import{decodeKey as Qe,decodeValueArgs as Ze,getKeySchema as Xe,getSchemaTypes as re,getValueSchema as eo}from"@latticexyz/protocol-parser/internal";async function ne({client:e,worldDeploy:o}){m("looking up function selectors for",o.address);let r=(await Ge(e,{strict:!0,fromBlock:o.deployBlock,toBlock:o.stateBlock,address:o.address,event:qe(Ye),args:{tableId:A.world__FunctionSelectors.tableId}})).map(a=>({...Ze(re(eo(A.world__FunctionSelectors)),a.args),...Qe(re(Xe(A.world__FunctionSelectors)),a.args.keyTuple)}));m("found",r.length,"function selectors for",o.address),m("looking up function signatures for",o.address);let t=await Ge(e,{strict:!0,fromBlock:o.deployBlock,toBlock:o.stateBlock,address:o.address,event:qe(Ye),args:{tableId:A.world__FunctionSignatures.tableId}}),s=Object.fromEntries(t.map(a=>[Qe(re(Xe(A.world__FunctionSignatures)),a.args.keyTuple).functionSelector,Ze(re(eo(A.world__FunctionSignatures)),a.args).functionSignature]));return m("found",t.length,"function signatures for",o.address),r.map(({worldFunctionSelector:a,systemFunctionSelector:l,systemId:d})=>({selector:a,signature:s[a],systemFunctionSelector:l,systemFunctionSignature:s[l],systemId:d}))}import{parseAbiItem as Sr,getAddress as Cr}from"viem";import{storeSpliceStaticDataEvent as Dr}from"@latticexyz/store";import{getLogs as Ar}from"viem/actions";import{decodeKey as vr,getKeySchema as Tr,getSchemaTypes as kr}from"@latticexyz/protocol-parser/internal";async function se({client:e,worldDeploy:o}){m("looking up resource access for",o.address);let r=(await Ar(e,{strict:!0,fromBlock:o.deployBlock,toBlock:o.stateBlock,address:o.address,event:Sr(Dr),args:{tableId:A.world__ResourceAccess.tableId}})).map(s=>vr(kr(Tr(A.world__ResourceAccess)),s.args.keyTuple)),t=(await Promise.all(r.map(async s=>[s,await R({client:e,worldDeploy:o,table:A.world__ResourceAccess,key:s})]))).filter(([,s])=>s.access).map(([s])=>({resourceId:s.resourceId,address:Cr(s.caller)}));return m("found",t.length,"resource<>address access pairs"),t}async function oo({client:e,worldDeploy:o}){let[n,r,t]=await Promise.all([te({client:e,worldDeploy:o}),ne({client:e,worldDeploy:o}),se({client:e,worldDeploy:o})]),s=n.map(Pr).filter(i=>i.type==="system");return m("looking up systems",s.map(Or).join(", ")),await Promise.all(s.map(async i=>{let{system:a,publicAccess:l}=await R({client:e,worldDeploy:o,table:A.world__Systems,key:{systemId:i.resourceId}}),d=r.filter(c=>c.systemId===i.resourceId);return{address:a,namespace:i.namespace,name:i.name,systemId:i.resourceId,allowAll:l,allowedAddresses:t.filter(({resourceId:c})=>c===i.resourceId).map(({address:c})=>c),functions:d}}))}import{wait as fe}from"@latticexyz/common/utils";import ye from"p-retry";async function to({client:e,deployerAddress:o,libraries:n,worldDeploy:r,systems:t}){let[s,i]=await Promise.all([oo({client:e,worldDeploy:r}),se({client:e,worldDeploy:r})]),a=t.filter(f=>s.some(b=>b.systemId===f.systemId&&O(b.address)===O(f.prepareDeploy(o,n).address)));a.length&&m("existing systems",a.map(_).join(", "));let l=a.map(f=>f.systemId),d=t.filter(f=>!l.includes(f.systemId));if(!d.length)return[];let c=d.filter(f=>s.some(b=>b.systemId===f.systemId&&O(b.address)!==O(f.prepareDeploy(o,n).address)));c.length&&m("upgrading systems",c.map(_).join(", "));let p=d.filter(f=>!s.some(b=>b.systemId===f.systemId));p.length&&m("registering new systems",p.map(_).join(", ")),await k({client:e,deployerAddress:o,contracts:d.map(f=>({bytecode:f.prepareDeploy(o,n).bytecode,deployedBytecodeSize:f.deployedBytecodeSize,label:`${_(f)} system`}))});let u=await Promise.all(d.map(f=>ye(()=>pe(e,{chain:e.chain??null,address:r.address,abi:C,functionName:"registerSystem",args:[f.systemId,f.prepareDeploy(o,n).address,f.allowAll]}),{retries:3,onFailedAttempt:async b=>{let D=b.attemptNumber*500;m(`failed to register system ${_(f)}, retrying in ${D}ms...`),await fe(D)}}))),h=t.map(f=>f.systemId),w=i.filter(({resourceId:f})=>h.includes(f)),g=[...t.flatMap(f=>f.allowedAddresses.map(b=>({resourceId:f.systemId,address:b}))),...t.flatMap(f=>f.allowedSystemIds.map(b=>({resourceId:f.systemId,address:s.find(D=>D.systemId===b)?.address??t.find(D=>D.systemId===b)?.prepareDeploy(o,n).address})).filter(b=>b.address!=null))],x=g.filter(f=>!w.some(({resourceId:b,address:D})=>b===f.resourceId&&O(D)===O(f.address))),y=w.filter(f=>!g.some(({resourceId:b,address:D})=>b===f.resourceId&&O(D)===O(f.address)));y.length&&m("revoking",y.length,"access grants"),x.length&&m("adding",x.length,"access grants");let M=await Promise.all([...y.map(f=>ye(()=>pe(e,{chain:e.chain??null,address:r.address,abi:C,functionName:"revokeAccess",args:[f.resourceId,f.address]}),{retries:3,onFailedAttempt:async b=>{let D=b.attemptNumber*500;m(`failed to revoke access, retrying in ${D}ms...`),await fe(D)}})),...x.map(f=>ye(()=>pe(e,{chain:e.chain??null,address:r.address,abi:C,functionName:"grantAccess",args:[f.resourceId,f.address]}),{retries:3,onFailedAttempt:async b=>{let D=b.attemptNumber*500;m(`failed to grant access, retrying in ${D}ms...`),await fe(D)}}))]);return[...u,...M]}import{waitForTransactionReceipt as go}from"viem/actions";import{getAddress as Ir,parseAbi as Mr}from"viem";import{getBlockNumber as Br,getLogs as Wr}from"viem/actions";var ro=new Map;async function no(e,o){let n=Ir(o),r=ro.get(n);if(r!=null)return r;m("looking up world deploy for",n);let t=await Br(e),s=await Wr(e,{strict:!0,address:n,events:Mr(G),fromBlock:"earliest",toBlock:t});return r={...oe(s),stateBlock:t},ro.set(n,r),m("found world deploy for",n,"at block",r.deployBlock),r}import{hexToResource as Rr,writeContract as so}from"@latticexyz/common";import ao from"p-retry";import{wait as io}from"@latticexyz/common/utils";async function co({client:e,worldDeploy:o,functions:n}){let r=await ne({client:e,worldDeploy:o}),t=Object.fromEntries(r.map(a=>[a.selector,a])),s=n.filter(a=>t[a.selector]),i=n.filter(a=>!s.includes(a));if(s.length){m("functions already registered:",s.map(l=>l.signature).join(", "));let a=s.filter(l=>l.systemId!==t[l.selector]?.systemId);a.length&&console.warn("found",a.length,"functions already registered but pointing at a different system ID:",a.map(l=>l.signature).join(", "))}return i.length?(m("registering functions:",i.map(a=>a.signature).join(", ")),Promise.all(i.map(a=>{let{namespace:l}=Rr(a.systemId);return l===""?ao(()=>so(e,{chain:e.chain??null,address:o.address,abi:C,functionName:"registerRootFunctionSelector",args:[a.systemId,a.systemFunctionSignature,a.systemFunctionSignature]}),{retries:3,onFailedAttempt:async d=>{let c=d.attemptNumber*500;m(`failed to register function ${a.signature}, retrying in ${c}ms...`),await io(c)}}):ao(()=>so(e,{chain:e.chain??null,address:o.address,abi:C,functionName:"registerFunctionSelector",args:[a.systemId,a.systemFunctionSignature]}),{retries:3,onFailedAttempt:async d=>{let c=d.attemptNumber*500;m(`failed to register function ${a.signature}, retrying in ${c}ms...`),await io(c)}})}))):[]}import{BaseError as jr}from"viem";import{writeContract as lo}from"@latticexyz/common";import{isDefined as Hr,wait as $r}from"@latticexyz/common/utils";import Er from"p-retry";async function mo({client:e,deployerAddress:o,libraries:n,worldDeploy:r,modules:t}){return t.length?(await k({client:e,deployerAddress:o,contracts:t.map(s=>({bytecode:s.prepareDeploy(o,n).bytecode,deployedBytecodeSize:s.deployedBytecodeSize,label:`${s.name} module`}))}),m("installing modules:",t.map(s=>s.name).join(", ")),(await Promise.all(t.map(s=>Er(async()=>{try{let i=[...C,...s.abi],a=s.prepareDeploy(o,n).address;return s.installAsRoot?await lo(e,{chain:e.chain??null,address:r.address,abi:i,functionName:"installRootModule",args:[a,s.installData]}):await lo(e,{chain:e.chain??null,address:r.address,abi:i,functionName:"installModule",args:[a,s.installData]})}catch(i){if(i instanceof jr&&i.message.includes("Module_AlreadyInstalled")){m(`module ${s.name} already installed`);return}throw i}},{retries:3,onFailedAttempt:async i=>{let a=i.attemptNumber*500;m(`failed to install module ${s.name}, retrying in ${a}ms...`),await $r(a)}})))).filter(Hr)):[]}import{getAddress as po}from"viem";import{hexToResource as fo,resourceToHex as yo,writeContract as Fr}from"@latticexyz/common";async function uo({client:e,worldDeploy:o,resourceIds:n}){let r=Array.from(new Set(n.map(p=>fo(p).namespace))),t=await te({client:e,worldDeploy:o}),s=new Set(t.map(p=>fo(p).namespace));s.size&&m("found",s.size,"existing namespaces:",Array.from(s).map(p=>p===""?"<root>":p).join(", "));let i=r.filter(p=>s.has(p)),l=(await Promise.all(i.map(async p=>{let{owner:u}=await R({client:e,worldDeploy:o,table:A.world__NamespaceOwner,key:{namespaceId:yo({type:"namespace",namespace:p,name:""})}});return[p,u]}))).filter(([,p])=>po(p)!==po(e.account.address)).map(([p])=>p);if(l.length)throw new Error(`You are attempting to deploy to namespaces you do not own: ${l.join(", ")}`);let d=r.filter(p=>!s.has(p));return d.length>0&&m("registering namespaces",Array.from(d).join(", ")),Promise.all(d.map(p=>Fr(e,{chain:e.chain??null,address:o.address,abi:C,functionName:"registerNamespace",args:[yo({namespace:p,type:"namespace",name:""})]})))}import{resourceToLabel as zr}from"@latticexyz/common";import{randomBytes as Nr}from"crypto";async function bo({client:e,tables:o,systems:n,libraries:r,modules:t=[],salt:s,worldAddress:i,deployerAddress:a,withWorldProxy:l}){let d=a??await ke(e);await ee(e,d,l),await k({client:e,deployerAddress:d,contracts:[...r.map(y=>({bytecode:y.prepareDeploy(d,r).bytecode,deployedBytecodeSize:y.deployedBytecodeSize,label:`${y.path}:${y.name} library`})),...n.map(y=>({bytecode:y.prepareDeploy(d,r).bytecode,deployedBytecodeSize:y.deployedBytecodeSize,label:`${zr(y)} system`})),...t.map(y=>({bytecode:y.prepareDeploy(d,r).bytecode,deployedBytecodeSize:y.deployedBytecodeSize,label:`${y.name} module`}))]});let c=i?await no(e,i):await Fe(e,d,s??`0x${Nr(32).toString("hex")}`,l);if(!Pe.includes(c.storeVersion))throw new Error(`Unsupported Store version: ${c.storeVersion}`);if(!Oe.includes(c.worldVersion))throw new Error(`Unsupported World version: ${c.worldVersion}`);let p=await uo({client:e,worldDeploy:c,resourceIds:[...o.map(y=>y.tableId),...n.map(y=>y.systemId)]});m("waiting for all namespace registration transactions to confirm");for(let y of p)await go(e,{hash:y});let u=await Ke({client:e,worldDeploy:c,tables:o}),h=await to({client:e,deployerAddress:d,libraries:r,worldDeploy:c,systems:n}),w=await co({client:e,worldDeploy:c,functions:n.flatMap(y=>y.functions)}),g=await mo({client:e,deployerAddress:d,libraries:r,worldDeploy:c,modules:t}),x=[...u,...h,...w,...g];m("waiting for all transactions to confirm");for(let y of x)await go(e,{hash:y});return m("deploy complete"),c}import{createWalletClient as bn,http as hn,isHex as wn}from"viem";import{privateKeyToAccount as xn}from"viem/accounts";import{loadConfig as Sn,resolveConfigPath as Cn}from"@latticexyz/config/node";import{getOutDirectory as Dn,getRpcUrl as An}from"@latticexyz/common/foundry";import H from"chalk";import{MUDError as ge}from"@latticexyz/common/errors";import Gr from"path";import{resolveSystems as Qr}from"@latticexyz/world/node";import{isHex as xo,toFunctionSelector as So,toFunctionSignature as Co}from"viem";import{readFileSync as _r}from"fs";import Lr from"path";import{MUDError as ie}from"@latticexyz/common/errors";import{size as Vr}from"viem";function ae(e){return Object.entries(e).flatMap(([o,n])=>Object.entries(n).flatMap(([r,t])=>t.map(s=>({path:o,name:r,start:s.start,length:s.length}))))}function j(e,o,n){let r,t=Lr.join(n,e,o+".json");try{r=JSON.parse(_r(t,"utf8"))}catch{throw new ie(`Error reading file at ${t}`)}let s=r?.bytecode?.object;if(!s)throw new ie(`No bytecode found in ${t}`);let i=r?.deployedBytecode?.object;if(!i)throw new ie(`No deployed bytecode found in ${t}`);let a=r?.abi;if(!a)throw new ie(`No ABI found in ${t}`);let l=ae(r?.bytecode?.linkReferences??{});return{abi:a,bytecode:s,placeholders:l,deployedBytecodeSize:Vr(i)}}import{groupBy as Zr}from"@latticexyz/common/utils";import{readFileSync as Kr}from"fs";import{globSync as Jr}from"glob";import Ur from"toposort";function ho(e,o,n){let r=Ur(e.flatMap(t=>n(t).map(s=>[o(t),s])));return[...e].sort((t,s)=>r.indexOf(o(t))-r.indexOf(o(s)))}function wo(e){let n=Jr(`${e}/**/*.json`,{ignore:"**/*.abi.json"}).sort().map(r=>JSON.parse(Kr(r,"utf8"))).flatMap(r=>{if(!r.metadata)return[];let t=Object.keys(r.metadata.settings.compilationTarget)[0],s=r.metadata.settings.compilationTarget[t],i=r.bytecode.linkReferences;return Object.entries(i).flatMap(([a,l])=>Object.keys(l).map(d=>({path:a,name:d,dependentPath:t,dependentName:s})))});return ho(n,r=>`${r.path}:${r.name}`,r=>[`${r.dependentPath}:${r.dependentName}`])}import{spliceHex as qr}from"@latticexyz/common";import{getCreate2Address as Yr}from"viem";function L(e,o){return function(r,t){let s=e;for(let i of o){let a=t.find(l=>l.path===i.path&&l.name===i.name);if(!a)throw new Error(`Could not find library for bytecode placeholder ${i.path}:${i.name}`);s=qr(s,i.start,i.length,a.prepareDeploy(r,t).address)}return{bytecode:s,address:Yr({from:r,bytecode:s,salt:S})}}}async function Do({rootDir:e,config:o,forgeOutDir:n}){let r=wo(n).map(c=>{let p=j(Gr.basename(c.path),c.name,n);return{path:c.path,name:c.name,abi:p.abi,prepareDeploy:L(p.bytecode,p.placeholders),deployedBytecodeSize:p.deployedBytecodeSize}}),s=j("System.sol","System",n).abi.filter(c=>c.type==="function").map(Co),i=await Qr({rootDir:e,config:o}),a=i.map(c=>{let p=j(`${c.label}.sol`,c.label,n),u=p.abi.filter(g=>g.type==="function").map(Co).filter(g=>!s.includes(g)).map(g=>{let x=c.namespace===""?g:`${c.namespace}__${g}`;return{signature:x,selector:So(x),systemId:c.systemId,systemFunctionSignature:g,systemFunctionSelector:So(g)}}),h=c.accessList.filter(g=>xo(g)),w=c.accessList.filter(g=>!xo(g)).map(g=>i.find(y=>y.label===g).systemId);return{...c,allowAll:c.openAccess,allowedAddresses:h,allowedSystemIds:w,prepareDeploy:L(p.bytecode,p.placeholders),deployedBytecodeSize:p.deployedBytecodeSize,abi:p.abi,functions:u}}),l=Zr(a,c=>c.systemId),d=Array.from(l.values()).filter(c=>c.length>1).flat();if(d.length){let c=d.map(p=>p.name);throw new Error(`Found systems with overlapping system ID: ${c.join(", ")}.
14
+
15
+ System IDs are generated from the first 16 bytes of the name, so you may need to rename them to avoid the overlap.`)}return{systems:a,libraries:r}}import{getChainId as vn}from"viem/actions";import{existsSync as Xr}from"fs";import en from"path";import on from"chalk";import{getScriptDirectory as tn,forge as rn}from"@latticexyz/common/foundry";async function Ao(e,o,n,r,t,s){let i=t?.replaceAll("\\","").split(" ")??[],a=en.join(await tn(),e+".s.sol");if(!Xr(a)){console.log(`No script at ${a}, skipping post deploy hook`);return}console.log(on.blue(`Executing post deploy script at ${a}`)),await rn(["script",e,"--broadcast","--sig","run(address)",o,"--rpc-url",n,"-vvv",s?"--aws":"",...i],{profile:r})}import{kmsKeyToAccount as Tn}from"@latticexyz/common/kms";import Po from"node:path";import{encodeField as mn}from"@latticexyz/protocol-parser/internal";import{bytesToHex as pn}from"viem";import{isHex as nn,size as sn}from"viem";import{z as P}from"zod";import{Abi as an}from"abitype/zod";import{createRequire as dn}from"node:module";import{findUp as cn}from"find-up";var vo=P.object({object:P.string().refine(nn),linkReferences:P.record(P.record(P.array(P.object({start:P.number(),length:P.number()}))))}),ln=P.object({bytecode:vo,deployedBytecode:vo,abi:an});async function To({packageJsonPath:e,artifactPath:o}){let n;try{let s=e??await cn("package.json",{cwd:process.cwd()});if(!s)throw new Error("Could not find package.json to import relative to.");n=dn(s)(o)}catch(s){throw console.error(),console.error("Could not import contract artifact at",o),console.error(),s}let r=ln.parse(n),t=ae(r.bytecode.linkReferences);return{abi:r.abi,bytecode:r.bytecode.object,placeholders:t,deployedBytecodeSize:sn(r.deployedBytecode.object)}}var ko={KeysWithValueModule:"@latticexyz/world-modules/out/KeysWithValueModule.sol/KeysWithValueModule.json",KeysInTableModule:"@latticexyz/world-modules/out/KeysInTableModule.sol/KeysInTableModule.json",UniqueEntityModule:"@latticexyz/world-modules/out/UniqueEntityModule.sol/UniqueEntityModule.json",Unstable_CallWithSignatureModule:"@latticexyz/world-modules/out/Unstable_CallWithSignatureModule.sol/Unstable_CallWithSignatureModule.json"};import{resolveWithContext as fn}from"@latticexyz/world/internal";async function de(e,o){return await Promise.all(e.modules.map(async r=>{let t=r.artifactPath;if(!t)if(r.name)t=ko[r.name]??Po.join(o,`${r.name}.sol`,`${r.name}.json`),console.warn(["","\u26A0\uFE0F Your `mud.config.ts` is using a module with a `name`, but this option is deprecated.","","To resolve this, you can replace this:","",` name: ${JSON.stringify(r.name)}`,"","with this:","",` artifactPath: ${JSON.stringify(t)}`,""].join(`
16
+ `));else throw new Error("No `artifactPath` provided for module.");let s=Po.basename(t,".json"),i=await To({artifactPath:t}),a=r.args.map(l=>fn(l,{config:e})).map(l=>{let d=l.value instanceof Uint8Array?pn(l.value):l.value;return mn(l.type,d)});if(a.length>1)throw new Error(`${s} module should only have 0-1 args, but had ${a.length} args.`);return{name:s,installAsRoot:r.root,installData:a.length===0?"0x":a[0],prepareDeploy:L(i.bytecode,i.placeholders),deployedBytecodeSize:i.deployedBytecodeSize,abi:i.abi}}))}var T={configPath:{type:"string",desc:"Path to the MUD config file"},printConfig:{type:"boolean",desc:"Print the resolved config"},profile:{type:"string",desc:"The foundry profile to use"},saveDeployment:{type:"boolean",desc:"Save the deployment info to a file",default:!0},rpc:{type:"string",desc:"The RPC URL to use. Defaults to the RPC url from the local foundry.toml"},rpcBatch:{type:"boolean",desc:"Enable batch processing of RPC requests in viem client (defaults to batch size of 100 and wait of 1s)"},deployerAddress:{type:"string",desc:"Deploy using an existing deterministic deployer (https://github.com/Arachnid/deterministic-deployment-proxy)"},worldAddress:{type:"string",desc:"Deploy to an existing World at the given address"},skipBuild:{type:"boolean",desc:"Skip rebuilding the contracts before deploying"},alwaysRunPostDeploy:{type:"boolean",desc:"Always run PostDeploy.s.sol after each deploy (including during upgrades). By default, PostDeploy.s.sol is only run once after a new world is deployed."},forgeScriptOptions:{type:"string",description:"Options to pass to forge script PostDeploy.s.sol"},salt:{type:"string",desc:"The deployment salt to use. Defaults to a random salt."},kms:{type:"boolean",desc:"Deploy the World with an AWS KMS key instead of local private key."}};async function $(e){let o=e.salt;if(o!=null&&!wn(o))throw new ge("Expected hex string for salt");let n=e.profile??process.env.FOUNDRY_PROFILE,r=await Cn(e.configPath),t=await Sn(r),s=ce.dirname(r);e.printConfig&&console.log(H.green(`
17
+ Resolved config:
18
+ `),JSON.stringify(t,null,2));let i=await Dn(n),a=e.rpc??await An(n);console.log(H.bgBlue(H.whiteBright(`
19
+ Deploying MUD contracts${n?" with profile "+n:""} to RPC ${a}
20
+ `))),e.skipBuild||await K({rootDir:s,config:t,foundryProfile:n});let{systems:l,libraries:d}=await Do({rootDir:s,config:t,forgeOutDir:i}),c=await de(t,i),p=await(async()=>{if(e.kms){let x=process.env.AWS_KMS_KEY_ID;if(!x)throw new ge("Missing `AWS_KMS_KEY_ID` environment variable. This is required when using with `--kms` option.");return await Tn({keyId:x})}else{let x=process.env.PRIVATE_KEY;if(!x)throw new ge(`Missing PRIVATE_KEY environment variable.
21
+ Run 'echo "PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" > .env'
22
+ in your contracts directory to use the default anvil private key.`);return xn(x)}})(),u=bn({transport:hn(a,{batch:e.rpcBatch?{batchSize:100,wait:1e3}:void 0}),account:p});console.log("Deploying from",u.account.address);let h=Date.now(),w=await bo({deployerAddress:e.deployerAddress,salt:o,worldAddress:e.worldAddress,client:u,tables:Object.values(t.tables),systems:l,libraries:d,modules:c,withWorldProxy:t.deploy.upgradeableWorldImplementation});(e.worldAddress==null||e.alwaysRunPostDeploy)&&await Ao(t.deploy.postDeployScript,w.address,a,n,e.forgeScriptOptions,!!e.kms),console.log(H.green("Deployment completed in",(Date.now()-h)/1e3,"seconds"));let g={worldAddress:w.address,blockNumber:Number(w.deployBlock)};if(e.saveDeployment){let x=await vn(u),y=ce.join(t.deploy.deploysDirectory,x.toString());un(y,{recursive:!0}),ue(ce.join(y,"latest.json"),JSON.stringify(g,null,2)),ue(ce.join(y,Date.now()+".json"),JSON.stringify(g,null,2));let M=[1337,31337],f=yn(t.deploy.worldsFile)?JSON.parse(gn(t.deploy.worldsFile,"utf-8")):{};f[x]={address:g.worldAddress,blockNumber:M.includes(x)?void 0:g.blockNumber},ue(t.deploy.worldsFile,JSON.stringify(f,null,2)),console.log(H.bgGreen(H.whiteBright(`
23
+ Deployment result (written to ${t.deploy.worldsFile} and ${y}):
24
+ `)))}return console.log(g),w}var kn={command:"deploy",describe:"Deploy MUD contracts",builder(e){return e.options(T)},async handler(e){try{await $(e)}catch(o){U(o),process.exit(1)}process.exit(0)}},Oo=kn;import Pn from"node:path";import{loadConfig as On,resolveConfigPath as In}from"@latticexyz/config/node";import{worldgen as Mn}from"@latticexyz/world/node";var Bn={command:"worldgen",describe:"Autogenerate interfaces for Systems and World based on existing contracts and the config file",builder(e){return e.options({configPath:{type:"string",desc:"Path to the MUD config file"},clean:{type:"boolean",desc:"Clear the worldgen directory before generating new interfaces (defaults to true)",default:!0}})},async handler(e){await Wn(e),process.exit(0)}};async function Wn(e){let o=await In(e.configPath),n=await On(o),r=Pn.dirname(o);await Mn({rootDir:r,config:n,clean:e.clean})}var Io=Bn;import V from"chalk";import{readFileSync as En,writeFileSync as Fn}from"fs";import he from"path";import{MUDError as E}from"@latticexyz/common/errors";var Mo={name:"@latticexyz/cli",version:"2.0.12",description:"Command line interface for mud",repository:{type:"git",url:"https://github.com/latticexyz/mud.git",directory:"packages/cli"},license:"MIT",type:"module",exports:{".":"./dist/index.js"},typesVersions:{"*":{index:["./dist/index.d.ts"]}},bin:{mud:"./dist/mud.js"},files:["dist"],scripts:{build:"pnpm run build:js && pnpm run build:test-tables","build:js":"tsup && chmod +x ./dist/mud.js","build:test-tables":"tsx ./scripts/generate-test-tables.ts",clean:"pnpm run clean:js && pnpm run clean:test-tables","clean:js":"rimraf dist","clean:test-tables":"rimraf src/codegen",dev:"tsup --watch",lint:"eslint . --ext .ts",prepare:"mkdir -p ./dist && touch ./dist/mud.js",test:"tsc --noEmit && forge test","test:ci":"pnpm run test"},dependencies:{"@aws-sdk/client-kms":"^3.556.0","@latticexyz/abi-ts":"workspace:*","@latticexyz/common":"workspace:*","@latticexyz/config":"workspace:*","@latticexyz/gas-report":"workspace:*","@latticexyz/protocol-parser":"workspace:*","@latticexyz/schema-type":"workspace:*","@latticexyz/store":"workspace:*","@latticexyz/utils":"workspace:*","@latticexyz/world":"workspace:*",abitype:"1.0.0","asn1.js":"^5.4.1",chalk:"^5.0.1",chokidar:"^3.5.3",debug:"^4.3.4",dotenv:"^16.0.3",execa:"^7.0.0","find-up":"^6.3.0",glob:"^10.4.2",openurl:"^1.1.1","p-queue":"^7.4.1","p-retry":"^5.1.2",path:"^0.12.7",rxjs:"7.5.5","throttle-debounce":"^5.0.0",toposort:"^2.0.2",typescript:"5.4.2",viem:"2.9.20",yargs:"^17.7.1",zod:"^3.22.2","zod-validation-error":"^1.3.0"},devDependencies:{"@types/debug":"^4.1.7","@types/ejs":"^3.1.1","@types/node":"^18.15.11","@types/openurl":"^1.0.0","@types/throttle-debounce":"^5.0.0","@types/toposort":"^2.0.6","@types/yargs":"^17.0.10","ds-test":"https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0","forge-std":"https://github.com/foundry-rs/forge-std.git#74cfb77e308dd188d2f58864aaf44963ae6b88b1",tsup:"^6.7.0",tsx:"^3.12.6",vitest:"0.34.6"}};import{globSync as zn}from"glob";import{ZodError as jn,z as Bo}from"zod";var Hn=Bo.object({MUD_PACKAGES:Bo.string().transform(e=>JSON.parse(e))});function $n(){try{return Hn.parse({MUD_PACKAGES:'{"@latticexyz/abi-ts":{"localPath":"packages/abi-ts"},"@latticexyz/block-logs-stream":{"localPath":"packages/block-logs-stream"},"@latticexyz/cli":{"localPath":"packages/cli"},"@latticexyz/common":{"localPath":"packages/common"},"@latticexyz/config":{"localPath":"packages/config"},"create-mud":{"localPath":"packages/create-mud"},"@latticexyz/dev-tools":{"localPath":"packages/dev-tools"},"@latticexyz/faucet":{"localPath":"packages/faucet"},"@latticexyz/gas-report":{"localPath":"packages/gas-report"},"@latticexyz/protocol-parser":{"localPath":"packages/protocol-parser"},"@latticexyz/query":{"localPath":"packages/query"},"@latticexyz/react":{"localPath":"packages/react"},"@latticexyz/recs":{"localPath":"packages/recs"},"@latticexyz/schema-type":{"localPath":"packages/schema-type"},"solhint-config-mud":{"localPath":"packages/solhint-config-mud"},"solhint-plugin-mud":{"localPath":"packages/solhint-plugin-mud"},"@latticexyz/store-indexer":{"localPath":"packages/store-indexer"},"@latticexyz/store-sync":{"localPath":"packages/store-sync"},"@latticexyz/store":{"localPath":"packages/store"},"@latticexyz/utils":{"localPath":"packages/utils"},"@latticexyz/world-modules":{"localPath":"packages/world-modules"},"@latticexyz/world":{"localPath":"packages/world"}}'})}catch(e){if(e instanceof jn){let{...o}=e.format();console.error(`
25
+ Missing or invalid environment variables:
26
+
27
+ ${Object.keys(o).join(`
28
+ `)}
29
+ `),process.exit(1)}throw e}}var be=$n().MUD_PACKAGES;var Nn={command:"set-version",describe:"Set MUD version in all package.json files and optionally backup the previously installed version",builder(e){return e.options({mudVersion:{alias:"v",type:"string",description:"Set MUD to the given version"},tag:{alias:"t",type:"string",description:"Set MUD to the latest version with the given tag from npm"},commit:{alias:"c",type:"string",description:"Set MUD to the version based on a given git commit hash from npm"},link:{alias:"l",type:"string",description:"Relative path to the local MUD root directory to link"}})},async handler(e){try{let o=["mudVersion","link","tag","commit","restore"],n=o.reduce((t,s)=>e[s]?t+1:t,0);if(n===0)throw new E(`You need to provide one these options: ${o.join(", ")}`);if(n>1)throw new E(`These options are mutually exclusive: ${o.join(", ")}`);e.link||(e.mudVersion=await _n(e));let r=zn("**/package.json").sort().filter(t=>!t.includes("node_modules"));for(let t of r)Ln(t,e)}catch(o){U(o)}finally{process.exit(0)}}};async function _n(e){e.mudVersion==="canary"&&(e.tag="main");let o;try{console.log(V.blue("Fetching available versions")),o=await(await fetch(`https://registry.npmjs.org/${Mo.name}`)).json()}catch{throw new E("Could not fetch available MUD versions")}if(e.tag){let n=o["dist-tags"][e.tag];if(!n)throw new E(`Could not find npm version with tag "${e.tag}"`);return console.log(V.green(`Latest version with tag ${e.tag}: ${n}`)),n}if(e.commit){let n=e.commit.substring(0,8),r=Object.keys(o.versions).find(t=>t.includes(n));if(!r)throw new E(`Could not find npm version based on commit "${e.commit}"`);return console.log(V.green(`Version from commit ${e.commit}: ${r}`)),r}return e.mudVersion}function Ln(e,o){let{link:n}=o,{mudVersion:r}=o,t=Vn(e),s=Object.keys(be),i={};for(let d in t.dependencies)s.includes(d)&&(i[d]=t.dependencies[d]);let a={};for(let d in t.devDependencies)s.includes(d)&&(a[d]=t.devDependencies[d]);for(let d in t.dependencies)s.includes(d)&&(t.dependencies[d]=l(d,"dependencies"));for(let d in t.devDependencies)s.includes(d)&&(t.devDependencies[d]=l(d,"devDependencies"));return Fn(e,JSON.stringify(t,null,2)+`
30
+ `),console.log(`Updating ${e}`),Wo(i,t.dependencies),Wo(a,t.devDependencies),t;function l(d,c){return n&&(r=Un(e,n,d)),r||t[c][d]}}function Vn(e){try{let o=En(e,"utf8");return JSON.parse(o)}catch{throw new E("Could not read JSON at "+e)}}function Wo(e,o){for(let n in e)e[n]!==o[n]&&console.log(`${n}: ${V.red(e[n])} -> ${V.green(o[n])}`)}function Un(e,o,n){let r=he.relative(he.dirname(e),process.cwd());return"link:"+he.join(r,o,be[n].localPath)}var Ro=Nn;import{anvil as Kn,forge as Jn,getRpcUrl as qn}from"@latticexyz/common/foundry";import Yn from"chalk";var Gn={...T,port:{type:"number",description:"Port to run internal node for fork testing on",default:4242},worldAddress:{type:"string",description:"Address of an existing world contract. If provided, deployment is skipped and the RPC provided in the foundry.toml is used for fork testing."},forgeOptions:{type:"string",description:"Options to pass to forge test"}},Qn={command:"test",describe:"Run tests in MUD contracts",builder(e){return e.options(Gn)},async handler(e){if(!e.worldAddress){let t=["--block-base-fee-per-gas","0","--port",String(e.port)];Kn(t)}let o=e.worldAddress?await qn(e.profile):`http://127.0.0.1:${e.port}`,n=e.worldAddress??(await $({...e,saveDeployment:!1,rpc:o})).address;console.log(Yn.blue("World address",n));let r=e.forgeOptions?.replaceAll("\\","").split(" ")??[];try{await Jn(["test","--fork-url",o,...r],{profile:e.profile,env:{WORLD_ADDRESS:n}}),process.exit(0)}catch(t){console.error(t),process.exit(1)}}},jo=Qn;import Zn from"node:path";import Ho from"node:fs";import{loadConfig as Xn,resolveConfigPath as es}from"@latticexyz/config/node";import{MUDError as $o}from"@latticexyz/common/errors";import{cast as os,getRpcUrl as ts}from"@latticexyz/common/foundry";import rs from"@latticexyz/world/mud.config";import{createClient as ns,http as ss}from"viem";import{getChainId as as,readContract as is}from"viem/actions";import{resolveSystems as ds}from"@latticexyz/world/node";var cs=rs.tables.world__Systems.tableId;function ls(e,o){if(!Ho.existsSync(e))throw new $o(`Missing expected worlds.json file at "${e}"`);let n=JSON.parse(Ho.readFileSync(e,"utf-8"));if(!n[o])throw new $o(`Missing chain ID ${o} in "${e}"`);return n[o].address}var ms={command:"trace",describe:"Display the trace of a transaction",builder(e){return e.options({tx:{type:"string",required:!0,description:"Transaction hash to replay"},worldAddress:{type:"string",description:"World contract address. Defaults to the value from worlds.json, based on rpc's chainId"},configPath:{type:"string",description:"Path to the MUD config file"},profile:{type:"string",description:"The foundry profile to use"},rpc:{type:"string",description:"json rpc endpoint. Defaults to foundry's configured eth_rpc_url"}})},async handler(e){let o=await es(e.configPath),n=Zn.dirname(o),r=e.profile??process.env.FOUNDRY_PROFILE,t=e.rpc??await ts(r),s=await Xn(o),i=ns({transport:ss(t)}),a=await as(i),l=e.worldAddress??ls(s.deploy.worldsFile,a),d=await ds({rootDir:n,config:s}),c=await Promise.all(d.map(async u=>({label:u.label,address:await is(i,{abi:C,address:l,functionName:"getField",args:[cs,[u.systemId],0]})}))),p=await os(["run","--label",`${l}:World`,...c.map(({label:u,address:h})=>["--label",`${h}:${u}`]).flat(),`${e.tx}`]);console.log(p),process.exit(0)}},Eo=ms;import{anvil as ps,getScriptDirectory as fs,getSrcDirectory as ys}from"@latticexyz/common/foundry";import I from"chalk";import us from"chokidar";import{loadConfig as gs,resolveConfigPath as bs}from"@latticexyz/config/node";import hs from"path";import{homedir as ws}from"os";import{rmSync as xs}from"fs";import{BehaviorSubject as Ss,debounceTime as Cs,exhaustMap as Ds,filter as As}from"rxjs";import{isDefined as vs}from"@latticexyz/common/utils";var Ts={rpc:T.rpc,configPath:T.configPath,alwaysRunPostDeploy:T.alwaysRunPostDeploy,forgeScriptOptions:T.forgeScriptOptions,worldAddress:T.worldAddress},ks={command:"dev-contracts",describe:"Start a development server for MUD contracts",builder(e){return e.options(Ts)},async handler(e){let o=e.rpc,n=e.configPath??await bs(e.configPath),r=await ys(),t=await fs(),s=await gs(n);if(!e.rpc){console.log(I.gray("Cleaning devnode cache"));let d=ws();xs(hs.join(d,".foundry","anvil","tmp"),{recursive:!0,force:!0}),ps(["--block-time","1","--block-base-fee-per-gas","0"]),o="http://127.0.0.1:8545"}let i=new Ss(Date.now());us.watch([n,r,t],{ignoreInitial:!0}).on("all",async(d,c)=>{c.includes(n)&&(console.log(I.blue("Config changed, queuing deploy\u2026")),i.next(Date.now())),(c.includes(r)||c.includes(t))&&(c.includes(s.codegen.outputDirectory)||(console.log(I.blue("Contracts changed, queuing deploy\u2026")),i.next(Date.now())))});let a=e.worldAddress;i.pipe(Cs(200),Ds(async d=>{a&&console.log(I.blue("Rebuilding and upgrading world\u2026"));try{let c=await $({...e,configPath:n,rpc:o,rpcBatch:!1,skipBuild:!1,printConfig:!1,profile:void 0,saveDeployment:!0,deployerAddress:void 0,worldAddress:a,salt:"0x",kms:void 0});return a=c.address,d<i.value?i.next(i.value):console.log(I.gray(`
31
+ Waiting for file changes\u2026
32
+ `)),c}catch(c){console.error(I.bgRed(I.whiteBright(`
33
+ Error while attempting deploy
34
+ `))),console.error(c),console.log(I.gray(`
35
+ Waiting for file changes\u2026
36
+ `))}}),As(vs)).subscribe()}},Fo=ks;import{getCreate2Address as zo,sliceHex as Os,zeroHash as Is}from"viem";import{forge as Ps}from"@latticexyz/common/foundry";async function B(e){let o=["verify-contract",e.address,e.name,"--rpc-url",e.rpc];e.verifier&&o.push("--verifier",e.verifier),e.verifierUrl&&o.push("--verifier-url",e.verifierUrl),await Ps(o,{cwd:e.cwd})}import Ms from"p-queue";import{MUDError as Bs}from"@latticexyz/common/errors";import{getStorageAt as Ws}from"viem/actions";import{execa as we}from"execa";var Rs="0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc";async function No({client:e,rpc:o,systems:n,modules:r,worldAddress:t,deployerAddress:s,verifier:i,verifierUrl:a}){let l=s??await Y(e);if(!l)throw new Bs("No deployer address provided or found.");let d=await Ws(e,{address:t,slot:Rs}),c=d&&d!==Is,p=new Ms({concurrency:4});if(n.map(({name:u,bytecode:h})=>p.add(()=>B({name:u,rpc:o,verifier:i,verifierUrl:a,address:zo({from:l,bytecode:h,salt:S})}).catch(w=>{console.error(`Error verifying system contract ${u}:`,w)}))),i==="sourcify")if(await we("npm",["install"],{cwd:"node_modules/@latticexyz/store"}),await we("npm",["install"],{cwd:"node_modules/@latticexyz/world"}),await we("npm",["install"],{cwd:"node_modules/@latticexyz/world-modules"}),Object.entries(c?X(l):Z(l)).map(([u,{bytecode:h}])=>p.add(()=>B({cwd:"node_modules/@latticexyz/world",name:u,rpc:o,verifier:i,verifierUrl:a,address:zo({from:l,bytecode:h,salt:S})}).catch(w=>{console.error(`Error verifying world factory contract ${u}:`,w)}))),r.map(({name:u,prepareDeploy:h})=>{let{address:w}=h(l,[]);return p.add(()=>B({cwd:"node_modules/@latticexyz/world-modules",name:u,rpc:o,verifier:i,verifierUrl:a,address:w}).catch(g=>{console.error(`Error verifying module contract ${u}:`,g)}))}),c){let u=Os(d,-20);p.add(()=>B({cwd:"node_modules/@latticexyz/world",name:"WorldProxy",rpc:o,verifier:i,verifierUrl:a,address:t}).catch(h=>{console.error("Error verifying WorldProxy contract:",h)})),p.add(()=>B({cwd:"node_modules/@latticexyz/world",name:"World",rpc:o,verifier:i,verifierUrl:a,address:u}).catch(h=>{console.error("Error verifying World contract:",h)}))}else p.add(()=>B({cwd:"node_modules/@latticexyz/world",name:"World",rpc:o,verifier:i,verifierUrl:a,address:t}).catch(u=>{console.error("Error verifying World contract:",u)}));else console.log(""),console.log(`Note: MUD is currently unable to verify store, world, and world-modules contracts with ${i}. We are planning to expand support in a future version.`),console.log("")}import{loadConfig as js,resolveConfigPath as Hs}from"@latticexyz/config/node";import{resolveSystems as $s}from"@latticexyz/world/node";import{getOutDirectory as Es,getRpcUrl as Fs}from"@latticexyz/common/foundry";import{createWalletClient as zs,http as Ns}from"viem";import _o from"chalk";import _s from"node:path";var Ls={deployerAddress:{type:"string",desc:"Deploy using an existing deterministic deployer (https://github.com/Arachnid/deterministic-deployment-proxy)"},worldAddress:{type:"string",required:!0,desc:"Verify an existing World at the given address"},configPath:{type:"string",desc:"Path to the MUD config file"},profile:{type:"string",desc:"The foundry profile to use"},rpc:{type:"string",desc:"The RPC URL to use. Defaults to the RPC url from the local foundry.toml"},rpcBatch:{type:"boolean",desc:"Enable batch processing of RPC requests in viem client (defaults to batch size of 100 and wait of 1s)"},verifier:{type:"string",desc:"The verifier to use. Defaults to blockscout",default:"blockscout"},verifierUrl:{type:"string",desc:"The verification provider."}},Vs={command:"verify",describe:"Verify contracts",builder(e){return e.options(Ls)},async handler(e){let o=e.profile??process.env.FOUNDRY_PROFILE,n=await Hs(e.configPath),r=_s.dirname(n),t=await js(n),s=await Es(o),i=e.rpc??await Fs(o);console.log(_o.bgBlue(_o.whiteBright(`
37
+ Verifying MUD contracts${o?" with profile "+o:""} to RPC ${i}
38
+ `)));let a=zs({transport:Ns(i,{batch:e.rpcBatch?{batchSize:100,wait:1e3}:void 0})}),d=(await $s({rootDir:r,config:t})).map(p=>{let u=j(`${p.name}.sol`,p.name,s);return{name:p.name,bytecode:u.bytecode}}),c=await de(t,s);await No({client:a,rpc:i,systems:d,modules:c,deployerAddress:e.deployerAddress,worldAddress:e.worldAddress,verifier:e.verifier,verifierUrl:e.verifierUrl})}},Lo=Vs;var Nf=[xe,Oo,Se,Us,Ce,De,Io,Ro,jo,Eo,Fo,Ks,Lo];export{Nf as commands};
39
+ //# sourceMappingURL=commands-2DM4XJGZ.js.map