@secondlayer/cli 1.4.1 → 1.6.0
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 +634 -1031
- package/dist/cli.js.map +26 -33
- package/dist/index.d.ts +6 -1
- package/dist/index.js +86 -120
- package/dist/index.js.map +8 -7
- package/dist/plugin-manager.d.ts +6 -1
- package/dist/plugin-manager.js +34 -53
- package/dist/plugin-manager.js.map +4 -4
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AbiContract as AbiContract2 } from "@secondlayer/stacks/clarity";
|
|
1
2
|
import { AbiContract } from "@secondlayer/stacks/clarity";
|
|
2
3
|
/** Supported Stacks network identifiers for contract resolution. */
|
|
3
4
|
type NetworkName = "mainnet" | "testnet" | "devnet";
|
|
@@ -96,7 +97,7 @@ interface ContractConfig {
|
|
|
96
97
|
name?: string;
|
|
97
98
|
address?: string | Partial<Record<NetworkName, string>>;
|
|
98
99
|
source?: string;
|
|
99
|
-
abi?:
|
|
100
|
+
abi?: AbiContract2;
|
|
100
101
|
metadata?: Record<string, any>;
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
@@ -259,6 +260,10 @@ declare class PluginManager {
|
|
|
259
260
|
*/
|
|
260
261
|
getExecutionResults(): Map<string, HookResult[]>;
|
|
261
262
|
/**
|
|
263
|
+
* Convert a contract config with an ABI into a ProcessedContract
|
|
264
|
+
*/
|
|
265
|
+
private contractToProcessed;
|
|
266
|
+
/**
|
|
262
267
|
* Augment existing output with additional content
|
|
263
268
|
*/
|
|
264
269
|
private augmentOutput;
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
2
|
var __defProp = Object.defineProperty;
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
-
for (let key of __getOwnPropNames(mod))
|
|
11
|
-
if (!__hasOwnProp.call(to, key))
|
|
12
|
-
__defProp(to, key, {
|
|
13
|
-
get: () => mod[key],
|
|
14
|
-
enumerable: true
|
|
15
|
-
});
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
18
7
|
var __export = (target, all) => {
|
|
19
8
|
for (var name in all)
|
|
20
9
|
__defProp(target, name, {
|
|
21
10
|
get: all[name],
|
|
22
11
|
enumerable: true,
|
|
23
12
|
configurable: true,
|
|
24
|
-
set: (
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
25
14
|
});
|
|
26
15
|
};
|
|
27
16
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
@@ -82,6 +71,8 @@ var init_format = () => {};
|
|
|
82
71
|
import { promises as fs } from "fs";
|
|
83
72
|
import path from "path";
|
|
84
73
|
import { isValidAddress as _validateStacksAddress } from "@secondlayer/stacks";
|
|
74
|
+
import { getErrorMessage } from "@secondlayer/shared";
|
|
75
|
+
import { toCamelCase } from "@secondlayer/stacks/clarity";
|
|
85
76
|
|
|
86
77
|
// src/utils/contract-id.ts
|
|
87
78
|
function parseContractId(contractId) {
|
|
@@ -147,12 +138,11 @@ class PluginManager {
|
|
|
147
138
|
success: true
|
|
148
139
|
});
|
|
149
140
|
} catch (error) {
|
|
150
|
-
const err = error;
|
|
151
141
|
this.recordHookResult(plugin.name, "transformConfig", {
|
|
152
142
|
success: false,
|
|
153
|
-
error:
|
|
143
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
154
144
|
});
|
|
155
|
-
throw new Error(`Plugin "${plugin.name}" failed during config transformation: ${
|
|
145
|
+
throw new Error(`Plugin "${plugin.name}" failed during config transformation: ${getErrorMessage(error)}`);
|
|
156
146
|
}
|
|
157
147
|
}
|
|
158
148
|
}
|
|
@@ -166,31 +156,11 @@ class PluginManager {
|
|
|
166
156
|
const processedContracts = [];
|
|
167
157
|
for (let contract of contracts) {
|
|
168
158
|
if (isClarinetContract(contract) && contract.abi) {
|
|
169
|
-
|
|
170
|
-
const parsed = parseContractId(address);
|
|
171
|
-
const processed = {
|
|
172
|
-
name: contract.name || parsed.contractName,
|
|
173
|
-
address: parsed.address,
|
|
174
|
-
contractName: parsed.contractName,
|
|
175
|
-
abi: contract.abi,
|
|
176
|
-
source: "local",
|
|
177
|
-
metadata: { source: "clarinet" }
|
|
178
|
-
};
|
|
179
|
-
processedContracts.push(processed);
|
|
159
|
+
processedContracts.push(this.contractToProcessed(contract, "clarinet"));
|
|
180
160
|
continue;
|
|
181
161
|
}
|
|
182
162
|
if (isDirectFileContract(contract) && contract.abi) {
|
|
183
|
-
|
|
184
|
-
const parsed = parseContractId(address);
|
|
185
|
-
const processed = {
|
|
186
|
-
name: contract.name || parsed.contractName,
|
|
187
|
-
address: parsed.address,
|
|
188
|
-
contractName: parsed.contractName,
|
|
189
|
-
abi: contract.abi,
|
|
190
|
-
source: "local",
|
|
191
|
-
metadata: { source: "direct" }
|
|
192
|
-
};
|
|
193
|
-
processedContracts.push(processed);
|
|
163
|
+
processedContracts.push(this.contractToProcessed(contract, "direct"));
|
|
194
164
|
continue;
|
|
195
165
|
}
|
|
196
166
|
for (const plugin of this.plugins) {
|
|
@@ -202,27 +172,16 @@ class PluginManager {
|
|
|
202
172
|
success: true
|
|
203
173
|
});
|
|
204
174
|
} catch (error) {
|
|
205
|
-
const err = error;
|
|
206
175
|
this.recordHookResult(plugin.name, "transformContract", {
|
|
207
176
|
success: false,
|
|
208
|
-
error:
|
|
177
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
209
178
|
});
|
|
210
|
-
this.logger.warn(`Plugin "${plugin.name}" failed to transform contract: ${
|
|
179
|
+
this.logger.warn(`Plugin "${plugin.name}" failed to transform contract: ${getErrorMessage(error)}`);
|
|
211
180
|
}
|
|
212
181
|
}
|
|
213
182
|
}
|
|
214
183
|
if (contract.abi) {
|
|
215
|
-
|
|
216
|
-
const parsed = parseContractId(addressStr);
|
|
217
|
-
const processed = {
|
|
218
|
-
name: contract.name || parsed.contractName || "unknown",
|
|
219
|
-
address: parsed.address || "unknown",
|
|
220
|
-
contractName: parsed.contractName || contract.name || "unknown",
|
|
221
|
-
abi: contract.abi,
|
|
222
|
-
source: "api",
|
|
223
|
-
metadata: contract.metadata
|
|
224
|
-
};
|
|
225
|
-
processedContracts.push(processed);
|
|
184
|
+
processedContracts.push(this.contractToProcessed(contract, "api"));
|
|
226
185
|
}
|
|
227
186
|
}
|
|
228
187
|
return processedContracts;
|
|
@@ -238,12 +197,11 @@ class PluginManager {
|
|
|
238
197
|
success: true
|
|
239
198
|
});
|
|
240
199
|
} catch (error) {
|
|
241
|
-
const err = error;
|
|
242
200
|
this.recordHookResult(plugin.name, hookName, {
|
|
243
201
|
success: false,
|
|
244
|
-
error:
|
|
202
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
245
203
|
});
|
|
246
|
-
this.logger.error(`Plugin "${plugin.name}" failed during ${hookName}: ${
|
|
204
|
+
this.logger.error(`Plugin "${plugin.name}" failed during ${hookName}: ${getErrorMessage(error)}`);
|
|
247
205
|
}
|
|
248
206
|
}
|
|
249
207
|
}
|
|
@@ -283,12 +241,11 @@ class PluginManager {
|
|
|
283
241
|
success: true
|
|
284
242
|
});
|
|
285
243
|
} catch (error) {
|
|
286
|
-
const err = error;
|
|
287
244
|
this.recordHookResult(plugin.name, "transformOutput", {
|
|
288
245
|
success: false,
|
|
289
|
-
error:
|
|
246
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
290
247
|
});
|
|
291
|
-
this.logger.warn(`Plugin "${plugin.name}" failed to transform output: ${
|
|
248
|
+
this.logger.warn(`Plugin "${plugin.name}" failed to transform output: ${getErrorMessage(error)}`);
|
|
292
249
|
}
|
|
293
250
|
}
|
|
294
251
|
}
|
|
@@ -306,15 +263,26 @@ class PluginManager {
|
|
|
306
263
|
await this.utils.ensureDir(path.dirname(resolvedPath));
|
|
307
264
|
await this.utils.writeFile(resolvedPath, output.content);
|
|
308
265
|
} catch (error) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
throw err;
|
|
266
|
+
this.logger.error(`Failed to write ${output.path}: ${getErrorMessage(error)}`);
|
|
267
|
+
throw error;
|
|
312
268
|
}
|
|
313
269
|
}
|
|
314
270
|
}
|
|
315
271
|
getExecutionResults() {
|
|
316
272
|
return new Map(this.executionContext.results);
|
|
317
273
|
}
|
|
274
|
+
contractToProcessed(contract, source) {
|
|
275
|
+
const address = typeof contract.address === "string" ? contract.address : "";
|
|
276
|
+
const parsed = parseContractId(address);
|
|
277
|
+
return {
|
|
278
|
+
name: contract.name || parsed.contractName || "unknown",
|
|
279
|
+
address: parsed.address || "unknown",
|
|
280
|
+
contractName: parsed.contractName || contract.name || "unknown",
|
|
281
|
+
abi: contract.abi,
|
|
282
|
+
source: source === "api" ? "api" : "local",
|
|
283
|
+
metadata: contract.metadata ?? { source }
|
|
284
|
+
};
|
|
285
|
+
}
|
|
318
286
|
augmentOutput(outputs, outputKey, contractName, content) {
|
|
319
287
|
const existing = outputs.get(outputKey);
|
|
320
288
|
if (!existing) {
|
|
@@ -351,9 +319,7 @@ ${JSON.stringify(content, null, 2)}`;
|
|
|
351
319
|
}
|
|
352
320
|
createUtils() {
|
|
353
321
|
return {
|
|
354
|
-
toCamelCase
|
|
355
|
-
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
356
|
-
},
|
|
322
|
+
toCamelCase,
|
|
357
323
|
toKebabCase: (str) => {
|
|
358
324
|
return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
359
325
|
},
|
|
@@ -392,17 +358,17 @@ ${JSON.stringify(content, null, 2)}`;
|
|
|
392
358
|
}
|
|
393
359
|
// src/plugins/clarinet/index.ts
|
|
394
360
|
import { initSimnet } from "@hirosystems/clarinet-sdk";
|
|
395
|
-
import { toCamelCase as
|
|
361
|
+
import { toCamelCase as toCamelCase6 } from "@secondlayer/stacks/clarity";
|
|
396
362
|
|
|
397
363
|
// src/generators/contract.ts
|
|
398
364
|
init_format();
|
|
399
365
|
import {
|
|
400
|
-
toCamelCase as
|
|
366
|
+
toCamelCase as toCamelCase5
|
|
401
367
|
} from "@secondlayer/stacks/clarity";
|
|
402
368
|
|
|
403
369
|
// src/utils/type-mapping.ts
|
|
404
370
|
import {
|
|
405
|
-
toCamelCase,
|
|
371
|
+
toCamelCase as toCamelCase2,
|
|
406
372
|
isAbiList,
|
|
407
373
|
isAbiTuple,
|
|
408
374
|
isAbiOptional,
|
|
@@ -461,7 +427,7 @@ function clarityTypeToTS(type) {
|
|
|
461
427
|
return `${innerType}[]`;
|
|
462
428
|
}
|
|
463
429
|
if (isAbiTuple(type)) {
|
|
464
|
-
const fields = type.tuple.map((field) => `${
|
|
430
|
+
const fields = type.tuple.map((field) => `${toCamelCase2(field.name)}: ${clarityTypeToTS(field.type)}`).join("; ");
|
|
465
431
|
return `{ ${fields} }`;
|
|
466
432
|
}
|
|
467
433
|
if (isAbiResponse(type)) {
|
|
@@ -477,7 +443,7 @@ function getTypeForArg(arg) {
|
|
|
477
443
|
|
|
478
444
|
// src/utils/clarity-conversion.ts
|
|
479
445
|
import {
|
|
480
|
-
toCamelCase as
|
|
446
|
+
toCamelCase as toCamelCase3,
|
|
481
447
|
isAbiStringAscii as isAbiStringAscii2,
|
|
482
448
|
isAbiStringUtf8 as isAbiStringUtf82,
|
|
483
449
|
isAbiBuffer as isAbiBuffer2,
|
|
@@ -575,7 +541,7 @@ function generateClarityConversion(argName, argType) {
|
|
|
575
541
|
const requiredFields = type.tuple.map((f) => f.name);
|
|
576
542
|
const fieldNames = JSON.stringify(requiredFields);
|
|
577
543
|
const fields = type.tuple.map((field) => {
|
|
578
|
-
const camelFieldName =
|
|
544
|
+
const camelFieldName = toCamelCase3(field.name);
|
|
579
545
|
const fieldConversion = generateClarityConversion(`tupleValue.${camelFieldName}`, { type: field.type });
|
|
580
546
|
return `"${field.name}": ${fieldConversion}`;
|
|
581
547
|
}).join(", ");
|
|
@@ -615,12 +581,12 @@ function generateClarityConversion(argName, argType) {
|
|
|
615
581
|
}
|
|
616
582
|
|
|
617
583
|
// src/utils/generator-helpers.ts
|
|
618
|
-
import { toCamelCase as
|
|
584
|
+
import { toCamelCase as toCamelCase4, isAbiTuple as isAbiTuple3 } from "@secondlayer/stacks/clarity";
|
|
619
585
|
function generateArgsSignature(args) {
|
|
620
586
|
if (args.length === 0)
|
|
621
587
|
return "";
|
|
622
588
|
const argsTypes = args.map((arg) => {
|
|
623
|
-
const camelName =
|
|
589
|
+
const camelName = toCamelCase4(arg.name);
|
|
624
590
|
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
625
591
|
}).join("; ");
|
|
626
592
|
return `args: { ${argsTypes} }, `;
|
|
@@ -629,14 +595,14 @@ function generateClarityArgs(args) {
|
|
|
629
595
|
if (args.length === 0)
|
|
630
596
|
return "";
|
|
631
597
|
return args.map((arg) => {
|
|
632
|
-
const argName = `args.${
|
|
598
|
+
const argName = `args.${toCamelCase4(arg.name)}`;
|
|
633
599
|
return generateClarityConversion(argName, arg);
|
|
634
600
|
}).join(", ");
|
|
635
601
|
}
|
|
636
602
|
function generateMapKeyConversion(keyType) {
|
|
637
603
|
if (isAbiTuple3(keyType)) {
|
|
638
604
|
const fields = keyType.tuple.map((field) => {
|
|
639
|
-
const camelFieldName =
|
|
605
|
+
const camelFieldName = toCamelCase4(field.name);
|
|
640
606
|
const fieldConversion = generateClarityConversion(`key.${camelFieldName}`, { type: field.type });
|
|
641
607
|
return `"${field.name}": ${fieldConversion}`;
|
|
642
608
|
}).join(", ");
|
|
@@ -736,7 +702,7 @@ function generateAbiConstant(name, abi) {
|
|
|
736
702
|
return `export const ${name}Abi = ${abiJson} as const`;
|
|
737
703
|
}
|
|
738
704
|
function generateMethod(func, address, contractName) {
|
|
739
|
-
const methodName =
|
|
705
|
+
const methodName = toCamelCase5(func.name);
|
|
740
706
|
if (func.args.length === 0) {
|
|
741
707
|
return `${methodName}() {
|
|
742
708
|
return {
|
|
@@ -749,7 +715,7 @@ function generateMethod(func, address, contractName) {
|
|
|
749
715
|
}
|
|
750
716
|
if (func.args.length === 1) {
|
|
751
717
|
const originalArgName = func.args[0].name;
|
|
752
|
-
const argName =
|
|
718
|
+
const argName = toCamelCase5(originalArgName);
|
|
753
719
|
const argType = getTypeForArg(func.args[0]);
|
|
754
720
|
const clarityConversion = generateClarityConversion(argName, func.args[0]);
|
|
755
721
|
return `${methodName}(...args: [{ ${argName}: ${argType} }] | [${argType}]) {
|
|
@@ -765,17 +731,17 @@ function generateMethod(func, address, contractName) {
|
|
|
765
731
|
}
|
|
766
732
|
}`;
|
|
767
733
|
}
|
|
768
|
-
const argsList = func.args.map((arg) =>
|
|
734
|
+
const argsList = func.args.map((arg) => toCamelCase5(arg.name)).join(", ");
|
|
769
735
|
const argsTypes = func.args.map((arg) => {
|
|
770
|
-
const camelName =
|
|
736
|
+
const camelName = toCamelCase5(arg.name);
|
|
771
737
|
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
772
738
|
}).join("; ");
|
|
773
739
|
const argsArray = func.args.map((arg) => {
|
|
774
|
-
const argName =
|
|
740
|
+
const argName = toCamelCase5(arg.name);
|
|
775
741
|
return generateClarityConversion(argName, arg);
|
|
776
742
|
}).join(", ");
|
|
777
743
|
const objectAccess = func.args.map((arg) => {
|
|
778
|
-
const camelName =
|
|
744
|
+
const camelName = toCamelCase5(arg.name);
|
|
779
745
|
return `args[0].${camelName}`;
|
|
780
746
|
}).join(", ");
|
|
781
747
|
const positionTypes = func.args.map((arg) => getTypeForArg(arg)).join(", ");
|
|
@@ -797,7 +763,7 @@ function generateMapsObject(maps, address, contractName) {
|
|
|
797
763
|
return "";
|
|
798
764
|
}
|
|
799
765
|
const mapMethods = maps.map((map) => {
|
|
800
|
-
const methodName =
|
|
766
|
+
const methodName = toCamelCase5(map.name);
|
|
801
767
|
const keyType = getTypeForArg({ type: map.key });
|
|
802
768
|
const valueType = getTypeForArg({ type: map.value });
|
|
803
769
|
const keyConversion = generateMapKeyConversion(map.key);
|
|
@@ -858,7 +824,7 @@ function generateVarsObject(variables, address, contractName) {
|
|
|
858
824
|
return "";
|
|
859
825
|
}
|
|
860
826
|
const varMethods = dataVars.map((variable) => {
|
|
861
|
-
const methodName =
|
|
827
|
+
const methodName = toCamelCase5(variable.name);
|
|
862
828
|
const valueType = getTypeForArg({ type: variable.type });
|
|
863
829
|
return `${methodName}: {
|
|
864
830
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -903,7 +869,7 @@ function generateConstantsObject(variables, address, contractName) {
|
|
|
903
869
|
return "";
|
|
904
870
|
}
|
|
905
871
|
const constMethods = constants.map((constant) => {
|
|
906
|
-
const methodName =
|
|
872
|
+
const methodName = toCamelCase5(constant.name);
|
|
907
873
|
const valueType = getTypeForArg({ type: constant.type });
|
|
908
874
|
return `${methodName}: {
|
|
909
875
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -1095,7 +1061,7 @@ function normalizeAbi(abi) {
|
|
|
1095
1061
|
|
|
1096
1062
|
// src/plugins/clarinet/index.ts
|
|
1097
1063
|
function sanitizeContractName(name) {
|
|
1098
|
-
return
|
|
1064
|
+
return toCamelCase6(name);
|
|
1099
1065
|
}
|
|
1100
1066
|
async function isUserDefinedContract(contractId, manifestPath) {
|
|
1101
1067
|
const { address, contractName } = parseContractId(contractId);
|
|
@@ -1205,7 +1171,7 @@ async function hasClarinetProject(path2 = "./Clarinet.toml") {
|
|
|
1205
1171
|
}
|
|
1206
1172
|
}
|
|
1207
1173
|
// src/plugins/actions/generators.ts
|
|
1208
|
-
import { toCamelCase as
|
|
1174
|
+
import { toCamelCase as toCamelCase7 } from "@secondlayer/stacks/clarity";
|
|
1209
1175
|
function generateReadHelpers(contract, options) {
|
|
1210
1176
|
const { abi } = contract;
|
|
1211
1177
|
const functions = abi.functions || [];
|
|
@@ -1226,7 +1192,7 @@ function generateReadHelpers(contract, options) {
|
|
|
1226
1192
|
return "";
|
|
1227
1193
|
}
|
|
1228
1194
|
const helpers = filteredFunctions.map((func) => {
|
|
1229
|
-
const methodName =
|
|
1195
|
+
const methodName = toCamelCase7(func.name);
|
|
1230
1196
|
const argsSignature = generateArgsSignature(func.args);
|
|
1231
1197
|
const clarityArgs = generateClarityArgs(func.args);
|
|
1232
1198
|
return `async ${methodName}(${argsSignature}options?: {
|
|
@@ -1270,7 +1236,7 @@ function generateWriteHelpers(contract, options) {
|
|
|
1270
1236
|
return "";
|
|
1271
1237
|
}
|
|
1272
1238
|
const helpers = filteredFunctions.map((func) => {
|
|
1273
|
-
const methodName =
|
|
1239
|
+
const methodName = toCamelCase7(func.name);
|
|
1274
1240
|
const argsSignature = generateArgsSignature(func.args);
|
|
1275
1241
|
const clarityArgs = generateClarityArgs(func.args);
|
|
1276
1242
|
return `async ${methodName}(${argsSignature}senderKey?: string, options?: {
|
|
@@ -2208,31 +2174,36 @@ function generateGenericHook(hookName) {
|
|
|
2208
2174
|
// src/plugins/react/generators/contract.ts
|
|
2209
2175
|
init_format();
|
|
2210
2176
|
|
|
2211
|
-
// src/
|
|
2212
|
-
import { toCamelCase as
|
|
2177
|
+
// src/utils/case-conversion.ts
|
|
2178
|
+
import { toCamelCase as toCamelCase8 } from "@secondlayer/stacks/clarity";
|
|
2213
2179
|
function capitalize(str) {
|
|
2214
2180
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2215
2181
|
}
|
|
2182
|
+
function toPascalCase(str) {
|
|
2183
|
+
return capitalize(toCamelCase8(str));
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
// src/plugins/react/generators/utils.ts
|
|
2216
2187
|
function generateHookArgsSignature(args) {
|
|
2217
2188
|
if (args.length === 0)
|
|
2218
2189
|
return "";
|
|
2219
|
-
const argsList = args.map((arg) => `${
|
|
2190
|
+
const argsList = args.map((arg) => `${toCamelCase8(arg.name)}: ${clarityTypeToTS(arg.type)}`).join(", ");
|
|
2220
2191
|
return `${argsList}`;
|
|
2221
2192
|
}
|
|
2222
2193
|
function generateArgsType(args) {
|
|
2223
2194
|
if (args.length === 0)
|
|
2224
2195
|
return "void";
|
|
2225
|
-
const argsList = args.map((arg) => `${
|
|
2196
|
+
const argsList = args.map((arg) => `${toCamelCase8(arg.name)}: ${clarityTypeToTS(arg.type)}`).join("; ");
|
|
2226
2197
|
return `{ ${argsList} }`;
|
|
2227
2198
|
}
|
|
2228
2199
|
function generateArgNames(args) {
|
|
2229
2200
|
if (args.length === 0)
|
|
2230
2201
|
return "";
|
|
2231
|
-
return args.map((arg) =>
|
|
2202
|
+
return args.map((arg) => toCamelCase8(arg.name)).join(", ");
|
|
2232
2203
|
}
|
|
2233
2204
|
function generateEnabledCondition(args) {
|
|
2234
2205
|
return args.map((arg) => {
|
|
2235
|
-
const camelName =
|
|
2206
|
+
const camelName = toCamelCase8(arg.name);
|
|
2236
2207
|
const type = clarityTypeToTS(arg.type);
|
|
2237
2208
|
if (type === "string")
|
|
2238
2209
|
return `!!${camelName}`;
|
|
@@ -2244,7 +2215,7 @@ function generateEnabledCondition(args) {
|
|
|
2244
2215
|
function generateObjectArgs(args) {
|
|
2245
2216
|
if (args.length === 0)
|
|
2246
2217
|
return "";
|
|
2247
|
-
return args.map((arg) => `${arg.name}: ${
|
|
2218
|
+
return args.map((arg) => `${arg.name}: ${toCamelCase8(arg.name)}`).join(", ");
|
|
2248
2219
|
}
|
|
2249
2220
|
|
|
2250
2221
|
// src/plugins/react/generators/contract.ts
|
|
@@ -2277,21 +2248,21 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2277
2248
|
const readOnlyFunctions = functions.filter((f) => f.access === "read-only");
|
|
2278
2249
|
const publicFunctions = functions.filter((f) => f.access === "public");
|
|
2279
2250
|
const readHooks = readOnlyFunctions.map((func) => {
|
|
2280
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2251
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(func.name))}`;
|
|
2281
2252
|
if (excludeList.includes(hookName)) {
|
|
2282
2253
|
return null;
|
|
2283
2254
|
}
|
|
2284
2255
|
return generateReadHook(func, name);
|
|
2285
2256
|
}).filter(Boolean);
|
|
2286
2257
|
const writeHooks = publicFunctions.map((func) => {
|
|
2287
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2258
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(func.name))}`;
|
|
2288
2259
|
if (excludeList.includes(hookName)) {
|
|
2289
2260
|
return null;
|
|
2290
2261
|
}
|
|
2291
2262
|
return generateWriteHook(func, name);
|
|
2292
2263
|
}).filter(Boolean);
|
|
2293
2264
|
const mapHooks = maps.map((map) => {
|
|
2294
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2265
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(map.name))}`;
|
|
2295
2266
|
if (excludeList.includes(hookName)) {
|
|
2296
2267
|
return null;
|
|
2297
2268
|
}
|
|
@@ -2299,7 +2270,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2299
2270
|
}).filter(Boolean);
|
|
2300
2271
|
const dataVars = variables.filter((v) => v.access === "variable");
|
|
2301
2272
|
const varHooks = dataVars.map((variable) => {
|
|
2302
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2273
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(variable.name))}`;
|
|
2303
2274
|
if (excludeList.includes(hookName)) {
|
|
2304
2275
|
return null;
|
|
2305
2276
|
}
|
|
@@ -2307,7 +2278,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2307
2278
|
}).filter(Boolean);
|
|
2308
2279
|
const constants = variables.filter((v) => v.access === "constant");
|
|
2309
2280
|
const constantHooks = constants.map((constant) => {
|
|
2310
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2281
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(constant.name))}`;
|
|
2311
2282
|
if (excludeList.includes(hookName)) {
|
|
2312
2283
|
return null;
|
|
2313
2284
|
}
|
|
@@ -2322,7 +2293,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2322
2293
|
`);
|
|
2323
2294
|
}
|
|
2324
2295
|
function generateReadHook(func, contractName) {
|
|
2325
|
-
const hookName = `use${capitalize(contractName)}${capitalize(
|
|
2296
|
+
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase8(func.name))}`;
|
|
2326
2297
|
const argsSignature = generateHookArgsSignature(func.args);
|
|
2327
2298
|
const enabledParam = func.args.length > 0 ? ", options?: { enabled?: boolean }" : "options?: { enabled?: boolean }";
|
|
2328
2299
|
const returnType = clarityTypeToTS(func.outputs);
|
|
@@ -2331,7 +2302,7 @@ function generateReadHook(func, contractName) {
|
|
|
2331
2302
|
|
|
2332
2303
|
return useQuery<${returnType}>({
|
|
2333
2304
|
queryKey: ['${func.name}', ${contractName}.address, ${generateArgNames(func.args)}],
|
|
2334
|
-
queryFn: () => ${contractName}.read.${
|
|
2305
|
+
queryFn: () => ${contractName}.read.${toCamelCase8(func.name)}(${generateArgNames(func.args) ? `{ ${generateObjectArgs(func.args)} }, ` : ""}{
|
|
2335
2306
|
network: config.network,
|
|
2336
2307
|
senderAddress: config.senderAddress || 'SP000000000000000000002Q6VF78'
|
|
2337
2308
|
}),
|
|
@@ -2341,7 +2312,7 @@ function generateReadHook(func, contractName) {
|
|
|
2341
2312
|
}`;
|
|
2342
2313
|
}
|
|
2343
2314
|
function generateWriteHook(func, contractName) {
|
|
2344
|
-
const hookName = `use${capitalize(contractName)}${capitalize(
|
|
2315
|
+
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase8(func.name))}`;
|
|
2345
2316
|
const argsType = generateArgsType(func.args);
|
|
2346
2317
|
return `export function ${hookName}() {
|
|
2347
2318
|
const config = useSecondLayerConfig()
|
|
@@ -2358,7 +2329,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2358
2329
|
};
|
|
2359
2330
|
}) => {
|
|
2360
2331
|
const { args, options = {} } = params
|
|
2361
|
-
const contractCallData = ${contractName}.${
|
|
2332
|
+
const contractCallData = ${contractName}.${toCamelCase8(func.name)}(args)
|
|
2362
2333
|
const { contractAddress, contractName: name, functionName, functionArgs } = contractCallData
|
|
2363
2334
|
const network = config.network || 'mainnet'
|
|
2364
2335
|
const contract = \`\${contractAddress}.\${name}\`
|
|
@@ -2383,7 +2354,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2383
2354
|
}
|
|
2384
2355
|
})
|
|
2385
2356
|
|
|
2386
|
-
const ${
|
|
2357
|
+
const ${toCamelCase8(func.name)} = useCallback(async (
|
|
2387
2358
|
args: ${argsType},
|
|
2388
2359
|
options?: {
|
|
2389
2360
|
postConditions?: PostCondition[];
|
|
@@ -2396,7 +2367,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2396
2367
|
}, [mutation])
|
|
2397
2368
|
|
|
2398
2369
|
return {
|
|
2399
|
-
${
|
|
2370
|
+
${toCamelCase8(func.name)},
|
|
2400
2371
|
// Expose mutation state
|
|
2401
2372
|
isPending: mutation.isPending,
|
|
2402
2373
|
isError: mutation.isError,
|
|
@@ -2408,7 +2379,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2408
2379
|
}`;
|
|
2409
2380
|
}
|
|
2410
2381
|
function generateMapHook(map, contractVarName, _address, _contractName) {
|
|
2411
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2382
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase8(map.name))}`;
|
|
2412
2383
|
const keyType = clarityTypeToTS(map.key);
|
|
2413
2384
|
const valueType = clarityTypeToTS(map.value);
|
|
2414
2385
|
return `export function ${hookName}(key: ${keyType}, options?: { enabled?: boolean }) {
|
|
@@ -2417,14 +2388,14 @@ function generateMapHook(map, contractVarName, _address, _contractName) {
|
|
|
2417
2388
|
return useQuery<${valueType} | null>({
|
|
2418
2389
|
queryKey: ['${contractVarName}', '${map.name}', 'map', key, config.network],
|
|
2419
2390
|
queryFn: async () => {
|
|
2420
|
-
return ${contractVarName}.maps.${
|
|
2391
|
+
return ${contractVarName}.maps.${toCamelCase8(map.name)}.get(key, { network: config.network })
|
|
2421
2392
|
},
|
|
2422
2393
|
enabled: options?.enabled ?? true
|
|
2423
2394
|
})
|
|
2424
2395
|
}`;
|
|
2425
2396
|
}
|
|
2426
2397
|
function generateVarHook(variable, contractVarName, _address, _contractName) {
|
|
2427
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2398
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase8(variable.name))}`;
|
|
2428
2399
|
const valueType = clarityTypeToTS(variable.type);
|
|
2429
2400
|
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2430
2401
|
const config = useSecondLayerConfig()
|
|
@@ -2432,14 +2403,14 @@ function generateVarHook(variable, contractVarName, _address, _contractName) {
|
|
|
2432
2403
|
return useQuery<${valueType}>({
|
|
2433
2404
|
queryKey: ['${contractVarName}', '${variable.name}', 'var', config.network],
|
|
2434
2405
|
queryFn: async () => {
|
|
2435
|
-
return ${contractVarName}.vars.${
|
|
2406
|
+
return ${contractVarName}.vars.${toCamelCase8(variable.name)}.get({ network: config.network })
|
|
2436
2407
|
},
|
|
2437
2408
|
enabled: options?.enabled ?? true
|
|
2438
2409
|
})
|
|
2439
2410
|
}`;
|
|
2440
2411
|
}
|
|
2441
2412
|
function generateConstantHook(constant, contractVarName, _address, _contractName) {
|
|
2442
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2413
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase8(constant.name))}`;
|
|
2443
2414
|
const valueType = clarityTypeToTS(constant.type);
|
|
2444
2415
|
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2445
2416
|
const config = useSecondLayerConfig()
|
|
@@ -2447,7 +2418,7 @@ function generateConstantHook(constant, contractVarName, _address, _contractName
|
|
|
2447
2418
|
return useQuery<${valueType}>({
|
|
2448
2419
|
queryKey: ['${contractVarName}', '${constant.name}', 'constant', config.network],
|
|
2449
2420
|
queryFn: async () => {
|
|
2450
|
-
return ${contractVarName}.constants.${
|
|
2421
|
+
return ${contractVarName}.constants.${toCamelCase8(constant.name)}.get({ network: config.network })
|
|
2451
2422
|
},
|
|
2452
2423
|
enabled: options?.enabled ?? true,
|
|
2453
2424
|
staleTime: Infinity // Constants never change
|
|
@@ -2495,13 +2466,8 @@ var react = (options = {}) => {
|
|
|
2495
2466
|
};
|
|
2496
2467
|
// src/plugins/testing/generators.ts
|
|
2497
2468
|
import {
|
|
2498
|
-
toCamelCase as toCamelCase8,
|
|
2499
2469
|
isAbiTuple as isAbiTuple4
|
|
2500
2470
|
} from "@secondlayer/stacks/clarity";
|
|
2501
|
-
function toPascalCase(str) {
|
|
2502
|
-
const camel = toCamelCase8(str);
|
|
2503
|
-
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
2504
|
-
}
|
|
2505
2471
|
function generatePublicFunction(func, contractId) {
|
|
2506
2472
|
const methodName = toCamelCase8(func.name);
|
|
2507
2473
|
const argsSignature = generateArgsSignature(func.args);
|
|
@@ -2757,5 +2723,5 @@ export {
|
|
|
2757
2723
|
PluginManager
|
|
2758
2724
|
};
|
|
2759
2725
|
|
|
2760
|
-
//# debugId=
|
|
2726
|
+
//# debugId=184A6BEF0A21821664756E2164756E21
|
|
2761
2727
|
//# sourceMappingURL=index.js.map
|