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