@secondlayer/cli 0.2.5 → 0.3.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 +188 -6
- package/dist/cli.js.map +4 -4
- package/dist/index.js +288 -39
- package/dist/index.js.map +8 -8
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -345,12 +345,18 @@ function generateContract(contract) {
|
|
|
345
345
|
const methods = abi.functions.filter((func) => func.access !== "private").map((func) => generateMethod(func, address, contractName)).join(`,
|
|
346
346
|
|
|
347
347
|
`);
|
|
348
|
+
const mapsObject = generateMapsObject(abi.maps || [], address, contractName);
|
|
349
|
+
const varsObject = generateVarsObject(abi.variables || [], address, contractName);
|
|
350
|
+
const constantsObject = generateConstantsObject(abi.variables || [], address, contractName);
|
|
351
|
+
const allMembers = [methods, mapsObject, varsObject, constantsObject].filter(Boolean);
|
|
348
352
|
const contractCode = `export const ${name} = {
|
|
349
353
|
address: '${address}',
|
|
350
354
|
contractAddress: '${address}',
|
|
351
355
|
contractName: '${contractName}',
|
|
352
|
-
|
|
353
|
-
${
|
|
356
|
+
|
|
357
|
+
${allMembers.join(`,
|
|
358
|
+
|
|
359
|
+
`)}
|
|
354
360
|
} as const`;
|
|
355
361
|
return `${abiCode}
|
|
356
362
|
|
|
@@ -561,6 +567,158 @@ function generateClarityConversion(argName, argType) {
|
|
|
561
567
|
}
|
|
562
568
|
return `${argName}`;
|
|
563
569
|
}
|
|
570
|
+
function generateMapsObject(maps, address, contractName) {
|
|
571
|
+
if (!maps || maps.length === 0) {
|
|
572
|
+
return "";
|
|
573
|
+
}
|
|
574
|
+
const mapMethods = maps.map((map) => {
|
|
575
|
+
const methodName = toCamelCase(map.name);
|
|
576
|
+
const keyType = getTypeForArg({ type: map.key });
|
|
577
|
+
const valueType = getTypeForArg({ type: map.value });
|
|
578
|
+
const keyConversion = generateMapKeyConversion(map.key);
|
|
579
|
+
return `${methodName}: {
|
|
580
|
+
async get(key: ${keyType}, options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType} | null> {
|
|
581
|
+
const { cvToJSON, serializeCV } = await import('@stacks/transactions');
|
|
582
|
+
const apiUrls: Record<string, string> = {
|
|
583
|
+
mainnet: 'https://api.hiro.so',
|
|
584
|
+
testnet: 'https://api.testnet.hiro.so',
|
|
585
|
+
devnet: 'http://localhost:3999'
|
|
586
|
+
};
|
|
587
|
+
const baseUrl = apiUrls[options?.network || 'mainnet'];
|
|
588
|
+
const mapKey = ${keyConversion};
|
|
589
|
+
const keyHex = serializeCV(mapKey).toString('hex');
|
|
590
|
+
|
|
591
|
+
const response = await fetch(
|
|
592
|
+
\`\${baseUrl}/v2/map_entry/${address}/${contractName}/${map.name}\`,
|
|
593
|
+
{
|
|
594
|
+
method: 'POST',
|
|
595
|
+
headers: { 'Content-Type': 'application/json' },
|
|
596
|
+
body: JSON.stringify(keyHex)
|
|
597
|
+
}
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
if (!response.ok) {
|
|
601
|
+
throw new Error(\`Failed to fetch map entry: \${response.statusText}\`);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const result = await response.json();
|
|
605
|
+
if (!result.data || result.data === '0x09') {
|
|
606
|
+
return null; // none value
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
const { deserializeCV } = await import('@stacks/transactions');
|
|
610
|
+
const cv = deserializeCV(result.data);
|
|
611
|
+
const parsed = cvToJSON(cv);
|
|
612
|
+
// Unwrap the (some ...) wrapper
|
|
613
|
+
return parsed.value?.value ?? parsed.value ?? null;
|
|
614
|
+
},
|
|
615
|
+
keyType: ${JSON.stringify(map.key)} as const,
|
|
616
|
+
valueType: ${JSON.stringify(map.value)} as const
|
|
617
|
+
}`;
|
|
618
|
+
});
|
|
619
|
+
return `maps: {
|
|
620
|
+
${mapMethods.join(`,
|
|
621
|
+
|
|
622
|
+
`)}
|
|
623
|
+
}`;
|
|
624
|
+
}
|
|
625
|
+
function generateVarsObject(variables, address, contractName) {
|
|
626
|
+
if (!variables || variables.length === 0) {
|
|
627
|
+
return "";
|
|
628
|
+
}
|
|
629
|
+
const dataVars = variables.filter((v) => v.access === "variable");
|
|
630
|
+
if (dataVars.length === 0) {
|
|
631
|
+
return "";
|
|
632
|
+
}
|
|
633
|
+
const varMethods = dataVars.map((variable) => {
|
|
634
|
+
const methodName = toCamelCase(variable.name);
|
|
635
|
+
const valueType = getTypeForArg({ type: variable.type });
|
|
636
|
+
return `${methodName}: {
|
|
637
|
+
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
638
|
+
const { cvToJSON, deserializeCV } = await import('@stacks/transactions');
|
|
639
|
+
const apiUrls: Record<string, string> = {
|
|
640
|
+
mainnet: 'https://api.hiro.so',
|
|
641
|
+
testnet: 'https://api.testnet.hiro.so',
|
|
642
|
+
devnet: 'http://localhost:3999'
|
|
643
|
+
};
|
|
644
|
+
const baseUrl = apiUrls[options?.network || 'mainnet'];
|
|
645
|
+
|
|
646
|
+
const response = await fetch(
|
|
647
|
+
\`\${baseUrl}/v2/data_var/${address}/${contractName}/${variable.name}?proof=0\`
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
if (!response.ok) {
|
|
651
|
+
throw new Error(\`Failed to fetch data var: \${response.statusText}\`);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const result = await response.json();
|
|
655
|
+
const cv = deserializeCV(result.data);
|
|
656
|
+
const parsed = cvToJSON(cv);
|
|
657
|
+
return parsed.value ?? parsed;
|
|
658
|
+
},
|
|
659
|
+
type: ${JSON.stringify(variable.type)} as const
|
|
660
|
+
}`;
|
|
661
|
+
});
|
|
662
|
+
return `vars: {
|
|
663
|
+
${varMethods.join(`,
|
|
664
|
+
|
|
665
|
+
`)}
|
|
666
|
+
}`;
|
|
667
|
+
}
|
|
668
|
+
function generateConstantsObject(variables, address, contractName) {
|
|
669
|
+
if (!variables || variables.length === 0) {
|
|
670
|
+
return "";
|
|
671
|
+
}
|
|
672
|
+
const constants = variables.filter((v) => v.access === "constant");
|
|
673
|
+
if (constants.length === 0) {
|
|
674
|
+
return "";
|
|
675
|
+
}
|
|
676
|
+
const constMethods = constants.map((constant) => {
|
|
677
|
+
const methodName = toCamelCase(constant.name);
|
|
678
|
+
const valueType = getTypeForArg({ type: constant.type });
|
|
679
|
+
return `${methodName}: {
|
|
680
|
+
async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
|
|
681
|
+
const { cvToJSON, deserializeCV } = await import('@stacks/transactions');
|
|
682
|
+
const apiUrls: Record<string, string> = {
|
|
683
|
+
mainnet: 'https://api.hiro.so',
|
|
684
|
+
testnet: 'https://api.testnet.hiro.so',
|
|
685
|
+
devnet: 'http://localhost:3999'
|
|
686
|
+
};
|
|
687
|
+
const baseUrl = apiUrls[options?.network || 'mainnet'];
|
|
688
|
+
|
|
689
|
+
const response = await fetch(
|
|
690
|
+
\`\${baseUrl}/v2/constant_val/${address}/${contractName}/${constant.name}?proof=0\`
|
|
691
|
+
);
|
|
692
|
+
|
|
693
|
+
if (!response.ok) {
|
|
694
|
+
throw new Error(\`Failed to fetch constant: \${response.statusText}\`);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
const result = await response.json();
|
|
698
|
+
const cv = deserializeCV(result.data);
|
|
699
|
+
const parsed = cvToJSON(cv);
|
|
700
|
+
return parsed.value ?? parsed;
|
|
701
|
+
},
|
|
702
|
+
type: ${JSON.stringify(constant.type)} as const
|
|
703
|
+
}`;
|
|
704
|
+
});
|
|
705
|
+
return `constants: {
|
|
706
|
+
${constMethods.join(`,
|
|
707
|
+
|
|
708
|
+
`)}
|
|
709
|
+
}`;
|
|
710
|
+
}
|
|
711
|
+
function generateMapKeyConversion(keyType) {
|
|
712
|
+
if (keyType.tuple) {
|
|
713
|
+
const fields = keyType.tuple.map((field) => {
|
|
714
|
+
const camelFieldName = toCamelCase(field.name);
|
|
715
|
+
const fieldConversion = generateClarityConversion(`key.${camelFieldName}`, { type: field.type });
|
|
716
|
+
return `"${field.name}": ${fieldConversion}`;
|
|
717
|
+
}).join(", ");
|
|
718
|
+
return `Cl.tuple({ ${fields} })`;
|
|
719
|
+
}
|
|
720
|
+
return generateClarityConversion("key", { type: keyType });
|
|
721
|
+
}
|
|
564
722
|
|
|
565
723
|
// src/plugins/clarinet/index.ts
|
|
566
724
|
function toCamelCase2(str) {
|
|
@@ -904,7 +1062,7 @@ function generateWriteHelpers(contract, options) {
|
|
|
904
1062
|
fee?: string | number | undefined;
|
|
905
1063
|
nonce?: bigint;
|
|
906
1064
|
anchorMode?: 1 | 2 | 3; // AnchorMode: OnChainOnly = 1, OffChainOnly = 2, Any = 3
|
|
907
|
-
postConditions?:
|
|
1065
|
+
postConditions?: PostCondition[];
|
|
908
1066
|
validateWithAbi?: boolean;
|
|
909
1067
|
}) {
|
|
910
1068
|
const { senderKey, network = 'mainnet', ...txOptions } = options;
|
|
@@ -988,17 +1146,27 @@ var actions = (options = {}) => {
|
|
|
988
1146
|
};
|
|
989
1147
|
};
|
|
990
1148
|
function addRequiredImports(content) {
|
|
991
|
-
if (content.includes("fetchCallReadOnlyFunction") && content.includes("makeContractCall")) {
|
|
992
|
-
return content;
|
|
993
|
-
}
|
|
994
1149
|
const transactionsImportRegex = /import\s+\{([^}]+)\}\s+from\s+['"]@stacks\/transactions['"];/;
|
|
995
1150
|
const match = content.match(transactionsImportRegex);
|
|
996
1151
|
if (match) {
|
|
997
1152
|
const existingImports = match[1].trim();
|
|
998
|
-
const
|
|
1153
|
+
const requiredImports = [
|
|
1154
|
+
"fetchCallReadOnlyFunction",
|
|
1155
|
+
"makeContractCall"
|
|
1156
|
+
];
|
|
1157
|
+
const newImports = requiredImports.filter((imp) => !existingImports.includes(imp)).join(", ");
|
|
999
1158
|
if (newImports) {
|
|
1000
1159
|
const updatedImport = `import { ${existingImports}, ${newImports} } from '@stacks/transactions';`;
|
|
1001
|
-
|
|
1160
|
+
let updatedContent = content.replace(transactionsImportRegex, updatedImport);
|
|
1161
|
+
if (!updatedContent.includes("type PostCondition")) {
|
|
1162
|
+
updatedContent = updatedContent.replace(updatedImport, `${updatedImport}
|
|
1163
|
+
import type { PostCondition } from '@stacks/transactions';`);
|
|
1164
|
+
}
|
|
1165
|
+
return updatedContent;
|
|
1166
|
+
}
|
|
1167
|
+
if (!content.includes("type PostCondition")) {
|
|
1168
|
+
return content.replace(transactionsImportRegex, `${match[0]}
|
|
1169
|
+
import type { PostCondition } from '@stacks/transactions';`);
|
|
1002
1170
|
}
|
|
1003
1171
|
}
|
|
1004
1172
|
return content;
|
|
@@ -1144,6 +1312,7 @@ import { useState, useCallback } from 'react'
|
|
|
1144
1312
|
import { useStacksConfig } from './provider'
|
|
1145
1313
|
import { connect, disconnect, isConnected, request, openContractCall as stacksOpenContractCall } from '@stacks/connect'
|
|
1146
1314
|
import { Cl, validateStacksAddress } from '@stacks/transactions'
|
|
1315
|
+
import type { PostCondition } from '@stacks/transactions'
|
|
1147
1316
|
import type { ExtractFunctionArgs, ExtractFunctionNames, ClarityContract } from '@secondlayer/clarity-types'`;
|
|
1148
1317
|
const header = `/**
|
|
1149
1318
|
* Generated generic Stacks React hooks
|
|
@@ -1471,7 +1640,7 @@ function generateGenericHook(hookName) {
|
|
|
1471
1640
|
functionName: string;
|
|
1472
1641
|
functionArgs: any[]; // Pre-converted Clarity values
|
|
1473
1642
|
network?: string;
|
|
1474
|
-
postConditions?:
|
|
1643
|
+
postConditions?: PostCondition[];
|
|
1475
1644
|
attachment?: string;
|
|
1476
1645
|
onFinish?: (data: any) => void;
|
|
1477
1646
|
onCancel?: () => void;
|
|
@@ -1547,7 +1716,7 @@ function generateGenericHook(hookName) {
|
|
|
1547
1716
|
abi: T;
|
|
1548
1717
|
functionArgs: ExtractFunctionArgs<T, FN>;
|
|
1549
1718
|
network?: string;
|
|
1550
|
-
postConditions?:
|
|
1719
|
+
postConditions?: PostCondition[];
|
|
1551
1720
|
attachment?: string;
|
|
1552
1721
|
onFinish?: (data: any) => void;
|
|
1553
1722
|
onCancel?: () => void;
|
|
@@ -1868,7 +2037,7 @@ function generateGenericHook(hookName) {
|
|
|
1868
2037
|
contractName: string;
|
|
1869
2038
|
codeBody: string;
|
|
1870
2039
|
network?: string;
|
|
1871
|
-
postConditions?:
|
|
2040
|
+
postConditions?: PostCondition[];
|
|
1872
2041
|
onFinish?: (data: any) => void;
|
|
1873
2042
|
onCancel?: () => void;
|
|
1874
2043
|
}) => {
|
|
@@ -1905,7 +2074,7 @@ function generateGenericHook(hookName) {
|
|
|
1905
2074
|
contractName: string;
|
|
1906
2075
|
codeBody: string;
|
|
1907
2076
|
network?: string;
|
|
1908
|
-
postConditions?:
|
|
2077
|
+
postConditions?: PostCondition[];
|
|
1909
2078
|
onFinish?: (data: any) => void;
|
|
1910
2079
|
onCancel?: () => void;
|
|
1911
2080
|
}) => {
|
|
@@ -1981,18 +2150,30 @@ function mapClarityTypeToTS(clarityType) {
|
|
|
1981
2150
|
return "boolean";
|
|
1982
2151
|
if (clarityType?.string || clarityType?.ascii)
|
|
1983
2152
|
return "string";
|
|
2153
|
+
if (clarityType?.["string-ascii"] || clarityType?.["string-utf8"])
|
|
2154
|
+
return "string";
|
|
1984
2155
|
if (clarityType?.buff)
|
|
1985
2156
|
return "Uint8Array";
|
|
1986
2157
|
if (clarityType?.optional) {
|
|
1987
2158
|
const innerType = mapClarityTypeToTS(clarityType.optional);
|
|
1988
2159
|
return `${innerType} | null`;
|
|
1989
2160
|
}
|
|
1990
|
-
if (clarityType?.response)
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
return
|
|
1994
|
-
|
|
1995
|
-
|
|
2161
|
+
if (clarityType?.response) {
|
|
2162
|
+
const okType = mapClarityTypeToTS(clarityType.response.ok);
|
|
2163
|
+
const errType = mapClarityTypeToTS(clarityType.response.error);
|
|
2164
|
+
return `{ ok: ${okType} } | { err: ${errType} }`;
|
|
2165
|
+
}
|
|
2166
|
+
if (clarityType?.tuple) {
|
|
2167
|
+
const fields = clarityType.tuple.map((field) => `${toCamelCase4(field.name)}: ${mapClarityTypeToTS(field.type)}`).join("; ");
|
|
2168
|
+
return `{ ${fields} }`;
|
|
2169
|
+
}
|
|
2170
|
+
if (clarityType?.list) {
|
|
2171
|
+
const innerType = mapClarityTypeToTS(clarityType.list.type);
|
|
2172
|
+
if (innerType.includes(" | ")) {
|
|
2173
|
+
return `(${innerType})[]`;
|
|
2174
|
+
}
|
|
2175
|
+
return `${innerType}[]`;
|
|
2176
|
+
}
|
|
1996
2177
|
return "any";
|
|
1997
2178
|
}
|
|
1998
2179
|
if (clarityType.includes("uint") || clarityType.includes("int"))
|
|
@@ -2009,12 +2190,6 @@ function mapClarityTypeToTS(clarityType) {
|
|
|
2009
2190
|
const innerType = clarityType.replace(/optional\s*/, "").trim();
|
|
2010
2191
|
return `${mapClarityTypeToTS(innerType)} | null`;
|
|
2011
2192
|
}
|
|
2012
|
-
if (clarityType.includes("response"))
|
|
2013
|
-
return "any";
|
|
2014
|
-
if (clarityType.includes("tuple"))
|
|
2015
|
-
return "any";
|
|
2016
|
-
if (clarityType.includes("list"))
|
|
2017
|
-
return "any[]";
|
|
2018
2193
|
return "any";
|
|
2019
2194
|
}
|
|
2020
2195
|
function generateObjectArgs(args) {
|
|
@@ -2029,6 +2204,7 @@ async function generateContractHooks(contracts, excludeList = []) {
|
|
|
2029
2204
|
import { useCallback } from 'react'
|
|
2030
2205
|
import { useStacksConfig } from './provider'
|
|
2031
2206
|
import { request, openContractCall as stacksOpenContractCall } from '@stacks/connect'
|
|
2207
|
+
import type { PostCondition } from '@stacks/transactions'
|
|
2032
2208
|
import { ${contracts.map((c) => c.name).join(", ")} } from './contracts'`;
|
|
2033
2209
|
const header = `/**
|
|
2034
2210
|
* Generated contract-specific React hooks
|
|
@@ -2052,8 +2228,10 @@ ${hooksCode}`;
|
|
|
2052
2228
|
return formatted;
|
|
2053
2229
|
}
|
|
2054
2230
|
function generateContractHookMethods(contract, excludeList) {
|
|
2055
|
-
const { abi, name } = contract;
|
|
2231
|
+
const { abi, name, address, contractName } = contract;
|
|
2056
2232
|
const functions = abi.functions || [];
|
|
2233
|
+
const maps = abi.maps || [];
|
|
2234
|
+
const variables = abi.variables || [];
|
|
2057
2235
|
const readOnlyFunctions = functions.filter((f) => f.access === "read_only" || f.access === "read-only");
|
|
2058
2236
|
const publicFunctions = functions.filter((f) => f.access === "public");
|
|
2059
2237
|
const readHooks = readOnlyFunctions.map((func) => {
|
|
@@ -2070,7 +2248,30 @@ function generateContractHookMethods(contract, excludeList) {
|
|
|
2070
2248
|
}
|
|
2071
2249
|
return generateWriteHook(func, name);
|
|
2072
2250
|
}).filter(Boolean);
|
|
2073
|
-
const
|
|
2251
|
+
const mapHooks = maps.map((map) => {
|
|
2252
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase4(map.name))}`;
|
|
2253
|
+
if (excludeList.includes(hookName)) {
|
|
2254
|
+
return null;
|
|
2255
|
+
}
|
|
2256
|
+
return generateMapHook(map, name, address, contractName);
|
|
2257
|
+
}).filter(Boolean);
|
|
2258
|
+
const dataVars = variables.filter((v) => v.access === "variable");
|
|
2259
|
+
const varHooks = dataVars.map((variable) => {
|
|
2260
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase4(variable.name))}`;
|
|
2261
|
+
if (excludeList.includes(hookName)) {
|
|
2262
|
+
return null;
|
|
2263
|
+
}
|
|
2264
|
+
return generateVarHook(variable, name, address, contractName);
|
|
2265
|
+
}).filter(Boolean);
|
|
2266
|
+
const constants = variables.filter((v) => v.access === "constant");
|
|
2267
|
+
const constantHooks = constants.map((constant) => {
|
|
2268
|
+
const hookName = `use${capitalize(name)}${capitalize(toCamelCase4(constant.name))}`;
|
|
2269
|
+
if (excludeList.includes(hookName)) {
|
|
2270
|
+
return null;
|
|
2271
|
+
}
|
|
2272
|
+
return generateConstantHook(constant, name, address, contractName);
|
|
2273
|
+
}).filter(Boolean);
|
|
2274
|
+
const allHooks = [...readHooks, ...writeHooks, ...mapHooks, ...varHooks, ...constantHooks];
|
|
2074
2275
|
if (allHooks.length === 0) {
|
|
2075
2276
|
return "";
|
|
2076
2277
|
}
|
|
@@ -2082,12 +2283,13 @@ function generateReadHook(func, contractName) {
|
|
|
2082
2283
|
const hookName = `use${capitalize(contractName)}${capitalize(toCamelCase4(func.name))}`;
|
|
2083
2284
|
const argsSignature = generateHookArgsSignature(func.args);
|
|
2084
2285
|
const enabledParam = func.args.length > 0 ? ", options?: { enabled?: boolean }" : "options?: { enabled?: boolean }";
|
|
2286
|
+
const returnType = mapClarityTypeToTS(func.outputs);
|
|
2085
2287
|
return `export function ${hookName}(${argsSignature}${enabledParam}) {
|
|
2086
2288
|
const config = useStacksConfig()
|
|
2087
|
-
|
|
2088
|
-
return useQuery({
|
|
2289
|
+
|
|
2290
|
+
return useQuery<${returnType}>({
|
|
2089
2291
|
queryKey: ['${func.name}', ${contractName}.address, ${generateQueryKeyArgs(func.args)}],
|
|
2090
|
-
queryFn: () => ${contractName}.read.${toCamelCase4(func.name)}(${generateFunctionCallArgs(func.args) ? `{ ${generateObjectArgs(func.args)} }, ` : ""}{
|
|
2292
|
+
queryFn: () => ${contractName}.read.${toCamelCase4(func.name)}(${generateFunctionCallArgs(func.args) ? `{ ${generateObjectArgs(func.args)} }, ` : ""}{
|
|
2091
2293
|
network: config.network,
|
|
2092
2294
|
senderAddress: config.senderAddress || 'SP000000000000000000002Q6VF78'
|
|
2093
2295
|
}),
|
|
@@ -2107,7 +2309,7 @@ function generateWriteHook(func, contractName) {
|
|
|
2107
2309
|
mutationFn: async (params: {
|
|
2108
2310
|
args: ${argsType};
|
|
2109
2311
|
options?: {
|
|
2110
|
-
postConditions?:
|
|
2312
|
+
postConditions?: PostCondition[];
|
|
2111
2313
|
attachment?: string;
|
|
2112
2314
|
onFinish?: (data: any) => void;
|
|
2113
2315
|
onCancel?: () => void;
|
|
@@ -2165,9 +2367,9 @@ function generateWriteHook(func, contractName) {
|
|
|
2165
2367
|
})
|
|
2166
2368
|
|
|
2167
2369
|
const ${toCamelCase4(func.name)} = useCallback(async (
|
|
2168
|
-
args: ${argsType},
|
|
2370
|
+
args: ${argsType},
|
|
2169
2371
|
options?: {
|
|
2170
|
-
postConditions?:
|
|
2372
|
+
postConditions?: PostCondition[];
|
|
2171
2373
|
attachment?: string;
|
|
2172
2374
|
onFinish?: (data: any) => void;
|
|
2173
2375
|
onCancel?: () => void;
|
|
@@ -2188,6 +2390,53 @@ function generateWriteHook(func, contractName) {
|
|
|
2188
2390
|
}
|
|
2189
2391
|
}`;
|
|
2190
2392
|
}
|
|
2393
|
+
function generateMapHook(map, contractVarName, _address, _contractName) {
|
|
2394
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase4(map.name))}`;
|
|
2395
|
+
const keyType = mapClarityTypeToTS(map.key);
|
|
2396
|
+
const valueType = mapClarityTypeToTS(map.value);
|
|
2397
|
+
return `export function ${hookName}(key: ${keyType}, options?: { enabled?: boolean }) {
|
|
2398
|
+
const config = useStacksConfig()
|
|
2399
|
+
|
|
2400
|
+
return useQuery<${valueType} | null>({
|
|
2401
|
+
queryKey: ['${contractVarName}', '${map.name}', 'map', key, config.network],
|
|
2402
|
+
queryFn: async () => {
|
|
2403
|
+
return ${contractVarName}.maps.${toCamelCase4(map.name)}.get(key, { network: config.network })
|
|
2404
|
+
},
|
|
2405
|
+
enabled: options?.enabled ?? true
|
|
2406
|
+
})
|
|
2407
|
+
}`;
|
|
2408
|
+
}
|
|
2409
|
+
function generateVarHook(variable, contractVarName, _address, _contractName) {
|
|
2410
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase4(variable.name))}`;
|
|
2411
|
+
const valueType = mapClarityTypeToTS(variable.type);
|
|
2412
|
+
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2413
|
+
const config = useStacksConfig()
|
|
2414
|
+
|
|
2415
|
+
return useQuery<${valueType}>({
|
|
2416
|
+
queryKey: ['${contractVarName}', '${variable.name}', 'var', config.network],
|
|
2417
|
+
queryFn: async () => {
|
|
2418
|
+
return ${contractVarName}.vars.${toCamelCase4(variable.name)}.get({ network: config.network })
|
|
2419
|
+
},
|
|
2420
|
+
enabled: options?.enabled ?? true
|
|
2421
|
+
})
|
|
2422
|
+
}`;
|
|
2423
|
+
}
|
|
2424
|
+
function generateConstantHook(constant, contractVarName, _address, _contractName) {
|
|
2425
|
+
const hookName = `use${capitalize(contractVarName)}${capitalize(toCamelCase4(constant.name))}`;
|
|
2426
|
+
const valueType = mapClarityTypeToTS(constant.type);
|
|
2427
|
+
return `export function ${hookName}(options?: { enabled?: boolean }) {
|
|
2428
|
+
const config = useStacksConfig()
|
|
2429
|
+
|
|
2430
|
+
return useQuery<${valueType}>({
|
|
2431
|
+
queryKey: ['${contractVarName}', '${constant.name}', 'constant', config.network],
|
|
2432
|
+
queryFn: async () => {
|
|
2433
|
+
return ${contractVarName}.constants.${toCamelCase4(constant.name)}.get({ network: config.network })
|
|
2434
|
+
},
|
|
2435
|
+
enabled: options?.enabled ?? true,
|
|
2436
|
+
staleTime: Infinity // Constants never change
|
|
2437
|
+
})
|
|
2438
|
+
}`;
|
|
2439
|
+
}
|
|
2191
2440
|
|
|
2192
2441
|
// src/plugins/react/index.ts
|
|
2193
2442
|
var react = (options = {}) => {
|
|
@@ -2441,7 +2690,7 @@ function getMapKeyType(keyType) {
|
|
|
2441
2690
|
}
|
|
2442
2691
|
return getTypeForArg3({ type: keyType });
|
|
2443
2692
|
}
|
|
2444
|
-
function
|
|
2693
|
+
function generateMapKeyConversion2(keyType) {
|
|
2445
2694
|
if (keyType.tuple) {
|
|
2446
2695
|
const fields = keyType.tuple.map((field) => {
|
|
2447
2696
|
const camelFieldName = toCamelCase5(field.name);
|
|
@@ -2455,7 +2704,7 @@ function generateMapKeyConversion(keyType) {
|
|
|
2455
2704
|
function generateMapEntryHelper(map, contractId) {
|
|
2456
2705
|
const methodName = toCamelCase5(map.name);
|
|
2457
2706
|
const keyType = getMapKeyType(map.key);
|
|
2458
|
-
const keyConversion =
|
|
2707
|
+
const keyConversion = generateMapKeyConversion2(map.key);
|
|
2459
2708
|
return `${methodName}: (key: ${keyType}) => {
|
|
2460
2709
|
return simnet.getMapEntry(
|
|
2461
2710
|
'${contractId}',
|
|
@@ -2464,7 +2713,7 @@ function generateMapEntryHelper(map, contractId) {
|
|
|
2464
2713
|
);
|
|
2465
2714
|
}`;
|
|
2466
2715
|
}
|
|
2467
|
-
function
|
|
2716
|
+
function generateVarsObject2(variables, contractId) {
|
|
2468
2717
|
const dataVars = variables.filter((v) => v.access === "variable");
|
|
2469
2718
|
if (dataVars.length === 0) {
|
|
2470
2719
|
return "";
|
|
@@ -2476,7 +2725,7 @@ function generateVarsObject(variables, contractId) {
|
|
|
2476
2725
|
`)}
|
|
2477
2726
|
}`;
|
|
2478
2727
|
}
|
|
2479
|
-
function
|
|
2728
|
+
function generateMapsObject2(maps, contractId) {
|
|
2480
2729
|
if (maps.length === 0) {
|
|
2481
2730
|
return "";
|
|
2482
2731
|
}
|
|
@@ -2499,8 +2748,8 @@ function generateContractHelper(contract, options) {
|
|
|
2499
2748
|
const publicHelpers = publicFns.map((f) => generatePublicFunction(f, address));
|
|
2500
2749
|
const readOnlyHelpers = readOnlyFns.map((f) => generateReadOnlyFunction(f, address));
|
|
2501
2750
|
const privateHelpers = privateFns.map((f) => generatePrivateFunction(f, address));
|
|
2502
|
-
const varsObject =
|
|
2503
|
-
const mapsObject =
|
|
2751
|
+
const varsObject = generateVarsObject2(variables, address);
|
|
2752
|
+
const mapsObject = generateMapsObject2(maps, address);
|
|
2504
2753
|
const allHelpers = [...publicHelpers, ...readOnlyHelpers, ...privateHelpers];
|
|
2505
2754
|
if (varsObject) {
|
|
2506
2755
|
allHelpers.push(varsObject);
|
|
@@ -2651,5 +2900,5 @@ export {
|
|
|
2651
2900
|
PluginManager
|
|
2652
2901
|
};
|
|
2653
2902
|
|
|
2654
|
-
//# debugId=
|
|
2903
|
+
//# debugId=3D064257DAF105D164756E2164756E21
|
|
2655
2904
|
//# sourceMappingURL=index.js.map
|