@codama/renderers-js 1.5.2 → 1.5.4
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.browser.cjs +523 -676
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.mjs +323 -568
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.node.cjs +523 -693
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +323 -568
- package/dist/index.node.mjs.map +1 -1
- package/dist/index.react-native.mjs +323 -568
- package/dist/index.react-native.mjs.map +1 -1
- package/dist/types/utils/formatCode.d.ts +7 -0
- package/dist/types/utils/formatCode.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/options.d.ts +1 -3
- package/dist/types/utils/options.d.ts.map +1 -1
- package/dist/types/visitors/renderVisitor.d.ts.map +1 -1
- package/package.json +6 -6
package/dist/index.node.mjs
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { camelCase, pascalCase, snakeCase, titleCase, kebabCase, capitalize, REGISTERED_TYPE_NODE_KINDS, REGISTERED_VALUE_NODE_KINDS, isNodeFilter, resolveNestedTypeNode, structTypeNodeFromInstructionArgumentNodes, isNode, isScalarEnum, structTypeNode, structFieldTypeNode, getAllPrograms, getAllPdas, getAllAccounts, getAllInstructionsWithSubs, getAllDefinedTypes, definedTypeLinkNode, definedTypeNode, isDataEnum, accountValueNode, argumentValueNode, parseOptionalAccountStrategy, VALUE_NODES, getAllInstructionArguments, constantDiscriminatorNode, constantValueNodeFromBytes, constantValueNode, assertIsNode } from '@codama/nodes';
|
|
2
|
+
import { setFragmentContent, mapFragmentContent, createRenderMap, mergeRenderMaps, deleteDirectory, writeRenderMap, createFragmentTemplate, mapRenderMapContentAsync, joinPath, fileExists, readJson, writeFile } from '@codama/renderers-core';
|
|
3
|
+
import { NodeStack, pipe, staticVisitor, extendVisitor, visit, findLastNodeFromPath, recordNodeStackVisitor, LinkableDictionary, getResolvedInstructionInputsVisitor, getByteSizeVisitor, recordLinkablesOnFirstVisitVisitor, rootNodeVisitor, findProgramNodeFromPath, getLastNodeFromPath, findInstructionNodeFromPath, deduplicateInstructionDependencies } from '@codama/visitors-core';
|
|
4
|
+
import { getBase64Encoder, getBase58Encoder, getBase16Encoder, getUtf8Encoder, getBase64Decoder } from '@solana/codecs-strings';
|
|
5
|
+
import { format, resolveConfig } from 'prettier';
|
|
6
|
+
import * as estreePlugin from 'prettier/plugins/estree';
|
|
7
|
+
import * as typeScriptPlugin from 'prettier/plugins/typescript';
|
|
8
|
+
import { CodamaError, CODAMA_ERROR__UNEXPECTED_NODE_KIND, logWarn, CODAMA_ERROR__RENDERERS__MISSING_DEPENDENCY_VERSIONS } from '@codama/errors';
|
|
9
|
+
import { subset, minVersion, lt } from 'semver';
|
|
10
|
+
|
|
1
11
|
// src/utils/importMap.ts
|
|
2
12
|
var DEFAULT_EXTERNAL_MODULE_MAP = {
|
|
3
13
|
solanaAccounts: "@solana/kit",
|
|
@@ -121,9 +131,6 @@ function importInfoToString({ importedIdentifier, isType, usedIdentifier }) {
|
|
|
121
131
|
const alias = importedIdentifier !== usedIdentifier ? ` as ${usedIdentifier}` : "";
|
|
122
132
|
return `${isType ? "type " : ""}${importedIdentifier}${alias}`;
|
|
123
133
|
}
|
|
124
|
-
|
|
125
|
-
// src/utils/nameTransformers.ts
|
|
126
|
-
import { camelCase, capitalize, kebabCase, pascalCase, snakeCase, titleCase } from "@codama/nodes";
|
|
127
134
|
function getNameApi(transformers) {
|
|
128
135
|
const helpers = {
|
|
129
136
|
camelCase,
|
|
@@ -185,9 +192,6 @@ var DEFAULT_NAME_TRANSFORMERS = {
|
|
|
185
192
|
programIsErrorFunction: (name) => `is${pascalCase(name)}Error`,
|
|
186
193
|
resolverFunction: (name) => `${camelCase(name)}`
|
|
187
194
|
};
|
|
188
|
-
|
|
189
|
-
// src/utils/fragment.ts
|
|
190
|
-
import { createFragmentTemplate } from "@codama/renderers-core";
|
|
191
195
|
function createFragment(content) {
|
|
192
196
|
return Object.freeze({ content, features: /* @__PURE__ */ new Set(), imports: createImportMap() });
|
|
193
197
|
}
|
|
@@ -267,40 +271,6 @@ function mergeTypeManifests(manifests, options = {}) {
|
|
|
267
271
|
value: merge((m) => m.value, mergeValues)
|
|
268
272
|
});
|
|
269
273
|
}
|
|
270
|
-
|
|
271
|
-
// src/visitors/getRenderMapVisitor.ts
|
|
272
|
-
import {
|
|
273
|
-
camelCase as camelCase15,
|
|
274
|
-
getAllAccounts,
|
|
275
|
-
getAllDefinedTypes,
|
|
276
|
-
getAllInstructionsWithSubs as getAllInstructionsWithSubs2,
|
|
277
|
-
getAllPdas,
|
|
278
|
-
getAllPrograms
|
|
279
|
-
} from "@codama/nodes";
|
|
280
|
-
import { createRenderMap, mergeRenderMaps } from "@codama/renderers-core";
|
|
281
|
-
import {
|
|
282
|
-
extendVisitor as extendVisitor2,
|
|
283
|
-
getByteSizeVisitor,
|
|
284
|
-
getResolvedInstructionInputsVisitor,
|
|
285
|
-
LinkableDictionary as LinkableDictionary3,
|
|
286
|
-
NodeStack as NodeStack2,
|
|
287
|
-
pipe as pipe16,
|
|
288
|
-
recordLinkablesOnFirstVisitVisitor,
|
|
289
|
-
recordNodeStackVisitor as recordNodeStackVisitor2,
|
|
290
|
-
staticVisitor as staticVisitor2,
|
|
291
|
-
visit as visit9
|
|
292
|
-
} from "@codama/visitors-core";
|
|
293
|
-
|
|
294
|
-
// src/fragments/accountFetchHelpers.ts
|
|
295
|
-
import { getLastNodeFromPath, pipe } from "@codama/visitors-core";
|
|
296
|
-
|
|
297
|
-
// src/utils/async.ts
|
|
298
|
-
import {
|
|
299
|
-
accountValueNode,
|
|
300
|
-
argumentValueNode,
|
|
301
|
-
isNode
|
|
302
|
-
} from "@codama/nodes";
|
|
303
|
-
import { deduplicateInstructionDependencies } from "@codama/visitors-core";
|
|
304
274
|
function hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers) {
|
|
305
275
|
const hasByteDeltasAsync = (instructionNode.byteDeltas ?? []).some(
|
|
306
276
|
({ value }) => isNode(value, "resolverValueNode") && asyncResolvers.includes(value.name)
|
|
@@ -370,9 +340,6 @@ function getInstructionDependencies(input, asyncResolvers, useAsync) {
|
|
|
370
340
|
}
|
|
371
341
|
return [];
|
|
372
342
|
}
|
|
373
|
-
|
|
374
|
-
// src/utils/codecs.ts
|
|
375
|
-
import { getBase16Encoder, getBase58Encoder, getBase64Encoder, getUtf8Encoder } from "@solana/codecs-strings";
|
|
376
343
|
function getBytesFromBytesValueNode(node) {
|
|
377
344
|
switch (node.encoding) {
|
|
378
345
|
case "utf8":
|
|
@@ -386,25 +353,16 @@ function getBytesFromBytesValueNode(node) {
|
|
|
386
353
|
return getBase64Encoder().encode(node.data);
|
|
387
354
|
}
|
|
388
355
|
}
|
|
389
|
-
|
|
390
|
-
// src/utils/customData.ts
|
|
391
|
-
import {
|
|
392
|
-
camelCase as camelCase2,
|
|
393
|
-
definedTypeLinkNode,
|
|
394
|
-
definedTypeNode,
|
|
395
|
-
isNode as isNode2,
|
|
396
|
-
structTypeNodeFromInstructionArgumentNodes
|
|
397
|
-
} from "@codama/nodes";
|
|
398
356
|
var parseCustomDataOptions = (customDataOptions, defaultSuffix) => new Map(
|
|
399
357
|
customDataOptions.map((o) => {
|
|
400
358
|
const options = typeof o === "string" ? { name: o } : o;
|
|
401
|
-
const importAs =
|
|
359
|
+
const importAs = camelCase(options.importAs ?? `${options.name}${defaultSuffix}`);
|
|
402
360
|
const importFrom = options.importFrom ?? "hooked";
|
|
403
361
|
return [
|
|
404
|
-
|
|
362
|
+
camelCase(options.name),
|
|
405
363
|
{
|
|
406
364
|
extract: options.extract ?? false,
|
|
407
|
-
extractAs: options.extractAs ?
|
|
365
|
+
extractAs: options.extractAs ? camelCase(options.extractAs) : importAs,
|
|
408
366
|
importAs,
|
|
409
367
|
importFrom,
|
|
410
368
|
linkNode: definedTypeLinkNode(importAs)
|
|
@@ -415,7 +373,7 @@ var parseCustomDataOptions = (customDataOptions, defaultSuffix) => new Map(
|
|
|
415
373
|
var getDefinedTypeNodesToExtract = (nodes, parsedCustomDataOptions) => nodes.flatMap((node) => {
|
|
416
374
|
const options = parsedCustomDataOptions.get(node.name);
|
|
417
375
|
if (!options || !options.extract) return [];
|
|
418
|
-
if (
|
|
376
|
+
if (isNode(node, "accountNode")) {
|
|
419
377
|
return [definedTypeNode({ name: options.extractAs, type: { ...node.data } })];
|
|
420
378
|
}
|
|
421
379
|
return [
|
|
@@ -425,9 +383,32 @@ var getDefinedTypeNodesToExtract = (nodes, parsedCustomDataOptions) => nodes.fla
|
|
|
425
383
|
})
|
|
426
384
|
];
|
|
427
385
|
});
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
386
|
+
var DEFAULT_PRETTIER_OPTIONS = {
|
|
387
|
+
arrowParens: "always",
|
|
388
|
+
parser: "typescript",
|
|
389
|
+
plugins: [estreePlugin, typeScriptPlugin],
|
|
390
|
+
printWidth: 80,
|
|
391
|
+
semi: true,
|
|
392
|
+
singleQuote: true,
|
|
393
|
+
tabWidth: 2,
|
|
394
|
+
trailingComma: "es5",
|
|
395
|
+
useTabs: false
|
|
396
|
+
};
|
|
397
|
+
async function formatCode(renderMap, options) {
|
|
398
|
+
const shouldFormatCode = options.formatCode ?? true;
|
|
399
|
+
if (!shouldFormatCode) return renderMap;
|
|
400
|
+
const prettierOptions = {
|
|
401
|
+
...DEFAULT_PRETTIER_OPTIONS,
|
|
402
|
+
...await resolvePrettierOptions(options.packageFolder),
|
|
403
|
+
...options.prettierOptions
|
|
404
|
+
};
|
|
405
|
+
return await mapRenderMapContentAsync(renderMap, (code) => format(code, prettierOptions));
|
|
406
|
+
}
|
|
407
|
+
async function resolvePrettierOptions(packageFolder) {
|
|
408
|
+
if (!packageFolder) return null;
|
|
409
|
+
const filePathToResolve = joinPath(packageFolder, "package.json");
|
|
410
|
+
return await resolveConfig(filePathToResolve);
|
|
411
|
+
}
|
|
431
412
|
function getImportFromFactory(overrides, customAccountData, customInstructionData) {
|
|
432
413
|
const customDataOverrides = Object.fromEntries(
|
|
433
414
|
[...customAccountData.values(), ...customInstructionData.values()].map(({ importFrom, importAs }) => [
|
|
@@ -474,11 +455,6 @@ function getImportFromFactory(overrides, customAccountData, customInstructionDat
|
|
|
474
455
|
}
|
|
475
456
|
};
|
|
476
457
|
}
|
|
477
|
-
|
|
478
|
-
// src/utils/packageJson.ts
|
|
479
|
-
import { CODAMA_ERROR__RENDERERS__MISSING_DEPENDENCY_VERSIONS, CodamaError as CodamaError2, logWarn } from "@codama/errors";
|
|
480
|
-
import { fileExists, joinPath, readJson, writeFile } from "@codama/renderers-core";
|
|
481
|
-
import { lt as ltVersion, minVersion, subset } from "semver";
|
|
482
458
|
var DEFAULT_DEPENDENCY_VERSIONS = {
|
|
483
459
|
"@solana/accounts": "^5.0.0",
|
|
484
460
|
"@solana/addresses": "^5.0.0",
|
|
@@ -612,7 +588,7 @@ function getUsedDependencyVersions(renderMap, dependencyMap, dependencyVersions,
|
|
|
612
588
|
[{}, /* @__PURE__ */ new Set()]
|
|
613
589
|
);
|
|
614
590
|
if (missingDependencies.size > 0) {
|
|
615
|
-
throw new
|
|
591
|
+
throw new CodamaError(CODAMA_ERROR__RENDERERS__MISSING_DEPENDENCY_VERSIONS, {
|
|
616
592
|
dependencies: [...missingDependencies],
|
|
617
593
|
message: "Please add these dependencies to the `dependencyVersions` option."
|
|
618
594
|
});
|
|
@@ -629,7 +605,7 @@ function shouldUpdateRange(dependency, currentRange, requiredRange) {
|
|
|
629
605
|
if (!minCurrentVersion || !minRequiredVersion) {
|
|
630
606
|
throw new Error("Could not determine minimum versions.");
|
|
631
607
|
}
|
|
632
|
-
if (
|
|
608
|
+
if (lt(minCurrentVersion, minRequiredVersion)) {
|
|
633
609
|
return true;
|
|
634
610
|
}
|
|
635
611
|
return false;
|
|
@@ -718,17 +694,9 @@ export async function ${fetchAllMaybeFunction}(
|
|
|
718
694
|
])
|
|
719
695
|
);
|
|
720
696
|
}
|
|
721
|
-
|
|
722
|
-
// src/fragments/accountPage.ts
|
|
723
|
-
import { resolveNestedTypeNode as resolveNestedTypeNode2 } from "@codama/nodes";
|
|
724
|
-
import { findProgramNodeFromPath, getLastNodeFromPath as getLastNodeFromPath5, visit as visit2 } from "@codama/visitors-core";
|
|
725
|
-
|
|
726
|
-
// src/fragments/accountPdaHelpers.ts
|
|
727
|
-
import { isNodeFilter } from "@codama/nodes";
|
|
728
|
-
import { getLastNodeFromPath as getLastNodeFromPath2, pipe as pipe2 } from "@codama/visitors-core";
|
|
729
697
|
function getAccountPdaHelpersFragment(scope) {
|
|
730
698
|
const { accountPath, nameApi, linkables, customAccountData, typeManifest: typeManifest2 } = scope;
|
|
731
|
-
const accountNode =
|
|
699
|
+
const accountNode = getLastNodeFromPath(accountPath);
|
|
732
700
|
const pdaNode = accountNode.pda ? linkables.get([...accountPath, accountNode.pda]) : void 0;
|
|
733
701
|
if (!pdaNode) return;
|
|
734
702
|
const accountType = customAccountData.has(accountNode.name) ? typeManifest2.strictType : nameApi.dataType(accountNode.name);
|
|
@@ -739,7 +707,7 @@ function getAccountPdaHelpersFragment(scope) {
|
|
|
739
707
|
const fetchFromSeedsFunction = nameApi.accountFetchFromSeedsFunction(accountNode.name);
|
|
740
708
|
const fetchMaybeFromSeedsFunction = nameApi.accountFetchMaybeFromSeedsFunction(accountNode.name);
|
|
741
709
|
const fetchMaybeFunction = nameApi.accountFetchMaybeFunction(accountNode.name);
|
|
742
|
-
return
|
|
710
|
+
return pipe(
|
|
743
711
|
fragment`export async function ${fetchFromSeedsFunction}(
|
|
744
712
|
rpc: Parameters<typeof fetchEncodedAccount>[0],
|
|
745
713
|
${hasVariableSeeds ? `seeds: ${pdaSeedsType},` : ""}
|
|
@@ -769,12 +737,9 @@ export async function ${fetchMaybeFromSeedsFunction}(
|
|
|
769
737
|
])
|
|
770
738
|
);
|
|
771
739
|
}
|
|
772
|
-
|
|
773
|
-
// src/fragments/accountSizeHelpers.ts
|
|
774
|
-
import { getLastNodeFromPath as getLastNodeFromPath3 } from "@codama/visitors-core";
|
|
775
740
|
function getAccountSizeHelpersFragment(scope) {
|
|
776
741
|
const { accountPath, nameApi } = scope;
|
|
777
|
-
const accountNode =
|
|
742
|
+
const accountNode = getLastNodeFromPath(accountPath);
|
|
778
743
|
if (accountNode.size == null) return;
|
|
779
744
|
const getSizeFunction = nameApi.accountGetSizeFunction(accountNode.name);
|
|
780
745
|
return fragment`export function ${getSizeFunction}(): number {
|
|
@@ -782,10 +747,6 @@ function getAccountSizeHelpersFragment(scope) {
|
|
|
782
747
|
}`;
|
|
783
748
|
}
|
|
784
749
|
|
|
785
|
-
// src/fragments/accountType.ts
|
|
786
|
-
import { resolveNestedTypeNode } from "@codama/nodes";
|
|
787
|
-
import { getLastNodeFromPath as getLastNodeFromPath4 } from "@codama/visitors-core";
|
|
788
|
-
|
|
789
750
|
// src/fragments/type.ts
|
|
790
751
|
function getTypeFragment(scope) {
|
|
791
752
|
const { name, manifest, nameApi, docs = [] } = scope;
|
|
@@ -799,9 +760,6 @@ function getTypeFragment(scope) {
|
|
|
799
760
|
const looseExport = manifest.strictType.content === manifest.looseType.content ? aliasedLooseName : fragment`export type ${looseName} = ${manifest.looseType};`;
|
|
800
761
|
return fragment`${docblock}export type ${strictName} = ${manifest.strictType};\n\n${looseExport}`;
|
|
801
762
|
}
|
|
802
|
-
|
|
803
|
-
// src/fragments/typeDecoder.ts
|
|
804
|
-
import { isDataEnum, isNode as isNode3 } from "@codama/nodes";
|
|
805
763
|
function getTypeDecoderFragment(scope) {
|
|
806
764
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
807
765
|
const decoderFunction = nameApi.decoderFunction(name);
|
|
@@ -811,15 +769,12 @@ function getTypeDecoderFragment(scope) {
|
|
|
811
769
|
typeof scope.size === "number" ? "type FixedSizeDecoder" : "type Decoder",
|
|
812
770
|
"solanaCodecsCore"
|
|
813
771
|
);
|
|
814
|
-
const useTypeCast =
|
|
772
|
+
const useTypeCast = isNode(node, "enumTypeNode") && isDataEnum(node) && typeof scope.size === "number";
|
|
815
773
|
const typeCast = useTypeCast ? fragment` as ${decoderType}<${strictName}>` : "";
|
|
816
774
|
return fragment`${docblock}export function ${decoderFunction}(): ${decoderType}<${strictName}> {
|
|
817
775
|
return ${manifest.decoder}${typeCast};
|
|
818
776
|
}`;
|
|
819
777
|
}
|
|
820
|
-
|
|
821
|
-
// src/fragments/typeEncoder.ts
|
|
822
|
-
import { isDataEnum as isDataEnum2, isNode as isNode4 } from "@codama/nodes";
|
|
823
778
|
function getTypeEncoderFragment(scope) {
|
|
824
779
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
825
780
|
const encoderFunction = nameApi.encoderFunction(name);
|
|
@@ -829,7 +784,7 @@ function getTypeEncoderFragment(scope) {
|
|
|
829
784
|
typeof scope.size === "number" ? "type FixedSizeEncoder" : "type Encoder",
|
|
830
785
|
"solanaCodecsCore"
|
|
831
786
|
);
|
|
832
|
-
const useTypeCast =
|
|
787
|
+
const useTypeCast = isNode(node, "enumTypeNode") && isDataEnum(node) && typeof scope.size === "number";
|
|
833
788
|
const typeCast = useTypeCast ? fragment` as ${encoderType}<${looseName}>` : "";
|
|
834
789
|
return fragment`${docblock}export function ${encoderFunction}(): ${encoderType}<${looseName}> {
|
|
835
790
|
return ${manifest.encoder}${typeCast};
|
|
@@ -869,7 +824,7 @@ function getTypeWithCodecFragment(scope) {
|
|
|
869
824
|
// src/fragments/accountType.ts
|
|
870
825
|
function getAccountTypeFragment(scope) {
|
|
871
826
|
const { accountPath, typeManifest: typeManifest2, nameApi, customAccountData } = scope;
|
|
872
|
-
const accountNode =
|
|
827
|
+
const accountNode = getLastNodeFromPath(accountPath);
|
|
873
828
|
if (customAccountData.has(accountNode.name)) return;
|
|
874
829
|
return getTypeWithCodecFragment({
|
|
875
830
|
codecDocs: [`Gets the codec for {@link ${nameApi.dataType(accountNode.name)}} account data.`],
|
|
@@ -883,15 +838,6 @@ function getAccountTypeFragment(scope) {
|
|
|
883
838
|
typeDocs: accountNode.docs
|
|
884
839
|
});
|
|
885
840
|
}
|
|
886
|
-
|
|
887
|
-
// src/fragments/discriminatorConstants.ts
|
|
888
|
-
import {
|
|
889
|
-
camelCase as camelCase3,
|
|
890
|
-
isNode as isNode5,
|
|
891
|
-
isNodeFilter as isNodeFilter2,
|
|
892
|
-
VALUE_NODES
|
|
893
|
-
} from "@codama/nodes";
|
|
894
|
-
import { visit } from "@codama/visitors-core";
|
|
895
841
|
function getDiscriminatorConstantsFragment(scope) {
|
|
896
842
|
const fragments = scope.discriminatorNodes.map((node) => getDiscriminatorConstantFragment(node, scope)).filter(Boolean);
|
|
897
843
|
return mergeFragments(fragments, (c) => c.join("\n\n"));
|
|
@@ -908,9 +854,9 @@ function getDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
|
908
854
|
}
|
|
909
855
|
function getConstantDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
910
856
|
const { discriminatorNodes, typeManifestVisitor, prefix } = scope;
|
|
911
|
-
const index = discriminatorNodes.filter(
|
|
857
|
+
const index = discriminatorNodes.filter(isNodeFilter("constantDiscriminatorNode")).indexOf(discriminatorNode);
|
|
912
858
|
const suffix = index <= 0 ? "" : `_${index + 1}`;
|
|
913
|
-
const name =
|
|
859
|
+
const name = camelCase(`${prefix}_discriminator${suffix}`);
|
|
914
860
|
const encoder = visit(discriminatorNode.constant.type, typeManifestVisitor).encoder;
|
|
915
861
|
const value = visit(discriminatorNode.constant.value, typeManifestVisitor).value;
|
|
916
862
|
return getConstantFragment({ ...scope, encoder, name, value });
|
|
@@ -918,10 +864,10 @@ function getConstantDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
|
918
864
|
function getFieldDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
919
865
|
const { fields, prefix, typeManifestVisitor } = scope;
|
|
920
866
|
const field = fields.find((f) => f.name === discriminatorNode.name);
|
|
921
|
-
if (!field || !field.defaultValue || !
|
|
867
|
+
if (!field || !field.defaultValue || !isNode(field.defaultValue, VALUE_NODES)) {
|
|
922
868
|
return null;
|
|
923
869
|
}
|
|
924
|
-
const name =
|
|
870
|
+
const name = camelCase(`${prefix}_${discriminatorNode.name}`);
|
|
925
871
|
const encoder = visit(field.type, typeManifestVisitor).encoder;
|
|
926
872
|
const value = visit(field.defaultValue, typeManifestVisitor).value;
|
|
927
873
|
return getConstantFragment({ ...scope, encoder, name, value });
|
|
@@ -935,12 +881,12 @@ function getConstantFragment(scope) {
|
|
|
935
881
|
|
|
936
882
|
// src/fragments/accountPage.ts
|
|
937
883
|
function getAccountPageFragment(scope) {
|
|
938
|
-
const node =
|
|
884
|
+
const node = getLastNodeFromPath(scope.accountPath);
|
|
939
885
|
if (!findProgramNodeFromPath(scope.accountPath)) {
|
|
940
886
|
throw new Error("Account must be visited inside a program.");
|
|
941
887
|
}
|
|
942
|
-
const typeManifest2 =
|
|
943
|
-
const fields =
|
|
888
|
+
const typeManifest2 = visit(node, scope.typeManifestVisitor);
|
|
889
|
+
const fields = resolveNestedTypeNode(node.data).fields;
|
|
944
890
|
return mergeFragments(
|
|
945
891
|
[
|
|
946
892
|
getDiscriminatorConstantsFragment({
|
|
@@ -957,29 +903,17 @@ function getAccountPageFragment(scope) {
|
|
|
957
903
|
(cs) => cs.join("\n\n")
|
|
958
904
|
);
|
|
959
905
|
}
|
|
960
|
-
|
|
961
|
-
// src/fragments/discriminatorCondition.ts
|
|
962
|
-
import {
|
|
963
|
-
constantDiscriminatorNode,
|
|
964
|
-
constantValueNode,
|
|
965
|
-
constantValueNodeFromBytes,
|
|
966
|
-
isNode as isNode6,
|
|
967
|
-
isNodeFilter as isNodeFilter3
|
|
968
|
-
} from "@codama/nodes";
|
|
969
|
-
import { mapFragmentContent } from "@codama/renderers-core";
|
|
970
|
-
import { pipe as pipe3, visit as visit3 } from "@codama/visitors-core";
|
|
971
|
-
import { getBase64Decoder } from "@solana/codecs-strings";
|
|
972
906
|
function getDiscriminatorConditionFragment(scope) {
|
|
973
|
-
return
|
|
907
|
+
return pipe(
|
|
974
908
|
mergeFragments(
|
|
975
909
|
scope.discriminators.flatMap((discriminator) => {
|
|
976
|
-
if (
|
|
910
|
+
if (isNode(discriminator, "sizeDiscriminatorNode")) {
|
|
977
911
|
return [getSizeConditionFragment(discriminator, scope)];
|
|
978
912
|
}
|
|
979
|
-
if (
|
|
913
|
+
if (isNode(discriminator, "constantDiscriminatorNode")) {
|
|
980
914
|
return [getByteConditionFragment(discriminator, scope)];
|
|
981
915
|
}
|
|
982
|
-
if (
|
|
916
|
+
if (isNode(discriminator, "fieldDiscriminatorNode")) {
|
|
983
917
|
return [getFieldConditionFragment(discriminator, scope)];
|
|
984
918
|
}
|
|
985
919
|
return [];
|
|
@@ -995,7 +929,7 @@ function getSizeConditionFragment(discriminator, scope) {
|
|
|
995
929
|
}
|
|
996
930
|
function getByteConditionFragment(discriminator, scope) {
|
|
997
931
|
const { dataName, typeManifestVisitor } = scope;
|
|
998
|
-
const constant =
|
|
932
|
+
const constant = visit(discriminator.constant, typeManifestVisitor).value;
|
|
999
933
|
return fragment`${use("containsBytes", "solanaCodecsCore")}(${dataName}, ${constant}, ${discriminator.offset})`;
|
|
1000
934
|
}
|
|
1001
935
|
function getFieldConditionFragment(discriminator, scope) {
|
|
@@ -1005,7 +939,7 @@ function getFieldConditionFragment(discriminator, scope) {
|
|
|
1005
939
|
`Field discriminator "${discriminator.name}" does not have a matching argument with default value.`
|
|
1006
940
|
);
|
|
1007
941
|
}
|
|
1008
|
-
if (
|
|
942
|
+
if (isNode(field.type, "arrayTypeNode") && isNode(field.type.item, "numberTypeNode") && field.type.item.format === "u8" && isNode(field.type.count, "fixedCountNode") && isNode(field.defaultValue, "arrayValueNode") && field.defaultValue.items.every(isNodeFilter("numberValueNode"))) {
|
|
1009
943
|
const base64Bytes = getBase64Decoder().decode(
|
|
1010
944
|
new Uint8Array(field.defaultValue.items.map((node) => node.number))
|
|
1011
945
|
);
|
|
@@ -1095,19 +1029,13 @@ function getIsErrorFunctionFragment(scope) {
|
|
|
1095
1029
|
return ${use("isProgramError", "solanaPrograms")}<TProgramErrorCode>(error, transactionMessage, ${programAddressConstant}, code);
|
|
1096
1030
|
}`;
|
|
1097
1031
|
}
|
|
1098
|
-
|
|
1099
|
-
// src/fragments/indexPage.ts
|
|
1100
|
-
import { camelCase as camelCase4 } from "@codama/nodes";
|
|
1101
1032
|
function getIndexPageFragment(items) {
|
|
1102
1033
|
if (items.length === 0) return;
|
|
1103
|
-
const names = items.map((item) =>
|
|
1034
|
+
const names = items.map((item) => camelCase(item.name)).sort((a, b) => a.localeCompare(b)).map((name) => getExportAllFragment(`./${name}`));
|
|
1104
1035
|
return mergeFragments(names, (cs) => cs.join("\n"));
|
|
1105
1036
|
}
|
|
1106
|
-
|
|
1107
|
-
// src/fragments/instructionAccountMeta.ts
|
|
1108
|
-
import { pascalCase as pascalCase2 } from "@codama/nodes";
|
|
1109
1037
|
function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
1110
|
-
const typeParam = `TAccount${
|
|
1038
|
+
const typeParam = `TAccount${pascalCase(instructionAccountNode.name)}`;
|
|
1111
1039
|
if (instructionAccountNode.isSigner === true && instructionAccountNode.isWritable) {
|
|
1112
1040
|
return fragment`${use("type WritableSignerAccount", "solanaInstructions")}<${typeParam}> & ${use("type AccountSignerMeta", "solanaSigners")}<${typeParam}>`;
|
|
1113
1041
|
}
|
|
@@ -1119,21 +1047,13 @@ function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
|
1119
1047
|
}
|
|
1120
1048
|
return fragment`${use("type ReadonlyAccount", "solanaInstructions")}<${typeParam}>`;
|
|
1121
1049
|
}
|
|
1122
|
-
|
|
1123
|
-
// src/fragments/instructionAccountTypeParam.ts
|
|
1124
|
-
import { pascalCase as pascalCase3 } from "@codama/nodes";
|
|
1125
|
-
import {
|
|
1126
|
-
findInstructionNodeFromPath,
|
|
1127
|
-
findProgramNodeFromPath as findProgramNodeFromPath2,
|
|
1128
|
-
getLastNodeFromPath as getLastNodeFromPath6
|
|
1129
|
-
} from "@codama/visitors-core";
|
|
1130
1050
|
function getInstructionAccountTypeParamFragment(scope) {
|
|
1131
|
-
const { instructionAccountPath,
|
|
1132
|
-
const instructionAccountNode =
|
|
1051
|
+
const { instructionAccountPath, linkables } = scope;
|
|
1052
|
+
const instructionAccountNode = getLastNodeFromPath(instructionAccountPath);
|
|
1133
1053
|
const instructionNode = findInstructionNodeFromPath(instructionAccountPath);
|
|
1134
|
-
const programNode =
|
|
1135
|
-
const typeParam = `TAccount${
|
|
1136
|
-
const accountMeta =
|
|
1054
|
+
const programNode = findProgramNodeFromPath(instructionAccountPath);
|
|
1055
|
+
const typeParam = `TAccount${pascalCase(instructionAccountNode.name)}`;
|
|
1056
|
+
const accountMeta = fragment` | ${use("type AccountMeta", "solanaInstructions")}<string>` ;
|
|
1137
1057
|
if (instructionNode.optionalAccountStrategy === "omitted" && instructionAccountNode.isOptional) {
|
|
1138
1058
|
return fragment`${typeParam} extends string${accountMeta} | undefined = undefined`;
|
|
1139
1059
|
}
|
|
@@ -1153,13 +1073,8 @@ function getDefaultAddress(defaultValue, programId, linkables) {
|
|
|
1153
1073
|
return "string";
|
|
1154
1074
|
}
|
|
1155
1075
|
}
|
|
1156
|
-
|
|
1157
|
-
// src/fragments/instructionByteDelta.ts
|
|
1158
|
-
import { assertIsNode, camelCase as camelCase5, isNode as isNode7 } from "@codama/nodes";
|
|
1159
|
-
import { mapFragmentContent as mapFragmentContent2 } from "@codama/renderers-core";
|
|
1160
|
-
import { getLastNodeFromPath as getLastNodeFromPath7, pipe as pipe4 } from "@codama/visitors-core";
|
|
1161
1076
|
function getInstructionByteDeltaFragment(scope) {
|
|
1162
|
-
const { byteDeltas } =
|
|
1077
|
+
const { byteDeltas } = getLastNodeFromPath(scope.instructionPath);
|
|
1163
1078
|
const fragments = (byteDeltas ?? []).flatMap((c) => getByteDeltaFragment(c, scope));
|
|
1164
1079
|
if (fragments.length === 0) return;
|
|
1165
1080
|
return mergeFragments(
|
|
@@ -1170,16 +1085,16 @@ const byteDelta: number = [${c.join(",")}].reduce((a, b) => a + b, 0);`
|
|
|
1170
1085
|
}
|
|
1171
1086
|
function getByteDeltaFragment(byteDelta, scope) {
|
|
1172
1087
|
let bytesFragment = (() => {
|
|
1173
|
-
if (
|
|
1088
|
+
if (isNode(byteDelta.value, "numberValueNode")) {
|
|
1174
1089
|
return getNumberValueNodeFragment(byteDelta);
|
|
1175
1090
|
}
|
|
1176
|
-
if (
|
|
1091
|
+
if (isNode(byteDelta.value, "argumentValueNode")) {
|
|
1177
1092
|
return getArgumentValueNodeFragment(byteDelta);
|
|
1178
1093
|
}
|
|
1179
|
-
if (
|
|
1094
|
+
if (isNode(byteDelta.value, "accountLinkNode")) {
|
|
1180
1095
|
return getAccountLinkNodeFragment(byteDelta, scope);
|
|
1181
1096
|
}
|
|
1182
|
-
if (
|
|
1097
|
+
if (isNode(byteDelta.value, "resolverValueNode")) {
|
|
1183
1098
|
return getResolverValueNodeFragment(byteDelta, scope);
|
|
1184
1099
|
}
|
|
1185
1100
|
return null;
|
|
@@ -1189,7 +1104,7 @@ function getByteDeltaFragment(byteDelta, scope) {
|
|
|
1189
1104
|
bytesFragment = fragment`${bytesFragment} + ${use("BASE_ACCOUNT_SIZE", "solanaAccounts")}`;
|
|
1190
1105
|
}
|
|
1191
1106
|
if (byteDelta.subtract) {
|
|
1192
|
-
bytesFragment =
|
|
1107
|
+
bytesFragment = pipe(bytesFragment, (f) => mapFragmentContent(f, (c) => `- (${c})`));
|
|
1193
1108
|
}
|
|
1194
1109
|
return [bytesFragment];
|
|
1195
1110
|
}
|
|
@@ -1199,7 +1114,7 @@ function getNumberValueNodeFragment(byteDelta) {
|
|
|
1199
1114
|
}
|
|
1200
1115
|
function getArgumentValueNodeFragment(byteDelta) {
|
|
1201
1116
|
assertIsNode(byteDelta.value, "argumentValueNode");
|
|
1202
|
-
const argumentName =
|
|
1117
|
+
const argumentName = camelCase(byteDelta.value.name);
|
|
1203
1118
|
return fragment`Number(args.${argumentName})`;
|
|
1204
1119
|
}
|
|
1205
1120
|
function getAccountLinkNodeFragment(byteDelta, scope) {
|
|
@@ -1219,59 +1134,32 @@ function getResolverValueNodeFragment(byteDelta, scope) {
|
|
|
1219
1134
|
scope.nameApi.resolverFunction(byteDelta.value.name),
|
|
1220
1135
|
scope.getImportFrom(byteDelta.value)
|
|
1221
1136
|
);
|
|
1222
|
-
return
|
|
1137
|
+
return pipe(
|
|
1223
1138
|
fragment`${awaitKeyword}${functionName}(resolverScope)`,
|
|
1224
1139
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1225
1140
|
);
|
|
1226
1141
|
}
|
|
1227
|
-
|
|
1228
|
-
// src/fragments/instructionData.ts
|
|
1229
|
-
import { structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes2 } from "@codama/nodes";
|
|
1230
|
-
import { getLastNodeFromPath as getLastNodeFromPath8 } from "@codama/visitors-core";
|
|
1231
1142
|
function getInstructionDataFragment(scope) {
|
|
1232
1143
|
const { instructionPath, dataArgsManifest, nameApi, customInstructionData } = scope;
|
|
1233
|
-
const instructionNode =
|
|
1144
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1234
1145
|
if (instructionNode.arguments.length === 0 || customInstructionData.has(instructionNode.name)) return;
|
|
1235
1146
|
const instructionDataName = nameApi.instructionDataType(instructionNode.name);
|
|
1236
1147
|
return getTypeWithCodecFragment({
|
|
1237
1148
|
manifest: dataArgsManifest,
|
|
1238
1149
|
name: instructionDataName,
|
|
1239
1150
|
nameApi,
|
|
1240
|
-
node:
|
|
1151
|
+
node: structTypeNodeFromInstructionArgumentNodes(instructionNode.arguments),
|
|
1241
1152
|
size: scope.size
|
|
1242
1153
|
});
|
|
1243
1154
|
}
|
|
1244
|
-
|
|
1245
|
-
// src/fragments/instructionExtraArgs.ts
|
|
1246
|
-
import { mapFragmentContent as mapFragmentContent3 } from "@codama/renderers-core";
|
|
1247
|
-
import { getLastNodeFromPath as getLastNodeFromPath9 } from "@codama/visitors-core";
|
|
1248
1155
|
function getInstructionExtraArgsFragment(scope) {
|
|
1249
1156
|
const { instructionPath, extraArgsManifest, nameApi } = scope;
|
|
1250
|
-
const instructionNode =
|
|
1157
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1251
1158
|
if ((instructionNode.extraArguments ?? []).length === 0) return;
|
|
1252
1159
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
1253
1160
|
const looseName = nameApi.dataArgsType(instructionExtraName);
|
|
1254
|
-
return
|
|
1161
|
+
return mapFragmentContent(extraArgsManifest.looseType, (c) => `export type ${looseName} = ${c};`);
|
|
1255
1162
|
}
|
|
1256
|
-
|
|
1257
|
-
// src/fragments/instructionFunction.ts
|
|
1258
|
-
import { camelCase as camelCase10, isNode as isNode12, isNodeFilter as isNodeFilter4, pascalCase as pascalCase5 } from "@codama/nodes";
|
|
1259
|
-
import { mapFragmentContent as mapFragmentContent7 } from "@codama/renderers-core";
|
|
1260
|
-
import {
|
|
1261
|
-
findProgramNodeFromPath as findProgramNodeFromPath3,
|
|
1262
|
-
getLastNodeFromPath as getLastNodeFromPath13,
|
|
1263
|
-
pipe as pipe8
|
|
1264
|
-
} from "@codama/visitors-core";
|
|
1265
|
-
|
|
1266
|
-
// src/fragments/instructionInputResolved.ts
|
|
1267
|
-
import { camelCase as camelCase7, isNode as isNode9, parseOptionalAccountStrategy } from "@codama/nodes";
|
|
1268
|
-
import { mapFragmentContent as mapFragmentContent5 } from "@codama/renderers-core";
|
|
1269
|
-
import { getLastNodeFromPath as getLastNodeFromPath10 } from "@codama/visitors-core";
|
|
1270
|
-
|
|
1271
|
-
// src/fragments/instructionInputDefault.ts
|
|
1272
|
-
import { camelCase as camelCase6, isNode as isNode8 } from "@codama/nodes";
|
|
1273
|
-
import { mapFragmentContent as mapFragmentContent4, setFragmentContent } from "@codama/renderers-core";
|
|
1274
|
-
import { pipe as pipe5, visit as visit4 } from "@codama/visitors-core";
|
|
1275
1163
|
function getInstructionInputDefaultFragment(scope) {
|
|
1276
1164
|
const { input, optionalAccountStrategy, asyncResolvers, useAsync, nameApi, typeManifestVisitor, getImportFrom } = scope;
|
|
1277
1165
|
if (!input.defaultValue) {
|
|
@@ -1282,8 +1170,8 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1282
1170
|
}
|
|
1283
1171
|
const { defaultValue } = input;
|
|
1284
1172
|
const defaultFragment = (renderedValue, isWritable) => {
|
|
1285
|
-
const inputName =
|
|
1286
|
-
if (input.kind === "instructionAccountNode" &&
|
|
1173
|
+
const inputName = camelCase(input.name);
|
|
1174
|
+
if (input.kind === "instructionAccountNode" && isNode(defaultValue, "resolverValueNode")) {
|
|
1287
1175
|
return fragment`accounts.${inputName} = { ...accounts.${inputName}, ...${renderedValue} };`;
|
|
1288
1176
|
}
|
|
1289
1177
|
if (input.kind === "instructionAccountNode" && isWritable === void 0) {
|
|
@@ -1301,7 +1189,7 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1301
1189
|
const addressType = use("type Address", "solanaAddresses");
|
|
1302
1190
|
switch (defaultValue.kind) {
|
|
1303
1191
|
case "accountValueNode":
|
|
1304
|
-
const name =
|
|
1192
|
+
const name = camelCase(defaultValue.name);
|
|
1305
1193
|
if (input.kind === "instructionAccountNode" && input.resolvedIsSigner && !input.isSigner) {
|
|
1306
1194
|
return defaultFragment(fragment`${expectTransactionSigner}(accounts.${name}.value).address`);
|
|
1307
1195
|
}
|
|
@@ -1311,13 +1199,13 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1311
1199
|
return defaultFragment(fragment`${expectAddress}(accounts.${name}.value)`);
|
|
1312
1200
|
case "pdaValueNode":
|
|
1313
1201
|
let pdaProgramValue;
|
|
1314
|
-
if (
|
|
1315
|
-
pdaProgramValue = fragment`${expectAddress}(accounts.${
|
|
1202
|
+
if (isNode(defaultValue.programId, "accountValueNode")) {
|
|
1203
|
+
pdaProgramValue = fragment`${expectAddress}(accounts.${camelCase(defaultValue.programId.name)}.value)`;
|
|
1316
1204
|
}
|
|
1317
|
-
if (
|
|
1318
|
-
pdaProgramValue = fragment`${expectAddress}(args.${
|
|
1205
|
+
if (isNode(defaultValue.programId, "argumentValueNode")) {
|
|
1206
|
+
pdaProgramValue = fragment`${expectAddress}(args.${camelCase(defaultValue.programId.name)})`;
|
|
1319
1207
|
}
|
|
1320
|
-
if (
|
|
1208
|
+
if (isNode(defaultValue.pda, "pdaNode")) {
|
|
1321
1209
|
let pdaProgram = fragment`programAddress`;
|
|
1322
1210
|
if (pdaProgramValue) {
|
|
1323
1211
|
pdaProgram = pdaProgramValue;
|
|
@@ -1325,29 +1213,29 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1325
1213
|
pdaProgram = fragment`'${defaultValue.pda.programId}' as ${addressType}<'${defaultValue.pda.programId}'>`;
|
|
1326
1214
|
}
|
|
1327
1215
|
const pdaSeeds2 = defaultValue.pda.seeds.flatMap((seed) => {
|
|
1328
|
-
if (
|
|
1216
|
+
if (isNode(seed, "constantPdaSeedNode") && isNode(seed.value, "programIdValueNode")) {
|
|
1329
1217
|
return [fragment`${use("getAddressEncoder", "solanaAddresses")}().encode(${pdaProgram})`];
|
|
1330
1218
|
}
|
|
1331
|
-
if (
|
|
1332
|
-
const typeManifest2 =
|
|
1333
|
-
const valueManifest2 =
|
|
1219
|
+
if (isNode(seed, "constantPdaSeedNode") && !isNode(seed.value, "programIdValueNode")) {
|
|
1220
|
+
const typeManifest2 = visit(seed.type, typeManifestVisitor);
|
|
1221
|
+
const valueManifest2 = visit(seed.value, typeManifestVisitor);
|
|
1334
1222
|
return [fragment`${typeManifest2.encoder}.encode(${valueManifest2.value})`];
|
|
1335
1223
|
}
|
|
1336
|
-
if (
|
|
1337
|
-
const typeManifest2 =
|
|
1224
|
+
if (isNode(seed, "variablePdaSeedNode")) {
|
|
1225
|
+
const typeManifest2 = visit(seed.type, typeManifestVisitor);
|
|
1338
1226
|
const valueSeed = defaultValue.seeds.find((s) => s.name === seed.name)?.value;
|
|
1339
1227
|
if (!valueSeed) return [];
|
|
1340
|
-
if (
|
|
1228
|
+
if (isNode(valueSeed, "accountValueNode")) {
|
|
1341
1229
|
return [
|
|
1342
|
-
fragment`${typeManifest2.encoder}.encode(${expectAddress}(accounts.${
|
|
1230
|
+
fragment`${typeManifest2.encoder}.encode(${expectAddress}(accounts.${camelCase(valueSeed.name)}.value))`
|
|
1343
1231
|
];
|
|
1344
1232
|
}
|
|
1345
|
-
if (
|
|
1233
|
+
if (isNode(valueSeed, "argumentValueNode")) {
|
|
1346
1234
|
return [
|
|
1347
|
-
fragment`${typeManifest2.encoder}.encode(${expectSome}(args.${
|
|
1235
|
+
fragment`${typeManifest2.encoder}.encode(${expectSome}(args.${camelCase(valueSeed.name)}))`
|
|
1348
1236
|
];
|
|
1349
1237
|
}
|
|
1350
|
-
const valueManifest2 =
|
|
1238
|
+
const valueManifest2 = visit(valueSeed, typeManifestVisitor);
|
|
1351
1239
|
return [fragment`${typeManifest2.encoder}.encode(${valueManifest2.value})`];
|
|
1352
1240
|
}
|
|
1353
1241
|
return [];
|
|
@@ -1362,20 +1250,20 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1362
1250
|
const pdaFunction = use(nameApi.pdaFindFunction(defaultValue.pda.name), getImportFrom(defaultValue.pda));
|
|
1363
1251
|
const pdaArgs = [];
|
|
1364
1252
|
const pdaSeeds = defaultValue.seeds.map((seed) => {
|
|
1365
|
-
if (
|
|
1366
|
-
return fragment`${seed.name}: ${expectAddress}(accounts.${
|
|
1253
|
+
if (isNode(seed.value, "accountValueNode")) {
|
|
1254
|
+
return fragment`${seed.name}: ${expectAddress}(accounts.${camelCase(seed.value.name)}.value)`;
|
|
1367
1255
|
}
|
|
1368
|
-
if (
|
|
1369
|
-
return fragment`${seed.name}: ${expectSome}(args.${
|
|
1256
|
+
if (isNode(seed.value, "argumentValueNode")) {
|
|
1257
|
+
return fragment`${seed.name}: ${expectSome}(args.${camelCase(seed.value.name)})`;
|
|
1370
1258
|
}
|
|
1371
|
-
return
|
|
1372
|
-
|
|
1373
|
-
(f) =>
|
|
1259
|
+
return pipe(
|
|
1260
|
+
visit(seed.value, typeManifestVisitor).value,
|
|
1261
|
+
(f) => mapFragmentContent(f, (c) => `${seed.name}: ${c}`)
|
|
1374
1262
|
);
|
|
1375
1263
|
});
|
|
1376
|
-
const pdaSeedsFragment =
|
|
1264
|
+
const pdaSeedsFragment = pipe(
|
|
1377
1265
|
mergeFragments(pdaSeeds, (renders) => renders.join(", ")),
|
|
1378
|
-
(f) =>
|
|
1266
|
+
(f) => mapFragmentContent(f, (c) => `{ ${c} }`)
|
|
1379
1267
|
);
|
|
1380
1268
|
if (pdaSeeds.length > 0) {
|
|
1381
1269
|
pdaArgs.push(pdaSeedsFragment);
|
|
@@ -1401,14 +1289,14 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1401
1289
|
return fragment``;
|
|
1402
1290
|
case "accountBumpValueNode":
|
|
1403
1291
|
return defaultFragment(
|
|
1404
|
-
fragment`${expectProgramDerivedAddress}(accounts.${
|
|
1292
|
+
fragment`${expectProgramDerivedAddress}(accounts.${camelCase(defaultValue.name)}.value)[1]`
|
|
1405
1293
|
);
|
|
1406
1294
|
case "argumentValueNode":
|
|
1407
|
-
return defaultFragment(fragment`${expectSome}(args.${
|
|
1295
|
+
return defaultFragment(fragment`${expectSome}(args.${camelCase(defaultValue.name)})`);
|
|
1408
1296
|
case "resolverValueNode":
|
|
1409
1297
|
const resolverFunction = use(nameApi.resolverFunction(defaultValue.name), getImportFrom(defaultValue));
|
|
1410
1298
|
const resolverAwait = useAsync && asyncResolvers.includes(defaultValue.name) ? "await " : "";
|
|
1411
|
-
return
|
|
1299
|
+
return pipe(
|
|
1412
1300
|
defaultFragment(fragment`${resolverAwait}${resolverFunction}(resolverScope)`),
|
|
1413
1301
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1414
1302
|
);
|
|
@@ -1433,10 +1321,10 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1433
1321
|
}
|
|
1434
1322
|
const negatedCondition = !ifTrueRenderer;
|
|
1435
1323
|
let condition = "true";
|
|
1436
|
-
if (
|
|
1324
|
+
if (isNode(defaultValue.condition, "resolverValueNode")) {
|
|
1437
1325
|
const conditionalResolverFunction = nameApi.resolverFunction(defaultValue.condition.name);
|
|
1438
1326
|
const module = getImportFrom(defaultValue.condition);
|
|
1439
|
-
conditionalFragment =
|
|
1327
|
+
conditionalFragment = pipe(
|
|
1440
1328
|
conditionalFragment,
|
|
1441
1329
|
(f) => addFragmentImports(f, module, [conditionalResolverFunction]),
|
|
1442
1330
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
@@ -1445,9 +1333,9 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1445
1333
|
condition = `${conditionalResolverAwait}${conditionalResolverFunction}(resolverScope)`;
|
|
1446
1334
|
condition = negatedCondition ? `!${condition}` : condition;
|
|
1447
1335
|
} else {
|
|
1448
|
-
const comparedInputName =
|
|
1336
|
+
const comparedInputName = isNode(defaultValue.condition, "accountValueNode") ? `accounts.${camelCase(defaultValue.condition.name)}.value` : `args.${camelCase(defaultValue.condition.name)}`;
|
|
1449
1337
|
if (defaultValue.value) {
|
|
1450
|
-
const comparedValue =
|
|
1338
|
+
const comparedValue = visit(defaultValue.value, typeManifestVisitor).value;
|
|
1451
1339
|
conditionalFragment = mergeFragments([conditionalFragment, comparedValue], (c) => c[0]);
|
|
1452
1340
|
const operator = negatedCondition ? "!==" : "===";
|
|
1453
1341
|
condition = `${comparedInputName} ${operator} ${comparedValue.content}`;
|
|
@@ -1472,7 +1360,7 @@ ${ifTrueRenderer ? ifTrueRenderer.content : ifFalseRenderer?.content}
|
|
|
1472
1360
|
}`
|
|
1473
1361
|
);
|
|
1474
1362
|
default:
|
|
1475
|
-
const valueManifest =
|
|
1363
|
+
const valueManifest = visit(defaultValue, typeManifestVisitor).value;
|
|
1476
1364
|
return defaultFragment(valueManifest);
|
|
1477
1365
|
}
|
|
1478
1366
|
}
|
|
@@ -1487,7 +1375,7 @@ function renderNestedInstructionDefault(scope) {
|
|
|
1487
1375
|
|
|
1488
1376
|
// src/fragments/instructionInputResolved.ts
|
|
1489
1377
|
function getInstructionInputResolvedFragment(scope) {
|
|
1490
|
-
const instructionNode =
|
|
1378
|
+
const instructionNode = getLastNodeFromPath(scope.instructionPath);
|
|
1491
1379
|
const resolvedInputFragments = scope.resolvedInputs.flatMap((input) => {
|
|
1492
1380
|
const inputFragment = getInstructionInputDefaultFragment({
|
|
1493
1381
|
...scope,
|
|
@@ -1495,11 +1383,11 @@ function getInstructionInputResolvedFragment(scope) {
|
|
|
1495
1383
|
optionalAccountStrategy: parseOptionalAccountStrategy(instructionNode.optionalAccountStrategy)
|
|
1496
1384
|
});
|
|
1497
1385
|
if (!inputFragment.content) return [];
|
|
1498
|
-
const camelName =
|
|
1386
|
+
const camelName = camelCase(input.name);
|
|
1499
1387
|
return [
|
|
1500
|
-
|
|
1388
|
+
mapFragmentContent(
|
|
1501
1389
|
inputFragment,
|
|
1502
|
-
(c) =>
|
|
1390
|
+
(c) => isNode(input, "instructionArgumentNode") ? `if (!args.${camelName}) {
|
|
1503
1391
|
${c}
|
|
1504
1392
|
}` : `if (!accounts.${camelName}.value) {
|
|
1505
1393
|
${c}
|
|
@@ -1512,27 +1400,14 @@ ${c}
|
|
|
1512
1400
|
}
|
|
1513
1401
|
return mergeFragments([fragment`// Resolve default values.`, ...resolvedInputFragments], (c) => c.join("\n"));
|
|
1514
1402
|
}
|
|
1515
|
-
|
|
1516
|
-
// src/fragments/instructionInputType.ts
|
|
1517
|
-
import {
|
|
1518
|
-
camelCase as camelCase8,
|
|
1519
|
-
getAllInstructionArguments,
|
|
1520
|
-
isNode as isNode10,
|
|
1521
|
-
pascalCase as pascalCase4
|
|
1522
|
-
} from "@codama/nodes";
|
|
1523
|
-
import { mapFragmentContent as mapFragmentContent6 } from "@codama/renderers-core";
|
|
1524
|
-
import {
|
|
1525
|
-
getLastNodeFromPath as getLastNodeFromPath11,
|
|
1526
|
-
pipe as pipe6
|
|
1527
|
-
} from "@codama/visitors-core";
|
|
1528
1403
|
function getInstructionInputTypeFragment(scope) {
|
|
1529
1404
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
1530
|
-
const instructionNode =
|
|
1405
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1531
1406
|
const instructionInputType = useAsync ? nameApi.instructionAsyncInputType(instructionNode.name) : nameApi.instructionSyncInputType(instructionNode.name);
|
|
1532
1407
|
const [dataArgumentsFragment, customDataArgumentsFragment] = getDataArgumentsFragments(scope);
|
|
1533
1408
|
let accountTypeParams = "";
|
|
1534
1409
|
if (instructionNode.accounts.length > 0) {
|
|
1535
|
-
accountTypeParams = instructionNode.accounts.map((account) => `TAccount${
|
|
1410
|
+
accountTypeParams = instructionNode.accounts.map((account) => `TAccount${pascalCase(account.name)} extends string = string`).join(", ");
|
|
1536
1411
|
accountTypeParams = `<${accountTypeParams}>`;
|
|
1537
1412
|
}
|
|
1538
1413
|
const typeBodyFragment = mergeFragments(
|
|
@@ -1550,20 +1425,20 @@ function getInstructionInputTypeFragment(scope) {
|
|
|
1550
1425
|
}
|
|
1551
1426
|
function getAccountsFragment(scope) {
|
|
1552
1427
|
const { instructionPath, resolvedInputs, useAsync, asyncResolvers } = scope;
|
|
1553
|
-
const instructionNode =
|
|
1428
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1554
1429
|
const fragments = instructionNode.accounts.map((account) => {
|
|
1555
1430
|
const resolvedAccount = resolvedInputs.find(
|
|
1556
1431
|
(input) => input.kind === "instructionAccountNode" && input.name === account.name
|
|
1557
1432
|
);
|
|
1558
|
-
const hasDefaultValue = !!resolvedAccount.defaultValue && !
|
|
1433
|
+
const hasDefaultValue = !!resolvedAccount.defaultValue && !isNode(resolvedAccount.defaultValue, ["identityValueNode", "payerValueNode"]) && (useAsync || !isAsyncDefaultValue(resolvedAccount.defaultValue, asyncResolvers));
|
|
1559
1434
|
const docs = getDocblockFragment(account.docs ?? [], true);
|
|
1560
1435
|
const optionalSign = hasDefaultValue || resolvedAccount.isOptional ? "?" : "";
|
|
1561
|
-
return fragment`${docs}${
|
|
1436
|
+
return fragment`${docs}${camelCase(account.name)}${optionalSign}: ${getAccountTypeFragment2(resolvedAccount)};`;
|
|
1562
1437
|
});
|
|
1563
1438
|
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1564
1439
|
}
|
|
1565
1440
|
function getAccountTypeFragment2(account) {
|
|
1566
|
-
const typeParam = `TAccount${
|
|
1441
|
+
const typeParam = `TAccount${pascalCase(account.name)}`;
|
|
1567
1442
|
const address = use("type Address", "solanaAddresses");
|
|
1568
1443
|
const signer = use("type TransactionSigner", "solanaSigners");
|
|
1569
1444
|
const pda = use("type ProgramDerivedAddress", "solanaAddresses");
|
|
@@ -1575,15 +1450,15 @@ function getAccountTypeFragment2(account) {
|
|
|
1575
1450
|
}
|
|
1576
1451
|
function getDataArgumentsFragments(scope) {
|
|
1577
1452
|
const { instructionPath, nameApi } = scope;
|
|
1578
|
-
const instructionNode =
|
|
1453
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1579
1454
|
const customData = scope.customInstructionData.get(instructionNode.name);
|
|
1580
1455
|
if (customData) {
|
|
1581
1456
|
return [
|
|
1582
1457
|
void 0,
|
|
1583
|
-
|
|
1458
|
+
pipe(
|
|
1584
1459
|
fragment`${nameApi.dataArgsType(customData.importAs)}`,
|
|
1585
1460
|
(f) => mergeFragmentImports(f, [scope.dataArgsManifest.looseType.imports]),
|
|
1586
|
-
(f) =>
|
|
1461
|
+
(f) => mapFragmentContent(f, (c) => `${c} & `)
|
|
1587
1462
|
)
|
|
1588
1463
|
];
|
|
1589
1464
|
}
|
|
@@ -1597,7 +1472,7 @@ function getDataArgumentsFragments(scope) {
|
|
|
1597
1472
|
}
|
|
1598
1473
|
function getExtraArgumentsFragment(scope) {
|
|
1599
1474
|
const { instructionPath, nameApi } = scope;
|
|
1600
|
-
const instructionNode =
|
|
1475
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1601
1476
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
1602
1477
|
const extraArgsType = nameApi.dataArgsType(instructionExtraName);
|
|
1603
1478
|
const fragments = (instructionNode.extraArguments ?? []).flatMap((arg) => {
|
|
@@ -1609,16 +1484,16 @@ function getExtraArgumentsFragment(scope) {
|
|
|
1609
1484
|
}
|
|
1610
1485
|
function getArgumentFragment(arg, argsType, resolvedInputs, renamedArgs) {
|
|
1611
1486
|
const resolvedArg = resolvedInputs.find(
|
|
1612
|
-
(input) =>
|
|
1487
|
+
(input) => isNode(input, "instructionArgumentNode") && input.name === arg.name
|
|
1613
1488
|
);
|
|
1614
1489
|
if (arg.defaultValue && arg.defaultValueStrategy === "omitted") return null;
|
|
1615
1490
|
const renamedName = renamedArgs.get(arg.name) ?? arg.name;
|
|
1616
1491
|
const optionalSign = arg.defaultValue || resolvedArg?.defaultValue ? "?" : "";
|
|
1617
|
-
return fragment`${
|
|
1492
|
+
return fragment`${camelCase(renamedName)}${optionalSign}: ${argsType}["${camelCase(arg.name)}"];`;
|
|
1618
1493
|
}
|
|
1619
1494
|
function getRemainingAccountsFragment(instructionNode) {
|
|
1620
1495
|
const fragments = (instructionNode.remainingAccounts ?? []).flatMap((remainingAccountsNode) => {
|
|
1621
|
-
if (
|
|
1496
|
+
if (isNode(remainingAccountsNode.value, "resolverValueNode")) return [];
|
|
1622
1497
|
const { name } = remainingAccountsNode.value;
|
|
1623
1498
|
const allArguments = getAllInstructionArguments(instructionNode);
|
|
1624
1499
|
const argumentExists = allArguments.some((arg) => arg.name === name);
|
|
@@ -1631,25 +1506,16 @@ function getRemainingAccountsFragment(instructionNode) {
|
|
|
1631
1506
|
if (isSigner === "either") return fragment`${signerFragment} | ${addressFragment}`;
|
|
1632
1507
|
return isSigner ? signerFragment : addressFragment;
|
|
1633
1508
|
})();
|
|
1634
|
-
return fragment`${
|
|
1509
|
+
return fragment`${camelCase(name)}${optionalSign}: Array<${typeFragment}>;`;
|
|
1635
1510
|
});
|
|
1636
1511
|
if (fragments.length === 0) return;
|
|
1637
1512
|
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1638
1513
|
}
|
|
1639
|
-
|
|
1640
|
-
// src/fragments/instructionRemainingAccounts.ts
|
|
1641
|
-
import {
|
|
1642
|
-
assertIsNode as assertIsNode2,
|
|
1643
|
-
camelCase as camelCase9,
|
|
1644
|
-
getAllInstructionArguments as getAllInstructionArguments2,
|
|
1645
|
-
isNode as isNode11
|
|
1646
|
-
} from "@codama/nodes";
|
|
1647
|
-
import { getLastNodeFromPath as getLastNodeFromPath12, pipe as pipe7 } from "@codama/visitors-core";
|
|
1648
1514
|
function getInstructionRemainingAccountsFragment(scope) {
|
|
1649
|
-
const { remainingAccounts } =
|
|
1515
|
+
const { remainingAccounts } = getLastNodeFromPath(scope.instructionPath);
|
|
1650
1516
|
const fragments = (remainingAccounts ?? []).flatMap((a) => getRemainingAccountsFragment2(a, scope));
|
|
1651
1517
|
if (fragments.length === 0) return;
|
|
1652
|
-
return
|
|
1518
|
+
return pipe(
|
|
1653
1519
|
mergeFragments(
|
|
1654
1520
|
fragments,
|
|
1655
1521
|
(c) => `// Remaining accounts.
|
|
@@ -1660,10 +1526,10 @@ const remainingAccounts: AccountMeta[] = ${c.length === 1 ? c[0] : `[...${c.join
|
|
|
1660
1526
|
}
|
|
1661
1527
|
function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
1662
1528
|
const remainingAccountsFragment = (() => {
|
|
1663
|
-
if (
|
|
1529
|
+
if (isNode(remainingAccounts.value, "argumentValueNode")) {
|
|
1664
1530
|
return getArgumentValueNodeFragment2(remainingAccounts, scope);
|
|
1665
1531
|
}
|
|
1666
|
-
if (
|
|
1532
|
+
if (isNode(remainingAccounts.value, "resolverValueNode")) {
|
|
1667
1533
|
return getResolverValueNodeFragment2(remainingAccounts, scope);
|
|
1668
1534
|
}
|
|
1669
1535
|
return null;
|
|
@@ -1672,9 +1538,9 @@ function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
|
1672
1538
|
return [remainingAccountsFragment];
|
|
1673
1539
|
}
|
|
1674
1540
|
function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
1675
|
-
const instructionNode =
|
|
1676
|
-
|
|
1677
|
-
const argumentName =
|
|
1541
|
+
const instructionNode = getLastNodeFromPath(scope.instructionPath);
|
|
1542
|
+
assertIsNode(remainingAccounts.value, "argumentValueNode");
|
|
1543
|
+
const argumentName = camelCase(remainingAccounts.value.name);
|
|
1678
1544
|
const isOptional = remainingAccounts.isOptional ?? false;
|
|
1679
1545
|
const isSigner = remainingAccounts.isSigner ?? false;
|
|
1680
1546
|
const isWritable = remainingAccounts.isWritable ?? false;
|
|
@@ -1683,7 +1549,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1683
1549
|
const signerRole = isWritable ? fragment`${accountRole}.WRITABLE_SIGNER` : fragment`${accountRole}.READONLY_SIGNER`;
|
|
1684
1550
|
const role = isSigner === true ? signerRole : nonSignerRole;
|
|
1685
1551
|
const argumentArray = isOptional ? `(args.${argumentName} ?? [])` : `args.${argumentName}`;
|
|
1686
|
-
const allArguments =
|
|
1552
|
+
const allArguments = getAllInstructionArguments(instructionNode);
|
|
1687
1553
|
const argumentExists = allArguments.some((arg) => arg.name === remainingAccounts.value.name);
|
|
1688
1554
|
if (argumentExists || isSigner === false) {
|
|
1689
1555
|
return fragment`${argumentArray}.map((address) => ({ address, role: ${role} }))`;
|
|
@@ -1694,7 +1560,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1694
1560
|
return fragment`${argumentArray}.map((signer) => ({ address: signer.address, role: ${signerRole}, signer }))`;
|
|
1695
1561
|
}
|
|
1696
1562
|
function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
1697
|
-
|
|
1563
|
+
assertIsNode(remainingAccounts.value, "resolverValueNode");
|
|
1698
1564
|
const isAsync = scope.asyncResolvers.includes(remainingAccounts.value.name);
|
|
1699
1565
|
if (!scope.useAsync && isAsync) return null;
|
|
1700
1566
|
const awaitKeyword = scope.useAsync && isAsync ? "await " : "";
|
|
@@ -1702,7 +1568,7 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1702
1568
|
scope.nameApi.resolverFunction(remainingAccounts.value.name),
|
|
1703
1569
|
scope.getImportFrom(remainingAccounts.value)
|
|
1704
1570
|
);
|
|
1705
|
-
return
|
|
1571
|
+
return pipe(
|
|
1706
1572
|
fragment`${awaitKeyword}${functionName}(resolverScope)`,
|
|
1707
1573
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1708
1574
|
);
|
|
@@ -1711,13 +1577,13 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1711
1577
|
// src/fragments/instructionFunction.ts
|
|
1712
1578
|
function getInstructionFunctionFragment(scope) {
|
|
1713
1579
|
const { useAsync, instructionPath, resolvedInputs, renamedArgs, asyncResolvers, nameApi, customInstructionData } = scope;
|
|
1714
|
-
const instructionNode =
|
|
1715
|
-
const programNode =
|
|
1580
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1581
|
+
const programNode = findProgramNodeFromPath(instructionPath);
|
|
1716
1582
|
if (useAsync && !hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers)) return;
|
|
1717
1583
|
const customData = customInstructionData.get(instructionNode.name);
|
|
1718
1584
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
1719
1585
|
const instructionDependencies = getInstructionDependencies(instructionNode, asyncResolvers, useAsync);
|
|
1720
|
-
const argDependencies = instructionDependencies.filter(
|
|
1586
|
+
const argDependencies = instructionDependencies.filter(isNodeFilter("argumentValueNode")).map((node) => node.name);
|
|
1721
1587
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
1722
1588
|
const argIsNotOmitted = (arg) => !(arg.defaultValue && arg.defaultValueStrategy === "omitted");
|
|
1723
1589
|
const argIsDependent = (arg) => argDependencies.includes(arg.name);
|
|
@@ -1730,7 +1596,7 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1730
1596
|
const hasExtraArgs = (instructionNode.extraArguments ?? []).filter(
|
|
1731
1597
|
(field) => argIsNotOmitted(field) && (argIsDependent(field) || argHasDefaultValue(field))
|
|
1732
1598
|
).length > 0;
|
|
1733
|
-
const hasRemainingAccountArgs = (instructionNode.remainingAccounts ?? []).filter(({ value }) =>
|
|
1599
|
+
const hasRemainingAccountArgs = (instructionNode.remainingAccounts ?? []).filter(({ value }) => isNode(value, "argumentValueNode")).length > 0;
|
|
1734
1600
|
const hasAnyArgs = hasDataArgs || hasExtraArgs || hasRemainingAccountArgs;
|
|
1735
1601
|
const hasInput = hasAccounts || hasAnyArgs;
|
|
1736
1602
|
const programAddressConstant = use(nameApi.programAddressConstant(programNode.name), "generatedPrograms");
|
|
@@ -1749,7 +1615,7 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1749
1615
|
const typeParams = getTypeParamsFragment(instructionNode, programAddressConstant);
|
|
1750
1616
|
const returnType = getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync);
|
|
1751
1617
|
const inputType = getInstructionInputTypeFragment(scope);
|
|
1752
|
-
const inputArg =
|
|
1618
|
+
const inputArg = mapFragmentContent(getInputTypeCallFragment(scope), (c) => hasInput ? `input: ${c}, ` : "");
|
|
1753
1619
|
const functionBody = mergeFragments(
|
|
1754
1620
|
[
|
|
1755
1621
|
getProgramAddressInitializationFragment(programAddressConstant),
|
|
@@ -1781,7 +1647,7 @@ function getAccountsInitializationFragment(instructionNode) {
|
|
|
1781
1647
|
if (instructionNode.accounts.length === 0) return;
|
|
1782
1648
|
const accounts = mergeFragments(
|
|
1783
1649
|
instructionNode.accounts.map((account) => {
|
|
1784
|
-
const name =
|
|
1650
|
+
const name = camelCase(account.name);
|
|
1785
1651
|
const isWritable = account.isWritable ? "true" : "false";
|
|
1786
1652
|
return fragment`${name}: { value: input.${name} ?? null, isWritable: ${isWritable} }`;
|
|
1787
1653
|
}),
|
|
@@ -1816,7 +1682,7 @@ function getReturnStatementFragment(scope) {
|
|
|
1816
1682
|
const hasLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted" && instructionNode.accounts.some((account) => account.isOptional);
|
|
1817
1683
|
const getAccountMeta = hasAccounts ? fragment`const getAccountMeta = ${use("getAccountMetaFactory", "shared")}(programAddress, '${optionalAccountStrategy}');` : "";
|
|
1818
1684
|
const accountItems = [
|
|
1819
|
-
...instructionNode.accounts.map((account) => `getAccountMeta(accounts.${
|
|
1685
|
+
...instructionNode.accounts.map((account) => `getAccountMeta(accounts.${camelCase(account.name)})`),
|
|
1820
1686
|
...hasRemainingAccounts ? ["...remainingAccounts"] : []
|
|
1821
1687
|
].join(", ");
|
|
1822
1688
|
let accounts;
|
|
@@ -1837,14 +1703,14 @@ function getReturnStatementFragment(scope) {
|
|
|
1837
1703
|
} else if (hasData) {
|
|
1838
1704
|
data = fragment`data: ${encoderFunctionFragment}.encode({})`;
|
|
1839
1705
|
}
|
|
1840
|
-
const instructionAttributes =
|
|
1706
|
+
const instructionAttributes = pipe(
|
|
1841
1707
|
[accounts, hasByteDeltas ? fragment`byteDelta` : void 0, data, fragment`programAddress`],
|
|
1842
1708
|
(fs) => mergeFragments(fs, (cs) => cs.join(", "))
|
|
1843
1709
|
);
|
|
1844
1710
|
return fragment`${getAccountMeta}\nreturn Object.freeze({ ${instructionAttributes} } as ${scope.syncReturnTypeFragment});`;
|
|
1845
1711
|
}
|
|
1846
1712
|
function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync) {
|
|
1847
|
-
return
|
|
1713
|
+
return pipe(
|
|
1848
1714
|
instructionTypeFragment,
|
|
1849
1715
|
(f) => hasByteDeltas ? fragment`${f} & ${use("type InstructionWithByteDelta", "shared")}` : f,
|
|
1850
1716
|
(f) => useAsync ? fragment`Promise<${f}>` : f
|
|
@@ -1853,7 +1719,7 @@ function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync)
|
|
|
1853
1719
|
function getTypeParamsFragment(instructionNode, programAddressConstant) {
|
|
1854
1720
|
return mergeFragments(
|
|
1855
1721
|
[
|
|
1856
|
-
...instructionNode.accounts.map((account) => fragment`TAccount${
|
|
1722
|
+
...instructionNode.accounts.map((account) => fragment`TAccount${pascalCase(account.name)} extends string`),
|
|
1857
1723
|
fragment`TProgramAddress extends ${use("type Address", "solanaAddresses")} = typeof ${programAddressConstant}`
|
|
1858
1724
|
],
|
|
1859
1725
|
(cs) => `<${cs.join(", ")}>`
|
|
@@ -1861,52 +1727,39 @@ function getTypeParamsFragment(instructionNode, programAddressConstant) {
|
|
|
1861
1727
|
}
|
|
1862
1728
|
function getInstructionTypeFragment(scope) {
|
|
1863
1729
|
const { instructionPath, nameApi } = scope;
|
|
1864
|
-
const instructionNode =
|
|
1730
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1865
1731
|
const instructionTypeName = nameApi.instructionType(instructionNode.name);
|
|
1866
1732
|
const accountTypeParamsFragments = instructionNode.accounts.map((account) => {
|
|
1867
|
-
const typeParam = fragment`TAccount${
|
|
1868
|
-
const camelName =
|
|
1733
|
+
const typeParam = fragment`TAccount${pascalCase(account.name)}`;
|
|
1734
|
+
const camelName = camelCase(account.name);
|
|
1869
1735
|
if (account.isSigner === "either") {
|
|
1870
1736
|
const signerRole = use(
|
|
1871
1737
|
account.isWritable ? "type WritableSignerAccount" : "type ReadonlySignerAccount",
|
|
1872
1738
|
"solanaInstructions"
|
|
1873
1739
|
);
|
|
1874
|
-
return
|
|
1740
|
+
return pipe(
|
|
1875
1741
|
fragment`typeof input["${camelName}"] extends TransactionSigner<${typeParam}> ? ${signerRole}<${typeParam}> & AccountSignerMeta<${typeParam}> : ${typeParam}`,
|
|
1876
1742
|
(f) => addFragmentImports(f, "solanaSigners", ["type AccountSignerMeta", "type TransactionSigner"])
|
|
1877
1743
|
);
|
|
1878
1744
|
}
|
|
1879
1745
|
return typeParam;
|
|
1880
1746
|
});
|
|
1881
|
-
return
|
|
1747
|
+
return pipe(
|
|
1882
1748
|
mergeFragments([fragment`TProgramAddress`, ...accountTypeParamsFragments], (c) => c.join(", ")),
|
|
1883
|
-
(f) =>
|
|
1749
|
+
(f) => mapFragmentContent(f, (c) => `${instructionTypeName}<${c}>`)
|
|
1884
1750
|
);
|
|
1885
1751
|
}
|
|
1886
1752
|
function getInputTypeCallFragment(scope) {
|
|
1887
1753
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
1888
|
-
const instructionNode =
|
|
1754
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1889
1755
|
const inputTypeName = useAsync ? nameApi.instructionAsyncInputType(instructionNode.name) : nameApi.instructionSyncInputType(instructionNode.name);
|
|
1890
1756
|
if (instructionNode.accounts.length === 0) return fragment`${inputTypeName}`;
|
|
1891
|
-
const accountTypeParams = instructionNode.accounts.map((account) => `TAccount${
|
|
1757
|
+
const accountTypeParams = instructionNode.accounts.map((account) => `TAccount${pascalCase(account.name)}`).join(", ");
|
|
1892
1758
|
return fragment`${inputTypeName}<${accountTypeParams}>`;
|
|
1893
1759
|
}
|
|
1894
|
-
|
|
1895
|
-
// src/fragments/instructionPage.ts
|
|
1896
|
-
import { logWarn as logWarn2 } from "@codama/errors";
|
|
1897
|
-
import { camelCase as camelCase12, definedTypeNode as definedTypeNode2, structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes3 } from "@codama/nodes";
|
|
1898
|
-
import {
|
|
1899
|
-
findProgramNodeFromPath as findProgramNodeFromPath6,
|
|
1900
|
-
getLastNodeFromPath as getLastNodeFromPath16,
|
|
1901
|
-
visit as visit5
|
|
1902
|
-
} from "@codama/visitors-core";
|
|
1903
|
-
|
|
1904
|
-
// src/fragments/instructionParseFunction.ts
|
|
1905
|
-
import { camelCase as camelCase11 } from "@codama/nodes";
|
|
1906
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath4, getLastNodeFromPath as getLastNodeFromPath14, pipe as pipe9 } from "@codama/visitors-core";
|
|
1907
1760
|
function getInstructionParseFunctionFragment(scope) {
|
|
1908
|
-
const instructionNode =
|
|
1909
|
-
const programNode =
|
|
1761
|
+
const instructionNode = getLastNodeFromPath(scope.instructionPath);
|
|
1762
|
+
const programNode = findProgramNodeFromPath(scope.instructionPath);
|
|
1910
1763
|
const programAddressConstant = use(scope.nameApi.programAddressConstant(programNode.name), "generatedPrograms");
|
|
1911
1764
|
const childScope = { ...scope, instructionNode, programAddressConstant };
|
|
1912
1765
|
return mergeFragments([getTypeFragment2(childScope), getFunctionFragment(childScope)], (cs) => cs.join("\n\n"));
|
|
@@ -1927,7 +1780,7 @@ function getTypeFragment2(scope) {
|
|
|
1927
1780
|
const accounts = mergeFragments(
|
|
1928
1781
|
scope.instructionNode.accounts.map((account, i) => {
|
|
1929
1782
|
const docs = getDocblockFragment(account.docs ?? [], true);
|
|
1930
|
-
const name =
|
|
1783
|
+
const name = camelCase(account.name);
|
|
1931
1784
|
return fragment`${docs}${name}${account.isOptional ? "?" : ""}: TAccountMetas[${i}]${account.isOptional ? " | undefined" : ""};`;
|
|
1932
1785
|
}),
|
|
1933
1786
|
(cs) => hasAccounts ? `
|
|
@@ -1961,7 +1814,7 @@ function getFunctionFragment(scope) {
|
|
|
1961
1814
|
[
|
|
1962
1815
|
fragment`${use("type Instruction", "solanaInstructions")}<TProgram>`,
|
|
1963
1816
|
hasAccounts ? fragment`${use("type InstructionWithAccounts", "solanaInstructions")}<TAccountMetas>` : void 0,
|
|
1964
|
-
hasData ?
|
|
1817
|
+
hasData ? pipe(
|
|
1965
1818
|
fragment`InstructionWithData<ReadonlyUint8Array>`,
|
|
1966
1819
|
(f) => addFragmentImports(f, "solanaInstructions", ["type InstructionWithData"]),
|
|
1967
1820
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
@@ -1999,7 +1852,7 @@ const getNextOptionalAccount = () => {
|
|
|
1999
1852
|
}
|
|
2000
1853
|
const accounts = mergeFragments(
|
|
2001
1854
|
scope.instructionNode.accounts.map(
|
|
2002
|
-
(account) => account.isOptional ? fragment`${
|
|
1855
|
+
(account) => account.isOptional ? fragment`${camelCase(account.name)}: getNextOptionalAccount()` : fragment`${camelCase(account.name)}: getNextAccount()`
|
|
2003
1856
|
),
|
|
2004
1857
|
(cs) => hasAccounts ? `, accounts: { ${cs.join(", ")} }` : ""
|
|
2005
1858
|
);
|
|
@@ -2009,15 +1862,10 @@ const getNextOptionalAccount = () => {
|
|
|
2009
1862
|
return { programAddress: instruction.programAddress${accounts}${data} };
|
|
2010
1863
|
}`;
|
|
2011
1864
|
}
|
|
2012
|
-
|
|
2013
|
-
// src/fragments/instructionType.ts
|
|
2014
|
-
import { pascalCase as pascalCase6 } from "@codama/nodes";
|
|
2015
|
-
import { mapFragmentContent as mapFragmentContent8 } from "@codama/renderers-core";
|
|
2016
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath5, getLastNodeFromPath as getLastNodeFromPath15 } from "@codama/visitors-core";
|
|
2017
1865
|
function getInstructionTypeFragment2(scope) {
|
|
2018
1866
|
const { instructionPath, nameApi, customInstructionData } = scope;
|
|
2019
|
-
const instructionNode =
|
|
2020
|
-
const programNode =
|
|
1867
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1868
|
+
const programNode = findProgramNodeFromPath(instructionPath);
|
|
2021
1869
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
2022
1870
|
const customData = customInstructionData.get(instructionNode.name);
|
|
2023
1871
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
@@ -2027,7 +1875,6 @@ function getInstructionTypeFragment2(scope) {
|
|
|
2027
1875
|
instructionNode.accounts.map(
|
|
2028
1876
|
(account) => getInstructionAccountTypeParamFragment({
|
|
2029
1877
|
...scope,
|
|
2030
|
-
allowAccountMeta: true,
|
|
2031
1878
|
instructionAccountPath: [...instructionPath, account]
|
|
2032
1879
|
})
|
|
2033
1880
|
),
|
|
@@ -2037,8 +1884,8 @@ function getInstructionTypeFragment2(scope) {
|
|
|
2037
1884
|
const usesLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted";
|
|
2038
1885
|
const accountMetasFragment = mergeFragments(
|
|
2039
1886
|
instructionNode.accounts.map(
|
|
2040
|
-
(account) =>
|
|
2041
|
-
const typeParam = `TAccount${
|
|
1887
|
+
(account) => mapFragmentContent(getInstructionAccountMetaFragment(account), (c) => {
|
|
1888
|
+
const typeParam = `TAccount${pascalCase(account.name)}`;
|
|
2042
1889
|
const isLegacyOptional = account.isOptional && usesLegacyOptionalAccounts;
|
|
2043
1890
|
const type = `${typeParam} extends string ? ${c} : ${typeParam}`;
|
|
2044
1891
|
if (!isLegacyOptional) return type;
|
|
@@ -2055,17 +1902,17 @@ ${use("type Instruction", "solanaInstructions")}<TProgram>${data}${accounts};`;
|
|
|
2055
1902
|
|
|
2056
1903
|
// src/fragments/instructionPage.ts
|
|
2057
1904
|
function getInstructionPageFragment(scope) {
|
|
2058
|
-
const node =
|
|
2059
|
-
if (!
|
|
1905
|
+
const node = getLastNodeFromPath(scope.instructionPath);
|
|
1906
|
+
if (!findProgramNodeFromPath(scope.instructionPath)) {
|
|
2060
1907
|
throw new Error("Instruction must be visited inside a program.");
|
|
2061
1908
|
}
|
|
2062
1909
|
const childScope = {
|
|
2063
1910
|
...scope,
|
|
2064
|
-
dataArgsManifest:
|
|
2065
|
-
extraArgsManifest:
|
|
2066
|
-
|
|
1911
|
+
dataArgsManifest: visit(node, scope.typeManifestVisitor),
|
|
1912
|
+
extraArgsManifest: visit(
|
|
1913
|
+
definedTypeNode({
|
|
2067
1914
|
name: scope.nameApi.instructionExtraType(node.name),
|
|
2068
|
-
type:
|
|
1915
|
+
type: structTypeNodeFromInstructionArgumentNodes(node.extraArguments ?? [])
|
|
2069
1916
|
}),
|
|
2070
1917
|
scope.typeManifestVisitor
|
|
2071
1918
|
),
|
|
@@ -2101,17 +1948,13 @@ function getRenamedArgsMap(instruction) {
|
|
|
2101
1948
|
const allNames = [...instruction.accounts.map((account) => account.name), ...argNames];
|
|
2102
1949
|
const duplicates = allNames.filter((e, i, a) => a.indexOf(e) !== i);
|
|
2103
1950
|
if (duplicates.length === 0) return /* @__PURE__ */ new Map();
|
|
2104
|
-
|
|
1951
|
+
logWarn(
|
|
2105
1952
|
`[JavaScript] Accounts and args of instruction [${instruction.name}] have the following conflicting attributes [${duplicates.join(", ")}]. Thus, the arguments have been renamed to avoid conflicts in the input type.`
|
|
2106
1953
|
);
|
|
2107
|
-
return new Map(duplicates.map((name) => [
|
|
1954
|
+
return new Map(duplicates.map((name) => [camelCase(name), camelCase(`${name}Arg`)]));
|
|
2108
1955
|
}
|
|
2109
|
-
|
|
2110
|
-
// src/fragments/pdaFunction.ts
|
|
2111
|
-
import { camelCase as camelCase13, isNode as isNode13, isNodeFilter as isNodeFilter5 } from "@codama/nodes";
|
|
2112
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath7, getLastNodeFromPath as getLastNodeFromPath17, visit as visit6 } from "@codama/visitors-core";
|
|
2113
1956
|
function getPdaFunctionFragment(scope) {
|
|
2114
|
-
const pdaNode =
|
|
1957
|
+
const pdaNode = getLastNodeFromPath(scope.pdaPath);
|
|
2115
1958
|
const seeds = parsePdaSeedNodes(pdaNode.seeds, scope);
|
|
2116
1959
|
return mergeFragments(
|
|
2117
1960
|
[getSeedInputTypeFragment(seeds, scope), getFunctionFragment2(seeds, scope)],
|
|
@@ -2119,9 +1962,9 @@ function getPdaFunctionFragment(scope) {
|
|
|
2119
1962
|
);
|
|
2120
1963
|
}
|
|
2121
1964
|
function getSeedInputTypeFragment(seeds, scope) {
|
|
2122
|
-
const variableSeeds = seeds.filter(
|
|
1965
|
+
const variableSeeds = seeds.filter(isNodeFilter("variablePdaSeedNode"));
|
|
2123
1966
|
if (variableSeeds.length === 0) return;
|
|
2124
|
-
const pdaNode =
|
|
1967
|
+
const pdaNode = getLastNodeFromPath(scope.pdaPath);
|
|
2125
1968
|
const seedTypeName = scope.nameApi.pdaSeedsType(pdaNode.name);
|
|
2126
1969
|
const seedAttributes = mergeFragments(
|
|
2127
1970
|
variableSeeds.map((seed) => seed.inputAttribute),
|
|
@@ -2130,15 +1973,15 @@ function getSeedInputTypeFragment(seeds, scope) {
|
|
|
2130
1973
|
return fragment`export type ${seedTypeName} = {\n${seedAttributes}\n};`;
|
|
2131
1974
|
}
|
|
2132
1975
|
function getFunctionFragment2(seeds, scope) {
|
|
2133
|
-
const pdaNode =
|
|
2134
|
-
const programNode =
|
|
1976
|
+
const pdaNode = getLastNodeFromPath(scope.pdaPath);
|
|
1977
|
+
const programNode = findProgramNodeFromPath(scope.pdaPath);
|
|
2135
1978
|
const addressType = use("type Address", "solanaAddresses");
|
|
2136
1979
|
const pdaType = use("type ProgramDerivedAddress", "solanaAddresses");
|
|
2137
1980
|
const getPdaFunction = use("getProgramDerivedAddress", "solanaAddresses");
|
|
2138
1981
|
const seedTypeName = scope.nameApi.pdaSeedsType(pdaNode.name);
|
|
2139
1982
|
const findPdaFunction = scope.nameApi.pdaFindFunction(pdaNode.name);
|
|
2140
1983
|
const docs = getDocblockFragment(pdaNode.docs ?? [], true);
|
|
2141
|
-
const hasVariableSeeds = seeds.filter(
|
|
1984
|
+
const hasVariableSeeds = seeds.filter(isNodeFilter("variablePdaSeedNode")).length > 0;
|
|
2142
1985
|
const seedArgument = hasVariableSeeds ? `seeds: ${seedTypeName}, ` : "";
|
|
2143
1986
|
const programAddress = pdaNode.programId ?? programNode.publicKey;
|
|
2144
1987
|
const encodedSeeds = mergeFragments(
|
|
@@ -2152,39 +1995,31 @@ function getFunctionFragment2(seeds, scope) {
|
|
|
2152
1995
|
}
|
|
2153
1996
|
function parsePdaSeedNodes(seeds, scope) {
|
|
2154
1997
|
return seeds.map((seed) => {
|
|
2155
|
-
if (
|
|
2156
|
-
const name =
|
|
1998
|
+
if (isNode(seed, "variablePdaSeedNode")) {
|
|
1999
|
+
const name = camelCase(seed.name);
|
|
2157
2000
|
const docs = getDocblockFragment(seed.docs ?? [], true);
|
|
2158
|
-
const { encoder: encoder2, looseType } =
|
|
2001
|
+
const { encoder: encoder2, looseType } = visit(seed.type, scope.typeManifestVisitor);
|
|
2159
2002
|
return {
|
|
2160
2003
|
...seed,
|
|
2161
2004
|
encodedValue: fragment`${encoder2}.encode(seeds.${name})`,
|
|
2162
2005
|
inputAttribute: fragment`${docs}${name}: ${looseType};`
|
|
2163
2006
|
};
|
|
2164
2007
|
}
|
|
2165
|
-
if (
|
|
2008
|
+
if (isNode(seed.value, "programIdValueNode")) {
|
|
2166
2009
|
const addressEncoder = use("getAddressEncoder", "solanaAddresses");
|
|
2167
2010
|
return { ...seed, encodedValue: fragment`${addressEncoder}().encode(programAddress)` };
|
|
2168
2011
|
}
|
|
2169
|
-
const { encoder } =
|
|
2170
|
-
const { value } =
|
|
2012
|
+
const { encoder } = visit(seed.type, scope.typeManifestVisitor);
|
|
2013
|
+
const { value } = visit(seed.value, scope.typeManifestVisitor);
|
|
2171
2014
|
return { ...seed, encodedValue: fragment`${encoder}.encode(${value})` };
|
|
2172
2015
|
});
|
|
2173
2016
|
}
|
|
2174
|
-
|
|
2175
|
-
// src/fragments/pdaPage.ts
|
|
2176
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath8 } from "@codama/visitors-core";
|
|
2177
2017
|
function getPdaPageFragment(scope) {
|
|
2178
|
-
if (!
|
|
2018
|
+
if (!findProgramNodeFromPath(scope.pdaPath)) {
|
|
2179
2019
|
throw new Error("PDA must be visited inside a program.");
|
|
2180
2020
|
}
|
|
2181
2021
|
return getPdaFunctionFragment(scope);
|
|
2182
2022
|
}
|
|
2183
|
-
|
|
2184
|
-
// src/fragments/programAccounts.ts
|
|
2185
|
-
import { resolveNestedTypeNode as resolveNestedTypeNode3 } from "@codama/nodes";
|
|
2186
|
-
import { mapFragmentContent as mapFragmentContent9 } from "@codama/renderers-core";
|
|
2187
|
-
import { pipe as pipe10 } from "@codama/visitors-core";
|
|
2188
2023
|
function getProgramAccountsFragment(scope) {
|
|
2189
2024
|
if (scope.programNode.accounts.length === 0) return;
|
|
2190
2025
|
return mergeFragments(
|
|
@@ -2209,7 +2044,7 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2209
2044
|
if (!hasAccountDiscriminators) return;
|
|
2210
2045
|
const programAccountsEnum = nameApi.programAccountsEnum(programNode.name);
|
|
2211
2046
|
const programAccountsIdentifierFunction = nameApi.programAccountsIdentifierFunction(programNode.name);
|
|
2212
|
-
return
|
|
2047
|
+
return pipe(
|
|
2213
2048
|
mergeFragments(
|
|
2214
2049
|
accountsWithDiscriminators.map((account) => {
|
|
2215
2050
|
const variant = nameApi.programAccountsEnumVariant(account.name);
|
|
@@ -2218,12 +2053,12 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2218
2053
|
dataName: "data",
|
|
2219
2054
|
discriminators: account.discriminators ?? [],
|
|
2220
2055
|
ifTrue: `return ${programAccountsEnum}.${variant};`,
|
|
2221
|
-
struct:
|
|
2056
|
+
struct: resolveNestedTypeNode(account.data)
|
|
2222
2057
|
});
|
|
2223
2058
|
}),
|
|
2224
2059
|
(c) => c.join("\n")
|
|
2225
2060
|
),
|
|
2226
|
-
(f) =>
|
|
2061
|
+
(f) => mapFragmentContent(
|
|
2227
2062
|
f,
|
|
2228
2063
|
(discriminators) => `export function ${programAccountsIdentifierFunction}(account: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programAccountsEnum} {
|
|
2229
2064
|
const data = 'data' in account ? account.data : account;
|
|
@@ -2234,25 +2069,14 @@ throw new Error("The provided account could not be identified as a ${programNode
|
|
|
2234
2069
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
2235
2070
|
);
|
|
2236
2071
|
}
|
|
2237
|
-
|
|
2238
|
-
// src/fragments/programConstant.ts
|
|
2239
|
-
import { pipe as pipe11 } from "@codama/visitors-core";
|
|
2240
2072
|
function getProgramConstantFragment(scope) {
|
|
2241
2073
|
const { programNode, nameApi } = scope;
|
|
2242
2074
|
const programAddressConstant = nameApi.programAddressConstant(programNode.name);
|
|
2243
|
-
return
|
|
2075
|
+
return pipe(
|
|
2244
2076
|
fragment`export const ${programAddressConstant} = '${programNode.publicKey}' as Address<'${programNode.publicKey}'>;`,
|
|
2245
2077
|
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
2246
2078
|
);
|
|
2247
2079
|
}
|
|
2248
|
-
|
|
2249
|
-
// src/fragments/programInstructions.ts
|
|
2250
|
-
import {
|
|
2251
|
-
getAllInstructionsWithSubs,
|
|
2252
|
-
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes4
|
|
2253
|
-
} from "@codama/nodes";
|
|
2254
|
-
import { mapFragmentContent as mapFragmentContent10 } from "@codama/renderers-core";
|
|
2255
|
-
import { pipe as pipe12 } from "@codama/visitors-core";
|
|
2256
2080
|
function getProgramInstructionsFragment(scope) {
|
|
2257
2081
|
if (scope.programNode.instructions.length === 0) return;
|
|
2258
2082
|
const allInstructions = getAllInstructionsWithSubs(scope.programNode, {
|
|
@@ -2294,14 +2118,14 @@ function getProgramInstructionsIdentifierFunctionFragment(scope) {
|
|
|
2294
2118
|
dataName: "data",
|
|
2295
2119
|
discriminators: instruction.discriminators ?? [],
|
|
2296
2120
|
ifTrue: `return ${programInstructionsEnum}.${variant};`,
|
|
2297
|
-
struct:
|
|
2121
|
+
struct: structTypeNodeFromInstructionArgumentNodes(instruction.arguments)
|
|
2298
2122
|
});
|
|
2299
2123
|
}),
|
|
2300
2124
|
(c) => c.join("\n")
|
|
2301
2125
|
);
|
|
2302
|
-
return
|
|
2126
|
+
return pipe(
|
|
2303
2127
|
discriminatorsFragment,
|
|
2304
|
-
(f) =>
|
|
2128
|
+
(f) => mapFragmentContent(
|
|
2305
2129
|
f,
|
|
2306
2130
|
(discriminators) => `export function ${programInstructionsIdentifierFunction}(instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programInstructionsEnum} {
|
|
2307
2131
|
const data = 'data' in instruction ? instruction.data : instruction;
|
|
@@ -2361,9 +2185,6 @@ function getRootIndexPageFragment(scope) {
|
|
|
2361
2185
|
(cs) => cs.join("\n")
|
|
2362
2186
|
);
|
|
2363
2187
|
}
|
|
2364
|
-
|
|
2365
|
-
// src/fragments/sharedPage.ts
|
|
2366
|
-
import { pipe as pipe13 } from "@codama/visitors-core";
|
|
2367
2188
|
function getSharedPageFragment() {
|
|
2368
2189
|
const sharedPage = fragment`/**
|
|
2369
2190
|
* Asserts that the given value is not null or undefined.
|
|
@@ -2464,7 +2285,7 @@ export function getAccountMetaFactory(
|
|
|
2464
2285
|
export function isTransactionSigner<TAddress extends string = string>(value: Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress>): value is TransactionSigner<TAddress> {
|
|
2465
2286
|
return !!value && typeof value === 'object' && 'address' in value && kitIsTransactionSigner(value);
|
|
2466
2287
|
}`;
|
|
2467
|
-
return
|
|
2288
|
+
return pipe(
|
|
2468
2289
|
sharedPage,
|
|
2469
2290
|
(f) => addFragmentImports(f, "solanaAddresses", [
|
|
2470
2291
|
"type Address",
|
|
@@ -2479,12 +2300,9 @@ export function isTransactionSigner<TAddress extends string = string>(value: Add
|
|
|
2479
2300
|
])
|
|
2480
2301
|
);
|
|
2481
2302
|
}
|
|
2482
|
-
|
|
2483
|
-
// src/fragments/typeDiscriminatedUnionHelpers.ts
|
|
2484
|
-
import { isDataEnum as isDataEnum3, isNode as isNode14 } from "@codama/nodes";
|
|
2485
2303
|
function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
2486
2304
|
const { name, typeNode, nameApi } = scope;
|
|
2487
|
-
const isDiscriminatedUnion =
|
|
2305
|
+
const isDiscriminatedUnion = isNode(typeNode, "enumTypeNode") && isDataEnum(typeNode);
|
|
2488
2306
|
if (!isDiscriminatedUnion) return;
|
|
2489
2307
|
const functionName = nameApi.discriminatedUnionFunction(name);
|
|
2490
2308
|
const isDiscriminatedUnionFunctionName = nameApi.isDiscriminatedUnionFunction(name);
|
|
@@ -2496,10 +2314,10 @@ function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
|
2496
2314
|
const variantSignatures = mergeFragments(
|
|
2497
2315
|
typeNode.variants.map((variant) => {
|
|
2498
2316
|
const variantName = nameApi.discriminatedUnionVariant(variant.name);
|
|
2499
|
-
if (
|
|
2317
|
+
if (isNode(variant, "enumStructVariantTypeNode")) {
|
|
2500
2318
|
return fragment`export function ${functionName}(kind: '${variantName}', data: ${getVariantContentType}<${looseName}, '${discriminatorName}', '${variantName}'>): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
2501
2319
|
}
|
|
2502
|
-
if (
|
|
2320
|
+
if (isNode(variant, "enumTupleVariantTypeNode")) {
|
|
2503
2321
|
return fragment`export function ${functionName}(kind: '${variantName}', data: ${getVariantContentType}<${looseName}, '${discriminatorName}', '${variantName}'>['fields']): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
2504
2322
|
}
|
|
2505
2323
|
return fragment`export function ${functionName}(kind: '${variantName}'): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
@@ -2517,13 +2335,10 @@ export function ${isDiscriminatedUnionFunctionName}<K extends ${strictName}['${d
|
|
|
2517
2335
|
};
|
|
2518
2336
|
`;
|
|
2519
2337
|
}
|
|
2520
|
-
|
|
2521
|
-
// src/fragments/typePage.ts
|
|
2522
|
-
import { pipe as pipe14, visit as visit7 } from "@codama/visitors-core";
|
|
2523
2338
|
function getTypePageFragment(scope) {
|
|
2524
2339
|
const node = scope.node;
|
|
2525
|
-
const manifest =
|
|
2526
|
-
return
|
|
2340
|
+
const manifest = visit(node, scope.typeManifestVisitor);
|
|
2341
|
+
return pipe(
|
|
2527
2342
|
mergeFragments(
|
|
2528
2343
|
[
|
|
2529
2344
|
getTypeWithCodecFragment({ ...scope, manifest, name: node.name, node: node.type, typeDocs: node.docs }),
|
|
@@ -2540,35 +2355,11 @@ function getTypePageFragment(scope) {
|
|
|
2540
2355
|
])
|
|
2541
2356
|
);
|
|
2542
2357
|
}
|
|
2543
|
-
|
|
2544
|
-
// src/visitors/getTypeManifestVisitor.ts
|
|
2545
|
-
import {
|
|
2546
|
-
camelCase as camelCase14,
|
|
2547
|
-
isNode as isNode15,
|
|
2548
|
-
isNodeFilter as isNodeFilter6,
|
|
2549
|
-
isScalarEnum,
|
|
2550
|
-
REGISTERED_TYPE_NODE_KINDS,
|
|
2551
|
-
REGISTERED_VALUE_NODE_KINDS,
|
|
2552
|
-
resolveNestedTypeNode as resolveNestedTypeNode4,
|
|
2553
|
-
structFieldTypeNode,
|
|
2554
|
-
structTypeNode,
|
|
2555
|
-
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes5
|
|
2556
|
-
} from "@codama/nodes";
|
|
2557
|
-
import { mapFragmentContent as mapFragmentContent11, setFragmentContent as setFragmentContent2 } from "@codama/renderers-core";
|
|
2558
|
-
import {
|
|
2559
|
-
extendVisitor,
|
|
2560
|
-
findLastNodeFromPath,
|
|
2561
|
-
NodeStack,
|
|
2562
|
-
pipe as pipe15,
|
|
2563
|
-
recordNodeStackVisitor,
|
|
2564
|
-
staticVisitor,
|
|
2565
|
-
visit as visit8
|
|
2566
|
-
} from "@codama/visitors-core";
|
|
2567
2358
|
function getTypeManifestVisitor(input) {
|
|
2568
2359
|
const { nameApi, linkables, nonScalarEnums, customAccountData, customInstructionData, getImportFrom } = input;
|
|
2569
2360
|
const stack = input.stack ?? new NodeStack();
|
|
2570
2361
|
let parentName = null;
|
|
2571
|
-
return
|
|
2362
|
+
return pipe(
|
|
2572
2363
|
staticVisitor(() => typeManifest(), {
|
|
2573
2364
|
keys: [
|
|
2574
2365
|
...REGISTERED_TYPE_NODE_KINDS,
|
|
@@ -2586,15 +2377,15 @@ function getTypeManifestVisitor(input) {
|
|
|
2586
2377
|
strict: nameApi.dataType(account.name)
|
|
2587
2378
|
};
|
|
2588
2379
|
const link = customAccountData.get(account.name)?.linkNode;
|
|
2589
|
-
const manifest = link ?
|
|
2380
|
+
const manifest = link ? visit(link, self) : visit(account.data, self);
|
|
2590
2381
|
parentName = null;
|
|
2591
2382
|
return manifest;
|
|
2592
2383
|
},
|
|
2593
2384
|
visitAmountType(amountType, { self }) {
|
|
2594
|
-
return
|
|
2385
|
+
return visit(amountType.number, self);
|
|
2595
2386
|
},
|
|
2596
2387
|
visitArrayType(arrayType, { self }) {
|
|
2597
|
-
const childManifest =
|
|
2388
|
+
const childManifest = visit(arrayType.item, self);
|
|
2598
2389
|
const sizeManifest = getArrayLikeSizeOption(arrayType.count, self);
|
|
2599
2390
|
const encoderOptions = sizeManifest.encoder ? fragment`, { ${sizeManifest.encoder} }` : "";
|
|
2600
2391
|
const decoderOptions = sizeManifest.decoder ? fragment`, { ${sizeManifest.decoder} }` : "";
|
|
@@ -2608,16 +2399,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2608
2399
|
},
|
|
2609
2400
|
visitArrayValue(node, { self }) {
|
|
2610
2401
|
return mergeTypeManifests(
|
|
2611
|
-
node.items.map((v) =>
|
|
2402
|
+
node.items.map((v) => visit(v, self)),
|
|
2612
2403
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
2613
2404
|
);
|
|
2614
2405
|
},
|
|
2615
2406
|
visitBooleanType(booleanType, { self }) {
|
|
2616
2407
|
let sizeEncoder = fragment``;
|
|
2617
2408
|
let sizeDecoder = fragment``;
|
|
2618
|
-
const resolvedSize =
|
|
2409
|
+
const resolvedSize = resolveNestedTypeNode(booleanType.size);
|
|
2619
2410
|
if (resolvedSize.format !== "u8" || resolvedSize.endian !== "le") {
|
|
2620
|
-
const size =
|
|
2411
|
+
const size = visit(booleanType.size, self);
|
|
2621
2412
|
sizeEncoder = fragment`{ size: ${size.encoder} }`;
|
|
2622
2413
|
sizeDecoder = fragment`{ size: ${size.decoder} }`;
|
|
2623
2414
|
}
|
|
@@ -2645,22 +2436,22 @@ function getTypeManifestVisitor(input) {
|
|
|
2645
2436
|
return typeManifest({ value: fragment`new Uint8Array([${Array.from(bytes).join(", ")}])` });
|
|
2646
2437
|
},
|
|
2647
2438
|
visitConstantValue(node, { self }) {
|
|
2648
|
-
if (
|
|
2649
|
-
return
|
|
2439
|
+
if (isNode(node.type, "bytesTypeNode") && isNode(node.value, "bytesValueNode")) {
|
|
2440
|
+
return visit(node.value, self);
|
|
2650
2441
|
}
|
|
2651
2442
|
return typeManifest({
|
|
2652
|
-
value: fragment`${
|
|
2443
|
+
value: fragment`${visit(node.type, self).encoder}.encode(${visit(node.value, self).value})`
|
|
2653
2444
|
});
|
|
2654
2445
|
},
|
|
2655
2446
|
visitDateTimeType(dateTimeType, { self }) {
|
|
2656
|
-
return
|
|
2447
|
+
return visit(dateTimeType.number, self);
|
|
2657
2448
|
},
|
|
2658
2449
|
visitDefinedType(definedType, { self }) {
|
|
2659
2450
|
parentName = {
|
|
2660
2451
|
loose: nameApi.dataArgsType(definedType.name),
|
|
2661
2452
|
strict: nameApi.dataType(definedType.name)
|
|
2662
2453
|
};
|
|
2663
|
-
const manifest =
|
|
2454
|
+
const manifest = visit(definedType.type, self);
|
|
2664
2455
|
parentName = null;
|
|
2665
2456
|
return manifest;
|
|
2666
2457
|
},
|
|
@@ -2678,7 +2469,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2678
2469
|
});
|
|
2679
2470
|
},
|
|
2680
2471
|
visitEnumEmptyVariantType(enumEmptyVariantType) {
|
|
2681
|
-
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2472
|
+
const discriminator = nameApi.discriminatedUnionDiscriminator(camelCase(parentName?.strict ?? ""));
|
|
2682
2473
|
const name = nameApi.discriminatedUnionVariant(enumEmptyVariantType.name);
|
|
2683
2474
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2684
2475
|
return typeManifest({
|
|
@@ -2691,31 +2482,31 @@ function getTypeManifestVisitor(input) {
|
|
|
2691
2482
|
visitEnumStructVariantType(enumStructVariantType, { self }) {
|
|
2692
2483
|
const currentParentName = parentName;
|
|
2693
2484
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2694
|
-
|
|
2485
|
+
camelCase(currentParentName?.strict ?? "")
|
|
2695
2486
|
);
|
|
2696
2487
|
const name = nameApi.discriminatedUnionVariant(enumStructVariantType.name);
|
|
2697
2488
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2698
2489
|
parentName = null;
|
|
2699
|
-
const structManifest =
|
|
2490
|
+
const structManifest = visit(enumStructVariantType.struct, self);
|
|
2700
2491
|
parentName = currentParentName;
|
|
2701
2492
|
return typeManifest({
|
|
2702
2493
|
...structManifest,
|
|
2703
2494
|
decoder: fragment`['${name}', ${structManifest.decoder}]`,
|
|
2704
2495
|
encoder: fragment`['${name}', ${structManifest.encoder}]`,
|
|
2705
|
-
looseType:
|
|
2496
|
+
looseType: pipe(
|
|
2706
2497
|
structManifest.looseType,
|
|
2707
|
-
(f) =>
|
|
2498
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2708
2499
|
),
|
|
2709
|
-
strictType:
|
|
2500
|
+
strictType: pipe(
|
|
2710
2501
|
structManifest.strictType,
|
|
2711
|
-
(f) =>
|
|
2502
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2712
2503
|
)
|
|
2713
2504
|
});
|
|
2714
2505
|
},
|
|
2715
2506
|
visitEnumTupleVariantType(enumTupleVariantType, { self }) {
|
|
2716
2507
|
const currentParentName = parentName;
|
|
2717
2508
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2718
|
-
|
|
2509
|
+
camelCase(currentParentName?.strict ?? "")
|
|
2719
2510
|
);
|
|
2720
2511
|
const name = nameApi.discriminatedUnionVariant(enumTupleVariantType.name);
|
|
2721
2512
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
@@ -2726,19 +2517,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2726
2517
|
})
|
|
2727
2518
|
]);
|
|
2728
2519
|
parentName = null;
|
|
2729
|
-
const structManifest =
|
|
2520
|
+
const structManifest = visit(struct, self);
|
|
2730
2521
|
parentName = currentParentName;
|
|
2731
2522
|
return typeManifest({
|
|
2732
2523
|
...structManifest,
|
|
2733
2524
|
decoder: fragment`['${name}', ${structManifest.decoder}]`,
|
|
2734
2525
|
encoder: fragment`['${name}', ${structManifest.encoder}]`,
|
|
2735
|
-
looseType:
|
|
2526
|
+
looseType: pipe(
|
|
2736
2527
|
structManifest.looseType,
|
|
2737
|
-
(f) =>
|
|
2528
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2738
2529
|
),
|
|
2739
|
-
strictType:
|
|
2530
|
+
strictType: pipe(
|
|
2740
2531
|
structManifest.strictType,
|
|
2741
|
-
(f) =>
|
|
2532
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2742
2533
|
)
|
|
2743
2534
|
});
|
|
2744
2535
|
},
|
|
@@ -2746,14 +2537,14 @@ function getTypeManifestVisitor(input) {
|
|
|
2746
2537
|
const currentParentName = parentName;
|
|
2747
2538
|
const encoderOptions = [];
|
|
2748
2539
|
const decoderOptions = [];
|
|
2749
|
-
const enumSize =
|
|
2540
|
+
const enumSize = resolveNestedTypeNode(enumType.size);
|
|
2750
2541
|
if (enumSize.format !== "u8" || enumSize.endian !== "le") {
|
|
2751
|
-
const sizeManifest =
|
|
2542
|
+
const sizeManifest = visit(enumType.size, self);
|
|
2752
2543
|
encoderOptions.push(fragment`size: ${sizeManifest.encoder}`);
|
|
2753
2544
|
decoderOptions.push(fragment`size: ${sizeManifest.decoder}`);
|
|
2754
2545
|
}
|
|
2755
2546
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2756
|
-
|
|
2547
|
+
camelCase(currentParentName?.strict ?? "")
|
|
2757
2548
|
);
|
|
2758
2549
|
if (!isScalarEnum(enumType) && discriminator !== "__kind") {
|
|
2759
2550
|
encoderOptions.push(fragment`discriminator: '${discriminator}'`);
|
|
@@ -2783,7 +2574,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2783
2574
|
});
|
|
2784
2575
|
}
|
|
2785
2576
|
const mergedManifest = mergeTypeManifests(
|
|
2786
|
-
enumType.variants.map((variant) =>
|
|
2577
|
+
enumType.variants.map((variant) => visit(variant, self)),
|
|
2787
2578
|
{
|
|
2788
2579
|
mergeCodecs: (renders) => renders.join(", "),
|
|
2789
2580
|
mergeTypes: (renders) => renders.join(" | ")
|
|
@@ -2801,14 +2592,14 @@ function getTypeManifestVisitor(input) {
|
|
|
2801
2592
|
const enumFunction = nameApi.discriminatedUnionFunction(node.enum.name);
|
|
2802
2593
|
const importFrom = getImportFrom(node.enum);
|
|
2803
2594
|
const enumNode = linkables.get([...stack.getPath(), node.enum])?.type;
|
|
2804
|
-
const isScalar = enumNode &&
|
|
2595
|
+
const isScalar = enumNode && isNode(enumNode, "enumTypeNode") ? isScalarEnum(enumNode) : !nonScalarEnums.includes(node.enum.name);
|
|
2805
2596
|
if (!node.value && isScalar) {
|
|
2806
2597
|
const variantName2 = nameApi.enumVariant(node.variant);
|
|
2807
2598
|
return typeManifest({
|
|
2808
2599
|
...manifest,
|
|
2809
|
-
value:
|
|
2600
|
+
value: pipe(
|
|
2810
2601
|
manifest.value,
|
|
2811
|
-
(f) =>
|
|
2602
|
+
(f) => setFragmentContent(f, `${enumName}.${variantName2}`),
|
|
2812
2603
|
(f) => addFragmentImports(f, importFrom, [enumName])
|
|
2813
2604
|
)
|
|
2814
2605
|
});
|
|
@@ -2817,24 +2608,24 @@ function getTypeManifestVisitor(input) {
|
|
|
2817
2608
|
if (!node.value) {
|
|
2818
2609
|
return typeManifest({
|
|
2819
2610
|
...manifest,
|
|
2820
|
-
value:
|
|
2611
|
+
value: pipe(
|
|
2821
2612
|
manifest.value,
|
|
2822
|
-
(f) =>
|
|
2613
|
+
(f) => setFragmentContent(f, `${enumFunction}('${variantName}')`),
|
|
2823
2614
|
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2824
2615
|
)
|
|
2825
2616
|
});
|
|
2826
2617
|
}
|
|
2827
2618
|
return typeManifest({
|
|
2828
2619
|
...manifest,
|
|
2829
|
-
value:
|
|
2830
|
-
|
|
2831
|
-
(f) =>
|
|
2620
|
+
value: pipe(
|
|
2621
|
+
visit(node.value, self).value,
|
|
2622
|
+
(f) => mapFragmentContent(f, (c) => `${enumFunction}('${variantName}', ${c})`),
|
|
2832
2623
|
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2833
2624
|
)
|
|
2834
2625
|
});
|
|
2835
2626
|
},
|
|
2836
2627
|
visitFixedSizeType(node, { self }) {
|
|
2837
|
-
const manifest =
|
|
2628
|
+
const manifest = visit(node.type, self);
|
|
2838
2629
|
return typeManifest({
|
|
2839
2630
|
...manifest,
|
|
2840
2631
|
decoder: fragment`${use("fixDecoderSize", "solanaCodecsCore")}(${manifest.decoder}, ${node.size})`,
|
|
@@ -2842,13 +2633,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2842
2633
|
});
|
|
2843
2634
|
},
|
|
2844
2635
|
visitHiddenPrefixType(node, { self }) {
|
|
2845
|
-
const manifest =
|
|
2846
|
-
const prefixes = node.prefix.map((c) =>
|
|
2847
|
-
const prefixEncoders =
|
|
2636
|
+
const manifest = visit(node.type, self);
|
|
2637
|
+
const prefixes = node.prefix.map((c) => visit(c, self).value);
|
|
2638
|
+
const prefixEncoders = pipe(
|
|
2848
2639
|
mergeFragments(prefixes, (cs) => cs.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2849
2640
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2850
2641
|
);
|
|
2851
|
-
const prefixDecoders =
|
|
2642
|
+
const prefixDecoders = pipe(
|
|
2852
2643
|
mergeFragments(prefixes, (cs) => cs.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2853
2644
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2854
2645
|
);
|
|
@@ -2859,13 +2650,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2859
2650
|
});
|
|
2860
2651
|
},
|
|
2861
2652
|
visitHiddenSuffixType(node, { self }) {
|
|
2862
|
-
const manifest =
|
|
2863
|
-
const suffixes = node.suffix.map((c) =>
|
|
2864
|
-
const suffixEncoders =
|
|
2653
|
+
const manifest = visit(node.type, self);
|
|
2654
|
+
const suffixes = node.suffix.map((c) => visit(c, self).value);
|
|
2655
|
+
const suffixEncoders = pipe(
|
|
2865
2656
|
mergeFragments(suffixes, (cs) => cs.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2866
2657
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2867
2658
|
);
|
|
2868
|
-
const suffixDecoders =
|
|
2659
|
+
const suffixDecoders = pipe(
|
|
2869
2660
|
mergeFragments(suffixes, (cs) => cs.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2870
2661
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2871
2662
|
);
|
|
@@ -2882,19 +2673,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2882
2673
|
strict: nameApi.dataType(instructionDataName)
|
|
2883
2674
|
};
|
|
2884
2675
|
const link = customInstructionData.get(instruction.name)?.linkNode;
|
|
2885
|
-
const struct =
|
|
2886
|
-
const manifest = link ?
|
|
2676
|
+
const struct = structTypeNodeFromInstructionArgumentNodes(instruction.arguments);
|
|
2677
|
+
const manifest = link ? visit(link, self) : visit(struct, self);
|
|
2887
2678
|
parentName = null;
|
|
2888
2679
|
return manifest;
|
|
2889
2680
|
},
|
|
2890
2681
|
visitMapEntryValue(node, { self }) {
|
|
2891
|
-
return mergeTypeManifests([
|
|
2682
|
+
return mergeTypeManifests([visit(node.key, self), visit(node.value, self)], {
|
|
2892
2683
|
mergeValues: (renders) => `[${renders.join(", ")}]`
|
|
2893
2684
|
});
|
|
2894
2685
|
},
|
|
2895
2686
|
visitMapType(mapType, { self }) {
|
|
2896
|
-
const key =
|
|
2897
|
-
const value =
|
|
2687
|
+
const key = visit(mapType.key, self);
|
|
2688
|
+
const value = visit(mapType.value, self);
|
|
2898
2689
|
const mergedManifest = mergeTypeManifests([key, value], {
|
|
2899
2690
|
mergeCodecs: ([k, v]) => `${k}, ${v}`,
|
|
2900
2691
|
mergeTypes: ([k, v]) => `Map<${k}, ${v}>`
|
|
@@ -2909,7 +2700,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2909
2700
|
});
|
|
2910
2701
|
},
|
|
2911
2702
|
visitMapValue(node, { self }) {
|
|
2912
|
-
const entryFragments = node.entries.map((entry) =>
|
|
2703
|
+
const entryFragments = node.entries.map((entry) => visit(entry, self));
|
|
2913
2704
|
return mergeTypeManifests(entryFragments, {
|
|
2914
2705
|
mergeValues: (renders) => `new Map([${renders.join(", ")}])`
|
|
2915
2706
|
});
|
|
@@ -2935,12 +2726,12 @@ function getTypeManifestVisitor(input) {
|
|
|
2935
2726
|
return typeManifest({ value: fragment`${JSON.stringify(node.number)}` });
|
|
2936
2727
|
},
|
|
2937
2728
|
visitOptionType(optionType, { self }) {
|
|
2938
|
-
const childManifest =
|
|
2729
|
+
const childManifest = visit(optionType.item, self);
|
|
2939
2730
|
const encoderOptions = [];
|
|
2940
2731
|
const decoderOptions = [];
|
|
2941
|
-
const optionPrefix =
|
|
2732
|
+
const optionPrefix = resolveNestedTypeNode(optionType.prefix);
|
|
2942
2733
|
if (optionPrefix.format !== "u8" || optionPrefix.endian !== "le") {
|
|
2943
|
-
const prefixManifest =
|
|
2734
|
+
const prefixManifest = visit(optionType.prefix, self);
|
|
2944
2735
|
encoderOptions.push(fragment`prefix: ${prefixManifest.encoder}`);
|
|
2945
2736
|
decoderOptions.push(fragment`prefix: ${prefixManifest.decoder}`);
|
|
2946
2737
|
}
|
|
@@ -2965,7 +2756,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2965
2756
|
});
|
|
2966
2757
|
},
|
|
2967
2758
|
visitPostOffsetType(node, { self }) {
|
|
2968
|
-
const manifest =
|
|
2759
|
+
const manifest = visit(node.type, self);
|
|
2969
2760
|
if (node.strategy === "padded") {
|
|
2970
2761
|
return typeManifest({
|
|
2971
2762
|
...manifest,
|
|
@@ -2991,7 +2782,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2991
2782
|
});
|
|
2992
2783
|
},
|
|
2993
2784
|
visitPreOffsetType(node, { self }) {
|
|
2994
|
-
const manifest =
|
|
2785
|
+
const manifest = visit(node.type, self);
|
|
2995
2786
|
if (node.strategy === "padded") {
|
|
2996
2787
|
return typeManifest({
|
|
2997
2788
|
...manifest,
|
|
@@ -3028,7 +2819,7 @@ function getTypeManifestVisitor(input) {
|
|
|
3028
2819
|
});
|
|
3029
2820
|
},
|
|
3030
2821
|
visitRemainderOptionType(node, { self }) {
|
|
3031
|
-
const childManifest =
|
|
2822
|
+
const childManifest = visit(node.item, self);
|
|
3032
2823
|
const encoderOptions = ["prefix: null"];
|
|
3033
2824
|
const decoderOptions = ["prefix: null"];
|
|
3034
2825
|
const encoderOptionsAsString = encoderOptions.length > 0 ? `, { ${encoderOptions.join(", ")} }` : "";
|
|
@@ -3042,8 +2833,8 @@ function getTypeManifestVisitor(input) {
|
|
|
3042
2833
|
});
|
|
3043
2834
|
},
|
|
3044
2835
|
visitSentinelType(node, { self }) {
|
|
3045
|
-
const manifest =
|
|
3046
|
-
const sentinel =
|
|
2836
|
+
const manifest = visit(node.type, self);
|
|
2837
|
+
const sentinel = visit(node.sentinel, self).value;
|
|
3047
2838
|
return typeManifest({
|
|
3048
2839
|
...manifest,
|
|
3049
2840
|
decoder: fragment`${use("addDecoderSentinel", "solanaCodecsCore")}(${manifest.decoder}, ${sentinel})`,
|
|
@@ -3051,7 +2842,7 @@ function getTypeManifestVisitor(input) {
|
|
|
3051
2842
|
});
|
|
3052
2843
|
},
|
|
3053
2844
|
visitSetType(setType, { self }) {
|
|
3054
|
-
const childManifest =
|
|
2845
|
+
const childManifest = visit(setType.item, self);
|
|
3055
2846
|
const sizeManifest = getArrayLikeSizeOption(setType.count, self);
|
|
3056
2847
|
const encoderOptions = sizeManifest.encoder ? fragment`, { ${sizeManifest.encoder} }` : "";
|
|
3057
2848
|
const decoderOptions = sizeManifest.decoder ? fragment`, { ${sizeManifest.decoder} }` : "";
|
|
@@ -3065,13 +2856,13 @@ function getTypeManifestVisitor(input) {
|
|
|
3065
2856
|
},
|
|
3066
2857
|
visitSetValue(node, { self }) {
|
|
3067
2858
|
return mergeTypeManifests(
|
|
3068
|
-
node.items.map((v) =>
|
|
2859
|
+
node.items.map((v) => visit(v, self)),
|
|
3069
2860
|
{ mergeValues: (renders) => `new Set([${renders.join(", ")}])` }
|
|
3070
2861
|
);
|
|
3071
2862
|
},
|
|
3072
2863
|
visitSizePrefixType(node, { self }) {
|
|
3073
|
-
const manifest =
|
|
3074
|
-
const prefix =
|
|
2864
|
+
const manifest = visit(node.type, self);
|
|
2865
|
+
const prefix = visit(node.prefix, self);
|
|
3075
2866
|
return typeManifest({
|
|
3076
2867
|
...manifest,
|
|
3077
2868
|
decoder: fragment`${use("addDecoderSizePrefix", "solanaCodecsCore")}(${manifest.decoder}, ${prefix.decoder})`,
|
|
@@ -3079,7 +2870,7 @@ function getTypeManifestVisitor(input) {
|
|
|
3079
2870
|
});
|
|
3080
2871
|
},
|
|
3081
2872
|
visitSolAmountType({ number }, { self }) {
|
|
3082
|
-
const numberManifest =
|
|
2873
|
+
const numberManifest = visit(number, self);
|
|
3083
2874
|
return typeManifest({
|
|
3084
2875
|
...numberManifest,
|
|
3085
2876
|
decoder: fragment`${use("getLamportsDecoder", "solanaRpcTypes")}(${numberManifest.decoder})`,
|
|
@@ -3089,7 +2880,7 @@ function getTypeManifestVisitor(input) {
|
|
|
3089
2880
|
});
|
|
3090
2881
|
},
|
|
3091
2882
|
visitSomeValue(node, { self }) {
|
|
3092
|
-
const innerValue =
|
|
2883
|
+
const innerValue = visit(node.value, self).value;
|
|
3093
2884
|
return typeManifest({
|
|
3094
2885
|
value: fragment`${use("some", "solanaOptions")}(${innerValue})`
|
|
3095
2886
|
});
|
|
@@ -3122,8 +2913,8 @@ function getTypeManifestVisitor(input) {
|
|
|
3122
2913
|
});
|
|
3123
2914
|
},
|
|
3124
2915
|
visitStructFieldType(structFieldType, { self }) {
|
|
3125
|
-
const name =
|
|
3126
|
-
const originalChildManifest =
|
|
2916
|
+
const name = camelCase(structFieldType.name);
|
|
2917
|
+
const originalChildManifest = visit(structFieldType.type, self);
|
|
3127
2918
|
let docs = getDocblockFragment(structFieldType.docs ?? [], true);
|
|
3128
2919
|
docs = docs ? fragment`\n${docs}` : docs;
|
|
3129
2920
|
const childManifest = typeManifest({
|
|
@@ -3145,16 +2936,16 @@ function getTypeManifestVisitor(input) {
|
|
|
3145
2936
|
return typeManifest({ ...childManifest, looseType: fragment`` });
|
|
3146
2937
|
},
|
|
3147
2938
|
visitStructFieldValue(node, { self }) {
|
|
3148
|
-
const innerValue =
|
|
2939
|
+
const innerValue = visit(node.value, self).value;
|
|
3149
2940
|
return typeManifest({
|
|
3150
2941
|
value: fragment`${node.name}: ${innerValue}`
|
|
3151
2942
|
});
|
|
3152
2943
|
},
|
|
3153
2944
|
visitStructType(structType, { self }) {
|
|
3154
2945
|
const optionalFields = structType.fields.filter((f) => !!f.defaultValue);
|
|
3155
|
-
const mergedManifest =
|
|
2946
|
+
const mergedManifest = pipe(
|
|
3156
2947
|
mergeTypeManifests(
|
|
3157
|
-
structType.fields.map((field) =>
|
|
2948
|
+
structType.fields.map((field) => visit(field, self)),
|
|
3158
2949
|
{
|
|
3159
2950
|
mergeCodecs: (renders) => `([${renders.join(", ")}])`,
|
|
3160
2951
|
mergeTypes: (renders) => `{ ${renders.join("")} }`
|
|
@@ -3174,16 +2965,16 @@ function getTypeManifestVisitor(input) {
|
|
|
3174
2965
|
const accountNode = findLastNodeFromPath(parentPath, "accountNode");
|
|
3175
2966
|
const discriminatorPrefix = instructionNode ? instructionNode.name : accountNode?.name;
|
|
3176
2967
|
const discriminators = (instructionNode ? instructionNode.discriminators : accountNode?.discriminators) ?? [];
|
|
3177
|
-
const fieldDiscriminators = discriminators.filter(
|
|
2968
|
+
const fieldDiscriminators = discriminators.filter(isNodeFilter("fieldDiscriminatorNode"));
|
|
3178
2969
|
const defaultValues = mergeFragments(
|
|
3179
2970
|
optionalFields.map((f) => {
|
|
3180
|
-
const key =
|
|
2971
|
+
const key = camelCase(f.name);
|
|
3181
2972
|
if (fieldDiscriminators.some((d) => d.name === f.name)) {
|
|
3182
|
-
const constantName = nameApi.constant(
|
|
2973
|
+
const constantName = nameApi.constant(camelCase(`${discriminatorPrefix}_${f.name}`));
|
|
3183
2974
|
return f.defaultValueStrategy === "omitted" ? fragment`${key}: ${constantName}` : fragment`${key}: value.${key} ?? ${constantName}`;
|
|
3184
2975
|
}
|
|
3185
2976
|
const defaultValue = f.defaultValue;
|
|
3186
|
-
const value =
|
|
2977
|
+
const value = visit(defaultValue, self).value;
|
|
3187
2978
|
return f.defaultValueStrategy === "omitted" ? fragment`${key}: ${value}` : fragment`${key}: value.${key} ?? ${value}`;
|
|
3188
2979
|
}),
|
|
3189
2980
|
(cs) => cs.join(", ")
|
|
@@ -3195,12 +2986,12 @@ function getTypeManifestVisitor(input) {
|
|
|
3195
2986
|
},
|
|
3196
2987
|
visitStructValue(node, { self }) {
|
|
3197
2988
|
return mergeTypeManifests(
|
|
3198
|
-
node.fields.map((field) =>
|
|
2989
|
+
node.fields.map((field) => visit(field, self)),
|
|
3199
2990
|
{ mergeValues: (renders) => `{ ${renders.join(", ")} }` }
|
|
3200
2991
|
);
|
|
3201
2992
|
},
|
|
3202
2993
|
visitTupleType(tupleType, { self }) {
|
|
3203
|
-
const items = tupleType.items.map((item) =>
|
|
2994
|
+
const items = tupleType.items.map((item) => visit(item, self));
|
|
3204
2995
|
const mergedManifest = mergeTypeManifests(items, {
|
|
3205
2996
|
mergeCodecs: (codecs) => `[${codecs.join(", ")}]`,
|
|
3206
2997
|
mergeTypes: (types) => `readonly [${types.join(", ")}]`
|
|
@@ -3213,16 +3004,16 @@ function getTypeManifestVisitor(input) {
|
|
|
3213
3004
|
},
|
|
3214
3005
|
visitTupleValue(node, { self }) {
|
|
3215
3006
|
return mergeTypeManifests(
|
|
3216
|
-
node.items.map((v) =>
|
|
3007
|
+
node.items.map((v) => visit(v, self)),
|
|
3217
3008
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
3218
3009
|
);
|
|
3219
3010
|
},
|
|
3220
3011
|
visitZeroableOptionType(node, { self }) {
|
|
3221
|
-
const childManifest =
|
|
3012
|
+
const childManifest = visit(node.item, self);
|
|
3222
3013
|
const encoderOptions = [fragment`prefix: null`];
|
|
3223
3014
|
const decoderOptions = [fragment`prefix: null`];
|
|
3224
3015
|
if (node.zeroValue) {
|
|
3225
|
-
const zeroValueManifest =
|
|
3016
|
+
const zeroValueManifest = visit(node.zeroValue, self);
|
|
3226
3017
|
encoderOptions.push(fragment`noneValue: ${zeroValueManifest.value}`);
|
|
3227
3018
|
decoderOptions.push(fragment`noneValue: ${zeroValueManifest.value}`);
|
|
3228
3019
|
} else {
|
|
@@ -3250,37 +3041,37 @@ function getTypeManifestVisitor(input) {
|
|
|
3250
3041
|
);
|
|
3251
3042
|
}
|
|
3252
3043
|
function getArrayLikeSizeOption(count, visitor) {
|
|
3253
|
-
if (
|
|
3044
|
+
if (isNode(count, "fixedCountNode")) {
|
|
3254
3045
|
return {
|
|
3255
3046
|
decoder: fragment`size: ${count.value}`,
|
|
3256
3047
|
encoder: fragment`size: ${count.value}`
|
|
3257
3048
|
};
|
|
3258
3049
|
}
|
|
3259
|
-
if (
|
|
3050
|
+
if (isNode(count, "remainderCountNode")) {
|
|
3260
3051
|
return {
|
|
3261
3052
|
decoder: fragment`size: 'remainder'`,
|
|
3262
3053
|
encoder: fragment`size: 'remainder'`
|
|
3263
3054
|
};
|
|
3264
3055
|
}
|
|
3265
|
-
const prefix =
|
|
3056
|
+
const prefix = resolveNestedTypeNode(count.prefix);
|
|
3266
3057
|
if (prefix.format === "u32" && prefix.endian === "le") {
|
|
3267
3058
|
return { decoder: void 0, encoder: void 0 };
|
|
3268
3059
|
}
|
|
3269
|
-
const prefixManifest =
|
|
3060
|
+
const prefixManifest = visit(count.prefix, visitor);
|
|
3270
3061
|
return {
|
|
3271
|
-
decoder:
|
|
3272
|
-
encoder:
|
|
3062
|
+
decoder: pipe(prefixManifest.decoder, (f) => mapFragmentContent(f, (c) => `size: ${c}`)),
|
|
3063
|
+
encoder: pipe(prefixManifest.encoder, (f) => mapFragmentContent(f, (c) => `size: ${c}`))
|
|
3273
3064
|
};
|
|
3274
3065
|
}
|
|
3275
3066
|
|
|
3276
3067
|
// src/visitors/getRenderMapVisitor.ts
|
|
3277
3068
|
function getRenderMapVisitor(options = {}) {
|
|
3278
|
-
const linkables = new
|
|
3279
|
-
const stack = new
|
|
3069
|
+
const linkables = new LinkableDictionary();
|
|
3070
|
+
const stack = new NodeStack();
|
|
3280
3071
|
const customAccountData = parseCustomDataOptions(options.customAccountData ?? [], "AccountData");
|
|
3281
3072
|
const customInstructionData = parseCustomDataOptions(options.customInstructionData ?? [], "InstructionData");
|
|
3282
3073
|
const renderScopeWithTypeManifestVisitor = {
|
|
3283
|
-
asyncResolvers: (options.asyncResolvers ?? []).map(
|
|
3074
|
+
asyncResolvers: (options.asyncResolvers ?? []).map(camelCase),
|
|
3284
3075
|
customAccountData,
|
|
3285
3076
|
customInstructionData,
|
|
3286
3077
|
dependencyMap: options.dependencyMap ?? {},
|
|
@@ -3288,13 +3079,13 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3288
3079
|
getImportFrom: getImportFromFactory(options.linkOverrides ?? {}, customAccountData, customInstructionData),
|
|
3289
3080
|
linkables,
|
|
3290
3081
|
nameApi: getNameApi({ ...DEFAULT_NAME_TRANSFORMERS, ...options.nameTransformers }),
|
|
3291
|
-
nonScalarEnums: (options.nonScalarEnums ?? []).map(
|
|
3082
|
+
nonScalarEnums: (options.nonScalarEnums ?? []).map(camelCase),
|
|
3292
3083
|
renderParentInstructions: options.renderParentInstructions ?? false,
|
|
3293
3084
|
useGranularImports: options.useGranularImports ?? false
|
|
3294
3085
|
};
|
|
3295
3086
|
const typeManifestVisitor = getTypeManifestVisitor({ ...renderScopeWithTypeManifestVisitor, stack });
|
|
3296
3087
|
const renderScope = { ...renderScopeWithTypeManifestVisitor, typeManifestVisitor };
|
|
3297
|
-
const internalNodes = (options.internalNodes ?? []).map(
|
|
3088
|
+
const internalNodes = (options.internalNodes ?? []).map(camelCase);
|
|
3298
3089
|
const resolvedInstructionInputVisitor = getResolvedInstructionInputsVisitor();
|
|
3299
3090
|
const byteSizeVisitor = getByteSizeVisitor(linkables, { stack });
|
|
3300
3091
|
const asPage = (fragment2, dependencyMap = {}) => {
|
|
@@ -3304,47 +3095,47 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3304
3095
|
dependencyMap: { ...renderScope.dependencyMap, ...dependencyMap }
|
|
3305
3096
|
});
|
|
3306
3097
|
};
|
|
3307
|
-
return
|
|
3308
|
-
|
|
3098
|
+
return pipe(
|
|
3099
|
+
staticVisitor(() => createRenderMap(), {
|
|
3309
3100
|
keys: ["rootNode", "programNode", "pdaNode", "accountNode", "definedTypeNode", "instructionNode"]
|
|
3310
3101
|
}),
|
|
3311
|
-
(v) =>
|
|
3102
|
+
(v) => extendVisitor(v, {
|
|
3312
3103
|
visitAccount(node) {
|
|
3313
3104
|
return createRenderMap(
|
|
3314
|
-
`accounts/${
|
|
3105
|
+
`accounts/${camelCase(node.name)}.ts`,
|
|
3315
3106
|
asPage(
|
|
3316
3107
|
getAccountPageFragment({
|
|
3317
3108
|
...renderScope,
|
|
3318
3109
|
accountPath: stack.getPath("accountNode"),
|
|
3319
|
-
size:
|
|
3110
|
+
size: visit(node, byteSizeVisitor)
|
|
3320
3111
|
})
|
|
3321
3112
|
)
|
|
3322
3113
|
);
|
|
3323
3114
|
},
|
|
3324
3115
|
visitDefinedType(node) {
|
|
3325
3116
|
return createRenderMap(
|
|
3326
|
-
`types/${
|
|
3327
|
-
asPage(getTypePageFragment({ ...renderScope, node, size:
|
|
3117
|
+
`types/${camelCase(node.name)}.ts`,
|
|
3118
|
+
asPage(getTypePageFragment({ ...renderScope, node, size: visit(node, byteSizeVisitor) }), {
|
|
3328
3119
|
generatedTypes: "."
|
|
3329
3120
|
})
|
|
3330
3121
|
);
|
|
3331
3122
|
},
|
|
3332
3123
|
visitInstruction(node) {
|
|
3333
3124
|
return createRenderMap(
|
|
3334
|
-
`instructions/${
|
|
3125
|
+
`instructions/${camelCase(node.name)}.ts`,
|
|
3335
3126
|
asPage(
|
|
3336
3127
|
getInstructionPageFragment({
|
|
3337
3128
|
...renderScope,
|
|
3338
3129
|
instructionPath: stack.getPath("instructionNode"),
|
|
3339
|
-
resolvedInputs:
|
|
3340
|
-
size:
|
|
3130
|
+
resolvedInputs: visit(node, resolvedInstructionInputVisitor),
|
|
3131
|
+
size: visit(node, byteSizeVisitor)
|
|
3341
3132
|
})
|
|
3342
3133
|
)
|
|
3343
3134
|
);
|
|
3344
3135
|
},
|
|
3345
3136
|
visitPda(node) {
|
|
3346
3137
|
return createRenderMap(
|
|
3347
|
-
`pdas/${
|
|
3138
|
+
`pdas/${camelCase(node.name)}.ts`,
|
|
3348
3139
|
asPage(getPdaPageFragment({ ...renderScope, pdaPath: stack.getPath("pdaNode") }))
|
|
3349
3140
|
);
|
|
3350
3141
|
},
|
|
@@ -3356,15 +3147,15 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3356
3147
|
const scope = { ...renderScope, programNode: node };
|
|
3357
3148
|
return mergeRenderMaps([
|
|
3358
3149
|
createRenderMap({
|
|
3359
|
-
[`programs/${
|
|
3360
|
-
[`errors/${
|
|
3150
|
+
[`programs/${camelCase(node.name)}.ts`]: asPage(getProgramPageFragment(scope)),
|
|
3151
|
+
[`errors/${camelCase(node.name)}.ts`]: node.errors.length > 0 ? asPage(getErrorPageFragment(scope)) : void 0
|
|
3361
3152
|
}),
|
|
3362
|
-
...node.pdas.map((p) =>
|
|
3363
|
-
...node.accounts.map((a) =>
|
|
3364
|
-
...node.definedTypes.map((t) =>
|
|
3365
|
-
...customDataDefinedType.map((t) =>
|
|
3366
|
-
...
|
|
3367
|
-
(i) =>
|
|
3153
|
+
...node.pdas.map((p) => visit(p, self)),
|
|
3154
|
+
...node.accounts.map((a) => visit(a, self)),
|
|
3155
|
+
...node.definedTypes.map((t) => visit(t, self)),
|
|
3156
|
+
...customDataDefinedType.map((t) => visit(t, self)),
|
|
3157
|
+
...getAllInstructionsWithSubs(node, { leavesOnly: !renderScope.renderParentInstructions }).map(
|
|
3158
|
+
(i) => visit(i, self)
|
|
3368
3159
|
)
|
|
3369
3160
|
]);
|
|
3370
3161
|
},
|
|
@@ -3374,13 +3165,12 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3374
3165
|
const programsWithErrorsToExport = programsToExport.filter((p) => p.errors.length > 0);
|
|
3375
3166
|
const pdasToExport = getAllPdas(node);
|
|
3376
3167
|
const accountsToExport = getAllAccounts(node).filter(isNotInternal);
|
|
3377
|
-
const instructionsToExport =
|
|
3168
|
+
const instructionsToExport = getAllInstructionsWithSubs(node, {
|
|
3378
3169
|
leavesOnly: !renderScope.renderParentInstructions
|
|
3379
3170
|
}).filter(isNotInternal);
|
|
3380
3171
|
const definedTypesToExport = getAllDefinedTypes(node).filter(isNotInternal);
|
|
3381
3172
|
const hasAnythingToExport = programsToExport.length > 0 || accountsToExport.length > 0 || instructionsToExport.length > 0 || definedTypesToExport.length > 0;
|
|
3382
3173
|
const scope = {
|
|
3383
|
-
...renderScope,
|
|
3384
3174
|
accountsToExport,
|
|
3385
3175
|
definedTypesToExport,
|
|
3386
3176
|
instructionsToExport,
|
|
@@ -3398,61 +3188,26 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3398
3188
|
["shared/index.ts"]: hasAnythingToExport ? asPage(getSharedPageFragment()) : void 0,
|
|
3399
3189
|
["types/index.ts"]: asPage(getIndexPageFragment(definedTypesToExport))
|
|
3400
3190
|
}),
|
|
3401
|
-
...getAllPrograms(node).map((p) =>
|
|
3191
|
+
...getAllPrograms(node).map((p) => visit(p, self))
|
|
3402
3192
|
]);
|
|
3403
3193
|
}
|
|
3404
3194
|
}),
|
|
3405
|
-
(v) =>
|
|
3195
|
+
(v) => recordNodeStackVisitor(v, stack),
|
|
3406
3196
|
(v) => recordLinkablesOnFirstVisitVisitor(v, linkables)
|
|
3407
3197
|
);
|
|
3408
3198
|
}
|
|
3409
|
-
|
|
3410
|
-
// src/visitors/renderVisitor.ts
|
|
3411
|
-
import { deleteDirectory, mapRenderMapContentAsync, writeRenderMap } from "@codama/renderers-core";
|
|
3412
|
-
import { rootNodeVisitor, visit as visit10 } from "@codama/visitors-core";
|
|
3413
|
-
import * as estreePlugin from "prettier/plugins/estree";
|
|
3414
|
-
import * as typeScriptPlugin from "prettier/plugins/typescript";
|
|
3415
|
-
import { format } from "prettier/standalone";
|
|
3416
|
-
var DEFAULT_PRETTIER_OPTIONS = {
|
|
3417
|
-
arrowParens: "always",
|
|
3418
|
-
parser: "typescript",
|
|
3419
|
-
plugins: [estreePlugin, typeScriptPlugin],
|
|
3420
|
-
printWidth: 80,
|
|
3421
|
-
semi: true,
|
|
3422
|
-
singleQuote: true,
|
|
3423
|
-
tabWidth: 2,
|
|
3424
|
-
trailingComma: "es5",
|
|
3425
|
-
useTabs: false
|
|
3426
|
-
};
|
|
3427
3199
|
function renderVisitor(path, options = {}) {
|
|
3428
3200
|
return rootNodeVisitor(async (root) => {
|
|
3429
3201
|
if (options.deleteFolderBeforeRendering ?? true) {
|
|
3430
3202
|
deleteDirectory(path);
|
|
3431
3203
|
}
|
|
3432
|
-
let renderMap =
|
|
3433
|
-
|
|
3434
|
-
const prettierOptions = { ...DEFAULT_PRETTIER_OPTIONS, ...options.prettierOptions };
|
|
3435
|
-
renderMap = await mapRenderMapContentAsync(renderMap, (code) => format(code, prettierOptions));
|
|
3436
|
-
}
|
|
3204
|
+
let renderMap = visit(root, getRenderMapVisitor(options));
|
|
3205
|
+
renderMap = await formatCode(renderMap, options);
|
|
3437
3206
|
syncPackageJson(renderMap, options);
|
|
3438
3207
|
writeRenderMap(renderMap, path);
|
|
3439
3208
|
});
|
|
3440
3209
|
}
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
createImportMap,
|
|
3445
|
-
renderVisitor as default,
|
|
3446
|
-
getExternalDependencies,
|
|
3447
|
-
getNameApi,
|
|
3448
|
-
getRenderMapVisitor,
|
|
3449
|
-
getTypeManifestVisitor,
|
|
3450
|
-
importMapToString,
|
|
3451
|
-
mergeImportMaps,
|
|
3452
|
-
mergeTypeManifests,
|
|
3453
|
-
parseImportInput,
|
|
3454
|
-
removeFromImportMap,
|
|
3455
|
-
renderVisitor,
|
|
3456
|
-
typeManifest
|
|
3457
|
-
};
|
|
3210
|
+
|
|
3211
|
+
export { DEFAULT_NAME_TRANSFORMERS, addToImportMap, createImportMap, renderVisitor as default, getExternalDependencies, getNameApi, getRenderMapVisitor, getTypeManifestVisitor, importMapToString, mergeImportMaps, mergeTypeManifests, parseImportInput, removeFromImportMap, renderVisitor, typeManifest };
|
|
3212
|
+
//# sourceMappingURL=index.node.mjs.map
|
|
3458
3213
|
//# sourceMappingURL=index.node.mjs.map
|