@clarigen/cli 1.0.0-next.18 → 1.0.0-next.21

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/index.mjs DELETED
@@ -1,105 +0,0 @@
1
- var lt=Object.defineProperty,pt=Object.defineProperties;var ft=Object.getOwnPropertyDescriptors;var N=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var M=(t,e,r)=>e in t?lt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,y=(t,e)=>{for(var r in e||(e={}))R.call(e,r)&&M(t,r,e[r]);if(N)for(var r of N(e))B.call(e,r)&&M(t,r,e[r]);return t},w=(t,e)=>pt(t,ft(e));var mt=(t=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(t,{get:(e,r)=>(typeof require!="undefined"?require:e)[r]}):t)(function(t){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+t+'" is not supported')});var j=(t,e)=>{var r={};for(var n in t)R.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(t!=null&&N)for(var n of N(t))e.indexOf(n)<0&&B.call(t,n)&&(r[n]=t[n]);return r};import{fileURLToPath as ut}from"url";import gt from"path";var Ct=()=>ut(import.meta.url),yt=()=>gt.dirname(Ct()),C=yt();import{run as Zr}from"@oclif/command";import{resolve as H,join as Ft,relative as Tt}from"path";import{readFile as vt,access as Pt}from"fs/promises";import{constants as Nt}from"fs";import{parse as J}from"@ltd/j-toml";import{resolve as U}from"path";import{readFile as W}from"fs/promises";import{generateWallet as dt,getStxAddressFromAccount as $t}from"micro-stacks/wallet-sdk";import{array as bt}from"toposort";import{StacksNetworkVersion as xt}from"micro-stacks/crypto";async function wt(t){let e=U(t,"settings","Devnet.toml"),r=await W(e,{encoding:"utf-8"});return J(r,1,`
2
- `,!0,{longer:!0})}async function At(t){let e=U(t,"Clarinet.toml"),r=await W(e,{encoding:"utf-8"});return J(r,1,`
3
- `,!0)}async function z(t,e){let r=await At(t),n=e.deployer.address;return ht(r.contracts).map(a=>({file:r.contracts[a].path.replace(/^contracts\//,""),address:n,name:a}))}function ht(t){let e=[],r=[];return Object.entries(t).forEach(([o,i])=>{r.push(o),i.depends_on.forEach(a=>e.push([o,a]))}),bt(r,e).reverse()}async function K(t){let e=await wt(t),r=await Promise.all(Object.entries(e.accounts).map(async([o,i])=>{let a=await dt(i.mnemonic,"password"),[s]=a.accounts,p=$t(s,xt.testnetP2PKH);return[o,w(y({},i),{address:p})]}));return Object.fromEntries(r)}var q={outputDir:"src/clarigen",clarinet:"."};function jt(t){return H(t,"clarigen.config.json")}async function Et(t){try{return await Pt(t,Nt.R_OK),!0}catch{return!1}}async function It(t){let e=jt(t);if(await Et(e)){let n=await vt(e,{encoding:"utf-8"}),o=JSON.parse(n);return y(y({},q),o)}return q}async function E(t){let e=await It(t),r=H(t,e.clarinet||"."),n=await K(r),o=await z(r,n),i=Tt(process.cwd(),Ft(e.clarinet,"contracts"));return w(y({},e),{contracts:o,contractsDir:i,accounts:n,clarinet:e.clarinet||"."})}import{isClarityAbiBuffer as kt,isClarityAbiList as St,isClarityAbiOptional as Dt,isClarityAbiPrimitive as Vt,isClarityAbiResponse as Q,isClarityAbiStringAscii as Ot,isClarityAbiStringUtf8 as Lt,isClarityAbiTuple as _t}from"micro-stacks/transactions";import{toCamelCase as S}from"@clarigen/core";import{check as Mt}from"reserved-words";var f=(t,e=!1)=>{if(Vt(t)){if(t==="uint128")return e?"number | bigint":"bigint";if(t==="int128")return e?"number | bigint":"bigint";if(t==="bool")return"boolean";if(t==="principal")return"string";if(t==="none")return"null";if(t==="trait_reference")return"string";throw new Error(`Unexpected Clarity ABI type primitive: ${JSON.stringify(t)}`)}else{if(kt(t))return"Uint8Array";if(Q(t)){let r=f(t.response.ok),n=f(t.response.error);return`Response<${r}, ${n}>`}else{if(Dt(t))return`${f(t.optional)} | null`;if(_t(t)){let r=[];return t.tuple.forEach(({name:n,type:o})=>{let i=f(o);r.push(`"${n}": ${i}`)}),`{
4
- ${r.join(`;
5
- `)}
6
- }`}else{if(St(t))return`${f(t.list.type)}[]`;if(Ot(t))return"string";if(Lt(t))return"string";if(t==="trait_reference")return"string";throw new Error(`Unexpected Clarity ABI type: ${JSON.stringify(t)}`)}}}};function D(t){let e=S(t);return`${Mt(e,6)?"_":""}${e}`}var Rt={public:"Public",read_only:"ReadOnly",private:"Private"};function X(t){let e="";return t.functions.forEach((r,n)=>{let o=`${S(r.name)}: `;if(o+=`(${r.args.map(s=>`${D(s.name)}: ${f(s.type,!0)}`).join(", ")}) => `,o+=`ContractCalls.${Rt[r.access]}<`,r.access==="public"){let{type:s}=r.outputs;if(!Q(s))throw new Error("Expected response type for public function");let p=f(s.response.ok),l=f(s.response.error);o+=`${p}, ${l}>;`}else o+=`${f(r.outputs.type)}>;`;e+=`${n===0?"":`
7
- `} ${o}`}),t.maps.forEach(r=>{let n=`${S(r.name)}: `,o=f(r.key,!0),i=`key: ${o}`,a=f(r.value);n+=`(${i}) => ContractCalls.Map<${o}, ${a}>;`,e+=`
8
- ${n}`}),e}import{hasStdErr as Bt}from"@clarigen/native-bin";import{toCamelCase as F}from"@clarigen/core";import{dirname as Jt,join as Ut}from"path";var er=async({provider:t,contractFile:e,contractAddress:r,contractName:n})=>{let o=await t.runCommand(["launch",`${r}.${n}`,e,t.dbFilePath,"--output_analysis","--costs","--assets"]);if(Bt(o.stderr))throw new Error(`Error on ${e}:
9
- ${o.stderr}
10
- `);let i=JSON.parse(o.stdout);if(i.error){let{initialization:s}=i.error;if(s!=null&&s.includes(`
11
- Near:
12
- `)){let[p,l]=s.split(`
13
- Near:
14
- `),g="",u=/start_line: (\d+),/.exec(l);throw u&&(g=u[1]),new Error(`Error on ${e}:
15
- ${p}
16
- ${g?`Near line ${g}`:""}
17
- Raw trace:
18
- ${l}
19
- `)}throw new Error(`Error on ${e}:
20
- ${JSON.stringify(i.error,null,2)}
21
- `)}return i.analysis},Y=({contractName:t,abi:e})=>{let r=F(t,!0),s=e,{clarity_version:n}=s,o=j(s,["clarity_version"]),i=JSON.stringify(o,null,2);return`import { ClarityAbi } from '@clarigen/core';
22
-
23
- // prettier-ignore
24
- export const ${r}Interface: ClarityAbi = ${i};
25
- `},Z=({contractFile:t,contractAddress:e,contractName:r})=>{let n=F(r,!0),o=F(r),i=`${n}Contract`,a=`${n}Interface`;return`import { pureProxy, Contract } from '@clarigen/core';
26
- import type { ${i} } from './types';
27
- import { ${a} } from './abi';
28
- export type { ${i} } from './types';
29
-
30
- export function ${o}Contract(contractAddress: string, contractName: string) {
31
- return pureProxy<${i}>({
32
- abi: ${a},
33
- contractAddress,
34
- contractName,
35
- });
36
- }
37
-
38
- export const ${o}Info: Contract<${i}> = {
39
- contract: ${o}Contract,
40
- address: '${e}',
41
- contractFile: '${t}',
42
- name: '${r}',
43
- abi: ${a},
44
- };
45
- `},G=(t,e)=>{let r=F(e,!0),n=X(t);return`import { Response, ContractCalls } from '@clarigen/core';
46
-
47
- // prettier-ignore
48
- export interface ${r}Contract {
49
- ${n}
50
- }
51
- `},tt=t=>{let e=["import type { ContractInstances } from '@clarigen/core';"],r=[],n=[],o="";"accounts"in t&&(o=`
52
-
53
- // prettier-ignore
54
- export const accounts = {
55
- ${Object.keys(t.accounts).map(p=>{let l=t.accounts[p];return`"${p}": {
56
- mnemonic: "${l.mnemonic}",
57
- balance: ${l.balance.toString()}n,
58
- address: "${l.address}",
59
- },`}).join(`
60
- `)}
61
- };`),t.contracts.forEach(s=>{let p=s.name,l=F(p),g=`${l}Info`,x=`${F(p,!0)}Contract`,u=Jt(s.file),$=`'./${Ut(u||".",p)}'`,c=`import { ${g} } from ${$};`;e.push(c);let m=`export type { ${x} } from ${$};`;r.push(m);let h=`${l}: ${g},`;n.push(h)});let i=`
62
- export type Contracts = ContractInstances<typeof contracts>;
63
- `;return`${e.join(`
64
- `)}
65
- ${r.join(`
66
- `)}
67
- ${i}
68
- export const contracts = {
69
- ${n.join(`
70
- `)}
71
- };${o}
72
- `};import{Command as xe,flags as st}from"@oclif/command";import{createClarityBin as ge,deployContract as Ce}from"@clarigen/native-bin";import{resolve as b,relative as ye,dirname as de}from"path";import{mkdir as $e}from"fs/promises";import{createContractDocInfo as Wt,generateMarkdown as zt}from"@clarigen/claridocs";import{mkdir as Kt,readFile as qt,writeFile as et}from"fs/promises";import{relative as Ht,resolve as V}from"path";async function rt({contractFile:t,contractName:e,docsPath:r,abi:n,dirName:o}){let i=await qt(t,{encoding:"utf-8"}),a=Wt({contractSrc:i,abi:n}),s=V(process.cwd(),r,o||"."),p=V(s,`${e}.md`),l=zt({contract:a,contractFile:Ht(s,t),contractName:e,abi:n});await Kt(s,{recursive:!0}),await et(p,l)}async function nt(t){if(!t.docs)return;let r=`# Contracts
73
-
74
- ${t.contracts.map(o=>{let i=o.file.replace(".clar",".md");return`- [\`${o.name}\`](${i})`}).join(`
75
- `)}
76
- `,n=V(process.cwd(),t.docs,"README.md");await et(n,r)}import{toCamelCase as v}from"@clarigen/core";import{relative as Qt,resolve as Xt}from"path";import{readFile as Yt}from"fs/promises";import{inspect as O}from"util";function Zt(t){let{abi:e}=t,r=[],$=e,{functions:n,variables:o,maps:i}=$,a=j($,["functions","variables","maps"]);n.forEach(c=>{let m=`${v(c.name)}: `,P=`[${c.args.map(_=>`${D(_.name)}: ${f(_.type,!0)}`).join(", ")}]`;m+=JSON.stringify(c);let ct=f(c.outputs.type);m+=` as TypedAbiFunction<${P}, ${ct}>`,r.push(m)});let s=t.variables.map(c=>{let m=`${v(c.name)}: `,h=f(c.type);return m+=`${O(c,!1,null,!1)} as TypedAbiVariable<${h}>`,m}),l=t.variables.filter(c=>c.access==="constant").map(c=>`"${v(c.name)}": ${te(c.defaultValue)}`),g=i.map(c=>{let m=`${v(c.name)}: `,h=f(c.key),P=f(c.value);return m+=JSON.stringify(c),m+=` as TypedAbiMap<${h}, ${P}>`,m}),x=JSON.stringify(a),u=Qt(process.cwd(),t.contractFile);return`{
77
- ${I("functions",r)}
78
- ${I("variables",s)}
79
- ${I("maps",g)}
80
- ${I("constants",l)}
81
- ${x.slice(1,-1)},
82
- contractName: '${t.contractName}',
83
- contractFile: '${u}',
84
- }`}async function ot(t,e){let r=e.map(a=>{let s=Zt(a);return`${v(a.contractName)}: ${s}`}),n=await ee(),o=Gt(t);return`
85
- ${n}
86
-
87
- export const contracts: Record<string, TypedAbi> = {
88
- ${r.join(`,
89
- `)}
90
- } as const;
91
-
92
- ${o}
93
- `}function Gt(t){let e="";return"accounts"in t&&(e=`export const accounts = {
94
- ${Object.keys(t.accounts).map(n=>{let o=t.accounts[n];return`"${n}": {
95
- mnemonic: "${o.mnemonic}",
96
- balance: ${o.balance.toString()}n,
97
- address: "${o.address}",
98
- },`}).join(`
99
- `)}
100
- } as const;`),e}Uint8Array.prototype[O.custom]=function(){return`Uint8Array.from([${this.join(",")}])`};function te(t){return O(t,!1,null,!1)}function I(t,e){return`"${t}": {
101
- ${e.join(`,
102
- `)}
103
- },`}async function ee(){let t=Xt(C,"../../../core/src/abi-types.ts");return await Yt(t,{encoding:"utf-8"})}import{writeFile as re}from"fs/promises";import{basename as ne}from"path";import{resolveConfig as oe,format as ie}from"prettier";var se={printWidth:80,semi:!0,singleQuote:!0,trailingComma:"es5"};async function ae(){try{let t=await oe(process.cwd());if(t)return t}catch{}return se}async function ce(t,e){try{let r=ne(e),n=await ae();return ie(t,w(y({},n),{filepath:r}))}catch{}return t}async function T(t,e){let r=await ce(e,t);await re(t,r)}import{cvToValue as le}from"@clarigen/core";import{evalRaw as pe}from"@clarigen/native-bin";import{hexToCV as fe}from"micro-stacks/clarity";async function it({abi:t,contractIdentifier:e,provider:r}){let n=t.variables.map(i=>me({variable:i,provider:r,contractIdentifier:e}));return await Promise.all(n)}async function me({contractIdentifier:t,variable:e,provider:r}){let n=ue(e),o=await pe({contractAddress:t,code:n,provider:r}),i=fe(o.output_serialized),a=le(i,!0);return w(y({},e),{defaultValue:a})}function ue(t){let{access:e}=t;return e==="variable"?`(var-get ${t.name})`:t.name}var be=async({contractFile:t,outputFolder:e,provider:r,contractAddress:n,dirName:o,contractName:i,docsPath:a})=>{let s=b(process.cwd(),t),p=`${n}.${i}`,l=await Ce({contractIdentifier:p,contractFilePath:s,provider:r}),g=await it({abi:l,contractIdentifier:p,provider:r}),x=G(l,i),u=Z({contractFile:ye(process.cwd(),s),contractAddress:n,contractName:i}),$=Y({contractName:i,abi:l});typeof a<"u"&&await rt({contractFile:s,contractName:i,abi:l,docsPath:a,dirName:o});let c=b(e,o||".",i);return await $e(c,{recursive:!0}),await T(b(c,"abi.ts"),$),await T(b(c,"index.ts"),u),await T(b(c,"types.ts"),x),{abi:l,contractFile:s,contractName:i,dirName:o,contractAddress:n,variables:g}},k=async t=>{let e=await E(t),{contractsDir:r,outputDir:n,contracts:o}=e,i=b(t,n),a=await ge(),s=[];for(let u of o){let $=b(t,r,u.file),c=de(u.file),m=await be({contractFile:$,outputFolder:i,provider:a,contractAddress:u.address,dirName:c,contractName:u.name,docsPath:e.docs});s.push(m)}let p=tt(e);await nt(e);let l=b(i,"index.ts");await T(l,p);let g=await ot(e,s),x=b(i,"single.ts");await T(x,g)};import{watch as we}from"chokidar";import{basename as Ae}from"path";import{red as he,green as at}from"chalk";var Fe=mt("ora"),L=class extends xe{async run(){let{flags:e}=this.parse(L),r=process.cwd();if(e.watch){let n=Fe("Generating files").start(),{contractsDir:o}=await E(r),i=we([o],{cwd:r});try{await k(r),n.succeed("Finished generating files. Watching for changes.")}catch(a){n.fail(`Error generating files.
104
- ${a.message}`)}i.on("change",async a=>{let s=Ae(a);n.clear(),n.start(`Change detected for ${at(s)}, generating.`);try{await k(r),n.succeed(`Finished generating files for ${at(s)}. Watching for changes.`)}catch(p){let l=p.message;n.fail(`Error after saving ${he(s)}.
105
- ${l}`)}}),process.on("SIGINT",async()=>{await i.close(),process.exit()})}else await k(r)}},A=L;A.description="Generate project files",A.strict=!0,A.hidden=!1,A.flags={help:st.help({char:"h"}),watch:st.boolean({char:"w",description:"Watch for changes to your contracts"})},A.args=[];export{A as Generate,Et as configFileExists,jt as configFilePath,q as defaultConfigFile,Z as generateIndexFile,er as generateInterface,Y as generateInterfaceFile,tt as generateProjectIndexFile,G as generateTypesFile,D as getArgName,It as getConfigFile,E as getProjectConfig,f as jsTypeFromAbiType,X as makePureTypes,Zr as run};