@secondlayer/cli 0.3.5 → 0.3.6
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/cli.js +294 -164
- package/dist/cli.js.map +7 -5
- package/dist/index.js +287 -273
- package/dist/index.js.map +9 -8
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -15617,6 +15617,159 @@ var init_api = __esm(() => {
|
|
|
15617
15617
|
};
|
|
15618
15618
|
});
|
|
15619
15619
|
|
|
15620
|
+
// src/utils/abi-compat.ts
|
|
15621
|
+
function normalizeAccess(access) {
|
|
15622
|
+
if (access === "read_only")
|
|
15623
|
+
return "read-only";
|
|
15624
|
+
return access;
|
|
15625
|
+
}
|
|
15626
|
+
function normalizeType(type) {
|
|
15627
|
+
if (typeof type === "string") {
|
|
15628
|
+
switch (type) {
|
|
15629
|
+
case "uint128":
|
|
15630
|
+
case "int128":
|
|
15631
|
+
case "bool":
|
|
15632
|
+
case "principal":
|
|
15633
|
+
case "trait_reference":
|
|
15634
|
+
return type;
|
|
15635
|
+
default:
|
|
15636
|
+
return type;
|
|
15637
|
+
}
|
|
15638
|
+
}
|
|
15639
|
+
if (typeof type !== "object" || type === null) {
|
|
15640
|
+
return "uint128";
|
|
15641
|
+
}
|
|
15642
|
+
const typeObj = type;
|
|
15643
|
+
if ("buffer" in typeObj) {
|
|
15644
|
+
const buffer = typeObj.buffer;
|
|
15645
|
+
return {
|
|
15646
|
+
buff: {
|
|
15647
|
+
length: buffer?.length ?? 32
|
|
15648
|
+
}
|
|
15649
|
+
};
|
|
15650
|
+
}
|
|
15651
|
+
if ("buff" in typeObj) {
|
|
15652
|
+
const buff = typeObj.buff;
|
|
15653
|
+
return {
|
|
15654
|
+
buff: {
|
|
15655
|
+
length: buff?.length ?? 32
|
|
15656
|
+
}
|
|
15657
|
+
};
|
|
15658
|
+
}
|
|
15659
|
+
if ("string-ascii" in typeObj) {
|
|
15660
|
+
const strAscii = typeObj["string-ascii"];
|
|
15661
|
+
return {
|
|
15662
|
+
"string-ascii": {
|
|
15663
|
+
length: strAscii?.length ?? 256
|
|
15664
|
+
}
|
|
15665
|
+
};
|
|
15666
|
+
}
|
|
15667
|
+
if ("string-utf8" in typeObj) {
|
|
15668
|
+
const strUtf8 = typeObj["string-utf8"];
|
|
15669
|
+
return {
|
|
15670
|
+
"string-utf8": {
|
|
15671
|
+
length: strUtf8?.length ?? 256
|
|
15672
|
+
}
|
|
15673
|
+
};
|
|
15674
|
+
}
|
|
15675
|
+
if ("response" in typeObj) {
|
|
15676
|
+
const response2 = typeObj.response;
|
|
15677
|
+
return {
|
|
15678
|
+
response: {
|
|
15679
|
+
ok: normalizeType(response2?.ok ?? "bool"),
|
|
15680
|
+
error: normalizeType(response2?.error ?? "uint128")
|
|
15681
|
+
}
|
|
15682
|
+
};
|
|
15683
|
+
}
|
|
15684
|
+
if ("optional" in typeObj) {
|
|
15685
|
+
return {
|
|
15686
|
+
optional: normalizeType(typeObj.optional)
|
|
15687
|
+
};
|
|
15688
|
+
}
|
|
15689
|
+
if ("list" in typeObj) {
|
|
15690
|
+
const list = typeObj.list;
|
|
15691
|
+
return {
|
|
15692
|
+
list: {
|
|
15693
|
+
type: normalizeType(list?.type ?? "uint128"),
|
|
15694
|
+
length: list?.length ?? 100
|
|
15695
|
+
}
|
|
15696
|
+
};
|
|
15697
|
+
}
|
|
15698
|
+
if ("tuple" in typeObj) {
|
|
15699
|
+
const tuple = typeObj.tuple;
|
|
15700
|
+
return {
|
|
15701
|
+
tuple: tuple.map((field) => ({
|
|
15702
|
+
name: field.name,
|
|
15703
|
+
type: normalizeType(field.type)
|
|
15704
|
+
}))
|
|
15705
|
+
};
|
|
15706
|
+
}
|
|
15707
|
+
return "uint128";
|
|
15708
|
+
}
|
|
15709
|
+
function normalizeFunction(func) {
|
|
15710
|
+
const access = normalizeAccess(func.access);
|
|
15711
|
+
const args = func.args ?? [];
|
|
15712
|
+
const outputs = func.outputs;
|
|
15713
|
+
return {
|
|
15714
|
+
name: func.name,
|
|
15715
|
+
access,
|
|
15716
|
+
args: args.map((arg) => ({
|
|
15717
|
+
name: arg.name,
|
|
15718
|
+
type: normalizeType(arg.type)
|
|
15719
|
+
})),
|
|
15720
|
+
outputs: normalizeType(typeof outputs === "object" && outputs !== null && "type" in outputs ? outputs.type : outputs)
|
|
15721
|
+
};
|
|
15722
|
+
}
|
|
15723
|
+
function normalizeMap(map) {
|
|
15724
|
+
return {
|
|
15725
|
+
name: map.name,
|
|
15726
|
+
key: normalizeType(map.key),
|
|
15727
|
+
value: normalizeType(map.value)
|
|
15728
|
+
};
|
|
15729
|
+
}
|
|
15730
|
+
function normalizeVariable(variable) {
|
|
15731
|
+
return {
|
|
15732
|
+
name: variable.name,
|
|
15733
|
+
type: normalizeType(variable.type),
|
|
15734
|
+
access: variable.access
|
|
15735
|
+
};
|
|
15736
|
+
}
|
|
15737
|
+
function normalizeAbi(abi) {
|
|
15738
|
+
if (typeof abi !== "object" || abi === null) {
|
|
15739
|
+
return { functions: [] };
|
|
15740
|
+
}
|
|
15741
|
+
const abiObj = abi;
|
|
15742
|
+
const functions = [];
|
|
15743
|
+
const maps = [];
|
|
15744
|
+
const variables = [];
|
|
15745
|
+
if (Array.isArray(abiObj.functions)) {
|
|
15746
|
+
for (const func of abiObj.functions) {
|
|
15747
|
+
if (typeof func === "object" && func !== null) {
|
|
15748
|
+
functions.push(normalizeFunction(func));
|
|
15749
|
+
}
|
|
15750
|
+
}
|
|
15751
|
+
}
|
|
15752
|
+
if (Array.isArray(abiObj.maps)) {
|
|
15753
|
+
for (const map of abiObj.maps) {
|
|
15754
|
+
if (typeof map === "object" && map !== null) {
|
|
15755
|
+
maps.push(normalizeMap(map));
|
|
15756
|
+
}
|
|
15757
|
+
}
|
|
15758
|
+
}
|
|
15759
|
+
if (Array.isArray(abiObj.variables)) {
|
|
15760
|
+
for (const variable of abiObj.variables) {
|
|
15761
|
+
if (typeof variable === "object" && variable !== null) {
|
|
15762
|
+
variables.push(normalizeVariable(variable));
|
|
15763
|
+
}
|
|
15764
|
+
}
|
|
15765
|
+
}
|
|
15766
|
+
return {
|
|
15767
|
+
functions,
|
|
15768
|
+
maps: maps.length > 0 ? maps : undefined,
|
|
15769
|
+
variables: variables.length > 0 ? variables : undefined
|
|
15770
|
+
};
|
|
15771
|
+
}
|
|
15772
|
+
|
|
15620
15773
|
// src/parsers/clarity.ts
|
|
15621
15774
|
import { promises as fs3 } from "fs";
|
|
15622
15775
|
async function parseClarityFile(filePath) {
|
|
@@ -15703,116 +15856,89 @@ function inferReturnType(body) {
|
|
|
15703
15856
|
}
|
|
15704
15857
|
function parseApiResponse(apiResponse) {
|
|
15705
15858
|
try {
|
|
15706
|
-
|
|
15707
|
-
const maps = [];
|
|
15708
|
-
const variables = [];
|
|
15709
|
-
if (apiResponse.functions) {
|
|
15710
|
-
for (const func of apiResponse.functions) {
|
|
15711
|
-
const access = func.access === "read_only" ? "read-only" : func.access;
|
|
15712
|
-
functions.push({
|
|
15713
|
-
name: func.name,
|
|
15714
|
-
access,
|
|
15715
|
-
args: func.args.map((arg) => ({
|
|
15716
|
-
name: arg.name,
|
|
15717
|
-
type: convertApiType(arg.type)
|
|
15718
|
-
})),
|
|
15719
|
-
outputs: convertApiType(func.outputs.type)
|
|
15720
|
-
});
|
|
15721
|
-
}
|
|
15722
|
-
}
|
|
15723
|
-
if (apiResponse.maps) {
|
|
15724
|
-
for (const map of apiResponse.maps) {
|
|
15725
|
-
maps.push({
|
|
15726
|
-
name: map.name,
|
|
15727
|
-
key: convertApiType(map.key),
|
|
15728
|
-
value: convertApiType(map.value)
|
|
15729
|
-
});
|
|
15730
|
-
}
|
|
15731
|
-
}
|
|
15732
|
-
if (apiResponse.variables) {
|
|
15733
|
-
for (const variable of apiResponse.variables) {
|
|
15734
|
-
variables.push({
|
|
15735
|
-
name: variable.name,
|
|
15736
|
-
type: convertApiType(variable.type),
|
|
15737
|
-
access: variable.access
|
|
15738
|
-
});
|
|
15739
|
-
}
|
|
15740
|
-
}
|
|
15741
|
-
return {
|
|
15742
|
-
functions,
|
|
15743
|
-
maps: maps.length > 0 ? maps : undefined,
|
|
15744
|
-
variables: variables.length > 0 ? variables : undefined
|
|
15745
|
-
};
|
|
15859
|
+
return normalizeAbi(apiResponse);
|
|
15746
15860
|
} catch (error) {
|
|
15747
15861
|
throw new Error(`Failed to parse API response: ${error}`);
|
|
15748
15862
|
}
|
|
15749
15863
|
}
|
|
15750
|
-
|
|
15751
|
-
|
|
15752
|
-
|
|
15753
|
-
|
|
15864
|
+
var init_clarity = () => {};
|
|
15865
|
+
|
|
15866
|
+
// src/utils/type-mapping.ts
|
|
15867
|
+
import {
|
|
15868
|
+
toCamelCase,
|
|
15869
|
+
isClarityList,
|
|
15870
|
+
isClarityTuple,
|
|
15871
|
+
isClarityOptional,
|
|
15872
|
+
isClarityResponse,
|
|
15873
|
+
isClarityBuffer,
|
|
15874
|
+
isClarityStringAscii,
|
|
15875
|
+
isClarityStringUtf8
|
|
15876
|
+
} from "@secondlayer/clarity-types";
|
|
15877
|
+
function clarityTypeToTS(type) {
|
|
15878
|
+
if (typeof type === "string") {
|
|
15879
|
+
switch (type) {
|
|
15880
|
+
case "uint128":
|
|
15881
|
+
case "int128":
|
|
15882
|
+
return "bigint";
|
|
15883
|
+
case "bool":
|
|
15884
|
+
return "boolean";
|
|
15885
|
+
case "principal":
|
|
15886
|
+
case "trait_reference":
|
|
15887
|
+
return "string";
|
|
15888
|
+
default:
|
|
15889
|
+
if (type.includes("string") || type.includes("ascii") || type.includes("utf8")) {
|
|
15890
|
+
return "string";
|
|
15891
|
+
}
|
|
15892
|
+
if (type.includes("buff")) {
|
|
15893
|
+
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
15894
|
+
}
|
|
15895
|
+
if (type.includes("uint") || type.includes("int")) {
|
|
15896
|
+
return "bigint";
|
|
15897
|
+
}
|
|
15898
|
+
return "any";
|
|
15754
15899
|
}
|
|
15755
|
-
return parseType(apiType) || "uint128";
|
|
15756
15900
|
}
|
|
15757
|
-
if (
|
|
15758
|
-
return {
|
|
15759
|
-
response: {
|
|
15760
|
-
ok: convertApiType(apiType.response.ok),
|
|
15761
|
-
error: convertApiType(apiType.response.error)
|
|
15762
|
-
}
|
|
15763
|
-
};
|
|
15764
|
-
}
|
|
15765
|
-
if (apiType.optional) {
|
|
15766
|
-
return {
|
|
15767
|
-
optional: convertApiType(apiType.optional)
|
|
15768
|
-
};
|
|
15769
|
-
}
|
|
15770
|
-
if (apiType.list) {
|
|
15771
|
-
return {
|
|
15772
|
-
list: {
|
|
15773
|
-
type: convertApiType(apiType.list.type),
|
|
15774
|
-
length: apiType.list.length || 100
|
|
15775
|
-
}
|
|
15776
|
-
};
|
|
15901
|
+
if (isClarityBuffer(type)) {
|
|
15902
|
+
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
15777
15903
|
}
|
|
15778
|
-
if (
|
|
15779
|
-
return
|
|
15780
|
-
tuple: apiType.tuple.map((field) => ({
|
|
15781
|
-
name: field.name,
|
|
15782
|
-
type: convertApiType(field.type)
|
|
15783
|
-
}))
|
|
15784
|
-
};
|
|
15904
|
+
if (isClarityStringAscii(type) || isClarityStringUtf8(type)) {
|
|
15905
|
+
return "string";
|
|
15785
15906
|
}
|
|
15786
|
-
if (
|
|
15787
|
-
|
|
15788
|
-
|
|
15789
|
-
|
|
15790
|
-
|
|
15791
|
-
}
|
|
15907
|
+
if (isClarityOptional(type)) {
|
|
15908
|
+
const innerType = clarityTypeToTS(type.optional);
|
|
15909
|
+
if (innerType.includes(" | ") && !innerType.startsWith("(")) {
|
|
15910
|
+
return `(${innerType}) | null`;
|
|
15911
|
+
}
|
|
15912
|
+
return `${innerType} | null`;
|
|
15792
15913
|
}
|
|
15793
|
-
if (
|
|
15794
|
-
|
|
15795
|
-
|
|
15796
|
-
|
|
15797
|
-
|
|
15798
|
-
}
|
|
15914
|
+
if (isClarityList(type)) {
|
|
15915
|
+
const innerType = clarityTypeToTS(type.list.type);
|
|
15916
|
+
if (innerType.includes(" | ") && !innerType.startsWith("(")) {
|
|
15917
|
+
return `(${innerType})[]`;
|
|
15918
|
+
}
|
|
15919
|
+
return `${innerType}[]`;
|
|
15799
15920
|
}
|
|
15800
|
-
if (
|
|
15801
|
-
|
|
15802
|
-
|
|
15803
|
-
length: apiType["string-utf8"].length || 256
|
|
15804
|
-
}
|
|
15805
|
-
};
|
|
15921
|
+
if (isClarityTuple(type)) {
|
|
15922
|
+
const fields = type.tuple.map((field) => `${toCamelCase(field.name)}: ${clarityTypeToTS(field.type)}`).join("; ");
|
|
15923
|
+
return `{ ${fields} }`;
|
|
15806
15924
|
}
|
|
15807
|
-
if (
|
|
15808
|
-
|
|
15925
|
+
if (isClarityResponse(type)) {
|
|
15926
|
+
const okType = clarityTypeToTS(type.response.ok);
|
|
15927
|
+
const errType = clarityTypeToTS(type.response.error);
|
|
15928
|
+
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
15809
15929
|
}
|
|
15810
|
-
return "
|
|
15930
|
+
return "any";
|
|
15811
15931
|
}
|
|
15812
|
-
|
|
15932
|
+
function getTypeForArg(arg) {
|
|
15933
|
+
return clarityTypeToTS(arg.type);
|
|
15934
|
+
}
|
|
15935
|
+
var init_type_mapping = () => {};
|
|
15813
15936
|
|
|
15814
15937
|
// src/generators/contract.ts
|
|
15815
15938
|
import { format } from "prettier";
|
|
15939
|
+
import {
|
|
15940
|
+
toCamelCase as toCamelCase2
|
|
15941
|
+
} from "@secondlayer/clarity-types";
|
|
15816
15942
|
function generateNetworkUtils() {
|
|
15817
15943
|
return `/**
|
|
15818
15944
|
* API URLs for different networks
|
|
@@ -15845,7 +15971,8 @@ function getApiUrl(
|
|
|
15845
15971
|
}`;
|
|
15846
15972
|
}
|
|
15847
15973
|
async function generateContractInterface(contracts) {
|
|
15848
|
-
const imports = `import { Cl, validateStacksAddress } from '@stacks/transactions'
|
|
15974
|
+
const imports = `import { Cl, validateStacksAddress } from '@stacks/transactions'
|
|
15975
|
+
import { jsToClarity, ClarityConversionError, CONTRACT_NAME_REGEX } from '@secondlayer/clarity-types'`;
|
|
15849
15976
|
const header = `/**
|
|
15850
15977
|
* Generated by @secondlayer/cli
|
|
15851
15978
|
* DO NOT EDIT MANUALLY
|
|
@@ -15900,7 +16027,7 @@ function generateAbiConstant(name, abi) {
|
|
|
15900
16027
|
return `export const ${name}Abi = ${abiJson} as const`;
|
|
15901
16028
|
}
|
|
15902
16029
|
function generateMethod(func, address, contractName) {
|
|
15903
|
-
const methodName =
|
|
16030
|
+
const methodName = toCamelCase2(func.name);
|
|
15904
16031
|
if (func.args.length === 0) {
|
|
15905
16032
|
return `${methodName}() {
|
|
15906
16033
|
return {
|
|
@@ -15913,7 +16040,7 @@ function generateMethod(func, address, contractName) {
|
|
|
15913
16040
|
}
|
|
15914
16041
|
if (func.args.length === 1) {
|
|
15915
16042
|
const originalArgName = func.args[0].name;
|
|
15916
|
-
const argName =
|
|
16043
|
+
const argName = toCamelCase2(originalArgName);
|
|
15917
16044
|
const argType = getTypeForArg(func.args[0]);
|
|
15918
16045
|
const clarityConversion = generateClarityConversion(argName, func.args[0]);
|
|
15919
16046
|
return `${methodName}(...args: [{ ${argName}: ${argType} }] | [${argType}]) {
|
|
@@ -15929,17 +16056,17 @@ function generateMethod(func, address, contractName) {
|
|
|
15929
16056
|
}
|
|
15930
16057
|
}`;
|
|
15931
16058
|
}
|
|
15932
|
-
const argsList = func.args.map((arg) =>
|
|
16059
|
+
const argsList = func.args.map((arg) => toCamelCase2(arg.name)).join(", ");
|
|
15933
16060
|
const argsTypes = func.args.map((arg) => {
|
|
15934
|
-
const camelName =
|
|
16061
|
+
const camelName = toCamelCase2(arg.name);
|
|
15935
16062
|
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
15936
16063
|
}).join("; ");
|
|
15937
16064
|
const argsArray = func.args.map((arg) => {
|
|
15938
|
-
const argName =
|
|
16065
|
+
const argName = toCamelCase2(arg.name);
|
|
15939
16066
|
return generateClarityConversion(argName, arg);
|
|
15940
16067
|
}).join(", ");
|
|
15941
16068
|
const objectAccess = func.args.map((arg) => {
|
|
15942
|
-
const camelName =
|
|
16069
|
+
const camelName = toCamelCase2(arg.name);
|
|
15943
16070
|
return `args[0].${camelName}`;
|
|
15944
16071
|
}).join(", ");
|
|
15945
16072
|
const positionTypes = func.args.map((arg) => getTypeForArg(arg)).join(", ");
|
|
@@ -15956,50 +16083,6 @@ function generateMethod(func, address, contractName) {
|
|
|
15956
16083
|
}
|
|
15957
16084
|
}`;
|
|
15958
16085
|
}
|
|
15959
|
-
function getTypeForArg(arg) {
|
|
15960
|
-
const type = arg.type;
|
|
15961
|
-
if (typeof type === "string") {
|
|
15962
|
-
switch (type) {
|
|
15963
|
-
case "uint128":
|
|
15964
|
-
case "int128":
|
|
15965
|
-
return "bigint";
|
|
15966
|
-
case "bool":
|
|
15967
|
-
return "boolean";
|
|
15968
|
-
case "principal":
|
|
15969
|
-
case "trait_reference":
|
|
15970
|
-
return "string";
|
|
15971
|
-
default:
|
|
15972
|
-
return "any";
|
|
15973
|
-
}
|
|
15974
|
-
}
|
|
15975
|
-
if (type["string-ascii"] || type["string-utf8"]) {
|
|
15976
|
-
return "string";
|
|
15977
|
-
}
|
|
15978
|
-
if (type.buff) {
|
|
15979
|
-
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
15980
|
-
}
|
|
15981
|
-
if (type.optional) {
|
|
15982
|
-
const innerType = getTypeForArg({ type: type.optional });
|
|
15983
|
-
return `${innerType} | null`;
|
|
15984
|
-
}
|
|
15985
|
-
if (type.list) {
|
|
15986
|
-
const innerType = getTypeForArg({ type: type.list.type });
|
|
15987
|
-
return `${innerType}[]`;
|
|
15988
|
-
}
|
|
15989
|
-
if (type.tuple) {
|
|
15990
|
-
const fields = type.tuple.map((field) => `${toCamelCase(field.name)}: ${getTypeForArg({ type: field.type })}`).join("; ");
|
|
15991
|
-
return `{ ${fields} }`;
|
|
15992
|
-
}
|
|
15993
|
-
if (type.response) {
|
|
15994
|
-
const okType = getTypeForArg({ type: type.response.ok });
|
|
15995
|
-
const errType = getTypeForArg({ type: type.response.error });
|
|
15996
|
-
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
15997
|
-
}
|
|
15998
|
-
return "any";
|
|
15999
|
-
}
|
|
16000
|
-
function toCamelCase(str) {
|
|
16001
|
-
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/-([A-Z])/g, (_, letter) => letter).replace(/-(\d)/g, (_, digit) => digit).replace(/-/g, "");
|
|
16002
|
-
}
|
|
16003
16086
|
function generateClarityConversion(argName, argType) {
|
|
16004
16087
|
const type = argType.type;
|
|
16005
16088
|
if (typeof type === "string") {
|
|
@@ -16013,11 +16096,14 @@ function generateClarityConversion(argName, argType) {
|
|
|
16013
16096
|
case "principal":
|
|
16014
16097
|
case "trait_reference":
|
|
16015
16098
|
return `(() => {
|
|
16016
|
-
const [address, contractName] = ${argName}.split(".") as [string, string];
|
|
16099
|
+
const [address, contractName] = ${argName}.split(".") as [string, string | undefined];
|
|
16017
16100
|
if (!validateStacksAddress(address)) {
|
|
16018
16101
|
throw new Error("Invalid Stacks address format");
|
|
16019
16102
|
}
|
|
16020
|
-
if (
|
|
16103
|
+
if (contractName !== undefined) {
|
|
16104
|
+
if (!CONTRACT_NAME_REGEX.test(contractName)) {
|
|
16105
|
+
throw new Error("Invalid contract name format: must start with letter and contain only letters, numbers, and hyphens");
|
|
16106
|
+
}
|
|
16021
16107
|
return Cl.contractPrincipal(address, contractName);
|
|
16022
16108
|
}
|
|
16023
16109
|
return Cl.standardPrincipal(${argName});
|
|
@@ -16079,24 +16165,66 @@ function generateClarityConversion(argName, argType) {
|
|
|
16079
16165
|
const innerConversion = generateClarityConversion("item", {
|
|
16080
16166
|
type: type.list.type
|
|
16081
16167
|
});
|
|
16082
|
-
|
|
16168
|
+
const maxLength = type.list.length || 100;
|
|
16169
|
+
return `(() => {
|
|
16170
|
+
const listValue = ${argName};
|
|
16171
|
+
if (listValue.length > ${maxLength}) {
|
|
16172
|
+
throw new ClarityConversionError(
|
|
16173
|
+
\`List length \${listValue.length} exceeds max ${maxLength}\`,
|
|
16174
|
+
${JSON.stringify(type)},
|
|
16175
|
+
listValue
|
|
16176
|
+
);
|
|
16177
|
+
}
|
|
16178
|
+
return Cl.list(listValue.map(item => ${innerConversion}));
|
|
16179
|
+
})()`;
|
|
16083
16180
|
}
|
|
16084
16181
|
if (type.tuple) {
|
|
16182
|
+
const requiredFields = type.tuple.map((f) => f.name);
|
|
16183
|
+
const fieldNames = JSON.stringify(requiredFields);
|
|
16085
16184
|
const fields = type.tuple.map((field) => {
|
|
16086
|
-
const camelFieldName =
|
|
16087
|
-
const fieldConversion = generateClarityConversion(
|
|
16185
|
+
const camelFieldName = toCamelCase2(field.name);
|
|
16186
|
+
const fieldConversion = generateClarityConversion(`tupleValue.${camelFieldName}`, { type: field.type });
|
|
16088
16187
|
return `"${field.name}": ${fieldConversion}`;
|
|
16089
16188
|
}).join(", ");
|
|
16090
|
-
return `
|
|
16189
|
+
return `(() => {
|
|
16190
|
+
const tupleValue = ${argName};
|
|
16191
|
+
const requiredFields = ${fieldNames};
|
|
16192
|
+
for (const fieldName of requiredFields) {
|
|
16193
|
+
const camelName = fieldName.replace(/-([a-z])/g, (_: string, l: string) => l.toUpperCase());
|
|
16194
|
+
if (!(fieldName in tupleValue) && !(camelName in tupleValue)) {
|
|
16195
|
+
throw new ClarityConversionError(
|
|
16196
|
+
\`Missing tuple field: \${fieldName}\`,
|
|
16197
|
+
${JSON.stringify(type)},
|
|
16198
|
+
tupleValue
|
|
16199
|
+
);
|
|
16200
|
+
}
|
|
16201
|
+
}
|
|
16202
|
+
return Cl.tuple({ ${fields} });
|
|
16203
|
+
})()`;
|
|
16091
16204
|
}
|
|
16092
16205
|
if (type.response) {
|
|
16093
|
-
const okConversion = generateClarityConversion(
|
|
16206
|
+
const okConversion = generateClarityConversion(`responseValue.ok`, {
|
|
16094
16207
|
type: type.response.ok
|
|
16095
16208
|
});
|
|
16096
|
-
const errConversion = generateClarityConversion(
|
|
16209
|
+
const errConversion = generateClarityConversion(`responseValue.err`, {
|
|
16097
16210
|
type: type.response.error
|
|
16098
16211
|
});
|
|
16099
|
-
return `
|
|
16212
|
+
return `(() => {
|
|
16213
|
+
const responseValue = ${argName};
|
|
16214
|
+
const hasOk = 'ok' in responseValue;
|
|
16215
|
+
const hasErr = 'err' in responseValue;
|
|
16216
|
+
if (hasOk && !hasErr) {
|
|
16217
|
+
return Cl.ok(${okConversion});
|
|
16218
|
+
}
|
|
16219
|
+
if (hasErr && !hasOk) {
|
|
16220
|
+
return Cl.error(${errConversion});
|
|
16221
|
+
}
|
|
16222
|
+
throw new ClarityConversionError(
|
|
16223
|
+
"Response must have exactly 'ok' or 'err' property",
|
|
16224
|
+
${JSON.stringify(type)},
|
|
16225
|
+
responseValue
|
|
16226
|
+
);
|
|
16227
|
+
})()`;
|
|
16100
16228
|
}
|
|
16101
16229
|
return `${argName}`;
|
|
16102
16230
|
}
|
|
@@ -16105,7 +16233,7 @@ function generateMapsObject(maps, address, contractName) {
|
|
|
16105
16233
|
return "";
|
|
16106
16234
|
}
|
|
16107
16235
|
const mapMethods = maps.map((map) => {
|
|
16108
|
-
const methodName =
|
|
16236
|
+
const methodName = toCamelCase2(map.name);
|
|
16109
16237
|
const keyType = getTypeForArg({ type: map.key });
|
|
16110
16238
|
const valueType = getTypeForArg({ type: map.value });
|
|
16111
16239
|
const keyConversion = generateMapKeyConversion(map.key);
|
|
@@ -16159,7 +16287,7 @@ function generateVarsObject(variables, address, contractName) {
|
|
|
16159
16287
|
return "";
|
|
16160
16288
|
}
|
|
16161
16289
|
const varMethods = dataVars.map((variable) => {
|
|
16162
|
-
const methodName =
|
|
16290
|
+
const methodName = toCamelCase2(variable.name);
|
|
16163
16291
|
const valueType = getTypeForArg({ type: variable.type });
|
|
16164
16292
|
return `${methodName}: {
|
|
16165
16293
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -16197,7 +16325,7 @@ function generateConstantsObject(variables, address, contractName) {
|
|
|
16197
16325
|
return "";
|
|
16198
16326
|
}
|
|
16199
16327
|
const constMethods = constants.map((constant) => {
|
|
16200
|
-
const methodName =
|
|
16328
|
+
const methodName = toCamelCase2(constant.name);
|
|
16201
16329
|
const valueType = getTypeForArg({ type: constant.type });
|
|
16202
16330
|
return `${methodName}: {
|
|
16203
16331
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -16229,7 +16357,7 @@ function generateConstantsObject(variables, address, contractName) {
|
|
|
16229
16357
|
function generateMapKeyConversion(keyType) {
|
|
16230
16358
|
if (keyType.tuple) {
|
|
16231
16359
|
const fields = keyType.tuple.map((field) => {
|
|
16232
|
-
const camelFieldName =
|
|
16360
|
+
const camelFieldName = toCamelCase2(field.name);
|
|
16233
16361
|
const fieldConversion = generateClarityConversion(`key.${camelFieldName}`, { type: field.type });
|
|
16234
16362
|
return `"${field.name}": ${fieldConversion}`;
|
|
16235
16363
|
}).join(", ");
|
|
@@ -16237,7 +16365,9 @@ function generateMapKeyConversion(keyType) {
|
|
|
16237
16365
|
}
|
|
16238
16366
|
return generateClarityConversion("key", { type: keyType });
|
|
16239
16367
|
}
|
|
16240
|
-
var init_contract = () => {
|
|
16368
|
+
var init_contract = __esm(() => {
|
|
16369
|
+
init_type_mapping();
|
|
16370
|
+
});
|
|
16241
16371
|
|
|
16242
16372
|
// ../../node_modules/ansis/index.cjs
|
|
16243
16373
|
var require_ansis = __commonJS((exports, module) => {
|
|
@@ -27838,7 +27968,7 @@ function deriveContractName(filePath) {
|
|
|
27838
27968
|
const basename = path11.basename(filePath, ".clar");
|
|
27839
27969
|
return basename.replace(/[-_](.)/g, (_2, char) => char.toUpperCase()).replace(/^(.)/, (_2, char) => char.toLowerCase()).replace(/^\d/, "_$&");
|
|
27840
27970
|
}
|
|
27841
|
-
function
|
|
27971
|
+
function toCamelCase3(str) {
|
|
27842
27972
|
return str.replace(/[-_](.)/g, (_2, char) => char.toUpperCase()).replace(/^(.)/, (_2, char) => char.toLowerCase()).replace(/^\d/, "_$&");
|
|
27843
27973
|
}
|
|
27844
27974
|
async function buildConfigFromInputs(parsedInputs, outPath, apiKey) {
|
|
@@ -27860,7 +27990,7 @@ async function buildConfigFromInputs(parsedInputs, outPath, apiKey) {
|
|
|
27860
27990
|
const apiClient = new StacksApiClient(network, apiKey);
|
|
27861
27991
|
const contractInfo = await apiClient.getContractInfo(contractId);
|
|
27862
27992
|
const abi = parseApiResponse(contractInfo);
|
|
27863
|
-
const name =
|
|
27993
|
+
const name = toCamelCase3(contractName);
|
|
27864
27994
|
contracts.push({
|
|
27865
27995
|
name,
|
|
27866
27996
|
address: contractId,
|
|
@@ -28105,7 +28235,7 @@ var {
|
|
|
28105
28235
|
// package.json
|
|
28106
28236
|
var package_default = {
|
|
28107
28237
|
name: "@secondlayer/cli",
|
|
28108
|
-
version: "0.3.
|
|
28238
|
+
version: "0.3.6",
|
|
28109
28239
|
description: "CLI for generating type-safe contract interfaces for the Stacks blockchain",
|
|
28110
28240
|
type: "module",
|
|
28111
28241
|
bin: {
|
|
@@ -28144,7 +28274,7 @@ var package_default = {
|
|
|
28144
28274
|
author: "",
|
|
28145
28275
|
license: "MIT",
|
|
28146
28276
|
dependencies: {
|
|
28147
|
-
"@secondlayer/clarity-types": "^0.4.
|
|
28277
|
+
"@secondlayer/clarity-types": "^0.4.1",
|
|
28148
28278
|
"@stacks/transactions": "7.0.6",
|
|
28149
28279
|
esbuild: "^0.19.0",
|
|
28150
28280
|
prettier: "^3.1.0"
|
|
@@ -28181,5 +28311,5 @@ program.command("init").description("Initialize a new stacks.config.ts file").ac
|
|
|
28181
28311
|
});
|
|
28182
28312
|
program.parse();
|
|
28183
28313
|
|
|
28184
|
-
//# debugId=
|
|
28314
|
+
//# debugId=33B7848FD15116A264756E2164756E21
|
|
28185
28315
|
//# sourceMappingURL=cli.js.map
|