@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/index.js
CHANGED
|
@@ -318,6 +318,81 @@ import { initSimnet } from "@hirosystems/clarinet-sdk";
|
|
|
318
318
|
|
|
319
319
|
// src/generators/contract.ts
|
|
320
320
|
import { format } from "prettier";
|
|
321
|
+
import {
|
|
322
|
+
toCamelCase as toCamelCase2
|
|
323
|
+
} from "@secondlayer/clarity-types";
|
|
324
|
+
|
|
325
|
+
// src/utils/type-mapping.ts
|
|
326
|
+
import {
|
|
327
|
+
toCamelCase,
|
|
328
|
+
isClarityList,
|
|
329
|
+
isClarityTuple,
|
|
330
|
+
isClarityOptional,
|
|
331
|
+
isClarityResponse,
|
|
332
|
+
isClarityBuffer,
|
|
333
|
+
isClarityStringAscii,
|
|
334
|
+
isClarityStringUtf8
|
|
335
|
+
} from "@secondlayer/clarity-types";
|
|
336
|
+
function clarityTypeToTS(type) {
|
|
337
|
+
if (typeof type === "string") {
|
|
338
|
+
switch (type) {
|
|
339
|
+
case "uint128":
|
|
340
|
+
case "int128":
|
|
341
|
+
return "bigint";
|
|
342
|
+
case "bool":
|
|
343
|
+
return "boolean";
|
|
344
|
+
case "principal":
|
|
345
|
+
case "trait_reference":
|
|
346
|
+
return "string";
|
|
347
|
+
default:
|
|
348
|
+
if (type.includes("string") || type.includes("ascii") || type.includes("utf8")) {
|
|
349
|
+
return "string";
|
|
350
|
+
}
|
|
351
|
+
if (type.includes("buff")) {
|
|
352
|
+
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
353
|
+
}
|
|
354
|
+
if (type.includes("uint") || type.includes("int")) {
|
|
355
|
+
return "bigint";
|
|
356
|
+
}
|
|
357
|
+
return "any";
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (isClarityBuffer(type)) {
|
|
361
|
+
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
362
|
+
}
|
|
363
|
+
if (isClarityStringAscii(type) || isClarityStringUtf8(type)) {
|
|
364
|
+
return "string";
|
|
365
|
+
}
|
|
366
|
+
if (isClarityOptional(type)) {
|
|
367
|
+
const innerType = clarityTypeToTS(type.optional);
|
|
368
|
+
if (innerType.includes(" | ") && !innerType.startsWith("(")) {
|
|
369
|
+
return `(${innerType}) | null`;
|
|
370
|
+
}
|
|
371
|
+
return `${innerType} | null`;
|
|
372
|
+
}
|
|
373
|
+
if (isClarityList(type)) {
|
|
374
|
+
const innerType = clarityTypeToTS(type.list.type);
|
|
375
|
+
if (innerType.includes(" | ") && !innerType.startsWith("(")) {
|
|
376
|
+
return `(${innerType})[]`;
|
|
377
|
+
}
|
|
378
|
+
return `${innerType}[]`;
|
|
379
|
+
}
|
|
380
|
+
if (isClarityTuple(type)) {
|
|
381
|
+
const fields = type.tuple.map((field) => `${toCamelCase(field.name)}: ${clarityTypeToTS(field.type)}`).join("; ");
|
|
382
|
+
return `{ ${fields} }`;
|
|
383
|
+
}
|
|
384
|
+
if (isClarityResponse(type)) {
|
|
385
|
+
const okType = clarityTypeToTS(type.response.ok);
|
|
386
|
+
const errType = clarityTypeToTS(type.response.error);
|
|
387
|
+
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
388
|
+
}
|
|
389
|
+
return "any";
|
|
390
|
+
}
|
|
391
|
+
function getTypeForArg(arg) {
|
|
392
|
+
return clarityTypeToTS(arg.type);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// src/generators/contract.ts
|
|
321
396
|
function generateNetworkUtils() {
|
|
322
397
|
return `/**
|
|
323
398
|
* API URLs for different networks
|
|
@@ -350,7 +425,8 @@ function getApiUrl(
|
|
|
350
425
|
}`;
|
|
351
426
|
}
|
|
352
427
|
async function generateContractInterface(contracts) {
|
|
353
|
-
const imports = `import { Cl, validateStacksAddress } from '@stacks/transactions'
|
|
428
|
+
const imports = `import { Cl, validateStacksAddress } from '@stacks/transactions'
|
|
429
|
+
import { jsToClarity, ClarityConversionError, CONTRACT_NAME_REGEX } from '@secondlayer/clarity-types'`;
|
|
354
430
|
const header = `/**
|
|
355
431
|
* Generated by @secondlayer/cli
|
|
356
432
|
* DO NOT EDIT MANUALLY
|
|
@@ -405,7 +481,7 @@ function generateAbiConstant(name, abi) {
|
|
|
405
481
|
return `export const ${name}Abi = ${abiJson} as const`;
|
|
406
482
|
}
|
|
407
483
|
function generateMethod(func, address, contractName) {
|
|
408
|
-
const methodName =
|
|
484
|
+
const methodName = toCamelCase2(func.name);
|
|
409
485
|
if (func.args.length === 0) {
|
|
410
486
|
return `${methodName}() {
|
|
411
487
|
return {
|
|
@@ -418,7 +494,7 @@ function generateMethod(func, address, contractName) {
|
|
|
418
494
|
}
|
|
419
495
|
if (func.args.length === 1) {
|
|
420
496
|
const originalArgName = func.args[0].name;
|
|
421
|
-
const argName =
|
|
497
|
+
const argName = toCamelCase2(originalArgName);
|
|
422
498
|
const argType = getTypeForArg(func.args[0]);
|
|
423
499
|
const clarityConversion = generateClarityConversion(argName, func.args[0]);
|
|
424
500
|
return `${methodName}(...args: [{ ${argName}: ${argType} }] | [${argType}]) {
|
|
@@ -434,17 +510,17 @@ function generateMethod(func, address, contractName) {
|
|
|
434
510
|
}
|
|
435
511
|
}`;
|
|
436
512
|
}
|
|
437
|
-
const argsList = func.args.map((arg) =>
|
|
513
|
+
const argsList = func.args.map((arg) => toCamelCase2(arg.name)).join(", ");
|
|
438
514
|
const argsTypes = func.args.map((arg) => {
|
|
439
|
-
const camelName =
|
|
515
|
+
const camelName = toCamelCase2(arg.name);
|
|
440
516
|
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
441
517
|
}).join("; ");
|
|
442
518
|
const argsArray = func.args.map((arg) => {
|
|
443
|
-
const argName =
|
|
519
|
+
const argName = toCamelCase2(arg.name);
|
|
444
520
|
return generateClarityConversion(argName, arg);
|
|
445
521
|
}).join(", ");
|
|
446
522
|
const objectAccess = func.args.map((arg) => {
|
|
447
|
-
const camelName =
|
|
523
|
+
const camelName = toCamelCase2(arg.name);
|
|
448
524
|
return `args[0].${camelName}`;
|
|
449
525
|
}).join(", ");
|
|
450
526
|
const positionTypes = func.args.map((arg) => getTypeForArg(arg)).join(", ");
|
|
@@ -461,50 +537,6 @@ function generateMethod(func, address, contractName) {
|
|
|
461
537
|
}
|
|
462
538
|
}`;
|
|
463
539
|
}
|
|
464
|
-
function getTypeForArg(arg) {
|
|
465
|
-
const type = arg.type;
|
|
466
|
-
if (typeof type === "string") {
|
|
467
|
-
switch (type) {
|
|
468
|
-
case "uint128":
|
|
469
|
-
case "int128":
|
|
470
|
-
return "bigint";
|
|
471
|
-
case "bool":
|
|
472
|
-
return "boolean";
|
|
473
|
-
case "principal":
|
|
474
|
-
case "trait_reference":
|
|
475
|
-
return "string";
|
|
476
|
-
default:
|
|
477
|
-
return "any";
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
if (type["string-ascii"] || type["string-utf8"]) {
|
|
481
|
-
return "string";
|
|
482
|
-
}
|
|
483
|
-
if (type.buff) {
|
|
484
|
-
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
485
|
-
}
|
|
486
|
-
if (type.optional) {
|
|
487
|
-
const innerType = getTypeForArg({ type: type.optional });
|
|
488
|
-
return `${innerType} | null`;
|
|
489
|
-
}
|
|
490
|
-
if (type.list) {
|
|
491
|
-
const innerType = getTypeForArg({ type: type.list.type });
|
|
492
|
-
return `${innerType}[]`;
|
|
493
|
-
}
|
|
494
|
-
if (type.tuple) {
|
|
495
|
-
const fields = type.tuple.map((field) => `${toCamelCase(field.name)}: ${getTypeForArg({ type: field.type })}`).join("; ");
|
|
496
|
-
return `{ ${fields} }`;
|
|
497
|
-
}
|
|
498
|
-
if (type.response) {
|
|
499
|
-
const okType = getTypeForArg({ type: type.response.ok });
|
|
500
|
-
const errType = getTypeForArg({ type: type.response.error });
|
|
501
|
-
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
502
|
-
}
|
|
503
|
-
return "any";
|
|
504
|
-
}
|
|
505
|
-
function toCamelCase(str) {
|
|
506
|
-
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/-([A-Z])/g, (_, letter) => letter).replace(/-(\d)/g, (_, digit) => digit).replace(/-/g, "");
|
|
507
|
-
}
|
|
508
540
|
function generateClarityConversion(argName, argType) {
|
|
509
541
|
const type = argType.type;
|
|
510
542
|
if (typeof type === "string") {
|
|
@@ -518,11 +550,14 @@ function generateClarityConversion(argName, argType) {
|
|
|
518
550
|
case "principal":
|
|
519
551
|
case "trait_reference":
|
|
520
552
|
return `(() => {
|
|
521
|
-
const [address, contractName] = ${argName}.split(".") as [string, string];
|
|
553
|
+
const [address, contractName] = ${argName}.split(".") as [string, string | undefined];
|
|
522
554
|
if (!validateStacksAddress(address)) {
|
|
523
555
|
throw new Error("Invalid Stacks address format");
|
|
524
556
|
}
|
|
525
|
-
if (
|
|
557
|
+
if (contractName !== undefined) {
|
|
558
|
+
if (!CONTRACT_NAME_REGEX.test(contractName)) {
|
|
559
|
+
throw new Error("Invalid contract name format: must start with letter and contain only letters, numbers, and hyphens");
|
|
560
|
+
}
|
|
526
561
|
return Cl.contractPrincipal(address, contractName);
|
|
527
562
|
}
|
|
528
563
|
return Cl.standardPrincipal(${argName});
|
|
@@ -584,24 +619,66 @@ function generateClarityConversion(argName, argType) {
|
|
|
584
619
|
const innerConversion = generateClarityConversion("item", {
|
|
585
620
|
type: type.list.type
|
|
586
621
|
});
|
|
587
|
-
|
|
622
|
+
const maxLength = type.list.length || 100;
|
|
623
|
+
return `(() => {
|
|
624
|
+
const listValue = ${argName};
|
|
625
|
+
if (listValue.length > ${maxLength}) {
|
|
626
|
+
throw new ClarityConversionError(
|
|
627
|
+
\`List length \${listValue.length} exceeds max ${maxLength}\`,
|
|
628
|
+
${JSON.stringify(type)},
|
|
629
|
+
listValue
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
return Cl.list(listValue.map(item => ${innerConversion}));
|
|
633
|
+
})()`;
|
|
588
634
|
}
|
|
589
635
|
if (type.tuple) {
|
|
636
|
+
const requiredFields = type.tuple.map((f) => f.name);
|
|
637
|
+
const fieldNames = JSON.stringify(requiredFields);
|
|
590
638
|
const fields = type.tuple.map((field) => {
|
|
591
|
-
const camelFieldName =
|
|
592
|
-
const fieldConversion = generateClarityConversion(
|
|
639
|
+
const camelFieldName = toCamelCase2(field.name);
|
|
640
|
+
const fieldConversion = generateClarityConversion(`tupleValue.${camelFieldName}`, { type: field.type });
|
|
593
641
|
return `"${field.name}": ${fieldConversion}`;
|
|
594
642
|
}).join(", ");
|
|
595
|
-
return `
|
|
643
|
+
return `(() => {
|
|
644
|
+
const tupleValue = ${argName};
|
|
645
|
+
const requiredFields = ${fieldNames};
|
|
646
|
+
for (const fieldName of requiredFields) {
|
|
647
|
+
const camelName = fieldName.replace(/-([a-z])/g, (_: string, l: string) => l.toUpperCase());
|
|
648
|
+
if (!(fieldName in tupleValue) && !(camelName in tupleValue)) {
|
|
649
|
+
throw new ClarityConversionError(
|
|
650
|
+
\`Missing tuple field: \${fieldName}\`,
|
|
651
|
+
${JSON.stringify(type)},
|
|
652
|
+
tupleValue
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return Cl.tuple({ ${fields} });
|
|
657
|
+
})()`;
|
|
596
658
|
}
|
|
597
659
|
if (type.response) {
|
|
598
|
-
const okConversion = generateClarityConversion(
|
|
660
|
+
const okConversion = generateClarityConversion(`responseValue.ok`, {
|
|
599
661
|
type: type.response.ok
|
|
600
662
|
});
|
|
601
|
-
const errConversion = generateClarityConversion(
|
|
663
|
+
const errConversion = generateClarityConversion(`responseValue.err`, {
|
|
602
664
|
type: type.response.error
|
|
603
665
|
});
|
|
604
|
-
return `
|
|
666
|
+
return `(() => {
|
|
667
|
+
const responseValue = ${argName};
|
|
668
|
+
const hasOk = 'ok' in responseValue;
|
|
669
|
+
const hasErr = 'err' in responseValue;
|
|
670
|
+
if (hasOk && !hasErr) {
|
|
671
|
+
return Cl.ok(${okConversion});
|
|
672
|
+
}
|
|
673
|
+
if (hasErr && !hasOk) {
|
|
674
|
+
return Cl.error(${errConversion});
|
|
675
|
+
}
|
|
676
|
+
throw new ClarityConversionError(
|
|
677
|
+
"Response must have exactly 'ok' or 'err' property",
|
|
678
|
+
${JSON.stringify(type)},
|
|
679
|
+
responseValue
|
|
680
|
+
);
|
|
681
|
+
})()`;
|
|
605
682
|
}
|
|
606
683
|
return `${argName}`;
|
|
607
684
|
}
|
|
@@ -610,7 +687,7 @@ function generateMapsObject(maps, address, contractName) {
|
|
|
610
687
|
return "";
|
|
611
688
|
}
|
|
612
689
|
const mapMethods = maps.map((map) => {
|
|
613
|
-
const methodName =
|
|
690
|
+
const methodName = toCamelCase2(map.name);
|
|
614
691
|
const keyType = getTypeForArg({ type: map.key });
|
|
615
692
|
const valueType = getTypeForArg({ type: map.value });
|
|
616
693
|
const keyConversion = generateMapKeyConversion(map.key);
|
|
@@ -664,7 +741,7 @@ function generateVarsObject(variables, address, contractName) {
|
|
|
664
741
|
return "";
|
|
665
742
|
}
|
|
666
743
|
const varMethods = dataVars.map((variable) => {
|
|
667
|
-
const methodName =
|
|
744
|
+
const methodName = toCamelCase2(variable.name);
|
|
668
745
|
const valueType = getTypeForArg({ type: variable.type });
|
|
669
746
|
return `${methodName}: {
|
|
670
747
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -702,7 +779,7 @@ function generateConstantsObject(variables, address, contractName) {
|
|
|
702
779
|
return "";
|
|
703
780
|
}
|
|
704
781
|
const constMethods = constants.map((constant) => {
|
|
705
|
-
const methodName =
|
|
782
|
+
const methodName = toCamelCase2(constant.name);
|
|
706
783
|
const valueType = getTypeForArg({ type: constant.type });
|
|
707
784
|
return `${methodName}: {
|
|
708
785
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -734,7 +811,7 @@ function generateConstantsObject(variables, address, contractName) {
|
|
|
734
811
|
function generateMapKeyConversion(keyType) {
|
|
735
812
|
if (keyType.tuple) {
|
|
736
813
|
const fields = keyType.tuple.map((field) => {
|
|
737
|
-
const camelFieldName =
|
|
814
|
+
const camelFieldName = toCamelCase2(field.name);
|
|
738
815
|
const fieldConversion = generateClarityConversion(`key.${camelFieldName}`, { type: field.type });
|
|
739
816
|
return `"${field.name}": ${fieldConversion}`;
|
|
740
817
|
}).join(", ");
|
|
@@ -744,11 +821,11 @@ function generateMapKeyConversion(keyType) {
|
|
|
744
821
|
}
|
|
745
822
|
|
|
746
823
|
// src/plugins/clarinet/index.ts
|
|
747
|
-
function
|
|
824
|
+
function toCamelCase3(str) {
|
|
748
825
|
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/-([A-Z])/g, (_, letter) => letter).replace(/-(\d)/g, (_, digit) => digit).replace(/-/g, "").replace(/^\d/, "_$&");
|
|
749
826
|
}
|
|
750
827
|
function sanitizeContractName(name) {
|
|
751
|
-
return
|
|
828
|
+
return toCamelCase3(name);
|
|
752
829
|
}
|
|
753
830
|
async function isUserDefinedContract(contractId, manifestPath) {
|
|
754
831
|
const [address, contractName] = contractId.split(".");
|
|
@@ -858,56 +935,13 @@ async function hasClarinetProject(path2 = "./Clarinet.toml") {
|
|
|
858
935
|
}
|
|
859
936
|
}
|
|
860
937
|
// src/plugins/actions/generators.ts
|
|
861
|
-
|
|
862
|
-
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/-([A-Z])/g, (_, letter) => letter).replace(/-(\d)/g, (_, digit) => digit).replace(/-/g, "").replace(/^\d/, "_$&");
|
|
863
|
-
}
|
|
864
|
-
function getTypeForArg2(arg) {
|
|
865
|
-
const type = arg.type;
|
|
866
|
-
if (typeof type === "string") {
|
|
867
|
-
switch (type) {
|
|
868
|
-
case "uint128":
|
|
869
|
-
case "int128":
|
|
870
|
-
return "bigint";
|
|
871
|
-
case "bool":
|
|
872
|
-
return "boolean";
|
|
873
|
-
case "principal":
|
|
874
|
-
case "trait_reference":
|
|
875
|
-
return "string";
|
|
876
|
-
default:
|
|
877
|
-
return "any";
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
if (type["string-ascii"] || type["string-utf8"]) {
|
|
881
|
-
return "string";
|
|
882
|
-
}
|
|
883
|
-
if (type.buff) {
|
|
884
|
-
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
885
|
-
}
|
|
886
|
-
if (type.optional) {
|
|
887
|
-
const innerType = getTypeForArg2({ type: type.optional });
|
|
888
|
-
return `${innerType} | null`;
|
|
889
|
-
}
|
|
890
|
-
if (type.list) {
|
|
891
|
-
const innerType = getTypeForArg2({ type: type.list.type });
|
|
892
|
-
return `${innerType}[]`;
|
|
893
|
-
}
|
|
894
|
-
if (type.tuple) {
|
|
895
|
-
const fields = type.tuple.map((field) => `${toCamelCase3(field.name)}: ${getTypeForArg2({ type: field.type })}`).join("; ");
|
|
896
|
-
return `{ ${fields} }`;
|
|
897
|
-
}
|
|
898
|
-
if (type.response) {
|
|
899
|
-
const okType = getTypeForArg2({ type: type.response.ok });
|
|
900
|
-
const errType = getTypeForArg2({ type: type.response.error });
|
|
901
|
-
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
902
|
-
}
|
|
903
|
-
return "any";
|
|
904
|
-
}
|
|
938
|
+
import { toCamelCase as toCamelCase4 } from "@secondlayer/clarity-types";
|
|
905
939
|
function generateArgsSignature(args) {
|
|
906
940
|
if (args.length === 0)
|
|
907
941
|
return "";
|
|
908
942
|
const argsTypes = args.map((arg) => {
|
|
909
|
-
const camelName =
|
|
910
|
-
return `${camelName}: ${
|
|
943
|
+
const camelName = toCamelCase4(arg.name);
|
|
944
|
+
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
911
945
|
}).join("; ");
|
|
912
946
|
return `args: { ${argsTypes} }, `;
|
|
913
947
|
}
|
|
@@ -915,7 +949,7 @@ function generateClarityArgs(args, _contractName) {
|
|
|
915
949
|
if (args.length === 0)
|
|
916
950
|
return "";
|
|
917
951
|
return args.map((arg) => {
|
|
918
|
-
const argName = `args.${
|
|
952
|
+
const argName = `args.${toCamelCase4(arg.name)}`;
|
|
919
953
|
return generateClarityConversion2(argName, arg);
|
|
920
954
|
}).join(", ");
|
|
921
955
|
}
|
|
@@ -932,11 +966,14 @@ function generateClarityConversion2(argName, argType) {
|
|
|
932
966
|
case "principal":
|
|
933
967
|
case "trait_reference":
|
|
934
968
|
return `(() => {
|
|
935
|
-
const [address, contractName] = ${argName}.split(".") as [string, string];
|
|
969
|
+
const [address, contractName] = ${argName}.split(".") as [string, string | undefined];
|
|
936
970
|
if (!validateStacksAddress(address)) {
|
|
937
971
|
throw new Error("Invalid Stacks address format");
|
|
938
972
|
}
|
|
939
|
-
if (
|
|
973
|
+
if (contractName !== undefined) {
|
|
974
|
+
if (!CONTRACT_NAME_REGEX.test(contractName)) {
|
|
975
|
+
throw new Error("Invalid contract name format: must start with letter and contain only letters, numbers, and hyphens");
|
|
976
|
+
}
|
|
940
977
|
return Cl.contractPrincipal(address, contractName);
|
|
941
978
|
}
|
|
942
979
|
return Cl.standardPrincipal(${argName});
|
|
@@ -992,24 +1029,66 @@ function generateClarityConversion2(argName, argType) {
|
|
|
992
1029
|
const innerConversion = generateClarityConversion2("item", {
|
|
993
1030
|
type: type.list.type
|
|
994
1031
|
});
|
|
995
|
-
|
|
1032
|
+
const maxLength = type.list.length || 100;
|
|
1033
|
+
return `(() => {
|
|
1034
|
+
const listValue = ${argName};
|
|
1035
|
+
if (listValue.length > ${maxLength}) {
|
|
1036
|
+
throw new ClarityConversionError(
|
|
1037
|
+
\`List length \${listValue.length} exceeds max ${maxLength}\`,
|
|
1038
|
+
${JSON.stringify(type)},
|
|
1039
|
+
listValue
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
return Cl.list(listValue.map(item => ${innerConversion}));
|
|
1043
|
+
})()`;
|
|
996
1044
|
}
|
|
997
1045
|
if (type.tuple) {
|
|
1046
|
+
const requiredFields = type.tuple.map((f) => f.name);
|
|
1047
|
+
const fieldNames = JSON.stringify(requiredFields);
|
|
998
1048
|
const fields = type.tuple.map((field) => {
|
|
999
|
-
const camelFieldName =
|
|
1000
|
-
const fieldConversion = generateClarityConversion2(
|
|
1049
|
+
const camelFieldName = toCamelCase4(field.name);
|
|
1050
|
+
const fieldConversion = generateClarityConversion2(`tupleValue.${camelFieldName}`, { type: field.type });
|
|
1001
1051
|
return `"${field.name}": ${fieldConversion}`;
|
|
1002
1052
|
}).join(", ");
|
|
1003
|
-
return `
|
|
1053
|
+
return `(() => {
|
|
1054
|
+
const tupleValue = ${argName};
|
|
1055
|
+
const requiredFields = ${fieldNames};
|
|
1056
|
+
for (const fieldName of requiredFields) {
|
|
1057
|
+
const camelName = fieldName.replace(/-([a-z])/g, (_: string, l: string) => l.toUpperCase());
|
|
1058
|
+
if (!(fieldName in tupleValue) && !(camelName in tupleValue)) {
|
|
1059
|
+
throw new ClarityConversionError(
|
|
1060
|
+
\`Missing tuple field: \${fieldName}\`,
|
|
1061
|
+
${JSON.stringify(type)},
|
|
1062
|
+
tupleValue
|
|
1063
|
+
);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
return Cl.tuple({ ${fields} });
|
|
1067
|
+
})()`;
|
|
1004
1068
|
}
|
|
1005
1069
|
if (type.response) {
|
|
1006
|
-
const okConversion = generateClarityConversion2(
|
|
1070
|
+
const okConversion = generateClarityConversion2(`responseValue.ok`, {
|
|
1007
1071
|
type: type.response.ok
|
|
1008
1072
|
});
|
|
1009
|
-
const errConversion = generateClarityConversion2(
|
|
1073
|
+
const errConversion = generateClarityConversion2(`responseValue.err`, {
|
|
1010
1074
|
type: type.response.error
|
|
1011
1075
|
});
|
|
1012
|
-
return `
|
|
1076
|
+
return `(() => {
|
|
1077
|
+
const responseValue = ${argName};
|
|
1078
|
+
const hasOk = 'ok' in responseValue;
|
|
1079
|
+
const hasErr = 'err' in responseValue;
|
|
1080
|
+
if (hasOk && !hasErr) {
|
|
1081
|
+
return Cl.ok(${okConversion});
|
|
1082
|
+
}
|
|
1083
|
+
if (hasErr && !hasOk) {
|
|
1084
|
+
return Cl.error(${errConversion});
|
|
1085
|
+
}
|
|
1086
|
+
throw new ClarityConversionError(
|
|
1087
|
+
"Response must have exactly 'ok' or 'err' property",
|
|
1088
|
+
${JSON.stringify(type)},
|
|
1089
|
+
responseValue
|
|
1090
|
+
);
|
|
1091
|
+
})()`;
|
|
1013
1092
|
}
|
|
1014
1093
|
return `${argName}`;
|
|
1015
1094
|
}
|
|
@@ -1033,7 +1112,7 @@ function generateReadHelpers(contract, options) {
|
|
|
1033
1112
|
return "";
|
|
1034
1113
|
}
|
|
1035
1114
|
const helpers = filteredFunctions.map((func) => {
|
|
1036
|
-
const methodName =
|
|
1115
|
+
const methodName = toCamelCase4(func.name);
|
|
1037
1116
|
const argsSignature = generateArgsSignature(func.args);
|
|
1038
1117
|
const clarityArgs = generateClarityArgs(func.args, name);
|
|
1039
1118
|
return `async ${methodName}(${argsSignature}options?: {
|
|
@@ -1076,7 +1155,7 @@ function generateWriteHelpers(contract, options) {
|
|
|
1076
1155
|
return "";
|
|
1077
1156
|
}
|
|
1078
1157
|
const helpers = filteredFunctions.map((func) => {
|
|
1079
|
-
const methodName =
|
|
1158
|
+
const methodName = toCamelCase4(func.name);
|
|
1080
1159
|
const argsSignature = generateArgsSignature(func.args);
|
|
1081
1160
|
const clarityArgs = generateClarityArgs(func.args, name);
|
|
1082
1161
|
return `async ${methodName}(${argsSignature}options: {
|
|
@@ -2124,38 +2203,36 @@ function generateGenericHook(hookName) {
|
|
|
2124
2203
|
import { format as format4 } from "prettier";
|
|
2125
2204
|
|
|
2126
2205
|
// src/plugins/react/generators/utils.ts
|
|
2127
|
-
|
|
2128
|
-
return str.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
|
|
2129
|
-
}
|
|
2206
|
+
import { toCamelCase as toCamelCase5 } from "@secondlayer/clarity-types";
|
|
2130
2207
|
function capitalize(str) {
|
|
2131
2208
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2132
2209
|
}
|
|
2133
2210
|
function generateHookArgsSignature(args) {
|
|
2134
2211
|
if (args.length === 0)
|
|
2135
2212
|
return "";
|
|
2136
|
-
const argsList = args.map((arg) => `${
|
|
2213
|
+
const argsList = args.map((arg) => `${toCamelCase5(arg.name)}: ${clarityTypeToTS(arg.type)}`).join(", ");
|
|
2137
2214
|
return `${argsList}`;
|
|
2138
2215
|
}
|
|
2139
2216
|
function generateArgsType(args) {
|
|
2140
2217
|
if (args.length === 0)
|
|
2141
2218
|
return "void";
|
|
2142
|
-
const argsList = args.map((arg) => `${
|
|
2219
|
+
const argsList = args.map((arg) => `${toCamelCase5(arg.name)}: ${clarityTypeToTS(arg.type)}`).join("; ");
|
|
2143
2220
|
return `{ ${argsList} }`;
|
|
2144
2221
|
}
|
|
2145
2222
|
function generateQueryKeyArgs(args) {
|
|
2146
2223
|
if (args.length === 0)
|
|
2147
2224
|
return "";
|
|
2148
|
-
return args.map((arg) =>
|
|
2225
|
+
return args.map((arg) => toCamelCase5(arg.name)).join(", ");
|
|
2149
2226
|
}
|
|
2150
2227
|
function generateFunctionCallArgs(args) {
|
|
2151
2228
|
if (args.length === 0)
|
|
2152
2229
|
return "";
|
|
2153
|
-
return args.map((arg) =>
|
|
2230
|
+
return args.map((arg) => toCamelCase5(arg.name)).join(", ");
|
|
2154
2231
|
}
|
|
2155
2232
|
function generateEnabledCondition(args) {
|
|
2156
2233
|
return args.map((arg) => {
|
|
2157
|
-
const camelName =
|
|
2158
|
-
const type =
|
|
2234
|
+
const camelName = toCamelCase5(arg.name);
|
|
2235
|
+
const type = clarityTypeToTS(arg.type);
|
|
2159
2236
|
if (type === "string")
|
|
2160
2237
|
return `!!${camelName}`;
|
|
2161
2238
|
if (type === "bigint")
|
|
@@ -2163,62 +2240,10 @@ function generateEnabledCondition(args) {
|
|
|
2163
2240
|
return `${camelName} !== undefined`;
|
|
2164
2241
|
}).join(" && ");
|
|
2165
2242
|
}
|
|
2166
|
-
function mapClarityTypeToTS(clarityType) {
|
|
2167
|
-
if (typeof clarityType !== "string") {
|
|
2168
|
-
if (clarityType?.uint || clarityType?.int)
|
|
2169
|
-
return "bigint";
|
|
2170
|
-
if (clarityType?.principal)
|
|
2171
|
-
return "string";
|
|
2172
|
-
if (clarityType?.bool)
|
|
2173
|
-
return "boolean";
|
|
2174
|
-
if (clarityType?.string || clarityType?.ascii)
|
|
2175
|
-
return "string";
|
|
2176
|
-
if (clarityType?.["string-ascii"] || clarityType?.["string-utf8"])
|
|
2177
|
-
return "string";
|
|
2178
|
-
if (clarityType?.buff)
|
|
2179
|
-
return "Uint8Array";
|
|
2180
|
-
if (clarityType?.optional) {
|
|
2181
|
-
const innerType = mapClarityTypeToTS(clarityType.optional);
|
|
2182
|
-
return `${innerType} | null`;
|
|
2183
|
-
}
|
|
2184
|
-
if (clarityType?.response) {
|
|
2185
|
-
const okType = mapClarityTypeToTS(clarityType.response.ok);
|
|
2186
|
-
const errType = mapClarityTypeToTS(clarityType.response.error);
|
|
2187
|
-
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
2188
|
-
}
|
|
2189
|
-
if (clarityType?.tuple) {
|
|
2190
|
-
const fields = clarityType.tuple.map((field) => `${toCamelCase4(field.name)}: ${mapClarityTypeToTS(field.type)}`).join("; ");
|
|
2191
|
-
return `{ ${fields} }`;
|
|
2192
|
-
}
|
|
2193
|
-
if (clarityType?.list) {
|
|
2194
|
-
const innerType = mapClarityTypeToTS(clarityType.list.type);
|
|
2195
|
-
if (innerType.includes(" | ")) {
|
|
2196
|
-
return `(${innerType})[]`;
|
|
2197
|
-
}
|
|
2198
|
-
return `${innerType}[]`;
|
|
2199
|
-
}
|
|
2200
|
-
return "any";
|
|
2201
|
-
}
|
|
2202
|
-
if (clarityType.includes("uint") || clarityType.includes("int"))
|
|
2203
|
-
return "bigint";
|
|
2204
|
-
if (clarityType.includes("principal"))
|
|
2205
|
-
return "string";
|
|
2206
|
-
if (clarityType.includes("bool"))
|
|
2207
|
-
return "boolean";
|
|
2208
|
-
if (clarityType.includes("string") || clarityType.includes("ascii"))
|
|
2209
|
-
return "string";
|
|
2210
|
-
if (clarityType.includes("buff"))
|
|
2211
|
-
return "Uint8Array";
|
|
2212
|
-
if (clarityType.includes("optional")) {
|
|
2213
|
-
const innerType = clarityType.replace(/optional\s*/, "").trim();
|
|
2214
|
-
return `${mapClarityTypeToTS(innerType)} | null`;
|
|
2215
|
-
}
|
|
2216
|
-
return "any";
|
|
2217
|
-
}
|
|
2218
2243
|
function generateObjectArgs(args) {
|
|
2219
2244
|
if (args.length === 0)
|
|
2220
2245
|
return "";
|
|
2221
|
-
return args.map((arg) => `${arg.name}: ${
|
|
2246
|
+
return args.map((arg) => `${arg.name}: ${toCamelCase5(arg.name)}`).join(", ");
|
|
2222
2247
|
}
|
|
2223
2248
|
|
|
2224
2249
|
// src/plugins/react/generators/contract.ts
|
|
@@ -2258,21 +2283,21 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2258
2283
|
const readOnlyFunctions = functions.filter((f) => f.access === "read_only" || f.access === "read-only");
|
|
2259
2284
|
const publicFunctions = functions.filter((f) => f.access === "public");
|
|
2260
2285
|
const readHooks = readOnlyFunctions.map((func) => {
|
|
2261
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2286
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase5(func.name))}`;
|
|
2262
2287
|
if (excludeList.includes(hookName)) {
|
|
2263
2288
|
return null;
|
|
2264
2289
|
}
|
|
2265
2290
|
return generateReadHook(func, name);
|
|
2266
2291
|
}).filter(Boolean);
|
|
2267
2292
|
const writeHooks = publicFunctions.map((func) => {
|
|
2268
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2293
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase5(func.name))}`;
|
|
2269
2294
|
if (excludeList.includes(hookName)) {
|
|
2270
2295
|
return null;
|
|
2271
2296
|
}
|
|
2272
2297
|
return generateWriteHook(func, name);
|
|
2273
2298
|
}).filter(Boolean);
|
|
2274
2299
|
const mapHooks = maps.map((map) => {
|
|
2275
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2300
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase5(map.name))}`;
|
|
2276
2301
|
if (excludeList.includes(hookName)) {
|
|
2277
2302
|
return null;
|
|
2278
2303
|
}
|
|
@@ -2280,7 +2305,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2280
2305
|
}).filter(Boolean);
|
|
2281
2306
|
const dataVars = variables.filter((v) => v.access === "variable");
|
|
2282
2307
|
const varHooks = dataVars.map((variable) => {
|
|
2283
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2308
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase5(variable.name))}`;
|
|
2284
2309
|
if (excludeList.includes(hookName)) {
|
|
2285
2310
|
return null;
|
|
2286
2311
|
}
|
|
@@ -2288,7 +2313,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2288
2313
|
}).filter(Boolean);
|
|
2289
2314
|
const constants = variables.filter((v) => v.access === "constant");
|
|
2290
2315
|
const constantHooks = constants.map((constant) => {
|
|
2291
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2316
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase5(constant.name))}`;
|
|
2292
2317
|
if (excludeList.includes(hookName)) {
|
|
2293
2318
|
return null;
|
|
2294
2319
|
}
|
|
@@ -2303,16 +2328,16 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2303
2328
|
`);
|
|
2304
2329
|
}
|
|
2305
2330
|
function generateReadHook(func, contractName) {
|
|
2306
|
-
const hookName = `use${capitalize(contractName)}${capitalize(
|
|
2331
|
+
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase5(func.name))}`;
|
|
2307
2332
|
const argsSignature = generateHookArgsSignature(func.args);
|
|
2308
2333
|
const enabledParam = func.args.length > 0 ? ", options?: { enabled?: boolean }" : "options?: { enabled?: boolean }";
|
|
2309
|
-
const returnType =
|
|
2334
|
+
const returnType = clarityTypeToTS(func.outputs);
|
|
2310
2335
|
return `export function ${hookName}(${argsSignature}${enabledParam}) {
|
|
2311
2336
|
const config = useStacksConfig()
|
|
2312
2337
|
|
|
2313
2338
|
return useQuery<${returnType}>({
|
|
2314
2339
|
queryKey: ['${func.name}', ${contractName}.address, ${generateQueryKeyArgs(func.args)}],
|
|
2315
|
-
queryFn: () => ${contractName}.read.${
|
|
2340
|
+
queryFn: () => ${contractName}.read.${toCamelCase5(func.name)}(${generateFunctionCallArgs(func.args) ? `{ ${generateObjectArgs(func.args)} }, ` : ""}{
|
|
2316
2341
|
network: config.network,
|
|
2317
2342
|
senderAddress: config.senderAddress || 'SP000000000000000000002Q6VF78'
|
|
2318
2343
|
}),
|
|
@@ -2322,7 +2347,7 @@ function generateReadHook(func, contractName) {
|
|
|
2322
2347
|
}`;
|
|
2323
2348
|
}
|
|
2324
2349
|
function generateWriteHook(func, contractName) {
|
|
2325
|
-
const hookName = `use${capitalize(contractName)}${capitalize(
|
|
2350
|
+
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase5(func.name))}`;
|
|
2326
2351
|
const argsType = generateArgsType(func.args);
|
|
2327
2352
|
return `export function ${hookName}() {
|
|
2328
2353
|
const config = useStacksConfig()
|
|
@@ -2339,7 +2364,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2339
2364
|
};
|
|
2340
2365
|
}) => {
|
|
2341
2366
|
const { args, options = {} } = params
|
|
2342
|
-
const contractCallData = ${contractName}.${
|
|
2367
|
+
const contractCallData = ${contractName}.${toCamelCase5(func.name)}(args)
|
|
2343
2368
|
const { contractAddress, contractName: name, functionName, functionArgs } = contractCallData
|
|
2344
2369
|
const network = config.network || 'mainnet'
|
|
2345
2370
|
const contract = \`\${contractAddress}.\${name}\`
|
|
@@ -2389,7 +2414,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2389
2414
|
}
|
|
2390
2415
|
})
|
|
2391
2416
|
|
|
2392
|
-
const ${
|
|
2417
|
+
const ${toCamelCase5(func.name)} = useCallback(async (
|
|
2393
2418
|
args: ${argsType},
|
|
2394
2419
|
options?: {
|
|
2395
2420
|
postConditions?: PostCondition[];
|
|
@@ -2402,7 +2427,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2402
2427
|
}, [mutation])
|
|
2403
2428
|
|
|
2404
2429
|
return {
|
|
2405
|
-
${
|
|
2430
|
+
${toCamelCase5(func.name)},
|
|
2406
2431
|
// Expose mutation state
|
|
2407
2432
|
isPending: mutation.isPending,
|
|
2408
2433
|
isError: mutation.isError,
|
|
@@ -2414,46 +2439,46 @@ function generateWriteHook(func, contractName) {
|
|
|
2414
2439
|
}`;
|
|
2415
2440
|
}
|
|
2416
2441
|
function generateMapHook(map, contractVarName, _address, _contractName) {
|
|
2417
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2418
|
-
const keyType =
|
|
2419
|
-
const valueType =
|
|
2442
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase5(map.name))}`;
|
|
2443
|
+
const keyType = clarityTypeToTS(map.key);
|
|
2444
|
+
const valueType = clarityTypeToTS(map.value);
|
|
2420
2445
|
return `export function ${hookName}(key: ${keyType}, options?: { enabled?: boolean }) {
|
|
2421
2446
|
const config = useStacksConfig()
|
|
2422
2447
|
|
|
2423
2448
|
return useQuery<${valueType} | null>({
|
|
2424
2449
|
queryKey: ['${contractVarName}', '${map.name}', 'map', key, config.network],
|
|
2425
2450
|
queryFn: async () => {
|
|
2426
|
-
return ${contractVarName}.maps.${
|
|
2451
|
+
return ${contractVarName}.maps.${toCamelCase5(map.name)}.get(key, { network: config.network })
|
|
2427
2452
|
},
|
|
2428
2453
|
enabled: options?.enabled ?? true
|
|
2429
2454
|
})
|
|
2430
2455
|
}`;
|
|
2431
2456
|
}
|
|
2432
2457
|
function generateVarHook(variable, contractVarName, _address, _contractName) {
|
|
2433
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2434
|
-
const valueType =
|
|
2458
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase5(variable.name))}`;
|
|
2459
|
+
const valueType = clarityTypeToTS(variable.type);
|
|
2435
2460
|
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2436
2461
|
const config = useStacksConfig()
|
|
2437
2462
|
|
|
2438
2463
|
return useQuery<${valueType}>({
|
|
2439
2464
|
queryKey: ['${contractVarName}', '${variable.name}', 'var', config.network],
|
|
2440
2465
|
queryFn: async () => {
|
|
2441
|
-
return ${contractVarName}.vars.${
|
|
2466
|
+
return ${contractVarName}.vars.${toCamelCase5(variable.name)}.get({ network: config.network })
|
|
2442
2467
|
},
|
|
2443
2468
|
enabled: options?.enabled ?? true
|
|
2444
2469
|
})
|
|
2445
2470
|
}`;
|
|
2446
2471
|
}
|
|
2447
2472
|
function generateConstantHook(constant, contractVarName, _address, _contractName) {
|
|
2448
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2449
|
-
const valueType =
|
|
2473
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase5(constant.name))}`;
|
|
2474
|
+
const valueType = clarityTypeToTS(constant.type);
|
|
2450
2475
|
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2451
2476
|
const config = useStacksConfig()
|
|
2452
2477
|
|
|
2453
2478
|
return useQuery<${valueType}>({
|
|
2454
2479
|
queryKey: ['${contractVarName}', '${constant.name}', 'constant', config.network],
|
|
2455
2480
|
queryFn: async () => {
|
|
2456
|
-
return ${contractVarName}.constants.${
|
|
2481
|
+
return ${contractVarName}.constants.${toCamelCase5(constant.name)}.get({ network: config.network })
|
|
2457
2482
|
},
|
|
2458
2483
|
enabled: options?.enabled ?? true,
|
|
2459
2484
|
staleTime: Infinity // Constants never change
|
|
@@ -2500,60 +2525,19 @@ var react = (options = {}) => {
|
|
|
2500
2525
|
};
|
|
2501
2526
|
};
|
|
2502
2527
|
// src/plugins/testing/generators.ts
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
}
|
|
2528
|
+
import {
|
|
2529
|
+
toCamelCase as toCamelCase6
|
|
2530
|
+
} from "@secondlayer/clarity-types";
|
|
2506
2531
|
function toPascalCase(str) {
|
|
2507
|
-
const camel =
|
|
2532
|
+
const camel = toCamelCase6(str);
|
|
2508
2533
|
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
2509
2534
|
}
|
|
2510
|
-
function getTypeForArg3(arg) {
|
|
2511
|
-
const type = arg.type;
|
|
2512
|
-
if (typeof type === "string") {
|
|
2513
|
-
switch (type) {
|
|
2514
|
-
case "uint128":
|
|
2515
|
-
case "int128":
|
|
2516
|
-
return "bigint";
|
|
2517
|
-
case "bool":
|
|
2518
|
-
return "boolean";
|
|
2519
|
-
case "principal":
|
|
2520
|
-
case "trait_reference":
|
|
2521
|
-
return "string";
|
|
2522
|
-
default:
|
|
2523
|
-
return "any";
|
|
2524
|
-
}
|
|
2525
|
-
}
|
|
2526
|
-
if (type["string-ascii"] || type["string-utf8"]) {
|
|
2527
|
-
return "string";
|
|
2528
|
-
}
|
|
2529
|
-
if (type.buff) {
|
|
2530
|
-
return "Uint8Array | string | { type: 'ascii' | 'utf8' | 'hex'; value: string }";
|
|
2531
|
-
}
|
|
2532
|
-
if (type.optional) {
|
|
2533
|
-
const innerType = getTypeForArg3({ type: type.optional });
|
|
2534
|
-
return `${innerType} | null`;
|
|
2535
|
-
}
|
|
2536
|
-
if (type.list) {
|
|
2537
|
-
const innerType = getTypeForArg3({ type: type.list.type });
|
|
2538
|
-
return `${innerType}[]`;
|
|
2539
|
-
}
|
|
2540
|
-
if (type.tuple) {
|
|
2541
|
-
const fields = type.tuple.map((field) => `${toCamelCase5(field.name)}: ${getTypeForArg3({ type: field.type })}`).join("; ");
|
|
2542
|
-
return `{ ${fields} }`;
|
|
2543
|
-
}
|
|
2544
|
-
if (type.response) {
|
|
2545
|
-
const okType = getTypeForArg3({ type: type.response.ok });
|
|
2546
|
-
const errType = getTypeForArg3({ type: type.response.error });
|
|
2547
|
-
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
2548
|
-
}
|
|
2549
|
-
return "any";
|
|
2550
|
-
}
|
|
2551
2535
|
function generateArgsSignature2(args) {
|
|
2552
2536
|
if (args.length === 0)
|
|
2553
2537
|
return "";
|
|
2554
2538
|
const argsTypes = args.map((arg) => {
|
|
2555
|
-
const camelName =
|
|
2556
|
-
return `${camelName}: ${
|
|
2539
|
+
const camelName = toCamelCase6(arg.name);
|
|
2540
|
+
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
2557
2541
|
}).join("; ");
|
|
2558
2542
|
return `args: { ${argsTypes} }, `;
|
|
2559
2543
|
}
|
|
@@ -2628,24 +2612,54 @@ function generateClarityConversion3(argName, argType) {
|
|
|
2628
2612
|
const innerConversion = generateClarityConversion3("item", {
|
|
2629
2613
|
type: type.list.type
|
|
2630
2614
|
});
|
|
2631
|
-
|
|
2615
|
+
const maxLength = type.list.length || 100;
|
|
2616
|
+
return `(() => {
|
|
2617
|
+
const listValue = ${argName};
|
|
2618
|
+
if (listValue.length > ${maxLength}) {
|
|
2619
|
+
throw new Error(\`List length \${listValue.length} exceeds max ${maxLength}\`);
|
|
2620
|
+
}
|
|
2621
|
+
return Cl.list(listValue.map(item => ${innerConversion}));
|
|
2622
|
+
})()`;
|
|
2632
2623
|
}
|
|
2633
2624
|
if (type.tuple) {
|
|
2625
|
+
const requiredFields = type.tuple.map((f) => f.name);
|
|
2626
|
+
const fieldNames = JSON.stringify(requiredFields);
|
|
2634
2627
|
const fields = type.tuple.map((field) => {
|
|
2635
|
-
const camelFieldName =
|
|
2636
|
-
const fieldConversion = generateClarityConversion3(
|
|
2628
|
+
const camelFieldName = toCamelCase6(field.name);
|
|
2629
|
+
const fieldConversion = generateClarityConversion3(`tupleValue.${camelFieldName}`, { type: field.type });
|
|
2637
2630
|
return `"${field.name}": ${fieldConversion}`;
|
|
2638
2631
|
}).join(", ");
|
|
2639
|
-
return `
|
|
2632
|
+
return `(() => {
|
|
2633
|
+
const tupleValue = ${argName};
|
|
2634
|
+
const requiredFields = ${fieldNames};
|
|
2635
|
+
for (const fieldName of requiredFields) {
|
|
2636
|
+
const camelName = fieldName.replace(/-([a-z])/g, (_: string, l: string) => l.toUpperCase());
|
|
2637
|
+
if (!(fieldName in tupleValue) && !(camelName in tupleValue)) {
|
|
2638
|
+
throw new Error(\`Missing tuple field: \${fieldName}\`);
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2641
|
+
return Cl.tuple({ ${fields} });
|
|
2642
|
+
})()`;
|
|
2640
2643
|
}
|
|
2641
2644
|
if (type.response) {
|
|
2642
|
-
const okConversion = generateClarityConversion3(
|
|
2645
|
+
const okConversion = generateClarityConversion3(`responseValue.ok`, {
|
|
2643
2646
|
type: type.response.ok
|
|
2644
2647
|
});
|
|
2645
|
-
const errConversion = generateClarityConversion3(
|
|
2648
|
+
const errConversion = generateClarityConversion3(`responseValue.err`, {
|
|
2646
2649
|
type: type.response.error
|
|
2647
2650
|
});
|
|
2648
|
-
return `
|
|
2651
|
+
return `(() => {
|
|
2652
|
+
const responseValue = ${argName};
|
|
2653
|
+
const hasOk = 'ok' in responseValue;
|
|
2654
|
+
const hasErr = 'err' in responseValue;
|
|
2655
|
+
if (hasOk && !hasErr) {
|
|
2656
|
+
return Cl.ok(${okConversion});
|
|
2657
|
+
}
|
|
2658
|
+
if (hasErr && !hasOk) {
|
|
2659
|
+
return Cl.error(${errConversion});
|
|
2660
|
+
}
|
|
2661
|
+
throw new Error("Response must have exactly 'ok' or 'err' property");
|
|
2662
|
+
})()`;
|
|
2649
2663
|
}
|
|
2650
2664
|
return `${argName}`;
|
|
2651
2665
|
}
|
|
@@ -2653,12 +2667,12 @@ function generateClarityArgs2(args) {
|
|
|
2653
2667
|
if (args.length === 0)
|
|
2654
2668
|
return "";
|
|
2655
2669
|
return args.map((arg) => {
|
|
2656
|
-
const argName = `args.${
|
|
2670
|
+
const argName = `args.${toCamelCase6(arg.name)}`;
|
|
2657
2671
|
return generateClarityConversion3(argName, arg);
|
|
2658
2672
|
}).join(", ");
|
|
2659
2673
|
}
|
|
2660
2674
|
function generatePublicFunction(func, contractId) {
|
|
2661
|
-
const methodName =
|
|
2675
|
+
const methodName = toCamelCase6(func.name);
|
|
2662
2676
|
const argsSignature = generateArgsSignature2(func.args);
|
|
2663
2677
|
const clarityArgs = generateClarityArgs2(func.args);
|
|
2664
2678
|
return `${methodName}: (${argsSignature}caller: string) => {
|
|
@@ -2672,7 +2686,7 @@ function generatePublicFunction(func, contractId) {
|
|
|
2672
2686
|
}`;
|
|
2673
2687
|
}
|
|
2674
2688
|
function generateReadOnlyFunction(func, contractId) {
|
|
2675
|
-
const methodName =
|
|
2689
|
+
const methodName = toCamelCase6(func.name);
|
|
2676
2690
|
const argsSignature = generateArgsSignature2(func.args);
|
|
2677
2691
|
const clarityArgs = generateClarityArgs2(func.args);
|
|
2678
2692
|
const hasArgs = func.args.length > 0;
|
|
@@ -2687,7 +2701,7 @@ function generateReadOnlyFunction(func, contractId) {
|
|
|
2687
2701
|
}`;
|
|
2688
2702
|
}
|
|
2689
2703
|
function generatePrivateFunction(func, contractId) {
|
|
2690
|
-
const methodName =
|
|
2704
|
+
const methodName = toCamelCase6(func.name);
|
|
2691
2705
|
const argsSignature = generateArgsSignature2(func.args);
|
|
2692
2706
|
const clarityArgs = generateClarityArgs2(func.args);
|
|
2693
2707
|
return `${methodName}: (${argsSignature}caller: string) => {
|
|
@@ -2701,22 +2715,22 @@ function generatePrivateFunction(func, contractId) {
|
|
|
2701
2715
|
}`;
|
|
2702
2716
|
}
|
|
2703
2717
|
function generateDataVarHelper(variable, contractId) {
|
|
2704
|
-
const methodName =
|
|
2718
|
+
const methodName = toCamelCase6(variable.name);
|
|
2705
2719
|
return `${methodName}: () => {
|
|
2706
2720
|
return simnet.getDataVar('${contractId}', '${variable.name}');
|
|
2707
2721
|
}`;
|
|
2708
2722
|
}
|
|
2709
2723
|
function getMapKeyType(keyType) {
|
|
2710
2724
|
if (keyType.tuple) {
|
|
2711
|
-
const fields = keyType.tuple.map((field) => `${
|
|
2725
|
+
const fields = keyType.tuple.map((field) => `${toCamelCase6(field.name)}: ${getTypeForArg({ type: field.type })}`).join("; ");
|
|
2712
2726
|
return `{ ${fields} }`;
|
|
2713
2727
|
}
|
|
2714
|
-
return
|
|
2728
|
+
return getTypeForArg({ type: keyType });
|
|
2715
2729
|
}
|
|
2716
2730
|
function generateMapKeyConversion2(keyType) {
|
|
2717
2731
|
if (keyType.tuple) {
|
|
2718
2732
|
const fields = keyType.tuple.map((field) => {
|
|
2719
|
-
const camelFieldName =
|
|
2733
|
+
const camelFieldName = toCamelCase6(field.name);
|
|
2720
2734
|
const fieldConversion = generateClarityConversion3(`key.${camelFieldName}`, { type: field.type });
|
|
2721
2735
|
return `"${field.name}": ${fieldConversion}`;
|
|
2722
2736
|
}).join(", ");
|
|
@@ -2725,7 +2739,7 @@ function generateMapKeyConversion2(keyType) {
|
|
|
2725
2739
|
return generateClarityConversion3("key", { type: keyType });
|
|
2726
2740
|
}
|
|
2727
2741
|
function generateMapEntryHelper(map, contractId) {
|
|
2728
|
-
const methodName =
|
|
2742
|
+
const methodName = toCamelCase6(map.name);
|
|
2729
2743
|
const keyType = getMapKeyType(map.key);
|
|
2730
2744
|
const keyConversion = generateMapKeyConversion2(map.key);
|
|
2731
2745
|
return `${methodName}: (key: ${keyType}) => {
|
|
@@ -2795,7 +2809,7 @@ function generateContractHelper(contract, options) {
|
|
|
2795
2809
|
}
|
|
2796
2810
|
function generateGetContracts(contracts) {
|
|
2797
2811
|
const contractEntries = contracts.map((contract) => {
|
|
2798
|
-
const camelName =
|
|
2812
|
+
const camelName = toCamelCase6(contract.name);
|
|
2799
2813
|
const pascalName = toPascalCase(contract.name);
|
|
2800
2814
|
return `${camelName}: get${pascalName}(simnet)`;
|
|
2801
2815
|
}).join(`,
|
|
@@ -2923,5 +2937,5 @@ export {
|
|
|
2923
2937
|
PluginManager
|
|
2924
2938
|
};
|
|
2925
2939
|
|
|
2926
|
-
//# debugId=
|
|
2940
|
+
//# debugId=B5466C394D09A7B464756E2164756E21
|
|
2927
2941
|
//# sourceMappingURL=index.js.map
|