@secondlayer/cli 1.4.1 → 1.5.1
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 +172 -164
- package/dist/cli.js.map +14 -13
- package/dist/index.d.ts +6 -1
- package/dist/index.js +81 -104
- package/dist/index.js.map +8 -7
- package/dist/plugin-manager.d.ts +6 -1
- package/dist/plugin-manager.js +29 -52
- package/dist/plugin-manager.js.map +4 -4
- package/package.json +5 -5
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
|
@@ -82,6 +82,8 @@ var init_format = () => {};
|
|
|
82
82
|
import { promises as fs } from "fs";
|
|
83
83
|
import path from "path";
|
|
84
84
|
import { isValidAddress as _validateStacksAddress } from "@secondlayer/stacks";
|
|
85
|
+
import { getErrorMessage } from "@secondlayer/shared";
|
|
86
|
+
import { toCamelCase } from "@secondlayer/stacks/clarity";
|
|
85
87
|
|
|
86
88
|
// src/utils/contract-id.ts
|
|
87
89
|
function parseContractId(contractId) {
|
|
@@ -147,12 +149,11 @@ class PluginManager {
|
|
|
147
149
|
success: true
|
|
148
150
|
});
|
|
149
151
|
} catch (error) {
|
|
150
|
-
const err = error;
|
|
151
152
|
this.recordHookResult(plugin.name, "transformConfig", {
|
|
152
153
|
success: false,
|
|
153
|
-
error:
|
|
154
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
154
155
|
});
|
|
155
|
-
throw new Error(`Plugin "${plugin.name}" failed during config transformation: ${
|
|
156
|
+
throw new Error(`Plugin "${plugin.name}" failed during config transformation: ${getErrorMessage(error)}`);
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
159
|
}
|
|
@@ -166,31 +167,11 @@ class PluginManager {
|
|
|
166
167
|
const processedContracts = [];
|
|
167
168
|
for (let contract of contracts) {
|
|
168
169
|
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);
|
|
170
|
+
processedContracts.push(this.contractToProcessed(contract, "clarinet"));
|
|
180
171
|
continue;
|
|
181
172
|
}
|
|
182
173
|
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);
|
|
174
|
+
processedContracts.push(this.contractToProcessed(contract, "direct"));
|
|
194
175
|
continue;
|
|
195
176
|
}
|
|
196
177
|
for (const plugin of this.plugins) {
|
|
@@ -202,27 +183,16 @@ class PluginManager {
|
|
|
202
183
|
success: true
|
|
203
184
|
});
|
|
204
185
|
} catch (error) {
|
|
205
|
-
const err = error;
|
|
206
186
|
this.recordHookResult(plugin.name, "transformContract", {
|
|
207
187
|
success: false,
|
|
208
|
-
error:
|
|
188
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
209
189
|
});
|
|
210
|
-
this.logger.warn(`Plugin "${plugin.name}" failed to transform contract: ${
|
|
190
|
+
this.logger.warn(`Plugin "${plugin.name}" failed to transform contract: ${getErrorMessage(error)}`);
|
|
211
191
|
}
|
|
212
192
|
}
|
|
213
193
|
}
|
|
214
194
|
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);
|
|
195
|
+
processedContracts.push(this.contractToProcessed(contract, "api"));
|
|
226
196
|
}
|
|
227
197
|
}
|
|
228
198
|
return processedContracts;
|
|
@@ -238,12 +208,11 @@ class PluginManager {
|
|
|
238
208
|
success: true
|
|
239
209
|
});
|
|
240
210
|
} catch (error) {
|
|
241
|
-
const err = error;
|
|
242
211
|
this.recordHookResult(plugin.name, hookName, {
|
|
243
212
|
success: false,
|
|
244
|
-
error:
|
|
213
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
245
214
|
});
|
|
246
|
-
this.logger.error(`Plugin "${plugin.name}" failed during ${hookName}: ${
|
|
215
|
+
this.logger.error(`Plugin "${plugin.name}" failed during ${hookName}: ${getErrorMessage(error)}`);
|
|
247
216
|
}
|
|
248
217
|
}
|
|
249
218
|
}
|
|
@@ -283,12 +252,11 @@ class PluginManager {
|
|
|
283
252
|
success: true
|
|
284
253
|
});
|
|
285
254
|
} catch (error) {
|
|
286
|
-
const err = error;
|
|
287
255
|
this.recordHookResult(plugin.name, "transformOutput", {
|
|
288
256
|
success: false,
|
|
289
|
-
error:
|
|
257
|
+
error: error instanceof Error ? error : new Error(getErrorMessage(error))
|
|
290
258
|
});
|
|
291
|
-
this.logger.warn(`Plugin "${plugin.name}" failed to transform output: ${
|
|
259
|
+
this.logger.warn(`Plugin "${plugin.name}" failed to transform output: ${getErrorMessage(error)}`);
|
|
292
260
|
}
|
|
293
261
|
}
|
|
294
262
|
}
|
|
@@ -306,15 +274,26 @@ class PluginManager {
|
|
|
306
274
|
await this.utils.ensureDir(path.dirname(resolvedPath));
|
|
307
275
|
await this.utils.writeFile(resolvedPath, output.content);
|
|
308
276
|
} catch (error) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
throw err;
|
|
277
|
+
this.logger.error(`Failed to write ${output.path}: ${getErrorMessage(error)}`);
|
|
278
|
+
throw error;
|
|
312
279
|
}
|
|
313
280
|
}
|
|
314
281
|
}
|
|
315
282
|
getExecutionResults() {
|
|
316
283
|
return new Map(this.executionContext.results);
|
|
317
284
|
}
|
|
285
|
+
contractToProcessed(contract, source) {
|
|
286
|
+
const address = typeof contract.address === "string" ? contract.address : "";
|
|
287
|
+
const parsed = parseContractId(address);
|
|
288
|
+
return {
|
|
289
|
+
name: contract.name || parsed.contractName || "unknown",
|
|
290
|
+
address: parsed.address || "unknown",
|
|
291
|
+
contractName: parsed.contractName || contract.name || "unknown",
|
|
292
|
+
abi: contract.abi,
|
|
293
|
+
source: source === "api" ? "api" : "local",
|
|
294
|
+
metadata: contract.metadata ?? { source }
|
|
295
|
+
};
|
|
296
|
+
}
|
|
318
297
|
augmentOutput(outputs, outputKey, contractName, content) {
|
|
319
298
|
const existing = outputs.get(outputKey);
|
|
320
299
|
if (!existing) {
|
|
@@ -351,9 +330,7 @@ ${JSON.stringify(content, null, 2)}`;
|
|
|
351
330
|
}
|
|
352
331
|
createUtils() {
|
|
353
332
|
return {
|
|
354
|
-
toCamelCase
|
|
355
|
-
return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
356
|
-
},
|
|
333
|
+
toCamelCase,
|
|
357
334
|
toKebabCase: (str) => {
|
|
358
335
|
return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
359
336
|
},
|
|
@@ -392,17 +369,17 @@ ${JSON.stringify(content, null, 2)}`;
|
|
|
392
369
|
}
|
|
393
370
|
// src/plugins/clarinet/index.ts
|
|
394
371
|
import { initSimnet } from "@hirosystems/clarinet-sdk";
|
|
395
|
-
import { toCamelCase as
|
|
372
|
+
import { toCamelCase as toCamelCase6 } from "@secondlayer/stacks/clarity";
|
|
396
373
|
|
|
397
374
|
// src/generators/contract.ts
|
|
398
375
|
init_format();
|
|
399
376
|
import {
|
|
400
|
-
toCamelCase as
|
|
377
|
+
toCamelCase as toCamelCase5
|
|
401
378
|
} from "@secondlayer/stacks/clarity";
|
|
402
379
|
|
|
403
380
|
// src/utils/type-mapping.ts
|
|
404
381
|
import {
|
|
405
|
-
toCamelCase,
|
|
382
|
+
toCamelCase as toCamelCase2,
|
|
406
383
|
isAbiList,
|
|
407
384
|
isAbiTuple,
|
|
408
385
|
isAbiOptional,
|
|
@@ -461,7 +438,7 @@ function clarityTypeToTS(type) {
|
|
|
461
438
|
return `${innerType}[]`;
|
|
462
439
|
}
|
|
463
440
|
if (isAbiTuple(type)) {
|
|
464
|
-
const fields = type.tuple.map((field) => `${
|
|
441
|
+
const fields = type.tuple.map((field) => `${toCamelCase2(field.name)}: ${clarityTypeToTS(field.type)}`).join("; ");
|
|
465
442
|
return `{ ${fields} }`;
|
|
466
443
|
}
|
|
467
444
|
if (isAbiResponse(type)) {
|
|
@@ -477,7 +454,7 @@ function getTypeForArg(arg) {
|
|
|
477
454
|
|
|
478
455
|
// src/utils/clarity-conversion.ts
|
|
479
456
|
import {
|
|
480
|
-
toCamelCase as
|
|
457
|
+
toCamelCase as toCamelCase3,
|
|
481
458
|
isAbiStringAscii as isAbiStringAscii2,
|
|
482
459
|
isAbiStringUtf8 as isAbiStringUtf82,
|
|
483
460
|
isAbiBuffer as isAbiBuffer2,
|
|
@@ -575,7 +552,7 @@ function generateClarityConversion(argName, argType) {
|
|
|
575
552
|
const requiredFields = type.tuple.map((f) => f.name);
|
|
576
553
|
const fieldNames = JSON.stringify(requiredFields);
|
|
577
554
|
const fields = type.tuple.map((field) => {
|
|
578
|
-
const camelFieldName =
|
|
555
|
+
const camelFieldName = toCamelCase3(field.name);
|
|
579
556
|
const fieldConversion = generateClarityConversion(`tupleValue.${camelFieldName}`, { type: field.type });
|
|
580
557
|
return `"${field.name}": ${fieldConversion}`;
|
|
581
558
|
}).join(", ");
|
|
@@ -615,12 +592,12 @@ function generateClarityConversion(argName, argType) {
|
|
|
615
592
|
}
|
|
616
593
|
|
|
617
594
|
// src/utils/generator-helpers.ts
|
|
618
|
-
import { toCamelCase as
|
|
595
|
+
import { toCamelCase as toCamelCase4, isAbiTuple as isAbiTuple3 } from "@secondlayer/stacks/clarity";
|
|
619
596
|
function generateArgsSignature(args) {
|
|
620
597
|
if (args.length === 0)
|
|
621
598
|
return "";
|
|
622
599
|
const argsTypes = args.map((arg) => {
|
|
623
|
-
const camelName =
|
|
600
|
+
const camelName = toCamelCase4(arg.name);
|
|
624
601
|
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
625
602
|
}).join("; ");
|
|
626
603
|
return `args: { ${argsTypes} }, `;
|
|
@@ -629,14 +606,14 @@ function generateClarityArgs(args) {
|
|
|
629
606
|
if (args.length === 0)
|
|
630
607
|
return "";
|
|
631
608
|
return args.map((arg) => {
|
|
632
|
-
const argName = `args.${
|
|
609
|
+
const argName = `args.${toCamelCase4(arg.name)}`;
|
|
633
610
|
return generateClarityConversion(argName, arg);
|
|
634
611
|
}).join(", ");
|
|
635
612
|
}
|
|
636
613
|
function generateMapKeyConversion(keyType) {
|
|
637
614
|
if (isAbiTuple3(keyType)) {
|
|
638
615
|
const fields = keyType.tuple.map((field) => {
|
|
639
|
-
const camelFieldName =
|
|
616
|
+
const camelFieldName = toCamelCase4(field.name);
|
|
640
617
|
const fieldConversion = generateClarityConversion(`key.${camelFieldName}`, { type: field.type });
|
|
641
618
|
return `"${field.name}": ${fieldConversion}`;
|
|
642
619
|
}).join(", ");
|
|
@@ -736,7 +713,7 @@ function generateAbiConstant(name, abi) {
|
|
|
736
713
|
return `export const ${name}Abi = ${abiJson} as const`;
|
|
737
714
|
}
|
|
738
715
|
function generateMethod(func, address, contractName) {
|
|
739
|
-
const methodName =
|
|
716
|
+
const methodName = toCamelCase5(func.name);
|
|
740
717
|
if (func.args.length === 0) {
|
|
741
718
|
return `${methodName}() {
|
|
742
719
|
return {
|
|
@@ -749,7 +726,7 @@ function generateMethod(func, address, contractName) {
|
|
|
749
726
|
}
|
|
750
727
|
if (func.args.length === 1) {
|
|
751
728
|
const originalArgName = func.args[0].name;
|
|
752
|
-
const argName =
|
|
729
|
+
const argName = toCamelCase5(originalArgName);
|
|
753
730
|
const argType = getTypeForArg(func.args[0]);
|
|
754
731
|
const clarityConversion = generateClarityConversion(argName, func.args[0]);
|
|
755
732
|
return `${methodName}(...args: [{ ${argName}: ${argType} }] | [${argType}]) {
|
|
@@ -765,17 +742,17 @@ function generateMethod(func, address, contractName) {
|
|
|
765
742
|
}
|
|
766
743
|
}`;
|
|
767
744
|
}
|
|
768
|
-
const argsList = func.args.map((arg) =>
|
|
745
|
+
const argsList = func.args.map((arg) => toCamelCase5(arg.name)).join(", ");
|
|
769
746
|
const argsTypes = func.args.map((arg) => {
|
|
770
|
-
const camelName =
|
|
747
|
+
const camelName = toCamelCase5(arg.name);
|
|
771
748
|
return `${camelName}: ${getTypeForArg(arg)}`;
|
|
772
749
|
}).join("; ");
|
|
773
750
|
const argsArray = func.args.map((arg) => {
|
|
774
|
-
const argName =
|
|
751
|
+
const argName = toCamelCase5(arg.name);
|
|
775
752
|
return generateClarityConversion(argName, arg);
|
|
776
753
|
}).join(", ");
|
|
777
754
|
const objectAccess = func.args.map((arg) => {
|
|
778
|
-
const camelName =
|
|
755
|
+
const camelName = toCamelCase5(arg.name);
|
|
779
756
|
return `args[0].${camelName}`;
|
|
780
757
|
}).join(", ");
|
|
781
758
|
const positionTypes = func.args.map((arg) => getTypeForArg(arg)).join(", ");
|
|
@@ -797,7 +774,7 @@ function generateMapsObject(maps, address, contractName) {
|
|
|
797
774
|
return "";
|
|
798
775
|
}
|
|
799
776
|
const mapMethods = maps.map((map) => {
|
|
800
|
-
const methodName =
|
|
777
|
+
const methodName = toCamelCase5(map.name);
|
|
801
778
|
const keyType = getTypeForArg({ type: map.key });
|
|
802
779
|
const valueType = getTypeForArg({ type: map.value });
|
|
803
780
|
const keyConversion = generateMapKeyConversion(map.key);
|
|
@@ -858,7 +835,7 @@ function generateVarsObject(variables, address, contractName) {
|
|
|
858
835
|
return "";
|
|
859
836
|
}
|
|
860
837
|
const varMethods = dataVars.map((variable) => {
|
|
861
|
-
const methodName =
|
|
838
|
+
const methodName = toCamelCase5(variable.name);
|
|
862
839
|
const valueType = getTypeForArg({ type: variable.type });
|
|
863
840
|
return `${methodName}: {
|
|
864
841
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -903,7 +880,7 @@ function generateConstantsObject(variables, address, contractName) {
|
|
|
903
880
|
return "";
|
|
904
881
|
}
|
|
905
882
|
const constMethods = constants.map((constant) => {
|
|
906
|
-
const methodName =
|
|
883
|
+
const methodName = toCamelCase5(constant.name);
|
|
907
884
|
const valueType = getTypeForArg({ type: constant.type });
|
|
908
885
|
return `${methodName}: {
|
|
909
886
|
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
@@ -1095,7 +1072,7 @@ function normalizeAbi(abi) {
|
|
|
1095
1072
|
|
|
1096
1073
|
// src/plugins/clarinet/index.ts
|
|
1097
1074
|
function sanitizeContractName(name) {
|
|
1098
|
-
return
|
|
1075
|
+
return toCamelCase6(name);
|
|
1099
1076
|
}
|
|
1100
1077
|
async function isUserDefinedContract(contractId, manifestPath) {
|
|
1101
1078
|
const { address, contractName } = parseContractId(contractId);
|
|
@@ -1205,7 +1182,7 @@ async function hasClarinetProject(path2 = "./Clarinet.toml") {
|
|
|
1205
1182
|
}
|
|
1206
1183
|
}
|
|
1207
1184
|
// src/plugins/actions/generators.ts
|
|
1208
|
-
import { toCamelCase as
|
|
1185
|
+
import { toCamelCase as toCamelCase7 } from "@secondlayer/stacks/clarity";
|
|
1209
1186
|
function generateReadHelpers(contract, options) {
|
|
1210
1187
|
const { abi } = contract;
|
|
1211
1188
|
const functions = abi.functions || [];
|
|
@@ -1226,7 +1203,7 @@ function generateReadHelpers(contract, options) {
|
|
|
1226
1203
|
return "";
|
|
1227
1204
|
}
|
|
1228
1205
|
const helpers = filteredFunctions.map((func) => {
|
|
1229
|
-
const methodName =
|
|
1206
|
+
const methodName = toCamelCase7(func.name);
|
|
1230
1207
|
const argsSignature = generateArgsSignature(func.args);
|
|
1231
1208
|
const clarityArgs = generateClarityArgs(func.args);
|
|
1232
1209
|
return `async ${methodName}(${argsSignature}options?: {
|
|
@@ -1270,7 +1247,7 @@ function generateWriteHelpers(contract, options) {
|
|
|
1270
1247
|
return "";
|
|
1271
1248
|
}
|
|
1272
1249
|
const helpers = filteredFunctions.map((func) => {
|
|
1273
|
-
const methodName =
|
|
1250
|
+
const methodName = toCamelCase7(func.name);
|
|
1274
1251
|
const argsSignature = generateArgsSignature(func.args);
|
|
1275
1252
|
const clarityArgs = generateClarityArgs(func.args);
|
|
1276
1253
|
return `async ${methodName}(${argsSignature}senderKey?: string, options?: {
|
|
@@ -2208,31 +2185,36 @@ function generateGenericHook(hookName) {
|
|
|
2208
2185
|
// src/plugins/react/generators/contract.ts
|
|
2209
2186
|
init_format();
|
|
2210
2187
|
|
|
2211
|
-
// src/
|
|
2212
|
-
import { toCamelCase as
|
|
2188
|
+
// src/utils/case-conversion.ts
|
|
2189
|
+
import { toCamelCase as toCamelCase8 } from "@secondlayer/stacks/clarity";
|
|
2213
2190
|
function capitalize(str) {
|
|
2214
2191
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2215
2192
|
}
|
|
2193
|
+
function toPascalCase(str) {
|
|
2194
|
+
return capitalize(toCamelCase8(str));
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
// src/plugins/react/generators/utils.ts
|
|
2216
2198
|
function generateHookArgsSignature(args) {
|
|
2217
2199
|
if (args.length === 0)
|
|
2218
2200
|
return "";
|
|
2219
|
-
const argsList = args.map((arg) => `${
|
|
2201
|
+
const argsList = args.map((arg) => `${toCamelCase8(arg.name)}: ${clarityTypeToTS(arg.type)}`).join(", ");
|
|
2220
2202
|
return `${argsList}`;
|
|
2221
2203
|
}
|
|
2222
2204
|
function generateArgsType(args) {
|
|
2223
2205
|
if (args.length === 0)
|
|
2224
2206
|
return "void";
|
|
2225
|
-
const argsList = args.map((arg) => `${
|
|
2207
|
+
const argsList = args.map((arg) => `${toCamelCase8(arg.name)}: ${clarityTypeToTS(arg.type)}`).join("; ");
|
|
2226
2208
|
return `{ ${argsList} }`;
|
|
2227
2209
|
}
|
|
2228
2210
|
function generateArgNames(args) {
|
|
2229
2211
|
if (args.length === 0)
|
|
2230
2212
|
return "";
|
|
2231
|
-
return args.map((arg) =>
|
|
2213
|
+
return args.map((arg) => toCamelCase8(arg.name)).join(", ");
|
|
2232
2214
|
}
|
|
2233
2215
|
function generateEnabledCondition(args) {
|
|
2234
2216
|
return args.map((arg) => {
|
|
2235
|
-
const camelName =
|
|
2217
|
+
const camelName = toCamelCase8(arg.name);
|
|
2236
2218
|
const type = clarityTypeToTS(arg.type);
|
|
2237
2219
|
if (type === "string")
|
|
2238
2220
|
return `!!${camelName}`;
|
|
@@ -2244,7 +2226,7 @@ function generateEnabledCondition(args) {
|
|
|
2244
2226
|
function generateObjectArgs(args) {
|
|
2245
2227
|
if (args.length === 0)
|
|
2246
2228
|
return "";
|
|
2247
|
-
return args.map((arg) => `${arg.name}: ${
|
|
2229
|
+
return args.map((arg) => `${arg.name}: ${toCamelCase8(arg.name)}`).join(", ");
|
|
2248
2230
|
}
|
|
2249
2231
|
|
|
2250
2232
|
// src/plugins/react/generators/contract.ts
|
|
@@ -2277,21 +2259,21 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2277
2259
|
const readOnlyFunctions = functions.filter((f) => f.access === "read-only");
|
|
2278
2260
|
const publicFunctions = functions.filter((f) => f.access === "public");
|
|
2279
2261
|
const readHooks = readOnlyFunctions.map((func) => {
|
|
2280
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2262
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(func.name))}`;
|
|
2281
2263
|
if (excludeList.includes(hookName)) {
|
|
2282
2264
|
return null;
|
|
2283
2265
|
}
|
|
2284
2266
|
return generateReadHook(func, name);
|
|
2285
2267
|
}).filter(Boolean);
|
|
2286
2268
|
const writeHooks = publicFunctions.map((func) => {
|
|
2287
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2269
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(func.name))}`;
|
|
2288
2270
|
if (excludeList.includes(hookName)) {
|
|
2289
2271
|
return null;
|
|
2290
2272
|
}
|
|
2291
2273
|
return generateWriteHook(func, name);
|
|
2292
2274
|
}).filter(Boolean);
|
|
2293
2275
|
const mapHooks = maps.map((map) => {
|
|
2294
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2276
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(map.name))}`;
|
|
2295
2277
|
if (excludeList.includes(hookName)) {
|
|
2296
2278
|
return null;
|
|
2297
2279
|
}
|
|
@@ -2299,7 +2281,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2299
2281
|
}).filter(Boolean);
|
|
2300
2282
|
const dataVars = variables.filter((v) => v.access === "variable");
|
|
2301
2283
|
const varHooks = dataVars.map((variable) => {
|
|
2302
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2284
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(variable.name))}`;
|
|
2303
2285
|
if (excludeList.includes(hookName)) {
|
|
2304
2286
|
return null;
|
|
2305
2287
|
}
|
|
@@ -2307,7 +2289,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2307
2289
|
}).filter(Boolean);
|
|
2308
2290
|
const constants = variables.filter((v) => v.access === "constant");
|
|
2309
2291
|
const constantHooks = constants.map((constant) => {
|
|
2310
|
-
const hookName = `use${capitalize(name)}${capitalize(
|
|
2292
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase8(constant.name))}`;
|
|
2311
2293
|
if (excludeList.includes(hookName)) {
|
|
2312
2294
|
return null;
|
|
2313
2295
|
}
|
|
@@ -2322,7 +2304,7 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2322
2304
|
`);
|
|
2323
2305
|
}
|
|
2324
2306
|
function generateReadHook(func, contractName) {
|
|
2325
|
-
const hookName = `use${capitalize(contractName)}${capitalize(
|
|
2307
|
+
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase8(func.name))}`;
|
|
2326
2308
|
const argsSignature = generateHookArgsSignature(func.args);
|
|
2327
2309
|
const enabledParam = func.args.length > 0 ? ", options?: { enabled?: boolean }" : "options?: { enabled?: boolean }";
|
|
2328
2310
|
const returnType = clarityTypeToTS(func.outputs);
|
|
@@ -2331,7 +2313,7 @@ function generateReadHook(func, contractName) {
|
|
|
2331
2313
|
|
|
2332
2314
|
return useQuery<${returnType}>({
|
|
2333
2315
|
queryKey: ['${func.name}', ${contractName}.address, ${generateArgNames(func.args)}],
|
|
2334
|
-
queryFn: () => ${contractName}.read.${
|
|
2316
|
+
queryFn: () => ${contractName}.read.${toCamelCase8(func.name)}(${generateArgNames(func.args) ? `{ ${generateObjectArgs(func.args)} }, ` : ""}{
|
|
2335
2317
|
network: config.network,
|
|
2336
2318
|
senderAddress: config.senderAddress || 'SP000000000000000000002Q6VF78'
|
|
2337
2319
|
}),
|
|
@@ -2341,7 +2323,7 @@ function generateReadHook(func, contractName) {
|
|
|
2341
2323
|
}`;
|
|
2342
2324
|
}
|
|
2343
2325
|
function generateWriteHook(func, contractName) {
|
|
2344
|
-
const hookName = `use${capitalize(contractName)}${capitalize(
|
|
2326
|
+
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase8(func.name))}`;
|
|
2345
2327
|
const argsType = generateArgsType(func.args);
|
|
2346
2328
|
return `export function ${hookName}() {
|
|
2347
2329
|
const config = useSecondLayerConfig()
|
|
@@ -2358,7 +2340,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2358
2340
|
};
|
|
2359
2341
|
}) => {
|
|
2360
2342
|
const { args, options = {} } = params
|
|
2361
|
-
const contractCallData = ${contractName}.${
|
|
2343
|
+
const contractCallData = ${contractName}.${toCamelCase8(func.name)}(args)
|
|
2362
2344
|
const { contractAddress, contractName: name, functionName, functionArgs } = contractCallData
|
|
2363
2345
|
const network = config.network || 'mainnet'
|
|
2364
2346
|
const contract = \`\${contractAddress}.\${name}\`
|
|
@@ -2383,7 +2365,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2383
2365
|
}
|
|
2384
2366
|
})
|
|
2385
2367
|
|
|
2386
|
-
const ${
|
|
2368
|
+
const ${toCamelCase8(func.name)} = useCallback(async (
|
|
2387
2369
|
args: ${argsType},
|
|
2388
2370
|
options?: {
|
|
2389
2371
|
postConditions?: PostCondition[];
|
|
@@ -2396,7 +2378,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2396
2378
|
}, [mutation])
|
|
2397
2379
|
|
|
2398
2380
|
return {
|
|
2399
|
-
${
|
|
2381
|
+
${toCamelCase8(func.name)},
|
|
2400
2382
|
// Expose mutation state
|
|
2401
2383
|
isPending: mutation.isPending,
|
|
2402
2384
|
isError: mutation.isError,
|
|
@@ -2408,7 +2390,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2408
2390
|
}`;
|
|
2409
2391
|
}
|
|
2410
2392
|
function generateMapHook(map, contractVarName, _address, _contractName) {
|
|
2411
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2393
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase8(map.name))}`;
|
|
2412
2394
|
const keyType = clarityTypeToTS(map.key);
|
|
2413
2395
|
const valueType = clarityTypeToTS(map.value);
|
|
2414
2396
|
return `export function ${hookName}(key: ${keyType}, options?: { enabled?: boolean }) {
|
|
@@ -2417,14 +2399,14 @@ function generateMapHook(map, contractVarName, _address, _contractName) {
|
|
|
2417
2399
|
return useQuery<${valueType} | null>({
|
|
2418
2400
|
queryKey: ['${contractVarName}', '${map.name}', 'map', key, config.network],
|
|
2419
2401
|
queryFn: async () => {
|
|
2420
|
-
return ${contractVarName}.maps.${
|
|
2402
|
+
return ${contractVarName}.maps.${toCamelCase8(map.name)}.get(key, { network: config.network })
|
|
2421
2403
|
},
|
|
2422
2404
|
enabled: options?.enabled ?? true
|
|
2423
2405
|
})
|
|
2424
2406
|
}`;
|
|
2425
2407
|
}
|
|
2426
2408
|
function generateVarHook(variable, contractVarName, _address, _contractName) {
|
|
2427
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2409
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase8(variable.name))}`;
|
|
2428
2410
|
const valueType = clarityTypeToTS(variable.type);
|
|
2429
2411
|
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2430
2412
|
const config = useSecondLayerConfig()
|
|
@@ -2432,14 +2414,14 @@ function generateVarHook(variable, contractVarName, _address, _contractName) {
|
|
|
2432
2414
|
return useQuery<${valueType}>({
|
|
2433
2415
|
queryKey: ['${contractVarName}', '${variable.name}', 'var', config.network],
|
|
2434
2416
|
queryFn: async () => {
|
|
2435
|
-
return ${contractVarName}.vars.${
|
|
2417
|
+
return ${contractVarName}.vars.${toCamelCase8(variable.name)}.get({ network: config.network })
|
|
2436
2418
|
},
|
|
2437
2419
|
enabled: options?.enabled ?? true
|
|
2438
2420
|
})
|
|
2439
2421
|
}`;
|
|
2440
2422
|
}
|
|
2441
2423
|
function generateConstantHook(constant, contractVarName, _address, _contractName) {
|
|
2442
|
-
const hookName = `use${capitalize(contractVarName)}${capitalize(
|
|
2424
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase8(constant.name))}`;
|
|
2443
2425
|
const valueType = clarityTypeToTS(constant.type);
|
|
2444
2426
|
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2445
2427
|
const config = useSecondLayerConfig()
|
|
@@ -2447,7 +2429,7 @@ function generateConstantHook(constant, contractVarName, _address, _contractName
|
|
|
2447
2429
|
return useQuery<${valueType}>({
|
|
2448
2430
|
queryKey: ['${contractVarName}', '${constant.name}', 'constant', config.network],
|
|
2449
2431
|
queryFn: async () => {
|
|
2450
|
-
return ${contractVarName}.constants.${
|
|
2432
|
+
return ${contractVarName}.constants.${toCamelCase8(constant.name)}.get({ network: config.network })
|
|
2451
2433
|
},
|
|
2452
2434
|
enabled: options?.enabled ?? true,
|
|
2453
2435
|
staleTime: Infinity // Constants never change
|
|
@@ -2495,13 +2477,8 @@ var react = (options = {}) => {
|
|
|
2495
2477
|
};
|
|
2496
2478
|
// src/plugins/testing/generators.ts
|
|
2497
2479
|
import {
|
|
2498
|
-
toCamelCase as toCamelCase8,
|
|
2499
2480
|
isAbiTuple as isAbiTuple4
|
|
2500
2481
|
} from "@secondlayer/stacks/clarity";
|
|
2501
|
-
function toPascalCase(str) {
|
|
2502
|
-
const camel = toCamelCase8(str);
|
|
2503
|
-
return camel.charAt(0).toUpperCase() + camel.slice(1);
|
|
2504
|
-
}
|
|
2505
2482
|
function generatePublicFunction(func, contractId) {
|
|
2506
2483
|
const methodName = toCamelCase8(func.name);
|
|
2507
2484
|
const argsSignature = generateArgsSignature(func.args);
|
|
@@ -2757,5 +2734,5 @@ export {
|
|
|
2757
2734
|
PluginManager
|
|
2758
2735
|
};
|
|
2759
2736
|
|
|
2760
|
-
//# debugId=
|
|
2737
|
+
//# debugId=76F0203451B4697D64756E2164756E21
|
|
2761
2738
|
//# sourceMappingURL=index.js.map
|