@secondlayer/cli 0.3.0 → 0.3.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/index.js CHANGED
@@ -316,12 +316,44 @@ import { initSimnet } from "@hirosystems/clarinet-sdk";
316
316
 
317
317
  // src/generators/contract.ts
318
318
  import { format } from "prettier";
319
+ function generateNetworkUtils() {
320
+ return `/**
321
+ * API URLs for different networks
322
+ */
323
+ const API_URLS: Record<'mainnet' | 'testnet' | 'devnet', string> = {
324
+ mainnet: 'https://api.hiro.so',
325
+ testnet: 'https://api.testnet.hiro.so',
326
+ devnet: 'http://localhost:3999'
327
+ };
328
+
329
+ /**
330
+ * Infer network from Stacks address prefix
331
+ * SP/SM = mainnet, ST/SN = testnet
332
+ */
333
+ function inferNetworkFromAddress(address: string): 'mainnet' | 'testnet' | undefined {
334
+ if (address.startsWith('SP') || address.startsWith('SM')) return 'mainnet';
335
+ if (address.startsWith('ST') || address.startsWith('SN')) return 'testnet';
336
+ return undefined;
337
+ }
338
+
339
+ /**
340
+ * Get API URL, inferring network from contract address if not specified
341
+ */
342
+ function getApiUrl(
343
+ contractAddress: string,
344
+ explicitNetwork?: 'mainnet' | 'testnet' | 'devnet'
345
+ ): string {
346
+ const network = explicitNetwork ?? inferNetworkFromAddress(contractAddress) ?? 'mainnet';
347
+ return API_URLS[network];
348
+ }`;
349
+ }
319
350
  async function generateContractInterface(contracts) {
320
351
  const imports = `import { Cl, validateStacksAddress } from '@stacks/transactions'`;
321
352
  const header = `/**
322
353
  * Generated by @secondlayer/cli
323
354
  * DO NOT EDIT MANUALLY
324
355
  */`;
356
+ const networkUtils = generateNetworkUtils();
325
357
  const contractsCode = contracts.map((contract) => generateContract(contract)).join(`
326
358
 
327
359
  `);
@@ -329,6 +361,8 @@ async function generateContractInterface(contracts) {
329
361
 
330
362
  ${header}
331
363
 
364
+ ${networkUtils}
365
+
332
366
  ${contractsCode}`;
333
367
  const formatted = await format(code, {
334
368
  parser: "typescript",
@@ -579,12 +613,7 @@ function generateMapsObject(maps, address, contractName) {
579
613
  return `${methodName}: {
580
614
  async get(key: ${keyType}, options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType} | null> {
581
615
  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'];
616
+ const baseUrl = getApiUrl('${address}', options?.network);
588
617
  const mapKey = ${keyConversion};
589
618
  const keyHex = serializeCV(mapKey).toString('hex');
590
619
 
@@ -636,12 +665,7 @@ function generateVarsObject(variables, address, contractName) {
636
665
  return `${methodName}: {
637
666
  async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
638
667
  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'];
668
+ const baseUrl = getApiUrl('${address}', options?.network);
645
669
 
646
670
  const response = await fetch(
647
671
  \`\${baseUrl}/v2/data_var/${address}/${contractName}/${variable.name}?proof=0\`
@@ -679,12 +703,7 @@ function generateConstantsObject(variables, address, contractName) {
679
703
  return `${methodName}: {
680
704
  async get(options?: { network?: 'mainnet' | 'testnet' | 'devnet' }): Promise<${valueType}> {
681
705
  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'];
706
+ const baseUrl = getApiUrl('${address}', options?.network);
688
707
 
689
708
  const response = await fetch(
690
709
  \`\${baseUrl}/v2/constant_val/${address}/${contractName}/${constant.name}?proof=0\`
@@ -1013,7 +1032,7 @@ function generateReadHelpers(contract, options) {
1013
1032
  const methodName = toCamelCase3(func.name);
1014
1033
  const argsSignature = generateArgsSignature(func.args);
1015
1034
  const clarityArgs = generateClarityArgs(func.args, name);
1016
- return `async ${methodName}(${argsSignature}options?: {
1035
+ return `async ${methodName}(${argsSignature}options?: {
1017
1036
  network?: 'mainnet' | 'testnet' | 'devnet';
1018
1037
  senderAddress?: string;
1019
1038
  }) {
@@ -1022,7 +1041,7 @@ function generateReadHelpers(contract, options) {
1022
1041
  contractName: '${contract.contractName}',
1023
1042
  functionName: '${func.name}',
1024
1043
  functionArgs: [${clarityArgs}],
1025
- network: options?.network || 'mainnet',
1044
+ network: options?.network ?? inferNetworkFromAddress('${contract.address}') ?? 'mainnet',
1026
1045
  senderAddress: options?.senderAddress || 'SP000000000000000000002Q6VF78'
1027
1046
  });
1028
1047
  }`;
@@ -2900,5 +2919,5 @@ export {
2900
2919
  PluginManager
2901
2920
  };
2902
2921
 
2903
- //# debugId=3D064257DAF105D164756E2164756E21
2922
+ //# debugId=7F2D601996EE7CEE64756E2164756E21
2904
2923
  //# sourceMappingURL=index.js.map