@codama/renderers-js 1.5.5 → 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.
@@ -190,6 +190,7 @@ var DEFAULT_NAME_TRANSFORMERS = {
190
190
  programInstructionsEnum: (name) => `${pascalCase(name)}Instruction`,
191
191
  programInstructionsEnumVariant: (name) => `${pascalCase(name)}`,
192
192
  programInstructionsIdentifierFunction: (name) => `identify${pascalCase(name)}Instruction`,
193
+ programInstructionsParseFunction: (name) => `parse${pascalCase(name)}Instruction`,
193
194
  programInstructionsParsedUnionType: (name) => `Parsed${pascalCase(name)}Instruction`,
194
195
  programIsErrorFunction: (name) => `is${pascalCase(name)}Error`,
195
196
  resolverFunction: (name) => `${camelCase(name)}`
@@ -2087,7 +2088,8 @@ function getProgramInstructionsFragment(scope) {
2087
2088
  [
2088
2089
  getProgramInstructionsEnumFragment(scopeWithInstructions),
2089
2090
  getProgramInstructionsIdentifierFunctionFragment(scopeWithInstructions),
2090
- getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions)
2091
+ getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions),
2092
+ getProgramInstructionsParseFunctionFragment(scopeWithInstructions)
2091
2093
  ],
2092
2094
  (c) => c.join("\n\n")
2093
2095
  );
@@ -2156,6 +2158,39 @@ function getProgramInstructionsParsedUnionTypeFragment(scope) {
2156
2158
  (c) => c.join("\n")
2157
2159
  );
2158
2160
  }
2161
+ function getProgramInstructionsParseFunctionFragment(scope) {
2162
+ const { programNode, nameApi, allInstructions } = scope;
2163
+ const instructionsWithDiscriminators = allInstructions.filter(
2164
+ (instruction) => (instruction.discriminators ?? []).length > 0
2165
+ );
2166
+ if (instructionsWithDiscriminators.length === 0) return;
2167
+ const programInstructionsEnum = nameApi.programInstructionsEnum(programNode.name);
2168
+ const programInstructionsIdentifierFunction = nameApi.programInstructionsIdentifierFunction(programNode.name);
2169
+ const programInstructionsParsedUnionType = nameApi.programInstructionsParsedUnionType(programNode.name);
2170
+ const parseFunction = nameApi.programInstructionsParseFunction(programNode.name);
2171
+ const switchCases = mergeFragments(
2172
+ allInstructions.map((instruction) => {
2173
+ const enumVariant = nameApi.programInstructionsEnumVariant(instruction.name);
2174
+ const parseFunction2 = use(nameApi.instructionParseFunction(instruction.name), "generatedInstructions");
2175
+ const assertIsInstructionWithAccounts = use("assertIsInstructionWithAccounts", "solanaInstructions");
2176
+ const hasAccounts = instruction.accounts.length > 0;
2177
+ const assertionsCode = hasAccounts ? fragment`${assertIsInstructionWithAccounts}(instruction);\n` : fragment``;
2178
+ return fragment`case ${programInstructionsEnum}.${enumVariant}: { ${assertionsCode}return { instructionType: ${programInstructionsEnum}.${enumVariant}, ...${parseFunction2}(instruction) }; }`;
2179
+ }),
2180
+ (c) => c.join("\n")
2181
+ );
2182
+ return fragment`
2183
+ export function ${parseFunction}<TProgram extends string>(
2184
+ instruction: ${use("type Instruction", "solanaInstructions")}<TProgram>
2185
+ & ${use("type InstructionWithData", "solanaInstructions")}<${use("type ReadonlyUint8Array", "solanaCodecsCore")}>
2186
+ ): ${programInstructionsParsedUnionType}<TProgram> {
2187
+ const instructionType = ${programInstructionsIdentifierFunction}(instruction);
2188
+ switch (instructionType) {
2189
+ ${switchCases}
2190
+ default: throw new Error("Unrecognized instruction type: " + instructionType);
2191
+ }
2192
+ }`;
2193
+ }
2159
2194
 
2160
2195
  // src/fragments/programPage.ts
2161
2196
  function getProgramPageFragment(scope) {