codama-renderers-dart 0.4.0 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codama-renderers-dart",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Codama renderer for generating Dart code targeting the solana_kit SDK",
5
5
  "exports": {
6
6
  "node": {
@@ -1,4 +1,4 @@
1
- import type {
1
+ import {
2
2
  InstructionAccountNode,
3
3
  InstructionArgumentNode,
4
4
  InstructionNode,
@@ -16,6 +16,7 @@ import {
16
16
  import type { RenderScope } from "../utils/options.js";
17
17
  import { camelCase, pascalCase } from "../utils/nameTransformers.js";
18
18
  import { getDiscriminatorConstantsFragment } from "./discriminatorConstants.js";
19
+ import { WELL_KNOWN_ADDRESSES } from "../utils/wellKnownAddresses.js";
19
20
 
20
21
  /**
21
22
  * Generate a full Dart file for an instruction.
@@ -338,8 +339,14 @@ function getDefaultValue(arg: InstructionArgumentNode): string {
338
339
  return String(dv.boolean);
339
340
  case "stringValueNode":
340
341
  return `'${dv.string}'`;
341
- case "publicKeyValueNode":
342
+ case "publicKeyValueNode": {
343
+ const wellKnownName = WELL_KNOWN_ADDRESSES.get(dv.publicKey);
344
+ if (wellKnownName) {
345
+ use(wellKnownName, "solanaAddresses");
346
+ return wellKnownName;
347
+ }
342
348
  return `Address('${dv.publicKey}')`;
349
+ }
343
350
  case "noneValueNode":
344
351
  return "null";
345
352
  case "bytesValueNode": {
@@ -10,6 +10,7 @@ import {
10
10
  } from "../utils/fragment.js";
11
11
  import type { RenderScope } from "../utils/options.js";
12
12
  import { camelCase, pascalCase } from "../utils/nameTransformers.js";
13
+ import { WELL_KNOWN_ADDRESSES } from "../utils/wellKnownAddresses.js";
13
14
 
14
15
  /**
15
16
  * Generate a full Dart file for a PDA.
@@ -64,9 +65,17 @@ export function getPdaPageFragment(
64
65
  } else if (seed.value.kind === "stringValueNode") {
65
66
  seedValues.push(` '${seed.value.string}',`);
66
67
  } else if (seed.value.kind === "publicKeyValueNode") {
67
- seedValues.push(
68
- ` getAddressEncoder().encode(Address('${seed.value.publicKey}')),`,
69
- );
68
+ const wellKnownSeedName = WELL_KNOWN_ADDRESSES.get(seed.value.publicKey);
69
+ if (wellKnownSeedName) {
70
+ use(wellKnownSeedName, "solanaAddresses");
71
+ seedValues.push(
72
+ ` getAddressEncoder().encode(${wellKnownSeedName}),`,
73
+ );
74
+ } else {
75
+ seedValues.push(
76
+ ` getAddressEncoder().encode(Address('${seed.value.publicKey}')),`,
77
+ );
78
+ }
70
79
  } else if (seed.value.kind === "numberValueNode") {
71
80
  const manifest = visit(seed.type, scope.typeManifestVisitor);
72
81
  seedValues.push(` ${manifest.encoder.content}.encode(${seed.value.number}),`);
@@ -80,8 +89,16 @@ export function getPdaPageFragment(
80
89
  }
81
90
 
82
91
  // Program address parameter
92
+ const wellKnownProgramName = node.programId
93
+ ? WELL_KNOWN_ADDRESSES.get(node.programId)
94
+ : undefined;
95
+ if (wellKnownProgramName) {
96
+ use(wellKnownProgramName, "solanaAddresses");
97
+ }
83
98
  const programIdParam = node.programId
84
- ? `Address programAddress = const Address('${node.programId}')`
99
+ ? wellKnownProgramName
100
+ ? `Address programAddress = ${wellKnownProgramName}`
101
+ : `Address programAddress = const Address('${node.programId}')`
85
102
  : "required Address programAddress";
86
103
 
87
104
  const typedDataImport = hasByteSeeds
@@ -10,6 +10,7 @@ import {
10
10
  } from "../utils/fragment.js";
11
11
  import type { RenderScope } from "../utils/options.js";
12
12
  import { camelCase, pascalCase } from "../utils/nameTransformers.js";
13
+ import { WELL_KNOWN_ADDRESSES } from "../utils/wellKnownAddresses.js";
13
14
 
14
15
  /**
15
16
  * Generate a full Dart file for a program.
@@ -21,15 +22,36 @@ export function getProgramPageFragment(
21
22
  const name = node.name as string;
22
23
  const addressConstName = scope.nameApi.programAddressConstant(name);
23
24
 
24
- const parts: Fragment[] = [
25
- fragment`// Auto-generated. Do not edit.
25
+ const wellKnownName = WELL_KNOWN_ADDRESSES.get(node.publicKey);
26
+
27
+ // For well-known addresses, export and alias the canonical constant from
28
+ // solana_kit_addresses instead of hardcoding the address string.
29
+ // When the generated name matches the canonical name, a simple export suffices.
30
+ // When they differ, we also need a local const alias.
31
+ // For unknown addresses, fall back to the original Address('...') pattern.
32
+ const addressDeclaration: Fragment = wellKnownName
33
+ ? addressConstName === wellKnownName
34
+ ? fragment`// Auto-generated. Do not edit.
35
+ // ignore_for_file: type=lint
36
+
37
+ /// The address of the ${fragmentFromString(pascalCase(name))} program.
38
+ export 'package:solana_kit_addresses/solana_kit_addresses.dart' show ${fragmentFromString(wellKnownName)};`
39
+ : fragment`${use(wellKnownName, "solanaAddresses")}
40
+
41
+ // Auto-generated. Do not edit.
42
+ // ignore_for_file: type=lint
43
+
44
+ /// The address of the ${fragmentFromString(pascalCase(name))} program.
45
+ const ${fragmentFromString(addressConstName)} = ${fragmentFromString(wellKnownName)};`
46
+ : fragment`// Auto-generated. Do not edit.
26
47
  // ignore_for_file: type=lint
27
48
 
28
49
  ${use("Address", "solanaAddresses")}
29
50
 
30
51
  /// The address of the ${fragmentFromString(pascalCase(name))} program.
31
- const ${fragmentFromString(addressConstName)} = Address('${fragmentFromString(node.publicKey)}');`,
32
- ];
52
+ const ${fragmentFromString(addressConstName)} = Address('${fragmentFromString(node.publicKey)}');`;
53
+
54
+ const parts: Fragment[] = [addressDeclaration];
33
55
 
34
56
  // Account identifier enum
35
57
  const accounts = node.accounts ?? [];
@@ -60,4 +82,4 @@ ${fragmentFromString(instrVariants)}
60
82
  }
61
83
 
62
84
  return mergeFragments(parts, (cs) => cs.join("\n"));
63
- }
85
+ }
@@ -5,3 +5,4 @@ export * from "./importMap.js";
5
5
  export * from "./nameTransformers.js";
6
6
  export * from "./options.js";
7
7
  export * from "./typeManifest.js";
8
+ export * from "./wellKnownAddresses.js";
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Maps well-known Solana public keys to their canonical Dart constant names
3
+ * in `package:solana_kit_addresses`. When the renderer encounters a known
4
+ * address, it emits a re-export from that package instead of hardcoding the
5
+ * address string.
6
+ *
7
+ * The keys are the base-58 public key strings as they appear in IDL/Codama nodes.
8
+ * The values are the Dart constant names exported by `solana_kit_addresses`.
9
+ */
10
+ export const WELL_KNOWN_ADDRESSES: ReadonlyMap<string, string> = new Map([
11
+ // Agave / Solana native programs
12
+ ['11111111111111111111111111111111', 'systemProgramAddress'],
13
+ ['AddressLookupTab1e1111111111111111111111111', 'addressLookupTableProgramAddress'],
14
+ ['BPFLoader2111111111111111111111111111111111', 'bpfLoaderProgramAddress'],
15
+ ['BPFLoader1111111111111111111111111111111111', 'bpfLoaderDeprecatedProgramAddress'],
16
+ ['BPFLoaderUpgradeab1e11111111111111111111111', 'bpfLoaderUpgradeableProgramAddress'],
17
+ ['ComputeBudget111111111111111111111111111111', 'computeBudgetProgramAddress'],
18
+ ['Config1111111111111111111111111111111111111', 'configProgramAddress'],
19
+ ['Ed25519SigVerify111111111111111111111111111', 'ed25519ProgramAddress'],
20
+ ['Feature111111111111111111111111111111111111', 'featureProgramAddress'],
21
+ ['1nc1nerator11111111111111111111111111111111', 'incineratorAddress'],
22
+ ['LoaderV411111111111111111111111111111111111', 'loaderV4ProgramAddress'],
23
+ ['NativeLoader1111111111111111111111111111', 'nativeLoaderProgramAddress'],
24
+ ['KeccakSecp256k11111111111111111111111111111', 'secp256k1ProgramAddress'],
25
+ ['Secp256r1SigVerify1111111111111111111111111', 'secp256r1ProgramAddress'],
26
+ ['Stake11111111111111111111111111111111111111', 'stakeProgramAddress'],
27
+ ['StakeConfig1111111111111111111111111111', 'stakeConfigAddress'],
28
+ ['Vote111111111111111111111111111111111111111', 'voteProgramAddress'],
29
+ ['ZkTokenProof1111111111111111111111111111111', 'zkTokenProofProgramAddress'],
30
+ ['ZkE1Gama1Proof11111111111111111111111111111', 'zkElgamalProofProgramAddress'],
31
+
32
+ // Sysvars
33
+ ['SysvarC1ock11111111111111111111111111111111', 'sysvarClockAddress'],
34
+ ['Rent11111111111111111111111111111111111111', 'sysvarRentAddress'],
35
+ ['11111111111111111111111111111111111111111111', 'sysvarRecentBlockhashesAddress'], // note: 44 chars
36
+ ['SysvarFees1111111111111111111111111111111111', 'sysvarFeesAddress'],
37
+ ['Sysvar1nstruct1ons11111111111111111111111111', 'sysvarInstructionsAddress'],
38
+ ['SysvarEpochSchedu1e111111111111111111111111', 'sysvarEpochScheduleAddress'],
39
+ ['SysvarRewards111111111111111111111111111111', 'sysvarRewardsAddress'],
40
+ ['SysvarStakeHistory1111111111111111111111111', 'sysvarStakeHistoryAddress'],
41
+ ['SysvarS1otHashes111111111111111111111111111', 'sysvarSlotHashesAddress'],
42
+ ['SysvarS1otHistory11111111111111111111111111', 'sysvarSlotHistoryAddress'],
43
+ ['Sysvar1111111111111111111111111111111111111', 'sysvarOwnerAddress'],
44
+
45
+ // SPL programs
46
+ ['TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', 'tokenProgramAddress'],
47
+ ['TokenzQdBNb4qyze1S1U9AHB8MGXmNK1REkTPT5Z3Y', 'token2022ProgramAddress'],
48
+ ['ATokenGPvbdGVxr1b2hvZbsiqW5xWH25ef7s3c8BnQKu', 'associatedTokenProgramAddress'],
49
+ ['Memo1UhkJRfRhVq1sR7Y7Nto1s3J2mXTT3L6Wk4K2m6', 'memoProgramAddress'],
50
+ ['MemoSq4gqABXKKYWVqKkV3m3kb1cTR7F2UMy9r6kFNK', 'memoLegacyProgramAddress'],
51
+
52
+ // Metaplex programs
53
+ ['metaqbxxU9qX8rZXkmUJ7haCrXXz2W1PSUq1Rn3E1od', 'tokenMetadataProgramAddress'],
54
+ ['BGUMAp9V1mQ2KV8t34L5u3gZsXYZaYDBdKkc1xYk1qA1', 'mplBubblegumProgramAddress'],
55
+ ['auth9SigNpDKz4s8cHr3MFBPS1phbRKLSCN3aBe9sS', 'authRulesProgramAddress'],
56
+ ['CoREENxT6tW1Ho6BW6W5Z4N41ci5Xz2mmrR2kErmM5TR', 'metaplexCoreProgramAddress'],
57
+ ['cmtMqR4bPM9sEV3bPnJQq5s2EnYw4N5vQ8hWhEd1xzS', 'splAccountCompressionProgramAddress'],
58
+ ['noopb9skMVngR5Qi4sczwFr1jU4V4F7nm8xzFQK9C', 'noopProgramAddress'],
59
+
60
+ // Well-known mints
61
+ ['So11111111111111111111111111111111111111112', 'wrappedSolAddress'],
62
+ ['EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 'usdcAddress'],
63
+ ['Es9vMFrzaCERm6YKXx6bqKr1Rmd9b5v3J8rM5e4rfF4', 'usdtAddress'],
64
+ ]);