@codama/renderers-js 1.5.1 → 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/README.md +18 -15
- package/dist/index.browser.cjs +680 -655
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.mjs +492 -559
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.node.cjs +679 -671
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +491 -559
- package/dist/index.node.mjs.map +1 -1
- package/dist/index.react-native.mjs +492 -559
- 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/importMap.d.ts +1 -0
- package/dist/types/utils/importMap.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/options.d.ts +5 -3
- package/dist/types/utils/options.d.ts.map +1 -1
- package/dist/types/utils/packageJson.d.ts +25 -0
- package/dist/types/utils/packageJson.d.ts.map +1 -0
- package/dist/types/visitors/getRenderMapVisitor.d.ts.map +1 -1
- package/dist/types/visitors/renderVisitor.d.ts.map +1 -1
- package/package.json +9 -7
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",
|
|
@@ -100,6 +110,10 @@ function importMapToString(importMap, dependencyMap = {}, useGranularImports = f
|
|
|
100
110
|
return `import { ${innerImports} } from '${module}';`;
|
|
101
111
|
}).join("\n");
|
|
102
112
|
}
|
|
113
|
+
function getExternalDependencies(importMap, dependencyMap, useGranularImports) {
|
|
114
|
+
const resolvedImports = resolveImportMapModules(importMap, dependencyMap, useGranularImports);
|
|
115
|
+
return new Set([...resolvedImports.keys()].filter((module) => !module.startsWith(".")));
|
|
116
|
+
}
|
|
103
117
|
function resolveImportMapModules(importMap, dependencyMap, useGranularImports) {
|
|
104
118
|
const dependencyMapWithDefaults = {
|
|
105
119
|
...useGranularImports ? DEFAULT_GRANULAR_EXTERNAL_MODULE_MAP : DEFAULT_EXTERNAL_MODULE_MAP,
|
|
@@ -117,9 +131,6 @@ function importInfoToString({ importedIdentifier, isType, usedIdentifier }) {
|
|
|
117
131
|
const alias = importedIdentifier !== usedIdentifier ? ` as ${usedIdentifier}` : "";
|
|
118
132
|
return `${isType ? "type " : ""}${importedIdentifier}${alias}`;
|
|
119
133
|
}
|
|
120
|
-
|
|
121
|
-
// src/utils/nameTransformers.ts
|
|
122
|
-
import { camelCase, capitalize, kebabCase, pascalCase, snakeCase, titleCase } from "@codama/nodes";
|
|
123
134
|
function getNameApi(transformers) {
|
|
124
135
|
const helpers = {
|
|
125
136
|
camelCase,
|
|
@@ -181,9 +192,6 @@ var DEFAULT_NAME_TRANSFORMERS = {
|
|
|
181
192
|
programIsErrorFunction: (name) => `is${pascalCase(name)}Error`,
|
|
182
193
|
resolverFunction: (name) => `${camelCase(name)}`
|
|
183
194
|
};
|
|
184
|
-
|
|
185
|
-
// src/utils/fragment.ts
|
|
186
|
-
import { createFragmentTemplate } from "@codama/renderers-core";
|
|
187
195
|
function createFragment(content) {
|
|
188
196
|
return Object.freeze({ content, features: /* @__PURE__ */ new Set(), imports: createImportMap() });
|
|
189
197
|
}
|
|
@@ -263,40 +271,6 @@ function mergeTypeManifests(manifests, options = {}) {
|
|
|
263
271
|
value: merge((m) => m.value, mergeValues)
|
|
264
272
|
});
|
|
265
273
|
}
|
|
266
|
-
|
|
267
|
-
// src/visitors/getRenderMapVisitor.ts
|
|
268
|
-
import {
|
|
269
|
-
camelCase as camelCase15,
|
|
270
|
-
getAllAccounts,
|
|
271
|
-
getAllDefinedTypes,
|
|
272
|
-
getAllInstructionsWithSubs as getAllInstructionsWithSubs2,
|
|
273
|
-
getAllPdas,
|
|
274
|
-
getAllPrograms
|
|
275
|
-
} from "@codama/nodes";
|
|
276
|
-
import { createRenderMap, mergeRenderMaps } from "@codama/renderers-core";
|
|
277
|
-
import {
|
|
278
|
-
extendVisitor as extendVisitor2,
|
|
279
|
-
getByteSizeVisitor,
|
|
280
|
-
getResolvedInstructionInputsVisitor,
|
|
281
|
-
LinkableDictionary as LinkableDictionary3,
|
|
282
|
-
NodeStack as NodeStack2,
|
|
283
|
-
pipe as pipe16,
|
|
284
|
-
recordLinkablesOnFirstVisitVisitor,
|
|
285
|
-
recordNodeStackVisitor as recordNodeStackVisitor2,
|
|
286
|
-
staticVisitor as staticVisitor2,
|
|
287
|
-
visit as visit9
|
|
288
|
-
} from "@codama/visitors-core";
|
|
289
|
-
|
|
290
|
-
// src/fragments/accountFetchHelpers.ts
|
|
291
|
-
import { getLastNodeFromPath, pipe } from "@codama/visitors-core";
|
|
292
|
-
|
|
293
|
-
// src/utils/async.ts
|
|
294
|
-
import {
|
|
295
|
-
accountValueNode,
|
|
296
|
-
argumentValueNode,
|
|
297
|
-
isNode
|
|
298
|
-
} from "@codama/nodes";
|
|
299
|
-
import { deduplicateInstructionDependencies } from "@codama/visitors-core";
|
|
300
274
|
function hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers) {
|
|
301
275
|
const hasByteDeltasAsync = (instructionNode.byteDeltas ?? []).some(
|
|
302
276
|
({ value }) => isNode(value, "resolverValueNode") && asyncResolvers.includes(value.name)
|
|
@@ -366,9 +340,6 @@ function getInstructionDependencies(input, asyncResolvers, useAsync) {
|
|
|
366
340
|
}
|
|
367
341
|
return [];
|
|
368
342
|
}
|
|
369
|
-
|
|
370
|
-
// src/utils/codecs.ts
|
|
371
|
-
import { getBase16Encoder, getBase58Encoder, getBase64Encoder, getUtf8Encoder } from "@solana/codecs-strings";
|
|
372
343
|
function getBytesFromBytesValueNode(node) {
|
|
373
344
|
switch (node.encoding) {
|
|
374
345
|
case "utf8":
|
|
@@ -382,25 +353,16 @@ function getBytesFromBytesValueNode(node) {
|
|
|
382
353
|
return getBase64Encoder().encode(node.data);
|
|
383
354
|
}
|
|
384
355
|
}
|
|
385
|
-
|
|
386
|
-
// src/utils/customData.ts
|
|
387
|
-
import {
|
|
388
|
-
camelCase as camelCase2,
|
|
389
|
-
definedTypeLinkNode,
|
|
390
|
-
definedTypeNode,
|
|
391
|
-
isNode as isNode2,
|
|
392
|
-
structTypeNodeFromInstructionArgumentNodes
|
|
393
|
-
} from "@codama/nodes";
|
|
394
356
|
var parseCustomDataOptions = (customDataOptions, defaultSuffix) => new Map(
|
|
395
357
|
customDataOptions.map((o) => {
|
|
396
358
|
const options = typeof o === "string" ? { name: o } : o;
|
|
397
|
-
const importAs =
|
|
359
|
+
const importAs = camelCase(options.importAs ?? `${options.name}${defaultSuffix}`);
|
|
398
360
|
const importFrom = options.importFrom ?? "hooked";
|
|
399
361
|
return [
|
|
400
|
-
|
|
362
|
+
camelCase(options.name),
|
|
401
363
|
{
|
|
402
364
|
extract: options.extract ?? false,
|
|
403
|
-
extractAs: options.extractAs ?
|
|
365
|
+
extractAs: options.extractAs ? camelCase(options.extractAs) : importAs,
|
|
404
366
|
importAs,
|
|
405
367
|
importFrom,
|
|
406
368
|
linkNode: definedTypeLinkNode(importAs)
|
|
@@ -411,7 +373,7 @@ var parseCustomDataOptions = (customDataOptions, defaultSuffix) => new Map(
|
|
|
411
373
|
var getDefinedTypeNodesToExtract = (nodes, parsedCustomDataOptions) => nodes.flatMap((node) => {
|
|
412
374
|
const options = parsedCustomDataOptions.get(node.name);
|
|
413
375
|
if (!options || !options.extract) return [];
|
|
414
|
-
if (
|
|
376
|
+
if (isNode(node, "accountNode")) {
|
|
415
377
|
return [definedTypeNode({ name: options.extractAs, type: { ...node.data } })];
|
|
416
378
|
}
|
|
417
379
|
return [
|
|
@@ -421,9 +383,31 @@ var getDefinedTypeNodesToExtract = (nodes, parsedCustomDataOptions) => nodes.fla
|
|
|
421
383
|
})
|
|
422
384
|
];
|
|
423
385
|
});
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
+
}
|
|
427
411
|
function getImportFromFactory(overrides, customAccountData, customInstructionData) {
|
|
428
412
|
const customDataOverrides = Object.fromEntries(
|
|
429
413
|
[...customAccountData.values(), ...customInstructionData.values()].map(({ importFrom, importAs }) => [
|
|
@@ -470,6 +454,172 @@ function getImportFromFactory(overrides, customAccountData, customInstructionDat
|
|
|
470
454
|
}
|
|
471
455
|
};
|
|
472
456
|
}
|
|
457
|
+
var DEFAULT_DEPENDENCY_VERSIONS = {
|
|
458
|
+
"@solana/accounts": "^5.0.0",
|
|
459
|
+
"@solana/addresses": "^5.0.0",
|
|
460
|
+
"@solana/codecs": "^5.0.0",
|
|
461
|
+
"@solana/errors": "^5.0.0",
|
|
462
|
+
"@solana/instructions": "^5.0.0",
|
|
463
|
+
"@solana/kit": "^5.0.0",
|
|
464
|
+
"@solana/programs": "^5.0.0",
|
|
465
|
+
"@solana/rpc-types": "^5.0.0",
|
|
466
|
+
"@solana/signers": "^5.0.0"
|
|
467
|
+
};
|
|
468
|
+
function syncPackageJson(renderMap, options) {
|
|
469
|
+
const shouldSyncPackageJson = options.syncPackageJson ?? false;
|
|
470
|
+
const packageFolder = options.packageFolder;
|
|
471
|
+
if (!packageFolder) {
|
|
472
|
+
if (shouldSyncPackageJson) {
|
|
473
|
+
logWarn("Cannot sync package.json. Please provide the 'packageFolder' option.");
|
|
474
|
+
}
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const packageJsonPath = joinPath(packageFolder, "package.json");
|
|
478
|
+
const usedDependencies = getUsedDependencyVersions(
|
|
479
|
+
renderMap,
|
|
480
|
+
options.dependencyMap ?? {},
|
|
481
|
+
options.dependencyVersions ?? {},
|
|
482
|
+
options.useGranularImports ?? false
|
|
483
|
+
);
|
|
484
|
+
if (!shouldSyncPackageJson) {
|
|
485
|
+
if (fileExists(packageJsonPath)) {
|
|
486
|
+
checkExistingPackageJson(readJson(packageJsonPath), usedDependencies);
|
|
487
|
+
}
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (fileExists(packageJsonPath)) {
|
|
491
|
+
const packageJson = updateExistingPackageJson(readJson(packageJsonPath), usedDependencies);
|
|
492
|
+
writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
493
|
+
} else {
|
|
494
|
+
const packageJson = createNewPackageJson(usedDependencies);
|
|
495
|
+
writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
function createNewPackageJson(dependencyVersions) {
|
|
499
|
+
return updateExistingPackageJson(
|
|
500
|
+
{
|
|
501
|
+
name: "js-client",
|
|
502
|
+
version: "1.0.0",
|
|
503
|
+
// eslint-disable-next-line sort-keys-fix/sort-keys-fix
|
|
504
|
+
description: "",
|
|
505
|
+
main: "src/index.ts",
|
|
506
|
+
scripts: { test: 'echo "Error: no test specified" && exit 1' },
|
|
507
|
+
// eslint-disable-next-line sort-keys-fix/sort-keys-fix
|
|
508
|
+
keywords: [],
|
|
509
|
+
// eslint-disable-next-line sort-keys-fix/sort-keys-fix
|
|
510
|
+
author: ""
|
|
511
|
+
},
|
|
512
|
+
dependencyVersions
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
function updateExistingPackageJson(packageJson, dependencyVersions) {
|
|
516
|
+
const updatedDependencies = { ...packageJson.dependencies };
|
|
517
|
+
const updatedPeerDependencies = { ...packageJson.peerDependencies };
|
|
518
|
+
const updatedDevDependencies = { ...packageJson.devDependencies };
|
|
519
|
+
for (const [dependency, requiredRange] of Object.entries(dependencyVersions)) {
|
|
520
|
+
let found = false;
|
|
521
|
+
if (updatedDependencies[dependency]) {
|
|
522
|
+
updateDependency(updatedDependencies, dependency, requiredRange);
|
|
523
|
+
found = true;
|
|
524
|
+
}
|
|
525
|
+
if (updatedPeerDependencies[dependency]) {
|
|
526
|
+
updateDependency(updatedPeerDependencies, dependency, requiredRange);
|
|
527
|
+
found = true;
|
|
528
|
+
}
|
|
529
|
+
if (updatedDevDependencies[dependency]) {
|
|
530
|
+
updateDependency(updatedDevDependencies, dependency, requiredRange);
|
|
531
|
+
found = true;
|
|
532
|
+
}
|
|
533
|
+
if (!found) {
|
|
534
|
+
const dependencyGroupToAdd = dependency === "@solana/kit" ? updatedPeerDependencies : updatedDependencies;
|
|
535
|
+
dependencyGroupToAdd[dependency] = requiredRange;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return {
|
|
539
|
+
...packageJson,
|
|
540
|
+
...Object.entries(updatedPeerDependencies).length > 0 ? { peerDependencies: updatedPeerDependencies } : {},
|
|
541
|
+
...Object.entries(updatedDependencies).length > 0 ? { dependencies: updatedDependencies } : {},
|
|
542
|
+
...Object.entries(updatedDevDependencies).length > 0 ? { devDependencies: updatedDevDependencies } : {}
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
function checkExistingPackageJson(packageJson, dependencyVersions) {
|
|
546
|
+
const missingDependencies = [];
|
|
547
|
+
const dependenciesToUpdate = [];
|
|
548
|
+
const existingDependencies = {
|
|
549
|
+
...packageJson.devDependencies,
|
|
550
|
+
...packageJson.peerDependencies,
|
|
551
|
+
...packageJson.dependencies
|
|
552
|
+
};
|
|
553
|
+
for (const [dependency, requiredRange] of Object.entries(dependencyVersions)) {
|
|
554
|
+
if (!existingDependencies[dependency]) {
|
|
555
|
+
missingDependencies.push(dependency);
|
|
556
|
+
} else if (shouldUpdateRange(dependency, existingDependencies[dependency], requiredRange)) {
|
|
557
|
+
dependenciesToUpdate.push(dependency);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (missingDependencies.length === 0 && dependenciesToUpdate.length === 0) return;
|
|
561
|
+
const missingList = missingDependencies.map((d) => `- ${d} missing: ${dependencyVersions[d]}
|
|
562
|
+
`).join("");
|
|
563
|
+
const outdatedList = dependenciesToUpdate.map((d) => `- ${d} outdated: ${existingDependencies[d]} -> ${dependencyVersions[d]}
|
|
564
|
+
`).join("");
|
|
565
|
+
logWarn(
|
|
566
|
+
`The following dependencies in your \`package.json\` are out-of-date or missing:
|
|
567
|
+
${missingList}${outdatedList}`
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
function getUsedDependencyVersions(renderMap, dependencyMap, dependencyVersions, useGranularImports) {
|
|
571
|
+
const dependencyVersionsWithDefaults = {
|
|
572
|
+
...DEFAULT_DEPENDENCY_VERSIONS,
|
|
573
|
+
...dependencyVersions
|
|
574
|
+
};
|
|
575
|
+
const fragment2 = mergeFragments([...renderMap.values()], () => "");
|
|
576
|
+
const usedDependencies = getExternalDependencies(fragment2.imports, dependencyMap, useGranularImports);
|
|
577
|
+
const [usedDependencyVersion, missingDependencies] = [...usedDependencies].reduce(
|
|
578
|
+
([acc, missingDependencies2], dependency) => {
|
|
579
|
+
const version = dependencyVersionsWithDefaults[dependency];
|
|
580
|
+
if (version) {
|
|
581
|
+
acc[dependency] = version;
|
|
582
|
+
} else {
|
|
583
|
+
missingDependencies2.add(dependency);
|
|
584
|
+
}
|
|
585
|
+
return [acc, missingDependencies2];
|
|
586
|
+
},
|
|
587
|
+
[{}, /* @__PURE__ */ new Set()]
|
|
588
|
+
);
|
|
589
|
+
if (missingDependencies.size > 0) {
|
|
590
|
+
throw new CodamaError(CODAMA_ERROR__RENDERERS__MISSING_DEPENDENCY_VERSIONS, {
|
|
591
|
+
dependencies: [...missingDependencies],
|
|
592
|
+
message: "Please add these dependencies to the `dependencyVersions` option."
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
return usedDependencyVersion;
|
|
596
|
+
}
|
|
597
|
+
function shouldUpdateRange(dependency, currentRange, requiredRange) {
|
|
598
|
+
try {
|
|
599
|
+
if (subset(currentRange, requiredRange)) {
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
const minRequiredVersion = minVersion(requiredRange);
|
|
603
|
+
const minCurrentVersion = minVersion(currentRange);
|
|
604
|
+
if (!minCurrentVersion || !minRequiredVersion) {
|
|
605
|
+
throw new Error("Could not determine minimum versions.");
|
|
606
|
+
}
|
|
607
|
+
if (lt(minCurrentVersion, minRequiredVersion)) {
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
return false;
|
|
611
|
+
} catch (error) {
|
|
612
|
+
console.warn(
|
|
613
|
+
`Could not parse the following ranges for dependency "${dependency}": [${currentRange}] and/or [${requiredRange}]. Caused by: ${error.message}`
|
|
614
|
+
);
|
|
615
|
+
return false;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function updateDependency(dependencyGroup, dependency, requiredRange) {
|
|
619
|
+
const currentRange = dependencyGroup[dependency];
|
|
620
|
+
if (!shouldUpdateRange(dependency, currentRange, requiredRange)) return;
|
|
621
|
+
dependencyGroup[dependency] = requiredRange;
|
|
622
|
+
}
|
|
473
623
|
|
|
474
624
|
// src/fragments/accountFetchHelpers.ts
|
|
475
625
|
function getAccountFetchHelpersFragment(scope) {
|
|
@@ -543,17 +693,9 @@ export async function ${fetchAllMaybeFunction}(
|
|
|
543
693
|
])
|
|
544
694
|
);
|
|
545
695
|
}
|
|
546
|
-
|
|
547
|
-
// src/fragments/accountPage.ts
|
|
548
|
-
import { resolveNestedTypeNode as resolveNestedTypeNode2 } from "@codama/nodes";
|
|
549
|
-
import { findProgramNodeFromPath, getLastNodeFromPath as getLastNodeFromPath5, visit as visit2 } from "@codama/visitors-core";
|
|
550
|
-
|
|
551
|
-
// src/fragments/accountPdaHelpers.ts
|
|
552
|
-
import { isNodeFilter } from "@codama/nodes";
|
|
553
|
-
import { getLastNodeFromPath as getLastNodeFromPath2, pipe as pipe2 } from "@codama/visitors-core";
|
|
554
696
|
function getAccountPdaHelpersFragment(scope) {
|
|
555
697
|
const { accountPath, nameApi, linkables, customAccountData, typeManifest: typeManifest2 } = scope;
|
|
556
|
-
const accountNode =
|
|
698
|
+
const accountNode = getLastNodeFromPath(accountPath);
|
|
557
699
|
const pdaNode = accountNode.pda ? linkables.get([...accountPath, accountNode.pda]) : void 0;
|
|
558
700
|
if (!pdaNode) return;
|
|
559
701
|
const accountType = customAccountData.has(accountNode.name) ? typeManifest2.strictType : nameApi.dataType(accountNode.name);
|
|
@@ -564,7 +706,7 @@ function getAccountPdaHelpersFragment(scope) {
|
|
|
564
706
|
const fetchFromSeedsFunction = nameApi.accountFetchFromSeedsFunction(accountNode.name);
|
|
565
707
|
const fetchMaybeFromSeedsFunction = nameApi.accountFetchMaybeFromSeedsFunction(accountNode.name);
|
|
566
708
|
const fetchMaybeFunction = nameApi.accountFetchMaybeFunction(accountNode.name);
|
|
567
|
-
return
|
|
709
|
+
return pipe(
|
|
568
710
|
fragment`export async function ${fetchFromSeedsFunction}(
|
|
569
711
|
rpc: Parameters<typeof fetchEncodedAccount>[0],
|
|
570
712
|
${hasVariableSeeds ? `seeds: ${pdaSeedsType},` : ""}
|
|
@@ -594,12 +736,9 @@ export async function ${fetchMaybeFromSeedsFunction}(
|
|
|
594
736
|
])
|
|
595
737
|
);
|
|
596
738
|
}
|
|
597
|
-
|
|
598
|
-
// src/fragments/accountSizeHelpers.ts
|
|
599
|
-
import { getLastNodeFromPath as getLastNodeFromPath3 } from "@codama/visitors-core";
|
|
600
739
|
function getAccountSizeHelpersFragment(scope) {
|
|
601
740
|
const { accountPath, nameApi } = scope;
|
|
602
|
-
const accountNode =
|
|
741
|
+
const accountNode = getLastNodeFromPath(accountPath);
|
|
603
742
|
if (accountNode.size == null) return;
|
|
604
743
|
const getSizeFunction = nameApi.accountGetSizeFunction(accountNode.name);
|
|
605
744
|
return fragment`export function ${getSizeFunction}(): number {
|
|
@@ -607,10 +746,6 @@ function getAccountSizeHelpersFragment(scope) {
|
|
|
607
746
|
}`;
|
|
608
747
|
}
|
|
609
748
|
|
|
610
|
-
// src/fragments/accountType.ts
|
|
611
|
-
import { resolveNestedTypeNode } from "@codama/nodes";
|
|
612
|
-
import { getLastNodeFromPath as getLastNodeFromPath4 } from "@codama/visitors-core";
|
|
613
|
-
|
|
614
749
|
// src/fragments/type.ts
|
|
615
750
|
function getTypeFragment(scope) {
|
|
616
751
|
const { name, manifest, nameApi, docs = [] } = scope;
|
|
@@ -624,9 +759,6 @@ function getTypeFragment(scope) {
|
|
|
624
759
|
const looseExport = manifest.strictType.content === manifest.looseType.content ? aliasedLooseName : fragment`export type ${looseName} = ${manifest.looseType};`;
|
|
625
760
|
return fragment`${docblock}export type ${strictName} = ${manifest.strictType};\n\n${looseExport}`;
|
|
626
761
|
}
|
|
627
|
-
|
|
628
|
-
// src/fragments/typeDecoder.ts
|
|
629
|
-
import { isDataEnum, isNode as isNode3 } from "@codama/nodes";
|
|
630
762
|
function getTypeDecoderFragment(scope) {
|
|
631
763
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
632
764
|
const decoderFunction = nameApi.decoderFunction(name);
|
|
@@ -636,15 +768,12 @@ function getTypeDecoderFragment(scope) {
|
|
|
636
768
|
typeof scope.size === "number" ? "type FixedSizeDecoder" : "type Decoder",
|
|
637
769
|
"solanaCodecsCore"
|
|
638
770
|
);
|
|
639
|
-
const useTypeCast =
|
|
771
|
+
const useTypeCast = isNode(node, "enumTypeNode") && isDataEnum(node) && typeof scope.size === "number";
|
|
640
772
|
const typeCast = useTypeCast ? fragment` as ${decoderType}<${strictName}>` : "";
|
|
641
773
|
return fragment`${docblock}export function ${decoderFunction}(): ${decoderType}<${strictName}> {
|
|
642
774
|
return ${manifest.decoder}${typeCast};
|
|
643
775
|
}`;
|
|
644
776
|
}
|
|
645
|
-
|
|
646
|
-
// src/fragments/typeEncoder.ts
|
|
647
|
-
import { isDataEnum as isDataEnum2, isNode as isNode4 } from "@codama/nodes";
|
|
648
777
|
function getTypeEncoderFragment(scope) {
|
|
649
778
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
650
779
|
const encoderFunction = nameApi.encoderFunction(name);
|
|
@@ -654,7 +783,7 @@ function getTypeEncoderFragment(scope) {
|
|
|
654
783
|
typeof scope.size === "number" ? "type FixedSizeEncoder" : "type Encoder",
|
|
655
784
|
"solanaCodecsCore"
|
|
656
785
|
);
|
|
657
|
-
const useTypeCast =
|
|
786
|
+
const useTypeCast = isNode(node, "enumTypeNode") && isDataEnum(node) && typeof scope.size === "number";
|
|
658
787
|
const typeCast = useTypeCast ? fragment` as ${encoderType}<${looseName}>` : "";
|
|
659
788
|
return fragment`${docblock}export function ${encoderFunction}(): ${encoderType}<${looseName}> {
|
|
660
789
|
return ${manifest.encoder}${typeCast};
|
|
@@ -694,7 +823,7 @@ function getTypeWithCodecFragment(scope) {
|
|
|
694
823
|
// src/fragments/accountType.ts
|
|
695
824
|
function getAccountTypeFragment(scope) {
|
|
696
825
|
const { accountPath, typeManifest: typeManifest2, nameApi, customAccountData } = scope;
|
|
697
|
-
const accountNode =
|
|
826
|
+
const accountNode = getLastNodeFromPath(accountPath);
|
|
698
827
|
if (customAccountData.has(accountNode.name)) return;
|
|
699
828
|
return getTypeWithCodecFragment({
|
|
700
829
|
codecDocs: [`Gets the codec for {@link ${nameApi.dataType(accountNode.name)}} account data.`],
|
|
@@ -708,15 +837,6 @@ function getAccountTypeFragment(scope) {
|
|
|
708
837
|
typeDocs: accountNode.docs
|
|
709
838
|
});
|
|
710
839
|
}
|
|
711
|
-
|
|
712
|
-
// src/fragments/discriminatorConstants.ts
|
|
713
|
-
import {
|
|
714
|
-
camelCase as camelCase3,
|
|
715
|
-
isNode as isNode5,
|
|
716
|
-
isNodeFilter as isNodeFilter2,
|
|
717
|
-
VALUE_NODES
|
|
718
|
-
} from "@codama/nodes";
|
|
719
|
-
import { visit } from "@codama/visitors-core";
|
|
720
840
|
function getDiscriminatorConstantsFragment(scope) {
|
|
721
841
|
const fragments = scope.discriminatorNodes.map((node) => getDiscriminatorConstantFragment(node, scope)).filter(Boolean);
|
|
722
842
|
return mergeFragments(fragments, (c) => c.join("\n\n"));
|
|
@@ -733,9 +853,9 @@ function getDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
|
733
853
|
}
|
|
734
854
|
function getConstantDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
735
855
|
const { discriminatorNodes, typeManifestVisitor, prefix } = scope;
|
|
736
|
-
const index = discriminatorNodes.filter(
|
|
856
|
+
const index = discriminatorNodes.filter(isNodeFilter("constantDiscriminatorNode")).indexOf(discriminatorNode);
|
|
737
857
|
const suffix = index <= 0 ? "" : `_${index + 1}`;
|
|
738
|
-
const name =
|
|
858
|
+
const name = camelCase(`${prefix}_discriminator${suffix}`);
|
|
739
859
|
const encoder = visit(discriminatorNode.constant.type, typeManifestVisitor).encoder;
|
|
740
860
|
const value = visit(discriminatorNode.constant.value, typeManifestVisitor).value;
|
|
741
861
|
return getConstantFragment({ ...scope, encoder, name, value });
|
|
@@ -743,10 +863,10 @@ function getConstantDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
|
743
863
|
function getFieldDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
744
864
|
const { fields, prefix, typeManifestVisitor } = scope;
|
|
745
865
|
const field = fields.find((f) => f.name === discriminatorNode.name);
|
|
746
|
-
if (!field || !field.defaultValue || !
|
|
866
|
+
if (!field || !field.defaultValue || !isNode(field.defaultValue, VALUE_NODES)) {
|
|
747
867
|
return null;
|
|
748
868
|
}
|
|
749
|
-
const name =
|
|
869
|
+
const name = camelCase(`${prefix}_${discriminatorNode.name}`);
|
|
750
870
|
const encoder = visit(field.type, typeManifestVisitor).encoder;
|
|
751
871
|
const value = visit(field.defaultValue, typeManifestVisitor).value;
|
|
752
872
|
return getConstantFragment({ ...scope, encoder, name, value });
|
|
@@ -760,12 +880,12 @@ function getConstantFragment(scope) {
|
|
|
760
880
|
|
|
761
881
|
// src/fragments/accountPage.ts
|
|
762
882
|
function getAccountPageFragment(scope) {
|
|
763
|
-
const node =
|
|
883
|
+
const node = getLastNodeFromPath(scope.accountPath);
|
|
764
884
|
if (!findProgramNodeFromPath(scope.accountPath)) {
|
|
765
885
|
throw new Error("Account must be visited inside a program.");
|
|
766
886
|
}
|
|
767
|
-
const typeManifest2 =
|
|
768
|
-
const fields =
|
|
887
|
+
const typeManifest2 = visit(node, scope.typeManifestVisitor);
|
|
888
|
+
const fields = resolveNestedTypeNode(node.data).fields;
|
|
769
889
|
return mergeFragments(
|
|
770
890
|
[
|
|
771
891
|
getDiscriminatorConstantsFragment({
|
|
@@ -782,29 +902,17 @@ function getAccountPageFragment(scope) {
|
|
|
782
902
|
(cs) => cs.join("\n\n")
|
|
783
903
|
);
|
|
784
904
|
}
|
|
785
|
-
|
|
786
|
-
// src/fragments/discriminatorCondition.ts
|
|
787
|
-
import {
|
|
788
|
-
constantDiscriminatorNode,
|
|
789
|
-
constantValueNode,
|
|
790
|
-
constantValueNodeFromBytes,
|
|
791
|
-
isNode as isNode6,
|
|
792
|
-
isNodeFilter as isNodeFilter3
|
|
793
|
-
} from "@codama/nodes";
|
|
794
|
-
import { mapFragmentContent } from "@codama/renderers-core";
|
|
795
|
-
import { pipe as pipe3, visit as visit3 } from "@codama/visitors-core";
|
|
796
|
-
import { getBase64Decoder } from "@solana/codecs-strings";
|
|
797
905
|
function getDiscriminatorConditionFragment(scope) {
|
|
798
|
-
return
|
|
906
|
+
return pipe(
|
|
799
907
|
mergeFragments(
|
|
800
908
|
scope.discriminators.flatMap((discriminator) => {
|
|
801
|
-
if (
|
|
909
|
+
if (isNode(discriminator, "sizeDiscriminatorNode")) {
|
|
802
910
|
return [getSizeConditionFragment(discriminator, scope)];
|
|
803
911
|
}
|
|
804
|
-
if (
|
|
912
|
+
if (isNode(discriminator, "constantDiscriminatorNode")) {
|
|
805
913
|
return [getByteConditionFragment(discriminator, scope)];
|
|
806
914
|
}
|
|
807
|
-
if (
|
|
915
|
+
if (isNode(discriminator, "fieldDiscriminatorNode")) {
|
|
808
916
|
return [getFieldConditionFragment(discriminator, scope)];
|
|
809
917
|
}
|
|
810
918
|
return [];
|
|
@@ -820,7 +928,7 @@ function getSizeConditionFragment(discriminator, scope) {
|
|
|
820
928
|
}
|
|
821
929
|
function getByteConditionFragment(discriminator, scope) {
|
|
822
930
|
const { dataName, typeManifestVisitor } = scope;
|
|
823
|
-
const constant =
|
|
931
|
+
const constant = visit(discriminator.constant, typeManifestVisitor).value;
|
|
824
932
|
return fragment`${use("containsBytes", "solanaCodecsCore")}(${dataName}, ${constant}, ${discriminator.offset})`;
|
|
825
933
|
}
|
|
826
934
|
function getFieldConditionFragment(discriminator, scope) {
|
|
@@ -830,7 +938,7 @@ function getFieldConditionFragment(discriminator, scope) {
|
|
|
830
938
|
`Field discriminator "${discriminator.name}" does not have a matching argument with default value.`
|
|
831
939
|
);
|
|
832
940
|
}
|
|
833
|
-
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"))) {
|
|
834
942
|
const base64Bytes = getBase64Decoder().decode(
|
|
835
943
|
new Uint8Array(field.defaultValue.items.map((node) => node.number))
|
|
836
944
|
);
|
|
@@ -920,19 +1028,13 @@ function getIsErrorFunctionFragment(scope) {
|
|
|
920
1028
|
return ${use("isProgramError", "solanaPrograms")}<TProgramErrorCode>(error, transactionMessage, ${programAddressConstant}, code);
|
|
921
1029
|
}`;
|
|
922
1030
|
}
|
|
923
|
-
|
|
924
|
-
// src/fragments/indexPage.ts
|
|
925
|
-
import { camelCase as camelCase4 } from "@codama/nodes";
|
|
926
1031
|
function getIndexPageFragment(items) {
|
|
927
1032
|
if (items.length === 0) return;
|
|
928
|
-
const names = items.map((item) =>
|
|
1033
|
+
const names = items.map((item) => camelCase(item.name)).sort((a, b) => a.localeCompare(b)).map((name) => getExportAllFragment(`./${name}`));
|
|
929
1034
|
return mergeFragments(names, (cs) => cs.join("\n"));
|
|
930
1035
|
}
|
|
931
|
-
|
|
932
|
-
// src/fragments/instructionAccountMeta.ts
|
|
933
|
-
import { pascalCase as pascalCase2 } from "@codama/nodes";
|
|
934
1036
|
function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
935
|
-
const typeParam = `TAccount${
|
|
1037
|
+
const typeParam = `TAccount${pascalCase(instructionAccountNode.name)}`;
|
|
936
1038
|
if (instructionAccountNode.isSigner === true && instructionAccountNode.isWritable) {
|
|
937
1039
|
return fragment`${use("type WritableSignerAccount", "solanaInstructions")}<${typeParam}> & ${use("type AccountSignerMeta", "solanaSigners")}<${typeParam}>`;
|
|
938
1040
|
}
|
|
@@ -944,21 +1046,13 @@ function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
|
944
1046
|
}
|
|
945
1047
|
return fragment`${use("type ReadonlyAccount", "solanaInstructions")}<${typeParam}>`;
|
|
946
1048
|
}
|
|
947
|
-
|
|
948
|
-
// src/fragments/instructionAccountTypeParam.ts
|
|
949
|
-
import { pascalCase as pascalCase3 } from "@codama/nodes";
|
|
950
|
-
import {
|
|
951
|
-
findInstructionNodeFromPath,
|
|
952
|
-
findProgramNodeFromPath as findProgramNodeFromPath2,
|
|
953
|
-
getLastNodeFromPath as getLastNodeFromPath6
|
|
954
|
-
} from "@codama/visitors-core";
|
|
955
1049
|
function getInstructionAccountTypeParamFragment(scope) {
|
|
956
|
-
const { instructionAccountPath,
|
|
957
|
-
const instructionAccountNode =
|
|
1050
|
+
const { instructionAccountPath, linkables } = scope;
|
|
1051
|
+
const instructionAccountNode = getLastNodeFromPath(instructionAccountPath);
|
|
958
1052
|
const instructionNode = findInstructionNodeFromPath(instructionAccountPath);
|
|
959
|
-
const programNode =
|
|
960
|
-
const typeParam = `TAccount${
|
|
961
|
-
const accountMeta =
|
|
1053
|
+
const programNode = findProgramNodeFromPath(instructionAccountPath);
|
|
1054
|
+
const typeParam = `TAccount${pascalCase(instructionAccountNode.name)}`;
|
|
1055
|
+
const accountMeta = fragment` | ${use("type AccountMeta", "solanaInstructions")}<string>` ;
|
|
962
1056
|
if (instructionNode.optionalAccountStrategy === "omitted" && instructionAccountNode.isOptional) {
|
|
963
1057
|
return fragment`${typeParam} extends string${accountMeta} | undefined = undefined`;
|
|
964
1058
|
}
|
|
@@ -978,13 +1072,8 @@ function getDefaultAddress(defaultValue, programId, linkables) {
|
|
|
978
1072
|
return "string";
|
|
979
1073
|
}
|
|
980
1074
|
}
|
|
981
|
-
|
|
982
|
-
// src/fragments/instructionByteDelta.ts
|
|
983
|
-
import { assertIsNode, camelCase as camelCase5, isNode as isNode7 } from "@codama/nodes";
|
|
984
|
-
import { mapFragmentContent as mapFragmentContent2 } from "@codama/renderers-core";
|
|
985
|
-
import { getLastNodeFromPath as getLastNodeFromPath7, pipe as pipe4 } from "@codama/visitors-core";
|
|
986
1075
|
function getInstructionByteDeltaFragment(scope) {
|
|
987
|
-
const { byteDeltas } =
|
|
1076
|
+
const { byteDeltas } = getLastNodeFromPath(scope.instructionPath);
|
|
988
1077
|
const fragments = (byteDeltas ?? []).flatMap((c) => getByteDeltaFragment(c, scope));
|
|
989
1078
|
if (fragments.length === 0) return;
|
|
990
1079
|
return mergeFragments(
|
|
@@ -995,16 +1084,16 @@ const byteDelta: number = [${c.join(",")}].reduce((a, b) => a + b, 0);`
|
|
|
995
1084
|
}
|
|
996
1085
|
function getByteDeltaFragment(byteDelta, scope) {
|
|
997
1086
|
let bytesFragment = (() => {
|
|
998
|
-
if (
|
|
1087
|
+
if (isNode(byteDelta.value, "numberValueNode")) {
|
|
999
1088
|
return getNumberValueNodeFragment(byteDelta);
|
|
1000
1089
|
}
|
|
1001
|
-
if (
|
|
1090
|
+
if (isNode(byteDelta.value, "argumentValueNode")) {
|
|
1002
1091
|
return getArgumentValueNodeFragment(byteDelta);
|
|
1003
1092
|
}
|
|
1004
|
-
if (
|
|
1093
|
+
if (isNode(byteDelta.value, "accountLinkNode")) {
|
|
1005
1094
|
return getAccountLinkNodeFragment(byteDelta, scope);
|
|
1006
1095
|
}
|
|
1007
|
-
if (
|
|
1096
|
+
if (isNode(byteDelta.value, "resolverValueNode")) {
|
|
1008
1097
|
return getResolverValueNodeFragment(byteDelta, scope);
|
|
1009
1098
|
}
|
|
1010
1099
|
return null;
|
|
@@ -1014,7 +1103,7 @@ function getByteDeltaFragment(byteDelta, scope) {
|
|
|
1014
1103
|
bytesFragment = fragment`${bytesFragment} + ${use("BASE_ACCOUNT_SIZE", "solanaAccounts")}`;
|
|
1015
1104
|
}
|
|
1016
1105
|
if (byteDelta.subtract) {
|
|
1017
|
-
bytesFragment =
|
|
1106
|
+
bytesFragment = pipe(bytesFragment, (f) => mapFragmentContent(f, (c) => `- (${c})`));
|
|
1018
1107
|
}
|
|
1019
1108
|
return [bytesFragment];
|
|
1020
1109
|
}
|
|
@@ -1024,7 +1113,7 @@ function getNumberValueNodeFragment(byteDelta) {
|
|
|
1024
1113
|
}
|
|
1025
1114
|
function getArgumentValueNodeFragment(byteDelta) {
|
|
1026
1115
|
assertIsNode(byteDelta.value, "argumentValueNode");
|
|
1027
|
-
const argumentName =
|
|
1116
|
+
const argumentName = camelCase(byteDelta.value.name);
|
|
1028
1117
|
return fragment`Number(args.${argumentName})`;
|
|
1029
1118
|
}
|
|
1030
1119
|
function getAccountLinkNodeFragment(byteDelta, scope) {
|
|
@@ -1044,59 +1133,32 @@ function getResolverValueNodeFragment(byteDelta, scope) {
|
|
|
1044
1133
|
scope.nameApi.resolverFunction(byteDelta.value.name),
|
|
1045
1134
|
scope.getImportFrom(byteDelta.value)
|
|
1046
1135
|
);
|
|
1047
|
-
return
|
|
1136
|
+
return pipe(
|
|
1048
1137
|
fragment`${awaitKeyword}${functionName}(resolverScope)`,
|
|
1049
1138
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1050
1139
|
);
|
|
1051
1140
|
}
|
|
1052
|
-
|
|
1053
|
-
// src/fragments/instructionData.ts
|
|
1054
|
-
import { structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes2 } from "@codama/nodes";
|
|
1055
|
-
import { getLastNodeFromPath as getLastNodeFromPath8 } from "@codama/visitors-core";
|
|
1056
1141
|
function getInstructionDataFragment(scope) {
|
|
1057
1142
|
const { instructionPath, dataArgsManifest, nameApi, customInstructionData } = scope;
|
|
1058
|
-
const instructionNode =
|
|
1143
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1059
1144
|
if (instructionNode.arguments.length === 0 || customInstructionData.has(instructionNode.name)) return;
|
|
1060
1145
|
const instructionDataName = nameApi.instructionDataType(instructionNode.name);
|
|
1061
1146
|
return getTypeWithCodecFragment({
|
|
1062
1147
|
manifest: dataArgsManifest,
|
|
1063
1148
|
name: instructionDataName,
|
|
1064
1149
|
nameApi,
|
|
1065
|
-
node:
|
|
1150
|
+
node: structTypeNodeFromInstructionArgumentNodes(instructionNode.arguments),
|
|
1066
1151
|
size: scope.size
|
|
1067
1152
|
});
|
|
1068
1153
|
}
|
|
1069
|
-
|
|
1070
|
-
// src/fragments/instructionExtraArgs.ts
|
|
1071
|
-
import { mapFragmentContent as mapFragmentContent3 } from "@codama/renderers-core";
|
|
1072
|
-
import { getLastNodeFromPath as getLastNodeFromPath9 } from "@codama/visitors-core";
|
|
1073
1154
|
function getInstructionExtraArgsFragment(scope) {
|
|
1074
1155
|
const { instructionPath, extraArgsManifest, nameApi } = scope;
|
|
1075
|
-
const instructionNode =
|
|
1156
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1076
1157
|
if ((instructionNode.extraArguments ?? []).length === 0) return;
|
|
1077
1158
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
1078
1159
|
const looseName = nameApi.dataArgsType(instructionExtraName);
|
|
1079
|
-
return
|
|
1160
|
+
return mapFragmentContent(extraArgsManifest.looseType, (c) => `export type ${looseName} = ${c};`);
|
|
1080
1161
|
}
|
|
1081
|
-
|
|
1082
|
-
// src/fragments/instructionFunction.ts
|
|
1083
|
-
import { camelCase as camelCase10, isNode as isNode12, isNodeFilter as isNodeFilter4, pascalCase as pascalCase5 } from "@codama/nodes";
|
|
1084
|
-
import { mapFragmentContent as mapFragmentContent7 } from "@codama/renderers-core";
|
|
1085
|
-
import {
|
|
1086
|
-
findProgramNodeFromPath as findProgramNodeFromPath3,
|
|
1087
|
-
getLastNodeFromPath as getLastNodeFromPath13,
|
|
1088
|
-
pipe as pipe8
|
|
1089
|
-
} from "@codama/visitors-core";
|
|
1090
|
-
|
|
1091
|
-
// src/fragments/instructionInputResolved.ts
|
|
1092
|
-
import { camelCase as camelCase7, isNode as isNode9, parseOptionalAccountStrategy } from "@codama/nodes";
|
|
1093
|
-
import { mapFragmentContent as mapFragmentContent5 } from "@codama/renderers-core";
|
|
1094
|
-
import { getLastNodeFromPath as getLastNodeFromPath10 } from "@codama/visitors-core";
|
|
1095
|
-
|
|
1096
|
-
// src/fragments/instructionInputDefault.ts
|
|
1097
|
-
import { camelCase as camelCase6, isNode as isNode8 } from "@codama/nodes";
|
|
1098
|
-
import { mapFragmentContent as mapFragmentContent4, setFragmentContent } from "@codama/renderers-core";
|
|
1099
|
-
import { pipe as pipe5, visit as visit4 } from "@codama/visitors-core";
|
|
1100
1162
|
function getInstructionInputDefaultFragment(scope) {
|
|
1101
1163
|
const { input, optionalAccountStrategy, asyncResolvers, useAsync, nameApi, typeManifestVisitor, getImportFrom } = scope;
|
|
1102
1164
|
if (!input.defaultValue) {
|
|
@@ -1107,8 +1169,8 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1107
1169
|
}
|
|
1108
1170
|
const { defaultValue } = input;
|
|
1109
1171
|
const defaultFragment = (renderedValue, isWritable) => {
|
|
1110
|
-
const inputName =
|
|
1111
|
-
if (input.kind === "instructionAccountNode" &&
|
|
1172
|
+
const inputName = camelCase(input.name);
|
|
1173
|
+
if (input.kind === "instructionAccountNode" && isNode(defaultValue, "resolverValueNode")) {
|
|
1112
1174
|
return fragment`accounts.${inputName} = { ...accounts.${inputName}, ...${renderedValue} };`;
|
|
1113
1175
|
}
|
|
1114
1176
|
if (input.kind === "instructionAccountNode" && isWritable === void 0) {
|
|
@@ -1126,7 +1188,7 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1126
1188
|
const addressType = use("type Address", "solanaAddresses");
|
|
1127
1189
|
switch (defaultValue.kind) {
|
|
1128
1190
|
case "accountValueNode":
|
|
1129
|
-
const name =
|
|
1191
|
+
const name = camelCase(defaultValue.name);
|
|
1130
1192
|
if (input.kind === "instructionAccountNode" && input.resolvedIsSigner && !input.isSigner) {
|
|
1131
1193
|
return defaultFragment(fragment`${expectTransactionSigner}(accounts.${name}.value).address`);
|
|
1132
1194
|
}
|
|
@@ -1136,13 +1198,13 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1136
1198
|
return defaultFragment(fragment`${expectAddress}(accounts.${name}.value)`);
|
|
1137
1199
|
case "pdaValueNode":
|
|
1138
1200
|
let pdaProgramValue;
|
|
1139
|
-
if (
|
|
1140
|
-
pdaProgramValue = fragment`${expectAddress}(accounts.${
|
|
1201
|
+
if (isNode(defaultValue.programId, "accountValueNode")) {
|
|
1202
|
+
pdaProgramValue = fragment`${expectAddress}(accounts.${camelCase(defaultValue.programId.name)}.value)`;
|
|
1141
1203
|
}
|
|
1142
|
-
if (
|
|
1143
|
-
pdaProgramValue = fragment`${expectAddress}(args.${
|
|
1204
|
+
if (isNode(defaultValue.programId, "argumentValueNode")) {
|
|
1205
|
+
pdaProgramValue = fragment`${expectAddress}(args.${camelCase(defaultValue.programId.name)})`;
|
|
1144
1206
|
}
|
|
1145
|
-
if (
|
|
1207
|
+
if (isNode(defaultValue.pda, "pdaNode")) {
|
|
1146
1208
|
let pdaProgram = fragment`programAddress`;
|
|
1147
1209
|
if (pdaProgramValue) {
|
|
1148
1210
|
pdaProgram = pdaProgramValue;
|
|
@@ -1150,29 +1212,29 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1150
1212
|
pdaProgram = fragment`'${defaultValue.pda.programId}' as ${addressType}<'${defaultValue.pda.programId}'>`;
|
|
1151
1213
|
}
|
|
1152
1214
|
const pdaSeeds2 = defaultValue.pda.seeds.flatMap((seed) => {
|
|
1153
|
-
if (
|
|
1215
|
+
if (isNode(seed, "constantPdaSeedNode") && isNode(seed.value, "programIdValueNode")) {
|
|
1154
1216
|
return [fragment`${use("getAddressEncoder", "solanaAddresses")}().encode(${pdaProgram})`];
|
|
1155
1217
|
}
|
|
1156
|
-
if (
|
|
1157
|
-
const typeManifest2 =
|
|
1158
|
-
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);
|
|
1159
1221
|
return [fragment`${typeManifest2.encoder}.encode(${valueManifest2.value})`];
|
|
1160
1222
|
}
|
|
1161
|
-
if (
|
|
1162
|
-
const typeManifest2 =
|
|
1223
|
+
if (isNode(seed, "variablePdaSeedNode")) {
|
|
1224
|
+
const typeManifest2 = visit(seed.type, typeManifestVisitor);
|
|
1163
1225
|
const valueSeed = defaultValue.seeds.find((s) => s.name === seed.name)?.value;
|
|
1164
1226
|
if (!valueSeed) return [];
|
|
1165
|
-
if (
|
|
1227
|
+
if (isNode(valueSeed, "accountValueNode")) {
|
|
1166
1228
|
return [
|
|
1167
|
-
fragment`${typeManifest2.encoder}.encode(${expectAddress}(accounts.${
|
|
1229
|
+
fragment`${typeManifest2.encoder}.encode(${expectAddress}(accounts.${camelCase(valueSeed.name)}.value))`
|
|
1168
1230
|
];
|
|
1169
1231
|
}
|
|
1170
|
-
if (
|
|
1232
|
+
if (isNode(valueSeed, "argumentValueNode")) {
|
|
1171
1233
|
return [
|
|
1172
|
-
fragment`${typeManifest2.encoder}.encode(${expectSome}(args.${
|
|
1234
|
+
fragment`${typeManifest2.encoder}.encode(${expectSome}(args.${camelCase(valueSeed.name)}))`
|
|
1173
1235
|
];
|
|
1174
1236
|
}
|
|
1175
|
-
const valueManifest2 =
|
|
1237
|
+
const valueManifest2 = visit(valueSeed, typeManifestVisitor);
|
|
1176
1238
|
return [fragment`${typeManifest2.encoder}.encode(${valueManifest2.value})`];
|
|
1177
1239
|
}
|
|
1178
1240
|
return [];
|
|
@@ -1187,20 +1249,20 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1187
1249
|
const pdaFunction = use(nameApi.pdaFindFunction(defaultValue.pda.name), getImportFrom(defaultValue.pda));
|
|
1188
1250
|
const pdaArgs = [];
|
|
1189
1251
|
const pdaSeeds = defaultValue.seeds.map((seed) => {
|
|
1190
|
-
if (
|
|
1191
|
-
return fragment`${seed.name}: ${expectAddress}(accounts.${
|
|
1252
|
+
if (isNode(seed.value, "accountValueNode")) {
|
|
1253
|
+
return fragment`${seed.name}: ${expectAddress}(accounts.${camelCase(seed.value.name)}.value)`;
|
|
1192
1254
|
}
|
|
1193
|
-
if (
|
|
1194
|
-
return fragment`${seed.name}: ${expectSome}(args.${
|
|
1255
|
+
if (isNode(seed.value, "argumentValueNode")) {
|
|
1256
|
+
return fragment`${seed.name}: ${expectSome}(args.${camelCase(seed.value.name)})`;
|
|
1195
1257
|
}
|
|
1196
|
-
return
|
|
1197
|
-
|
|
1198
|
-
(f) =>
|
|
1258
|
+
return pipe(
|
|
1259
|
+
visit(seed.value, typeManifestVisitor).value,
|
|
1260
|
+
(f) => mapFragmentContent(f, (c) => `${seed.name}: ${c}`)
|
|
1199
1261
|
);
|
|
1200
1262
|
});
|
|
1201
|
-
const pdaSeedsFragment =
|
|
1263
|
+
const pdaSeedsFragment = pipe(
|
|
1202
1264
|
mergeFragments(pdaSeeds, (renders) => renders.join(", ")),
|
|
1203
|
-
(f) =>
|
|
1265
|
+
(f) => mapFragmentContent(f, (c) => `{ ${c} }`)
|
|
1204
1266
|
);
|
|
1205
1267
|
if (pdaSeeds.length > 0) {
|
|
1206
1268
|
pdaArgs.push(pdaSeedsFragment);
|
|
@@ -1226,14 +1288,14 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1226
1288
|
return fragment``;
|
|
1227
1289
|
case "accountBumpValueNode":
|
|
1228
1290
|
return defaultFragment(
|
|
1229
|
-
fragment`${expectProgramDerivedAddress}(accounts.${
|
|
1291
|
+
fragment`${expectProgramDerivedAddress}(accounts.${camelCase(defaultValue.name)}.value)[1]`
|
|
1230
1292
|
);
|
|
1231
1293
|
case "argumentValueNode":
|
|
1232
|
-
return defaultFragment(fragment`${expectSome}(args.${
|
|
1294
|
+
return defaultFragment(fragment`${expectSome}(args.${camelCase(defaultValue.name)})`);
|
|
1233
1295
|
case "resolverValueNode":
|
|
1234
1296
|
const resolverFunction = use(nameApi.resolverFunction(defaultValue.name), getImportFrom(defaultValue));
|
|
1235
1297
|
const resolverAwait = useAsync && asyncResolvers.includes(defaultValue.name) ? "await " : "";
|
|
1236
|
-
return
|
|
1298
|
+
return pipe(
|
|
1237
1299
|
defaultFragment(fragment`${resolverAwait}${resolverFunction}(resolverScope)`),
|
|
1238
1300
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1239
1301
|
);
|
|
@@ -1258,10 +1320,10 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1258
1320
|
}
|
|
1259
1321
|
const negatedCondition = !ifTrueRenderer;
|
|
1260
1322
|
let condition = "true";
|
|
1261
|
-
if (
|
|
1323
|
+
if (isNode(defaultValue.condition, "resolverValueNode")) {
|
|
1262
1324
|
const conditionalResolverFunction = nameApi.resolverFunction(defaultValue.condition.name);
|
|
1263
1325
|
const module = getImportFrom(defaultValue.condition);
|
|
1264
|
-
conditionalFragment =
|
|
1326
|
+
conditionalFragment = pipe(
|
|
1265
1327
|
conditionalFragment,
|
|
1266
1328
|
(f) => addFragmentImports(f, module, [conditionalResolverFunction]),
|
|
1267
1329
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
@@ -1270,9 +1332,9 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1270
1332
|
condition = `${conditionalResolverAwait}${conditionalResolverFunction}(resolverScope)`;
|
|
1271
1333
|
condition = negatedCondition ? `!${condition}` : condition;
|
|
1272
1334
|
} else {
|
|
1273
|
-
const comparedInputName =
|
|
1335
|
+
const comparedInputName = isNode(defaultValue.condition, "accountValueNode") ? `accounts.${camelCase(defaultValue.condition.name)}.value` : `args.${camelCase(defaultValue.condition.name)}`;
|
|
1274
1336
|
if (defaultValue.value) {
|
|
1275
|
-
const comparedValue =
|
|
1337
|
+
const comparedValue = visit(defaultValue.value, typeManifestVisitor).value;
|
|
1276
1338
|
conditionalFragment = mergeFragments([conditionalFragment, comparedValue], (c) => c[0]);
|
|
1277
1339
|
const operator = negatedCondition ? "!==" : "===";
|
|
1278
1340
|
condition = `${comparedInputName} ${operator} ${comparedValue.content}`;
|
|
@@ -1297,7 +1359,7 @@ ${ifTrueRenderer ? ifTrueRenderer.content : ifFalseRenderer?.content}
|
|
|
1297
1359
|
}`
|
|
1298
1360
|
);
|
|
1299
1361
|
default:
|
|
1300
|
-
const valueManifest =
|
|
1362
|
+
const valueManifest = visit(defaultValue, typeManifestVisitor).value;
|
|
1301
1363
|
return defaultFragment(valueManifest);
|
|
1302
1364
|
}
|
|
1303
1365
|
}
|
|
@@ -1312,7 +1374,7 @@ function renderNestedInstructionDefault(scope) {
|
|
|
1312
1374
|
|
|
1313
1375
|
// src/fragments/instructionInputResolved.ts
|
|
1314
1376
|
function getInstructionInputResolvedFragment(scope) {
|
|
1315
|
-
const instructionNode =
|
|
1377
|
+
const instructionNode = getLastNodeFromPath(scope.instructionPath);
|
|
1316
1378
|
const resolvedInputFragments = scope.resolvedInputs.flatMap((input) => {
|
|
1317
1379
|
const inputFragment = getInstructionInputDefaultFragment({
|
|
1318
1380
|
...scope,
|
|
@@ -1320,11 +1382,11 @@ function getInstructionInputResolvedFragment(scope) {
|
|
|
1320
1382
|
optionalAccountStrategy: parseOptionalAccountStrategy(instructionNode.optionalAccountStrategy)
|
|
1321
1383
|
});
|
|
1322
1384
|
if (!inputFragment.content) return [];
|
|
1323
|
-
const camelName =
|
|
1385
|
+
const camelName = camelCase(input.name);
|
|
1324
1386
|
return [
|
|
1325
|
-
|
|
1387
|
+
mapFragmentContent(
|
|
1326
1388
|
inputFragment,
|
|
1327
|
-
(c) =>
|
|
1389
|
+
(c) => isNode(input, "instructionArgumentNode") ? `if (!args.${camelName}) {
|
|
1328
1390
|
${c}
|
|
1329
1391
|
}` : `if (!accounts.${camelName}.value) {
|
|
1330
1392
|
${c}
|
|
@@ -1337,27 +1399,14 @@ ${c}
|
|
|
1337
1399
|
}
|
|
1338
1400
|
return mergeFragments([fragment`// Resolve default values.`, ...resolvedInputFragments], (c) => c.join("\n"));
|
|
1339
1401
|
}
|
|
1340
|
-
|
|
1341
|
-
// src/fragments/instructionInputType.ts
|
|
1342
|
-
import {
|
|
1343
|
-
camelCase as camelCase8,
|
|
1344
|
-
getAllInstructionArguments,
|
|
1345
|
-
isNode as isNode10,
|
|
1346
|
-
pascalCase as pascalCase4
|
|
1347
|
-
} from "@codama/nodes";
|
|
1348
|
-
import { mapFragmentContent as mapFragmentContent6 } from "@codama/renderers-core";
|
|
1349
|
-
import {
|
|
1350
|
-
getLastNodeFromPath as getLastNodeFromPath11,
|
|
1351
|
-
pipe as pipe6
|
|
1352
|
-
} from "@codama/visitors-core";
|
|
1353
1402
|
function getInstructionInputTypeFragment(scope) {
|
|
1354
1403
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
1355
|
-
const instructionNode =
|
|
1404
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1356
1405
|
const instructionInputType = useAsync ? nameApi.instructionAsyncInputType(instructionNode.name) : nameApi.instructionSyncInputType(instructionNode.name);
|
|
1357
1406
|
const [dataArgumentsFragment, customDataArgumentsFragment] = getDataArgumentsFragments(scope);
|
|
1358
1407
|
let accountTypeParams = "";
|
|
1359
1408
|
if (instructionNode.accounts.length > 0) {
|
|
1360
|
-
accountTypeParams = instructionNode.accounts.map((account) => `TAccount${
|
|
1409
|
+
accountTypeParams = instructionNode.accounts.map((account) => `TAccount${pascalCase(account.name)} extends string = string`).join(", ");
|
|
1361
1410
|
accountTypeParams = `<${accountTypeParams}>`;
|
|
1362
1411
|
}
|
|
1363
1412
|
const typeBodyFragment = mergeFragments(
|
|
@@ -1375,20 +1424,20 @@ function getInstructionInputTypeFragment(scope) {
|
|
|
1375
1424
|
}
|
|
1376
1425
|
function getAccountsFragment(scope) {
|
|
1377
1426
|
const { instructionPath, resolvedInputs, useAsync, asyncResolvers } = scope;
|
|
1378
|
-
const instructionNode =
|
|
1427
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1379
1428
|
const fragments = instructionNode.accounts.map((account) => {
|
|
1380
1429
|
const resolvedAccount = resolvedInputs.find(
|
|
1381
1430
|
(input) => input.kind === "instructionAccountNode" && input.name === account.name
|
|
1382
1431
|
);
|
|
1383
|
-
const hasDefaultValue = !!resolvedAccount.defaultValue && !
|
|
1432
|
+
const hasDefaultValue = !!resolvedAccount.defaultValue && !isNode(resolvedAccount.defaultValue, ["identityValueNode", "payerValueNode"]) && (useAsync || !isAsyncDefaultValue(resolvedAccount.defaultValue, asyncResolvers));
|
|
1384
1433
|
const docs = getDocblockFragment(account.docs ?? [], true);
|
|
1385
1434
|
const optionalSign = hasDefaultValue || resolvedAccount.isOptional ? "?" : "";
|
|
1386
|
-
return fragment`${docs}${
|
|
1435
|
+
return fragment`${docs}${camelCase(account.name)}${optionalSign}: ${getAccountTypeFragment2(resolvedAccount)};`;
|
|
1387
1436
|
});
|
|
1388
1437
|
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1389
1438
|
}
|
|
1390
1439
|
function getAccountTypeFragment2(account) {
|
|
1391
|
-
const typeParam = `TAccount${
|
|
1440
|
+
const typeParam = `TAccount${pascalCase(account.name)}`;
|
|
1392
1441
|
const address = use("type Address", "solanaAddresses");
|
|
1393
1442
|
const signer = use("type TransactionSigner", "solanaSigners");
|
|
1394
1443
|
const pda = use("type ProgramDerivedAddress", "solanaAddresses");
|
|
@@ -1400,15 +1449,15 @@ function getAccountTypeFragment2(account) {
|
|
|
1400
1449
|
}
|
|
1401
1450
|
function getDataArgumentsFragments(scope) {
|
|
1402
1451
|
const { instructionPath, nameApi } = scope;
|
|
1403
|
-
const instructionNode =
|
|
1452
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1404
1453
|
const customData = scope.customInstructionData.get(instructionNode.name);
|
|
1405
1454
|
if (customData) {
|
|
1406
1455
|
return [
|
|
1407
1456
|
void 0,
|
|
1408
|
-
|
|
1457
|
+
pipe(
|
|
1409
1458
|
fragment`${nameApi.dataArgsType(customData.importAs)}`,
|
|
1410
1459
|
(f) => mergeFragmentImports(f, [scope.dataArgsManifest.looseType.imports]),
|
|
1411
|
-
(f) =>
|
|
1460
|
+
(f) => mapFragmentContent(f, (c) => `${c} & `)
|
|
1412
1461
|
)
|
|
1413
1462
|
];
|
|
1414
1463
|
}
|
|
@@ -1422,7 +1471,7 @@ function getDataArgumentsFragments(scope) {
|
|
|
1422
1471
|
}
|
|
1423
1472
|
function getExtraArgumentsFragment(scope) {
|
|
1424
1473
|
const { instructionPath, nameApi } = scope;
|
|
1425
|
-
const instructionNode =
|
|
1474
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1426
1475
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
1427
1476
|
const extraArgsType = nameApi.dataArgsType(instructionExtraName);
|
|
1428
1477
|
const fragments = (instructionNode.extraArguments ?? []).flatMap((arg) => {
|
|
@@ -1434,16 +1483,16 @@ function getExtraArgumentsFragment(scope) {
|
|
|
1434
1483
|
}
|
|
1435
1484
|
function getArgumentFragment(arg, argsType, resolvedInputs, renamedArgs) {
|
|
1436
1485
|
const resolvedArg = resolvedInputs.find(
|
|
1437
|
-
(input) =>
|
|
1486
|
+
(input) => isNode(input, "instructionArgumentNode") && input.name === arg.name
|
|
1438
1487
|
);
|
|
1439
1488
|
if (arg.defaultValue && arg.defaultValueStrategy === "omitted") return null;
|
|
1440
1489
|
const renamedName = renamedArgs.get(arg.name) ?? arg.name;
|
|
1441
1490
|
const optionalSign = arg.defaultValue || resolvedArg?.defaultValue ? "?" : "";
|
|
1442
|
-
return fragment`${
|
|
1491
|
+
return fragment`${camelCase(renamedName)}${optionalSign}: ${argsType}["${camelCase(arg.name)}"];`;
|
|
1443
1492
|
}
|
|
1444
1493
|
function getRemainingAccountsFragment(instructionNode) {
|
|
1445
1494
|
const fragments = (instructionNode.remainingAccounts ?? []).flatMap((remainingAccountsNode) => {
|
|
1446
|
-
if (
|
|
1495
|
+
if (isNode(remainingAccountsNode.value, "resolverValueNode")) return [];
|
|
1447
1496
|
const { name } = remainingAccountsNode.value;
|
|
1448
1497
|
const allArguments = getAllInstructionArguments(instructionNode);
|
|
1449
1498
|
const argumentExists = allArguments.some((arg) => arg.name === name);
|
|
@@ -1456,25 +1505,16 @@ function getRemainingAccountsFragment(instructionNode) {
|
|
|
1456
1505
|
if (isSigner === "either") return fragment`${signerFragment} | ${addressFragment}`;
|
|
1457
1506
|
return isSigner ? signerFragment : addressFragment;
|
|
1458
1507
|
})();
|
|
1459
|
-
return fragment`${
|
|
1508
|
+
return fragment`${camelCase(name)}${optionalSign}: Array<${typeFragment}>;`;
|
|
1460
1509
|
});
|
|
1461
1510
|
if (fragments.length === 0) return;
|
|
1462
1511
|
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1463
1512
|
}
|
|
1464
|
-
|
|
1465
|
-
// src/fragments/instructionRemainingAccounts.ts
|
|
1466
|
-
import {
|
|
1467
|
-
assertIsNode as assertIsNode2,
|
|
1468
|
-
camelCase as camelCase9,
|
|
1469
|
-
getAllInstructionArguments as getAllInstructionArguments2,
|
|
1470
|
-
isNode as isNode11
|
|
1471
|
-
} from "@codama/nodes";
|
|
1472
|
-
import { getLastNodeFromPath as getLastNodeFromPath12, pipe as pipe7 } from "@codama/visitors-core";
|
|
1473
1513
|
function getInstructionRemainingAccountsFragment(scope) {
|
|
1474
|
-
const { remainingAccounts } =
|
|
1514
|
+
const { remainingAccounts } = getLastNodeFromPath(scope.instructionPath);
|
|
1475
1515
|
const fragments = (remainingAccounts ?? []).flatMap((a) => getRemainingAccountsFragment2(a, scope));
|
|
1476
1516
|
if (fragments.length === 0) return;
|
|
1477
|
-
return
|
|
1517
|
+
return pipe(
|
|
1478
1518
|
mergeFragments(
|
|
1479
1519
|
fragments,
|
|
1480
1520
|
(c) => `// Remaining accounts.
|
|
@@ -1485,10 +1525,10 @@ const remainingAccounts: AccountMeta[] = ${c.length === 1 ? c[0] : `[...${c.join
|
|
|
1485
1525
|
}
|
|
1486
1526
|
function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
1487
1527
|
const remainingAccountsFragment = (() => {
|
|
1488
|
-
if (
|
|
1528
|
+
if (isNode(remainingAccounts.value, "argumentValueNode")) {
|
|
1489
1529
|
return getArgumentValueNodeFragment2(remainingAccounts, scope);
|
|
1490
1530
|
}
|
|
1491
|
-
if (
|
|
1531
|
+
if (isNode(remainingAccounts.value, "resolverValueNode")) {
|
|
1492
1532
|
return getResolverValueNodeFragment2(remainingAccounts, scope);
|
|
1493
1533
|
}
|
|
1494
1534
|
return null;
|
|
@@ -1497,9 +1537,9 @@ function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
|
1497
1537
|
return [remainingAccountsFragment];
|
|
1498
1538
|
}
|
|
1499
1539
|
function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
1500
|
-
const instructionNode =
|
|
1501
|
-
|
|
1502
|
-
const argumentName =
|
|
1540
|
+
const instructionNode = getLastNodeFromPath(scope.instructionPath);
|
|
1541
|
+
assertIsNode(remainingAccounts.value, "argumentValueNode");
|
|
1542
|
+
const argumentName = camelCase(remainingAccounts.value.name);
|
|
1503
1543
|
const isOptional = remainingAccounts.isOptional ?? false;
|
|
1504
1544
|
const isSigner = remainingAccounts.isSigner ?? false;
|
|
1505
1545
|
const isWritable = remainingAccounts.isWritable ?? false;
|
|
@@ -1508,7 +1548,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1508
1548
|
const signerRole = isWritable ? fragment`${accountRole}.WRITABLE_SIGNER` : fragment`${accountRole}.READONLY_SIGNER`;
|
|
1509
1549
|
const role = isSigner === true ? signerRole : nonSignerRole;
|
|
1510
1550
|
const argumentArray = isOptional ? `(args.${argumentName} ?? [])` : `args.${argumentName}`;
|
|
1511
|
-
const allArguments =
|
|
1551
|
+
const allArguments = getAllInstructionArguments(instructionNode);
|
|
1512
1552
|
const argumentExists = allArguments.some((arg) => arg.name === remainingAccounts.value.name);
|
|
1513
1553
|
if (argumentExists || isSigner === false) {
|
|
1514
1554
|
return fragment`${argumentArray}.map((address) => ({ address, role: ${role} }))`;
|
|
@@ -1519,7 +1559,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1519
1559
|
return fragment`${argumentArray}.map((signer) => ({ address: signer.address, role: ${signerRole}, signer }))`;
|
|
1520
1560
|
}
|
|
1521
1561
|
function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
1522
|
-
|
|
1562
|
+
assertIsNode(remainingAccounts.value, "resolverValueNode");
|
|
1523
1563
|
const isAsync = scope.asyncResolvers.includes(remainingAccounts.value.name);
|
|
1524
1564
|
if (!scope.useAsync && isAsync) return null;
|
|
1525
1565
|
const awaitKeyword = scope.useAsync && isAsync ? "await " : "";
|
|
@@ -1527,7 +1567,7 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1527
1567
|
scope.nameApi.resolverFunction(remainingAccounts.value.name),
|
|
1528
1568
|
scope.getImportFrom(remainingAccounts.value)
|
|
1529
1569
|
);
|
|
1530
|
-
return
|
|
1570
|
+
return pipe(
|
|
1531
1571
|
fragment`${awaitKeyword}${functionName}(resolverScope)`,
|
|
1532
1572
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1533
1573
|
);
|
|
@@ -1536,13 +1576,13 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1536
1576
|
// src/fragments/instructionFunction.ts
|
|
1537
1577
|
function getInstructionFunctionFragment(scope) {
|
|
1538
1578
|
const { useAsync, instructionPath, resolvedInputs, renamedArgs, asyncResolvers, nameApi, customInstructionData } = scope;
|
|
1539
|
-
const instructionNode =
|
|
1540
|
-
const programNode =
|
|
1579
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1580
|
+
const programNode = findProgramNodeFromPath(instructionPath);
|
|
1541
1581
|
if (useAsync && !hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers)) return;
|
|
1542
1582
|
const customData = customInstructionData.get(instructionNode.name);
|
|
1543
1583
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
1544
1584
|
const instructionDependencies = getInstructionDependencies(instructionNode, asyncResolvers, useAsync);
|
|
1545
|
-
const argDependencies = instructionDependencies.filter(
|
|
1585
|
+
const argDependencies = instructionDependencies.filter(isNodeFilter("argumentValueNode")).map((node) => node.name);
|
|
1546
1586
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
1547
1587
|
const argIsNotOmitted = (arg) => !(arg.defaultValue && arg.defaultValueStrategy === "omitted");
|
|
1548
1588
|
const argIsDependent = (arg) => argDependencies.includes(arg.name);
|
|
@@ -1555,7 +1595,7 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1555
1595
|
const hasExtraArgs = (instructionNode.extraArguments ?? []).filter(
|
|
1556
1596
|
(field) => argIsNotOmitted(field) && (argIsDependent(field) || argHasDefaultValue(field))
|
|
1557
1597
|
).length > 0;
|
|
1558
|
-
const hasRemainingAccountArgs = (instructionNode.remainingAccounts ?? []).filter(({ value }) =>
|
|
1598
|
+
const hasRemainingAccountArgs = (instructionNode.remainingAccounts ?? []).filter(({ value }) => isNode(value, "argumentValueNode")).length > 0;
|
|
1559
1599
|
const hasAnyArgs = hasDataArgs || hasExtraArgs || hasRemainingAccountArgs;
|
|
1560
1600
|
const hasInput = hasAccounts || hasAnyArgs;
|
|
1561
1601
|
const programAddressConstant = use(nameApi.programAddressConstant(programNode.name), "generatedPrograms");
|
|
@@ -1574,7 +1614,7 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1574
1614
|
const typeParams = getTypeParamsFragment(instructionNode, programAddressConstant);
|
|
1575
1615
|
const returnType = getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync);
|
|
1576
1616
|
const inputType = getInstructionInputTypeFragment(scope);
|
|
1577
|
-
const inputArg =
|
|
1617
|
+
const inputArg = mapFragmentContent(getInputTypeCallFragment(scope), (c) => hasInput ? `input: ${c}, ` : "");
|
|
1578
1618
|
const functionBody = mergeFragments(
|
|
1579
1619
|
[
|
|
1580
1620
|
getProgramAddressInitializationFragment(programAddressConstant),
|
|
@@ -1606,7 +1646,7 @@ function getAccountsInitializationFragment(instructionNode) {
|
|
|
1606
1646
|
if (instructionNode.accounts.length === 0) return;
|
|
1607
1647
|
const accounts = mergeFragments(
|
|
1608
1648
|
instructionNode.accounts.map((account) => {
|
|
1609
|
-
const name =
|
|
1649
|
+
const name = camelCase(account.name);
|
|
1610
1650
|
const isWritable = account.isWritable ? "true" : "false";
|
|
1611
1651
|
return fragment`${name}: { value: input.${name} ?? null, isWritable: ${isWritable} }`;
|
|
1612
1652
|
}),
|
|
@@ -1641,7 +1681,7 @@ function getReturnStatementFragment(scope) {
|
|
|
1641
1681
|
const hasLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted" && instructionNode.accounts.some((account) => account.isOptional);
|
|
1642
1682
|
const getAccountMeta = hasAccounts ? fragment`const getAccountMeta = ${use("getAccountMetaFactory", "shared")}(programAddress, '${optionalAccountStrategy}');` : "";
|
|
1643
1683
|
const accountItems = [
|
|
1644
|
-
...instructionNode.accounts.map((account) => `getAccountMeta(accounts.${
|
|
1684
|
+
...instructionNode.accounts.map((account) => `getAccountMeta(accounts.${camelCase(account.name)})`),
|
|
1645
1685
|
...hasRemainingAccounts ? ["...remainingAccounts"] : []
|
|
1646
1686
|
].join(", ");
|
|
1647
1687
|
let accounts;
|
|
@@ -1662,14 +1702,14 @@ function getReturnStatementFragment(scope) {
|
|
|
1662
1702
|
} else if (hasData) {
|
|
1663
1703
|
data = fragment`data: ${encoderFunctionFragment}.encode({})`;
|
|
1664
1704
|
}
|
|
1665
|
-
const instructionAttributes =
|
|
1705
|
+
const instructionAttributes = pipe(
|
|
1666
1706
|
[accounts, hasByteDeltas ? fragment`byteDelta` : void 0, data, fragment`programAddress`],
|
|
1667
1707
|
(fs) => mergeFragments(fs, (cs) => cs.join(", "))
|
|
1668
1708
|
);
|
|
1669
1709
|
return fragment`${getAccountMeta}\nreturn Object.freeze({ ${instructionAttributes} } as ${scope.syncReturnTypeFragment});`;
|
|
1670
1710
|
}
|
|
1671
1711
|
function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync) {
|
|
1672
|
-
return
|
|
1712
|
+
return pipe(
|
|
1673
1713
|
instructionTypeFragment,
|
|
1674
1714
|
(f) => hasByteDeltas ? fragment`${f} & ${use("type InstructionWithByteDelta", "shared")}` : f,
|
|
1675
1715
|
(f) => useAsync ? fragment`Promise<${f}>` : f
|
|
@@ -1678,7 +1718,7 @@ function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync)
|
|
|
1678
1718
|
function getTypeParamsFragment(instructionNode, programAddressConstant) {
|
|
1679
1719
|
return mergeFragments(
|
|
1680
1720
|
[
|
|
1681
|
-
...instructionNode.accounts.map((account) => fragment`TAccount${
|
|
1721
|
+
...instructionNode.accounts.map((account) => fragment`TAccount${pascalCase(account.name)} extends string`),
|
|
1682
1722
|
fragment`TProgramAddress extends ${use("type Address", "solanaAddresses")} = typeof ${programAddressConstant}`
|
|
1683
1723
|
],
|
|
1684
1724
|
(cs) => `<${cs.join(", ")}>`
|
|
@@ -1686,52 +1726,39 @@ function getTypeParamsFragment(instructionNode, programAddressConstant) {
|
|
|
1686
1726
|
}
|
|
1687
1727
|
function getInstructionTypeFragment(scope) {
|
|
1688
1728
|
const { instructionPath, nameApi } = scope;
|
|
1689
|
-
const instructionNode =
|
|
1729
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1690
1730
|
const instructionTypeName = nameApi.instructionType(instructionNode.name);
|
|
1691
1731
|
const accountTypeParamsFragments = instructionNode.accounts.map((account) => {
|
|
1692
|
-
const typeParam = fragment`TAccount${
|
|
1693
|
-
const camelName =
|
|
1732
|
+
const typeParam = fragment`TAccount${pascalCase(account.name)}`;
|
|
1733
|
+
const camelName = camelCase(account.name);
|
|
1694
1734
|
if (account.isSigner === "either") {
|
|
1695
1735
|
const signerRole = use(
|
|
1696
1736
|
account.isWritable ? "type WritableSignerAccount" : "type ReadonlySignerAccount",
|
|
1697
1737
|
"solanaInstructions"
|
|
1698
1738
|
);
|
|
1699
|
-
return
|
|
1739
|
+
return pipe(
|
|
1700
1740
|
fragment`typeof input["${camelName}"] extends TransactionSigner<${typeParam}> ? ${signerRole}<${typeParam}> & AccountSignerMeta<${typeParam}> : ${typeParam}`,
|
|
1701
1741
|
(f) => addFragmentImports(f, "solanaSigners", ["type AccountSignerMeta", "type TransactionSigner"])
|
|
1702
1742
|
);
|
|
1703
1743
|
}
|
|
1704
1744
|
return typeParam;
|
|
1705
1745
|
});
|
|
1706
|
-
return
|
|
1746
|
+
return pipe(
|
|
1707
1747
|
mergeFragments([fragment`TProgramAddress`, ...accountTypeParamsFragments], (c) => c.join(", ")),
|
|
1708
|
-
(f) =>
|
|
1748
|
+
(f) => mapFragmentContent(f, (c) => `${instructionTypeName}<${c}>`)
|
|
1709
1749
|
);
|
|
1710
1750
|
}
|
|
1711
1751
|
function getInputTypeCallFragment(scope) {
|
|
1712
1752
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
1713
|
-
const instructionNode =
|
|
1753
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1714
1754
|
const inputTypeName = useAsync ? nameApi.instructionAsyncInputType(instructionNode.name) : nameApi.instructionSyncInputType(instructionNode.name);
|
|
1715
1755
|
if (instructionNode.accounts.length === 0) return fragment`${inputTypeName}`;
|
|
1716
|
-
const accountTypeParams = instructionNode.accounts.map((account) => `TAccount${
|
|
1756
|
+
const accountTypeParams = instructionNode.accounts.map((account) => `TAccount${pascalCase(account.name)}`).join(", ");
|
|
1717
1757
|
return fragment`${inputTypeName}<${accountTypeParams}>`;
|
|
1718
1758
|
}
|
|
1719
|
-
|
|
1720
|
-
// src/fragments/instructionPage.ts
|
|
1721
|
-
import { logWarn } from "@codama/errors";
|
|
1722
|
-
import { camelCase as camelCase12, definedTypeNode as definedTypeNode2, structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes3 } from "@codama/nodes";
|
|
1723
|
-
import {
|
|
1724
|
-
findProgramNodeFromPath as findProgramNodeFromPath6,
|
|
1725
|
-
getLastNodeFromPath as getLastNodeFromPath16,
|
|
1726
|
-
visit as visit5
|
|
1727
|
-
} from "@codama/visitors-core";
|
|
1728
|
-
|
|
1729
|
-
// src/fragments/instructionParseFunction.ts
|
|
1730
|
-
import { camelCase as camelCase11 } from "@codama/nodes";
|
|
1731
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath4, getLastNodeFromPath as getLastNodeFromPath14, pipe as pipe9 } from "@codama/visitors-core";
|
|
1732
1759
|
function getInstructionParseFunctionFragment(scope) {
|
|
1733
|
-
const instructionNode =
|
|
1734
|
-
const programNode =
|
|
1760
|
+
const instructionNode = getLastNodeFromPath(scope.instructionPath);
|
|
1761
|
+
const programNode = findProgramNodeFromPath(scope.instructionPath);
|
|
1735
1762
|
const programAddressConstant = use(scope.nameApi.programAddressConstant(programNode.name), "generatedPrograms");
|
|
1736
1763
|
const childScope = { ...scope, instructionNode, programAddressConstant };
|
|
1737
1764
|
return mergeFragments([getTypeFragment2(childScope), getFunctionFragment(childScope)], (cs) => cs.join("\n\n"));
|
|
@@ -1752,7 +1779,7 @@ function getTypeFragment2(scope) {
|
|
|
1752
1779
|
const accounts = mergeFragments(
|
|
1753
1780
|
scope.instructionNode.accounts.map((account, i) => {
|
|
1754
1781
|
const docs = getDocblockFragment(account.docs ?? [], true);
|
|
1755
|
-
const name =
|
|
1782
|
+
const name = camelCase(account.name);
|
|
1756
1783
|
return fragment`${docs}${name}${account.isOptional ? "?" : ""}: TAccountMetas[${i}]${account.isOptional ? " | undefined" : ""};`;
|
|
1757
1784
|
}),
|
|
1758
1785
|
(cs) => hasAccounts ? `
|
|
@@ -1786,7 +1813,7 @@ function getFunctionFragment(scope) {
|
|
|
1786
1813
|
[
|
|
1787
1814
|
fragment`${use("type Instruction", "solanaInstructions")}<TProgram>`,
|
|
1788
1815
|
hasAccounts ? fragment`${use("type InstructionWithAccounts", "solanaInstructions")}<TAccountMetas>` : void 0,
|
|
1789
|
-
hasData ?
|
|
1816
|
+
hasData ? pipe(
|
|
1790
1817
|
fragment`InstructionWithData<ReadonlyUint8Array>`,
|
|
1791
1818
|
(f) => addFragmentImports(f, "solanaInstructions", ["type InstructionWithData"]),
|
|
1792
1819
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
@@ -1824,7 +1851,7 @@ const getNextOptionalAccount = () => {
|
|
|
1824
1851
|
}
|
|
1825
1852
|
const accounts = mergeFragments(
|
|
1826
1853
|
scope.instructionNode.accounts.map(
|
|
1827
|
-
(account) => account.isOptional ? fragment`${
|
|
1854
|
+
(account) => account.isOptional ? fragment`${camelCase(account.name)}: getNextOptionalAccount()` : fragment`${camelCase(account.name)}: getNextAccount()`
|
|
1828
1855
|
),
|
|
1829
1856
|
(cs) => hasAccounts ? `, accounts: { ${cs.join(", ")} }` : ""
|
|
1830
1857
|
);
|
|
@@ -1834,15 +1861,10 @@ const getNextOptionalAccount = () => {
|
|
|
1834
1861
|
return { programAddress: instruction.programAddress${accounts}${data} };
|
|
1835
1862
|
}`;
|
|
1836
1863
|
}
|
|
1837
|
-
|
|
1838
|
-
// src/fragments/instructionType.ts
|
|
1839
|
-
import { pascalCase as pascalCase6 } from "@codama/nodes";
|
|
1840
|
-
import { mapFragmentContent as mapFragmentContent8 } from "@codama/renderers-core";
|
|
1841
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath5, getLastNodeFromPath as getLastNodeFromPath15 } from "@codama/visitors-core";
|
|
1842
1864
|
function getInstructionTypeFragment2(scope) {
|
|
1843
1865
|
const { instructionPath, nameApi, customInstructionData } = scope;
|
|
1844
|
-
const instructionNode =
|
|
1845
|
-
const programNode =
|
|
1866
|
+
const instructionNode = getLastNodeFromPath(instructionPath);
|
|
1867
|
+
const programNode = findProgramNodeFromPath(instructionPath);
|
|
1846
1868
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
1847
1869
|
const customData = customInstructionData.get(instructionNode.name);
|
|
1848
1870
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
@@ -1852,7 +1874,6 @@ function getInstructionTypeFragment2(scope) {
|
|
|
1852
1874
|
instructionNode.accounts.map(
|
|
1853
1875
|
(account) => getInstructionAccountTypeParamFragment({
|
|
1854
1876
|
...scope,
|
|
1855
|
-
allowAccountMeta: true,
|
|
1856
1877
|
instructionAccountPath: [...instructionPath, account]
|
|
1857
1878
|
})
|
|
1858
1879
|
),
|
|
@@ -1862,8 +1883,8 @@ function getInstructionTypeFragment2(scope) {
|
|
|
1862
1883
|
const usesLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted";
|
|
1863
1884
|
const accountMetasFragment = mergeFragments(
|
|
1864
1885
|
instructionNode.accounts.map(
|
|
1865
|
-
(account) =>
|
|
1866
|
-
const typeParam = `TAccount${
|
|
1886
|
+
(account) => mapFragmentContent(getInstructionAccountMetaFragment(account), (c) => {
|
|
1887
|
+
const typeParam = `TAccount${pascalCase(account.name)}`;
|
|
1867
1888
|
const isLegacyOptional = account.isOptional && usesLegacyOptionalAccounts;
|
|
1868
1889
|
const type = `${typeParam} extends string ? ${c} : ${typeParam}`;
|
|
1869
1890
|
if (!isLegacyOptional) return type;
|
|
@@ -1880,17 +1901,17 @@ ${use("type Instruction", "solanaInstructions")}<TProgram>${data}${accounts};`;
|
|
|
1880
1901
|
|
|
1881
1902
|
// src/fragments/instructionPage.ts
|
|
1882
1903
|
function getInstructionPageFragment(scope) {
|
|
1883
|
-
const node =
|
|
1884
|
-
if (!
|
|
1904
|
+
const node = getLastNodeFromPath(scope.instructionPath);
|
|
1905
|
+
if (!findProgramNodeFromPath(scope.instructionPath)) {
|
|
1885
1906
|
throw new Error("Instruction must be visited inside a program.");
|
|
1886
1907
|
}
|
|
1887
1908
|
const childScope = {
|
|
1888
1909
|
...scope,
|
|
1889
|
-
dataArgsManifest:
|
|
1890
|
-
extraArgsManifest:
|
|
1891
|
-
|
|
1910
|
+
dataArgsManifest: visit(node, scope.typeManifestVisitor),
|
|
1911
|
+
extraArgsManifest: visit(
|
|
1912
|
+
definedTypeNode({
|
|
1892
1913
|
name: scope.nameApi.instructionExtraType(node.name),
|
|
1893
|
-
type:
|
|
1914
|
+
type: structTypeNodeFromInstructionArgumentNodes(node.extraArguments ?? [])
|
|
1894
1915
|
}),
|
|
1895
1916
|
scope.typeManifestVisitor
|
|
1896
1917
|
),
|
|
@@ -1929,14 +1950,10 @@ function getRenamedArgsMap(instruction) {
|
|
|
1929
1950
|
logWarn(
|
|
1930
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.`
|
|
1931
1952
|
);
|
|
1932
|
-
return new Map(duplicates.map((name) => [
|
|
1953
|
+
return new Map(duplicates.map((name) => [camelCase(name), camelCase(`${name}Arg`)]));
|
|
1933
1954
|
}
|
|
1934
|
-
|
|
1935
|
-
// src/fragments/pdaFunction.ts
|
|
1936
|
-
import { camelCase as camelCase13, isNode as isNode13, isNodeFilter as isNodeFilter5 } from "@codama/nodes";
|
|
1937
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath7, getLastNodeFromPath as getLastNodeFromPath17, visit as visit6 } from "@codama/visitors-core";
|
|
1938
1955
|
function getPdaFunctionFragment(scope) {
|
|
1939
|
-
const pdaNode =
|
|
1956
|
+
const pdaNode = getLastNodeFromPath(scope.pdaPath);
|
|
1940
1957
|
const seeds = parsePdaSeedNodes(pdaNode.seeds, scope);
|
|
1941
1958
|
return mergeFragments(
|
|
1942
1959
|
[getSeedInputTypeFragment(seeds, scope), getFunctionFragment2(seeds, scope)],
|
|
@@ -1944,9 +1961,9 @@ function getPdaFunctionFragment(scope) {
|
|
|
1944
1961
|
);
|
|
1945
1962
|
}
|
|
1946
1963
|
function getSeedInputTypeFragment(seeds, scope) {
|
|
1947
|
-
const variableSeeds = seeds.filter(
|
|
1964
|
+
const variableSeeds = seeds.filter(isNodeFilter("variablePdaSeedNode"));
|
|
1948
1965
|
if (variableSeeds.length === 0) return;
|
|
1949
|
-
const pdaNode =
|
|
1966
|
+
const pdaNode = getLastNodeFromPath(scope.pdaPath);
|
|
1950
1967
|
const seedTypeName = scope.nameApi.pdaSeedsType(pdaNode.name);
|
|
1951
1968
|
const seedAttributes = mergeFragments(
|
|
1952
1969
|
variableSeeds.map((seed) => seed.inputAttribute),
|
|
@@ -1955,15 +1972,15 @@ function getSeedInputTypeFragment(seeds, scope) {
|
|
|
1955
1972
|
return fragment`export type ${seedTypeName} = {\n${seedAttributes}\n};`;
|
|
1956
1973
|
}
|
|
1957
1974
|
function getFunctionFragment2(seeds, scope) {
|
|
1958
|
-
const pdaNode =
|
|
1959
|
-
const programNode =
|
|
1975
|
+
const pdaNode = getLastNodeFromPath(scope.pdaPath);
|
|
1976
|
+
const programNode = findProgramNodeFromPath(scope.pdaPath);
|
|
1960
1977
|
const addressType = use("type Address", "solanaAddresses");
|
|
1961
1978
|
const pdaType = use("type ProgramDerivedAddress", "solanaAddresses");
|
|
1962
1979
|
const getPdaFunction = use("getProgramDerivedAddress", "solanaAddresses");
|
|
1963
1980
|
const seedTypeName = scope.nameApi.pdaSeedsType(pdaNode.name);
|
|
1964
1981
|
const findPdaFunction = scope.nameApi.pdaFindFunction(pdaNode.name);
|
|
1965
1982
|
const docs = getDocblockFragment(pdaNode.docs ?? [], true);
|
|
1966
|
-
const hasVariableSeeds = seeds.filter(
|
|
1983
|
+
const hasVariableSeeds = seeds.filter(isNodeFilter("variablePdaSeedNode")).length > 0;
|
|
1967
1984
|
const seedArgument = hasVariableSeeds ? `seeds: ${seedTypeName}, ` : "";
|
|
1968
1985
|
const programAddress = pdaNode.programId ?? programNode.publicKey;
|
|
1969
1986
|
const encodedSeeds = mergeFragments(
|
|
@@ -1977,39 +1994,31 @@ function getFunctionFragment2(seeds, scope) {
|
|
|
1977
1994
|
}
|
|
1978
1995
|
function parsePdaSeedNodes(seeds, scope) {
|
|
1979
1996
|
return seeds.map((seed) => {
|
|
1980
|
-
if (
|
|
1981
|
-
const name =
|
|
1997
|
+
if (isNode(seed, "variablePdaSeedNode")) {
|
|
1998
|
+
const name = camelCase(seed.name);
|
|
1982
1999
|
const docs = getDocblockFragment(seed.docs ?? [], true);
|
|
1983
|
-
const { encoder: encoder2, looseType } =
|
|
2000
|
+
const { encoder: encoder2, looseType } = visit(seed.type, scope.typeManifestVisitor);
|
|
1984
2001
|
return {
|
|
1985
2002
|
...seed,
|
|
1986
2003
|
encodedValue: fragment`${encoder2}.encode(seeds.${name})`,
|
|
1987
2004
|
inputAttribute: fragment`${docs}${name}: ${looseType};`
|
|
1988
2005
|
};
|
|
1989
2006
|
}
|
|
1990
|
-
if (
|
|
2007
|
+
if (isNode(seed.value, "programIdValueNode")) {
|
|
1991
2008
|
const addressEncoder = use("getAddressEncoder", "solanaAddresses");
|
|
1992
2009
|
return { ...seed, encodedValue: fragment`${addressEncoder}().encode(programAddress)` };
|
|
1993
2010
|
}
|
|
1994
|
-
const { encoder } =
|
|
1995
|
-
const { value } =
|
|
2011
|
+
const { encoder } = visit(seed.type, scope.typeManifestVisitor);
|
|
2012
|
+
const { value } = visit(seed.value, scope.typeManifestVisitor);
|
|
1996
2013
|
return { ...seed, encodedValue: fragment`${encoder}.encode(${value})` };
|
|
1997
2014
|
});
|
|
1998
2015
|
}
|
|
1999
|
-
|
|
2000
|
-
// src/fragments/pdaPage.ts
|
|
2001
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath8 } from "@codama/visitors-core";
|
|
2002
2016
|
function getPdaPageFragment(scope) {
|
|
2003
|
-
if (!
|
|
2017
|
+
if (!findProgramNodeFromPath(scope.pdaPath)) {
|
|
2004
2018
|
throw new Error("PDA must be visited inside a program.");
|
|
2005
2019
|
}
|
|
2006
2020
|
return getPdaFunctionFragment(scope);
|
|
2007
2021
|
}
|
|
2008
|
-
|
|
2009
|
-
// src/fragments/programAccounts.ts
|
|
2010
|
-
import { resolveNestedTypeNode as resolveNestedTypeNode3 } from "@codama/nodes";
|
|
2011
|
-
import { mapFragmentContent as mapFragmentContent9 } from "@codama/renderers-core";
|
|
2012
|
-
import { pipe as pipe10 } from "@codama/visitors-core";
|
|
2013
2022
|
function getProgramAccountsFragment(scope) {
|
|
2014
2023
|
if (scope.programNode.accounts.length === 0) return;
|
|
2015
2024
|
return mergeFragments(
|
|
@@ -2034,7 +2043,7 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2034
2043
|
if (!hasAccountDiscriminators) return;
|
|
2035
2044
|
const programAccountsEnum = nameApi.programAccountsEnum(programNode.name);
|
|
2036
2045
|
const programAccountsIdentifierFunction = nameApi.programAccountsIdentifierFunction(programNode.name);
|
|
2037
|
-
return
|
|
2046
|
+
return pipe(
|
|
2038
2047
|
mergeFragments(
|
|
2039
2048
|
accountsWithDiscriminators.map((account) => {
|
|
2040
2049
|
const variant = nameApi.programAccountsEnumVariant(account.name);
|
|
@@ -2043,12 +2052,12 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2043
2052
|
dataName: "data",
|
|
2044
2053
|
discriminators: account.discriminators ?? [],
|
|
2045
2054
|
ifTrue: `return ${programAccountsEnum}.${variant};`,
|
|
2046
|
-
struct:
|
|
2055
|
+
struct: resolveNestedTypeNode(account.data)
|
|
2047
2056
|
});
|
|
2048
2057
|
}),
|
|
2049
2058
|
(c) => c.join("\n")
|
|
2050
2059
|
),
|
|
2051
|
-
(f) =>
|
|
2060
|
+
(f) => mapFragmentContent(
|
|
2052
2061
|
f,
|
|
2053
2062
|
(discriminators) => `export function ${programAccountsIdentifierFunction}(account: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programAccountsEnum} {
|
|
2054
2063
|
const data = 'data' in account ? account.data : account;
|
|
@@ -2059,25 +2068,14 @@ throw new Error("The provided account could not be identified as a ${programNode
|
|
|
2059
2068
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
2060
2069
|
);
|
|
2061
2070
|
}
|
|
2062
|
-
|
|
2063
|
-
// src/fragments/programConstant.ts
|
|
2064
|
-
import { pipe as pipe11 } from "@codama/visitors-core";
|
|
2065
2071
|
function getProgramConstantFragment(scope) {
|
|
2066
2072
|
const { programNode, nameApi } = scope;
|
|
2067
2073
|
const programAddressConstant = nameApi.programAddressConstant(programNode.name);
|
|
2068
|
-
return
|
|
2074
|
+
return pipe(
|
|
2069
2075
|
fragment`export const ${programAddressConstant} = '${programNode.publicKey}' as Address<'${programNode.publicKey}'>;`,
|
|
2070
2076
|
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
2071
2077
|
);
|
|
2072
2078
|
}
|
|
2073
|
-
|
|
2074
|
-
// src/fragments/programInstructions.ts
|
|
2075
|
-
import {
|
|
2076
|
-
getAllInstructionsWithSubs,
|
|
2077
|
-
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes4
|
|
2078
|
-
} from "@codama/nodes";
|
|
2079
|
-
import { mapFragmentContent as mapFragmentContent10 } from "@codama/renderers-core";
|
|
2080
|
-
import { pipe as pipe12 } from "@codama/visitors-core";
|
|
2081
2079
|
function getProgramInstructionsFragment(scope) {
|
|
2082
2080
|
if (scope.programNode.instructions.length === 0) return;
|
|
2083
2081
|
const allInstructions = getAllInstructionsWithSubs(scope.programNode, {
|
|
@@ -2119,14 +2117,14 @@ function getProgramInstructionsIdentifierFunctionFragment(scope) {
|
|
|
2119
2117
|
dataName: "data",
|
|
2120
2118
|
discriminators: instruction.discriminators ?? [],
|
|
2121
2119
|
ifTrue: `return ${programInstructionsEnum}.${variant};`,
|
|
2122
|
-
struct:
|
|
2120
|
+
struct: structTypeNodeFromInstructionArgumentNodes(instruction.arguments)
|
|
2123
2121
|
});
|
|
2124
2122
|
}),
|
|
2125
2123
|
(c) => c.join("\n")
|
|
2126
2124
|
);
|
|
2127
|
-
return
|
|
2125
|
+
return pipe(
|
|
2128
2126
|
discriminatorsFragment,
|
|
2129
|
-
(f) =>
|
|
2127
|
+
(f) => mapFragmentContent(
|
|
2130
2128
|
f,
|
|
2131
2129
|
(discriminators) => `export function ${programInstructionsIdentifierFunction}(instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programInstructionsEnum} {
|
|
2132
2130
|
const data = 'data' in instruction ? instruction.data : instruction;
|
|
@@ -2186,9 +2184,6 @@ function getRootIndexPageFragment(scope) {
|
|
|
2186
2184
|
(cs) => cs.join("\n")
|
|
2187
2185
|
);
|
|
2188
2186
|
}
|
|
2189
|
-
|
|
2190
|
-
// src/fragments/sharedPage.ts
|
|
2191
|
-
import { pipe as pipe13 } from "@codama/visitors-core";
|
|
2192
2187
|
function getSharedPageFragment() {
|
|
2193
2188
|
const sharedPage = fragment`/**
|
|
2194
2189
|
* Asserts that the given value is not null or undefined.
|
|
@@ -2289,7 +2284,7 @@ export function getAccountMetaFactory(
|
|
|
2289
2284
|
export function isTransactionSigner<TAddress extends string = string>(value: Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress>): value is TransactionSigner<TAddress> {
|
|
2290
2285
|
return !!value && typeof value === 'object' && 'address' in value && kitIsTransactionSigner(value);
|
|
2291
2286
|
}`;
|
|
2292
|
-
return
|
|
2287
|
+
return pipe(
|
|
2293
2288
|
sharedPage,
|
|
2294
2289
|
(f) => addFragmentImports(f, "solanaAddresses", [
|
|
2295
2290
|
"type Address",
|
|
@@ -2304,12 +2299,9 @@ export function isTransactionSigner<TAddress extends string = string>(value: Add
|
|
|
2304
2299
|
])
|
|
2305
2300
|
);
|
|
2306
2301
|
}
|
|
2307
|
-
|
|
2308
|
-
// src/fragments/typeDiscriminatedUnionHelpers.ts
|
|
2309
|
-
import { isDataEnum as isDataEnum3, isNode as isNode14 } from "@codama/nodes";
|
|
2310
2302
|
function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
2311
2303
|
const { name, typeNode, nameApi } = scope;
|
|
2312
|
-
const isDiscriminatedUnion =
|
|
2304
|
+
const isDiscriminatedUnion = isNode(typeNode, "enumTypeNode") && isDataEnum(typeNode);
|
|
2313
2305
|
if (!isDiscriminatedUnion) return;
|
|
2314
2306
|
const functionName = nameApi.discriminatedUnionFunction(name);
|
|
2315
2307
|
const isDiscriminatedUnionFunctionName = nameApi.isDiscriminatedUnionFunction(name);
|
|
@@ -2321,10 +2313,10 @@ function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
|
2321
2313
|
const variantSignatures = mergeFragments(
|
|
2322
2314
|
typeNode.variants.map((variant) => {
|
|
2323
2315
|
const variantName = nameApi.discriminatedUnionVariant(variant.name);
|
|
2324
|
-
if (
|
|
2316
|
+
if (isNode(variant, "enumStructVariantTypeNode")) {
|
|
2325
2317
|
return fragment`export function ${functionName}(kind: '${variantName}', data: ${getVariantContentType}<${looseName}, '${discriminatorName}', '${variantName}'>): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
2326
2318
|
}
|
|
2327
|
-
if (
|
|
2319
|
+
if (isNode(variant, "enumTupleVariantTypeNode")) {
|
|
2328
2320
|
return fragment`export function ${functionName}(kind: '${variantName}', data: ${getVariantContentType}<${looseName}, '${discriminatorName}', '${variantName}'>['fields']): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
2329
2321
|
}
|
|
2330
2322
|
return fragment`export function ${functionName}(kind: '${variantName}'): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
@@ -2342,13 +2334,10 @@ export function ${isDiscriminatedUnionFunctionName}<K extends ${strictName}['${d
|
|
|
2342
2334
|
};
|
|
2343
2335
|
`;
|
|
2344
2336
|
}
|
|
2345
|
-
|
|
2346
|
-
// src/fragments/typePage.ts
|
|
2347
|
-
import { pipe as pipe14, visit as visit7 } from "@codama/visitors-core";
|
|
2348
2337
|
function getTypePageFragment(scope) {
|
|
2349
2338
|
const node = scope.node;
|
|
2350
|
-
const manifest =
|
|
2351
|
-
return
|
|
2339
|
+
const manifest = visit(node, scope.typeManifestVisitor);
|
|
2340
|
+
return pipe(
|
|
2352
2341
|
mergeFragments(
|
|
2353
2342
|
[
|
|
2354
2343
|
getTypeWithCodecFragment({ ...scope, manifest, name: node.name, node: node.type, typeDocs: node.docs }),
|
|
@@ -2365,35 +2354,11 @@ function getTypePageFragment(scope) {
|
|
|
2365
2354
|
])
|
|
2366
2355
|
);
|
|
2367
2356
|
}
|
|
2368
|
-
|
|
2369
|
-
// src/visitors/getTypeManifestVisitor.ts
|
|
2370
|
-
import {
|
|
2371
|
-
camelCase as camelCase14,
|
|
2372
|
-
isNode as isNode15,
|
|
2373
|
-
isNodeFilter as isNodeFilter6,
|
|
2374
|
-
isScalarEnum,
|
|
2375
|
-
REGISTERED_TYPE_NODE_KINDS,
|
|
2376
|
-
REGISTERED_VALUE_NODE_KINDS,
|
|
2377
|
-
resolveNestedTypeNode as resolveNestedTypeNode4,
|
|
2378
|
-
structFieldTypeNode,
|
|
2379
|
-
structTypeNode,
|
|
2380
|
-
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes5
|
|
2381
|
-
} from "@codama/nodes";
|
|
2382
|
-
import { mapFragmentContent as mapFragmentContent11, setFragmentContent as setFragmentContent2 } from "@codama/renderers-core";
|
|
2383
|
-
import {
|
|
2384
|
-
extendVisitor,
|
|
2385
|
-
findLastNodeFromPath,
|
|
2386
|
-
NodeStack,
|
|
2387
|
-
pipe as pipe15,
|
|
2388
|
-
recordNodeStackVisitor,
|
|
2389
|
-
staticVisitor,
|
|
2390
|
-
visit as visit8
|
|
2391
|
-
} from "@codama/visitors-core";
|
|
2392
2357
|
function getTypeManifestVisitor(input) {
|
|
2393
2358
|
const { nameApi, linkables, nonScalarEnums, customAccountData, customInstructionData, getImportFrom } = input;
|
|
2394
2359
|
const stack = input.stack ?? new NodeStack();
|
|
2395
2360
|
let parentName = null;
|
|
2396
|
-
return
|
|
2361
|
+
return pipe(
|
|
2397
2362
|
staticVisitor(() => typeManifest(), {
|
|
2398
2363
|
keys: [
|
|
2399
2364
|
...REGISTERED_TYPE_NODE_KINDS,
|
|
@@ -2411,15 +2376,15 @@ function getTypeManifestVisitor(input) {
|
|
|
2411
2376
|
strict: nameApi.dataType(account.name)
|
|
2412
2377
|
};
|
|
2413
2378
|
const link = customAccountData.get(account.name)?.linkNode;
|
|
2414
|
-
const manifest = link ?
|
|
2379
|
+
const manifest = link ? visit(link, self) : visit(account.data, self);
|
|
2415
2380
|
parentName = null;
|
|
2416
2381
|
return manifest;
|
|
2417
2382
|
},
|
|
2418
2383
|
visitAmountType(amountType, { self }) {
|
|
2419
|
-
return
|
|
2384
|
+
return visit(amountType.number, self);
|
|
2420
2385
|
},
|
|
2421
2386
|
visitArrayType(arrayType, { self }) {
|
|
2422
|
-
const childManifest =
|
|
2387
|
+
const childManifest = visit(arrayType.item, self);
|
|
2423
2388
|
const sizeManifest = getArrayLikeSizeOption(arrayType.count, self);
|
|
2424
2389
|
const encoderOptions = sizeManifest.encoder ? fragment`, { ${sizeManifest.encoder} }` : "";
|
|
2425
2390
|
const decoderOptions = sizeManifest.decoder ? fragment`, { ${sizeManifest.decoder} }` : "";
|
|
@@ -2433,16 +2398,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2433
2398
|
},
|
|
2434
2399
|
visitArrayValue(node, { self }) {
|
|
2435
2400
|
return mergeTypeManifests(
|
|
2436
|
-
node.items.map((v) =>
|
|
2401
|
+
node.items.map((v) => visit(v, self)),
|
|
2437
2402
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
2438
2403
|
);
|
|
2439
2404
|
},
|
|
2440
2405
|
visitBooleanType(booleanType, { self }) {
|
|
2441
2406
|
let sizeEncoder = fragment``;
|
|
2442
2407
|
let sizeDecoder = fragment``;
|
|
2443
|
-
const resolvedSize =
|
|
2408
|
+
const resolvedSize = resolveNestedTypeNode(booleanType.size);
|
|
2444
2409
|
if (resolvedSize.format !== "u8" || resolvedSize.endian !== "le") {
|
|
2445
|
-
const size =
|
|
2410
|
+
const size = visit(booleanType.size, self);
|
|
2446
2411
|
sizeEncoder = fragment`{ size: ${size.encoder} }`;
|
|
2447
2412
|
sizeDecoder = fragment`{ size: ${size.decoder} }`;
|
|
2448
2413
|
}
|
|
@@ -2470,22 +2435,22 @@ function getTypeManifestVisitor(input) {
|
|
|
2470
2435
|
return typeManifest({ value: fragment`new Uint8Array([${Array.from(bytes).join(", ")}])` });
|
|
2471
2436
|
},
|
|
2472
2437
|
visitConstantValue(node, { self }) {
|
|
2473
|
-
if (
|
|
2474
|
-
return
|
|
2438
|
+
if (isNode(node.type, "bytesTypeNode") && isNode(node.value, "bytesValueNode")) {
|
|
2439
|
+
return visit(node.value, self);
|
|
2475
2440
|
}
|
|
2476
2441
|
return typeManifest({
|
|
2477
|
-
value: fragment`${
|
|
2442
|
+
value: fragment`${visit(node.type, self).encoder}.encode(${visit(node.value, self).value})`
|
|
2478
2443
|
});
|
|
2479
2444
|
},
|
|
2480
2445
|
visitDateTimeType(dateTimeType, { self }) {
|
|
2481
|
-
return
|
|
2446
|
+
return visit(dateTimeType.number, self);
|
|
2482
2447
|
},
|
|
2483
2448
|
visitDefinedType(definedType, { self }) {
|
|
2484
2449
|
parentName = {
|
|
2485
2450
|
loose: nameApi.dataArgsType(definedType.name),
|
|
2486
2451
|
strict: nameApi.dataType(definedType.name)
|
|
2487
2452
|
};
|
|
2488
|
-
const manifest =
|
|
2453
|
+
const manifest = visit(definedType.type, self);
|
|
2489
2454
|
parentName = null;
|
|
2490
2455
|
return manifest;
|
|
2491
2456
|
},
|
|
@@ -2503,7 +2468,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2503
2468
|
});
|
|
2504
2469
|
},
|
|
2505
2470
|
visitEnumEmptyVariantType(enumEmptyVariantType) {
|
|
2506
|
-
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2471
|
+
const discriminator = nameApi.discriminatedUnionDiscriminator(camelCase(parentName?.strict ?? ""));
|
|
2507
2472
|
const name = nameApi.discriminatedUnionVariant(enumEmptyVariantType.name);
|
|
2508
2473
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2509
2474
|
return typeManifest({
|
|
@@ -2516,31 +2481,31 @@ function getTypeManifestVisitor(input) {
|
|
|
2516
2481
|
visitEnumStructVariantType(enumStructVariantType, { self }) {
|
|
2517
2482
|
const currentParentName = parentName;
|
|
2518
2483
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2519
|
-
|
|
2484
|
+
camelCase(currentParentName?.strict ?? "")
|
|
2520
2485
|
);
|
|
2521
2486
|
const name = nameApi.discriminatedUnionVariant(enumStructVariantType.name);
|
|
2522
2487
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2523
2488
|
parentName = null;
|
|
2524
|
-
const structManifest =
|
|
2489
|
+
const structManifest = visit(enumStructVariantType.struct, self);
|
|
2525
2490
|
parentName = currentParentName;
|
|
2526
2491
|
return typeManifest({
|
|
2527
2492
|
...structManifest,
|
|
2528
2493
|
decoder: fragment`['${name}', ${structManifest.decoder}]`,
|
|
2529
2494
|
encoder: fragment`['${name}', ${structManifest.encoder}]`,
|
|
2530
|
-
looseType:
|
|
2495
|
+
looseType: pipe(
|
|
2531
2496
|
structManifest.looseType,
|
|
2532
|
-
(f) =>
|
|
2497
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2533
2498
|
),
|
|
2534
|
-
strictType:
|
|
2499
|
+
strictType: pipe(
|
|
2535
2500
|
structManifest.strictType,
|
|
2536
|
-
(f) =>
|
|
2501
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2537
2502
|
)
|
|
2538
2503
|
});
|
|
2539
2504
|
},
|
|
2540
2505
|
visitEnumTupleVariantType(enumTupleVariantType, { self }) {
|
|
2541
2506
|
const currentParentName = parentName;
|
|
2542
2507
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2543
|
-
|
|
2508
|
+
camelCase(currentParentName?.strict ?? "")
|
|
2544
2509
|
);
|
|
2545
2510
|
const name = nameApi.discriminatedUnionVariant(enumTupleVariantType.name);
|
|
2546
2511
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
@@ -2551,19 +2516,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2551
2516
|
})
|
|
2552
2517
|
]);
|
|
2553
2518
|
parentName = null;
|
|
2554
|
-
const structManifest =
|
|
2519
|
+
const structManifest = visit(struct, self);
|
|
2555
2520
|
parentName = currentParentName;
|
|
2556
2521
|
return typeManifest({
|
|
2557
2522
|
...structManifest,
|
|
2558
2523
|
decoder: fragment`['${name}', ${structManifest.decoder}]`,
|
|
2559
2524
|
encoder: fragment`['${name}', ${structManifest.encoder}]`,
|
|
2560
|
-
looseType:
|
|
2525
|
+
looseType: pipe(
|
|
2561
2526
|
structManifest.looseType,
|
|
2562
|
-
(f) =>
|
|
2527
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2563
2528
|
),
|
|
2564
|
-
strictType:
|
|
2529
|
+
strictType: pipe(
|
|
2565
2530
|
structManifest.strictType,
|
|
2566
|
-
(f) =>
|
|
2531
|
+
(f) => mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2567
2532
|
)
|
|
2568
2533
|
});
|
|
2569
2534
|
},
|
|
@@ -2571,14 +2536,14 @@ function getTypeManifestVisitor(input) {
|
|
|
2571
2536
|
const currentParentName = parentName;
|
|
2572
2537
|
const encoderOptions = [];
|
|
2573
2538
|
const decoderOptions = [];
|
|
2574
|
-
const enumSize =
|
|
2539
|
+
const enumSize = resolveNestedTypeNode(enumType.size);
|
|
2575
2540
|
if (enumSize.format !== "u8" || enumSize.endian !== "le") {
|
|
2576
|
-
const sizeManifest =
|
|
2541
|
+
const sizeManifest = visit(enumType.size, self);
|
|
2577
2542
|
encoderOptions.push(fragment`size: ${sizeManifest.encoder}`);
|
|
2578
2543
|
decoderOptions.push(fragment`size: ${sizeManifest.decoder}`);
|
|
2579
2544
|
}
|
|
2580
2545
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2581
|
-
|
|
2546
|
+
camelCase(currentParentName?.strict ?? "")
|
|
2582
2547
|
);
|
|
2583
2548
|
if (!isScalarEnum(enumType) && discriminator !== "__kind") {
|
|
2584
2549
|
encoderOptions.push(fragment`discriminator: '${discriminator}'`);
|
|
@@ -2608,7 +2573,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2608
2573
|
});
|
|
2609
2574
|
}
|
|
2610
2575
|
const mergedManifest = mergeTypeManifests(
|
|
2611
|
-
enumType.variants.map((variant) =>
|
|
2576
|
+
enumType.variants.map((variant) => visit(variant, self)),
|
|
2612
2577
|
{
|
|
2613
2578
|
mergeCodecs: (renders) => renders.join(", "),
|
|
2614
2579
|
mergeTypes: (renders) => renders.join(" | ")
|
|
@@ -2626,14 +2591,14 @@ function getTypeManifestVisitor(input) {
|
|
|
2626
2591
|
const enumFunction = nameApi.discriminatedUnionFunction(node.enum.name);
|
|
2627
2592
|
const importFrom = getImportFrom(node.enum);
|
|
2628
2593
|
const enumNode = linkables.get([...stack.getPath(), node.enum])?.type;
|
|
2629
|
-
const isScalar = enumNode &&
|
|
2594
|
+
const isScalar = enumNode && isNode(enumNode, "enumTypeNode") ? isScalarEnum(enumNode) : !nonScalarEnums.includes(node.enum.name);
|
|
2630
2595
|
if (!node.value && isScalar) {
|
|
2631
2596
|
const variantName2 = nameApi.enumVariant(node.variant);
|
|
2632
2597
|
return typeManifest({
|
|
2633
2598
|
...manifest,
|
|
2634
|
-
value:
|
|
2599
|
+
value: pipe(
|
|
2635
2600
|
manifest.value,
|
|
2636
|
-
(f) =>
|
|
2601
|
+
(f) => setFragmentContent(f, `${enumName}.${variantName2}`),
|
|
2637
2602
|
(f) => addFragmentImports(f, importFrom, [enumName])
|
|
2638
2603
|
)
|
|
2639
2604
|
});
|
|
@@ -2642,24 +2607,24 @@ function getTypeManifestVisitor(input) {
|
|
|
2642
2607
|
if (!node.value) {
|
|
2643
2608
|
return typeManifest({
|
|
2644
2609
|
...manifest,
|
|
2645
|
-
value:
|
|
2610
|
+
value: pipe(
|
|
2646
2611
|
manifest.value,
|
|
2647
|
-
(f) =>
|
|
2612
|
+
(f) => setFragmentContent(f, `${enumFunction}('${variantName}')`),
|
|
2648
2613
|
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2649
2614
|
)
|
|
2650
2615
|
});
|
|
2651
2616
|
}
|
|
2652
2617
|
return typeManifest({
|
|
2653
2618
|
...manifest,
|
|
2654
|
-
value:
|
|
2655
|
-
|
|
2656
|
-
(f) =>
|
|
2619
|
+
value: pipe(
|
|
2620
|
+
visit(node.value, self).value,
|
|
2621
|
+
(f) => mapFragmentContent(f, (c) => `${enumFunction}('${variantName}', ${c})`),
|
|
2657
2622
|
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2658
2623
|
)
|
|
2659
2624
|
});
|
|
2660
2625
|
},
|
|
2661
2626
|
visitFixedSizeType(node, { self }) {
|
|
2662
|
-
const manifest =
|
|
2627
|
+
const manifest = visit(node.type, self);
|
|
2663
2628
|
return typeManifest({
|
|
2664
2629
|
...manifest,
|
|
2665
2630
|
decoder: fragment`${use("fixDecoderSize", "solanaCodecsCore")}(${manifest.decoder}, ${node.size})`,
|
|
@@ -2667,13 +2632,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2667
2632
|
});
|
|
2668
2633
|
},
|
|
2669
2634
|
visitHiddenPrefixType(node, { self }) {
|
|
2670
|
-
const manifest =
|
|
2671
|
-
const prefixes = node.prefix.map((c) =>
|
|
2672
|
-
const prefixEncoders =
|
|
2635
|
+
const manifest = visit(node.type, self);
|
|
2636
|
+
const prefixes = node.prefix.map((c) => visit(c, self).value);
|
|
2637
|
+
const prefixEncoders = pipe(
|
|
2673
2638
|
mergeFragments(prefixes, (cs) => cs.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2674
2639
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2675
2640
|
);
|
|
2676
|
-
const prefixDecoders =
|
|
2641
|
+
const prefixDecoders = pipe(
|
|
2677
2642
|
mergeFragments(prefixes, (cs) => cs.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2678
2643
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2679
2644
|
);
|
|
@@ -2684,13 +2649,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2684
2649
|
});
|
|
2685
2650
|
},
|
|
2686
2651
|
visitHiddenSuffixType(node, { self }) {
|
|
2687
|
-
const manifest =
|
|
2688
|
-
const suffixes = node.suffix.map((c) =>
|
|
2689
|
-
const suffixEncoders =
|
|
2652
|
+
const manifest = visit(node.type, self);
|
|
2653
|
+
const suffixes = node.suffix.map((c) => visit(c, self).value);
|
|
2654
|
+
const suffixEncoders = pipe(
|
|
2690
2655
|
mergeFragments(suffixes, (cs) => cs.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2691
2656
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2692
2657
|
);
|
|
2693
|
-
const suffixDecoders =
|
|
2658
|
+
const suffixDecoders = pipe(
|
|
2694
2659
|
mergeFragments(suffixes, (cs) => cs.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2695
2660
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2696
2661
|
);
|
|
@@ -2707,19 +2672,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2707
2672
|
strict: nameApi.dataType(instructionDataName)
|
|
2708
2673
|
};
|
|
2709
2674
|
const link = customInstructionData.get(instruction.name)?.linkNode;
|
|
2710
|
-
const struct =
|
|
2711
|
-
const manifest = link ?
|
|
2675
|
+
const struct = structTypeNodeFromInstructionArgumentNodes(instruction.arguments);
|
|
2676
|
+
const manifest = link ? visit(link, self) : visit(struct, self);
|
|
2712
2677
|
parentName = null;
|
|
2713
2678
|
return manifest;
|
|
2714
2679
|
},
|
|
2715
2680
|
visitMapEntryValue(node, { self }) {
|
|
2716
|
-
return mergeTypeManifests([
|
|
2681
|
+
return mergeTypeManifests([visit(node.key, self), visit(node.value, self)], {
|
|
2717
2682
|
mergeValues: (renders) => `[${renders.join(", ")}]`
|
|
2718
2683
|
});
|
|
2719
2684
|
},
|
|
2720
2685
|
visitMapType(mapType, { self }) {
|
|
2721
|
-
const key =
|
|
2722
|
-
const value =
|
|
2686
|
+
const key = visit(mapType.key, self);
|
|
2687
|
+
const value = visit(mapType.value, self);
|
|
2723
2688
|
const mergedManifest = mergeTypeManifests([key, value], {
|
|
2724
2689
|
mergeCodecs: ([k, v]) => `${k}, ${v}`,
|
|
2725
2690
|
mergeTypes: ([k, v]) => `Map<${k}, ${v}>`
|
|
@@ -2734,7 +2699,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2734
2699
|
});
|
|
2735
2700
|
},
|
|
2736
2701
|
visitMapValue(node, { self }) {
|
|
2737
|
-
const entryFragments = node.entries.map((entry) =>
|
|
2702
|
+
const entryFragments = node.entries.map((entry) => visit(entry, self));
|
|
2738
2703
|
return mergeTypeManifests(entryFragments, {
|
|
2739
2704
|
mergeValues: (renders) => `new Map([${renders.join(", ")}])`
|
|
2740
2705
|
});
|
|
@@ -2760,12 +2725,12 @@ function getTypeManifestVisitor(input) {
|
|
|
2760
2725
|
return typeManifest({ value: fragment`${JSON.stringify(node.number)}` });
|
|
2761
2726
|
},
|
|
2762
2727
|
visitOptionType(optionType, { self }) {
|
|
2763
|
-
const childManifest =
|
|
2728
|
+
const childManifest = visit(optionType.item, self);
|
|
2764
2729
|
const encoderOptions = [];
|
|
2765
2730
|
const decoderOptions = [];
|
|
2766
|
-
const optionPrefix =
|
|
2731
|
+
const optionPrefix = resolveNestedTypeNode(optionType.prefix);
|
|
2767
2732
|
if (optionPrefix.format !== "u8" || optionPrefix.endian !== "le") {
|
|
2768
|
-
const prefixManifest =
|
|
2733
|
+
const prefixManifest = visit(optionType.prefix, self);
|
|
2769
2734
|
encoderOptions.push(fragment`prefix: ${prefixManifest.encoder}`);
|
|
2770
2735
|
decoderOptions.push(fragment`prefix: ${prefixManifest.decoder}`);
|
|
2771
2736
|
}
|
|
@@ -2790,7 +2755,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2790
2755
|
});
|
|
2791
2756
|
},
|
|
2792
2757
|
visitPostOffsetType(node, { self }) {
|
|
2793
|
-
const manifest =
|
|
2758
|
+
const manifest = visit(node.type, self);
|
|
2794
2759
|
if (node.strategy === "padded") {
|
|
2795
2760
|
return typeManifest({
|
|
2796
2761
|
...manifest,
|
|
@@ -2816,7 +2781,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2816
2781
|
});
|
|
2817
2782
|
},
|
|
2818
2783
|
visitPreOffsetType(node, { self }) {
|
|
2819
|
-
const manifest =
|
|
2784
|
+
const manifest = visit(node.type, self);
|
|
2820
2785
|
if (node.strategy === "padded") {
|
|
2821
2786
|
return typeManifest({
|
|
2822
2787
|
...manifest,
|
|
@@ -2853,7 +2818,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2853
2818
|
});
|
|
2854
2819
|
},
|
|
2855
2820
|
visitRemainderOptionType(node, { self }) {
|
|
2856
|
-
const childManifest =
|
|
2821
|
+
const childManifest = visit(node.item, self);
|
|
2857
2822
|
const encoderOptions = ["prefix: null"];
|
|
2858
2823
|
const decoderOptions = ["prefix: null"];
|
|
2859
2824
|
const encoderOptionsAsString = encoderOptions.length > 0 ? `, { ${encoderOptions.join(", ")} }` : "";
|
|
@@ -2867,8 +2832,8 @@ function getTypeManifestVisitor(input) {
|
|
|
2867
2832
|
});
|
|
2868
2833
|
},
|
|
2869
2834
|
visitSentinelType(node, { self }) {
|
|
2870
|
-
const manifest =
|
|
2871
|
-
const sentinel =
|
|
2835
|
+
const manifest = visit(node.type, self);
|
|
2836
|
+
const sentinel = visit(node.sentinel, self).value;
|
|
2872
2837
|
return typeManifest({
|
|
2873
2838
|
...manifest,
|
|
2874
2839
|
decoder: fragment`${use("addDecoderSentinel", "solanaCodecsCore")}(${manifest.decoder}, ${sentinel})`,
|
|
@@ -2876,7 +2841,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2876
2841
|
});
|
|
2877
2842
|
},
|
|
2878
2843
|
visitSetType(setType, { self }) {
|
|
2879
|
-
const childManifest =
|
|
2844
|
+
const childManifest = visit(setType.item, self);
|
|
2880
2845
|
const sizeManifest = getArrayLikeSizeOption(setType.count, self);
|
|
2881
2846
|
const encoderOptions = sizeManifest.encoder ? fragment`, { ${sizeManifest.encoder} }` : "";
|
|
2882
2847
|
const decoderOptions = sizeManifest.decoder ? fragment`, { ${sizeManifest.decoder} }` : "";
|
|
@@ -2890,13 +2855,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2890
2855
|
},
|
|
2891
2856
|
visitSetValue(node, { self }) {
|
|
2892
2857
|
return mergeTypeManifests(
|
|
2893
|
-
node.items.map((v) =>
|
|
2858
|
+
node.items.map((v) => visit(v, self)),
|
|
2894
2859
|
{ mergeValues: (renders) => `new Set([${renders.join(", ")}])` }
|
|
2895
2860
|
);
|
|
2896
2861
|
},
|
|
2897
2862
|
visitSizePrefixType(node, { self }) {
|
|
2898
|
-
const manifest =
|
|
2899
|
-
const prefix =
|
|
2863
|
+
const manifest = visit(node.type, self);
|
|
2864
|
+
const prefix = visit(node.prefix, self);
|
|
2900
2865
|
return typeManifest({
|
|
2901
2866
|
...manifest,
|
|
2902
2867
|
decoder: fragment`${use("addDecoderSizePrefix", "solanaCodecsCore")}(${manifest.decoder}, ${prefix.decoder})`,
|
|
@@ -2904,7 +2869,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2904
2869
|
});
|
|
2905
2870
|
},
|
|
2906
2871
|
visitSolAmountType({ number }, { self }) {
|
|
2907
|
-
const numberManifest =
|
|
2872
|
+
const numberManifest = visit(number, self);
|
|
2908
2873
|
return typeManifest({
|
|
2909
2874
|
...numberManifest,
|
|
2910
2875
|
decoder: fragment`${use("getLamportsDecoder", "solanaRpcTypes")}(${numberManifest.decoder})`,
|
|
@@ -2914,7 +2879,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2914
2879
|
});
|
|
2915
2880
|
},
|
|
2916
2881
|
visitSomeValue(node, { self }) {
|
|
2917
|
-
const innerValue =
|
|
2882
|
+
const innerValue = visit(node.value, self).value;
|
|
2918
2883
|
return typeManifest({
|
|
2919
2884
|
value: fragment`${use("some", "solanaOptions")}(${innerValue})`
|
|
2920
2885
|
});
|
|
@@ -2947,8 +2912,8 @@ function getTypeManifestVisitor(input) {
|
|
|
2947
2912
|
});
|
|
2948
2913
|
},
|
|
2949
2914
|
visitStructFieldType(structFieldType, { self }) {
|
|
2950
|
-
const name =
|
|
2951
|
-
const originalChildManifest =
|
|
2915
|
+
const name = camelCase(structFieldType.name);
|
|
2916
|
+
const originalChildManifest = visit(structFieldType.type, self);
|
|
2952
2917
|
let docs = getDocblockFragment(structFieldType.docs ?? [], true);
|
|
2953
2918
|
docs = docs ? fragment`\n${docs}` : docs;
|
|
2954
2919
|
const childManifest = typeManifest({
|
|
@@ -2970,16 +2935,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2970
2935
|
return typeManifest({ ...childManifest, looseType: fragment`` });
|
|
2971
2936
|
},
|
|
2972
2937
|
visitStructFieldValue(node, { self }) {
|
|
2973
|
-
const innerValue =
|
|
2938
|
+
const innerValue = visit(node.value, self).value;
|
|
2974
2939
|
return typeManifest({
|
|
2975
2940
|
value: fragment`${node.name}: ${innerValue}`
|
|
2976
2941
|
});
|
|
2977
2942
|
},
|
|
2978
2943
|
visitStructType(structType, { self }) {
|
|
2979
2944
|
const optionalFields = structType.fields.filter((f) => !!f.defaultValue);
|
|
2980
|
-
const mergedManifest =
|
|
2945
|
+
const mergedManifest = pipe(
|
|
2981
2946
|
mergeTypeManifests(
|
|
2982
|
-
structType.fields.map((field) =>
|
|
2947
|
+
structType.fields.map((field) => visit(field, self)),
|
|
2983
2948
|
{
|
|
2984
2949
|
mergeCodecs: (renders) => `([${renders.join(", ")}])`,
|
|
2985
2950
|
mergeTypes: (renders) => `{ ${renders.join("")} }`
|
|
@@ -2999,16 +2964,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2999
2964
|
const accountNode = findLastNodeFromPath(parentPath, "accountNode");
|
|
3000
2965
|
const discriminatorPrefix = instructionNode ? instructionNode.name : accountNode?.name;
|
|
3001
2966
|
const discriminators = (instructionNode ? instructionNode.discriminators : accountNode?.discriminators) ?? [];
|
|
3002
|
-
const fieldDiscriminators = discriminators.filter(
|
|
2967
|
+
const fieldDiscriminators = discriminators.filter(isNodeFilter("fieldDiscriminatorNode"));
|
|
3003
2968
|
const defaultValues = mergeFragments(
|
|
3004
2969
|
optionalFields.map((f) => {
|
|
3005
|
-
const key =
|
|
2970
|
+
const key = camelCase(f.name);
|
|
3006
2971
|
if (fieldDiscriminators.some((d) => d.name === f.name)) {
|
|
3007
|
-
const constantName = nameApi.constant(
|
|
2972
|
+
const constantName = nameApi.constant(camelCase(`${discriminatorPrefix}_${f.name}`));
|
|
3008
2973
|
return f.defaultValueStrategy === "omitted" ? fragment`${key}: ${constantName}` : fragment`${key}: value.${key} ?? ${constantName}`;
|
|
3009
2974
|
}
|
|
3010
2975
|
const defaultValue = f.defaultValue;
|
|
3011
|
-
const value =
|
|
2976
|
+
const value = visit(defaultValue, self).value;
|
|
3012
2977
|
return f.defaultValueStrategy === "omitted" ? fragment`${key}: ${value}` : fragment`${key}: value.${key} ?? ${value}`;
|
|
3013
2978
|
}),
|
|
3014
2979
|
(cs) => cs.join(", ")
|
|
@@ -3020,12 +2985,12 @@ function getTypeManifestVisitor(input) {
|
|
|
3020
2985
|
},
|
|
3021
2986
|
visitStructValue(node, { self }) {
|
|
3022
2987
|
return mergeTypeManifests(
|
|
3023
|
-
node.fields.map((field) =>
|
|
2988
|
+
node.fields.map((field) => visit(field, self)),
|
|
3024
2989
|
{ mergeValues: (renders) => `{ ${renders.join(", ")} }` }
|
|
3025
2990
|
);
|
|
3026
2991
|
},
|
|
3027
2992
|
visitTupleType(tupleType, { self }) {
|
|
3028
|
-
const items = tupleType.items.map((item) =>
|
|
2993
|
+
const items = tupleType.items.map((item) => visit(item, self));
|
|
3029
2994
|
const mergedManifest = mergeTypeManifests(items, {
|
|
3030
2995
|
mergeCodecs: (codecs) => `[${codecs.join(", ")}]`,
|
|
3031
2996
|
mergeTypes: (types) => `readonly [${types.join(", ")}]`
|
|
@@ -3038,16 +3003,16 @@ function getTypeManifestVisitor(input) {
|
|
|
3038
3003
|
},
|
|
3039
3004
|
visitTupleValue(node, { self }) {
|
|
3040
3005
|
return mergeTypeManifests(
|
|
3041
|
-
node.items.map((v) =>
|
|
3006
|
+
node.items.map((v) => visit(v, self)),
|
|
3042
3007
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
3043
3008
|
);
|
|
3044
3009
|
},
|
|
3045
3010
|
visitZeroableOptionType(node, { self }) {
|
|
3046
|
-
const childManifest =
|
|
3011
|
+
const childManifest = visit(node.item, self);
|
|
3047
3012
|
const encoderOptions = [fragment`prefix: null`];
|
|
3048
3013
|
const decoderOptions = [fragment`prefix: null`];
|
|
3049
3014
|
if (node.zeroValue) {
|
|
3050
|
-
const zeroValueManifest =
|
|
3015
|
+
const zeroValueManifest = visit(node.zeroValue, self);
|
|
3051
3016
|
encoderOptions.push(fragment`noneValue: ${zeroValueManifest.value}`);
|
|
3052
3017
|
decoderOptions.push(fragment`noneValue: ${zeroValueManifest.value}`);
|
|
3053
3018
|
} else {
|
|
@@ -3075,50 +3040,51 @@ function getTypeManifestVisitor(input) {
|
|
|
3075
3040
|
);
|
|
3076
3041
|
}
|
|
3077
3042
|
function getArrayLikeSizeOption(count, visitor) {
|
|
3078
|
-
if (
|
|
3043
|
+
if (isNode(count, "fixedCountNode")) {
|
|
3079
3044
|
return {
|
|
3080
3045
|
decoder: fragment`size: ${count.value}`,
|
|
3081
3046
|
encoder: fragment`size: ${count.value}`
|
|
3082
3047
|
};
|
|
3083
3048
|
}
|
|
3084
|
-
if (
|
|
3049
|
+
if (isNode(count, "remainderCountNode")) {
|
|
3085
3050
|
return {
|
|
3086
3051
|
decoder: fragment`size: 'remainder'`,
|
|
3087
3052
|
encoder: fragment`size: 'remainder'`
|
|
3088
3053
|
};
|
|
3089
3054
|
}
|
|
3090
|
-
const prefix =
|
|
3055
|
+
const prefix = resolveNestedTypeNode(count.prefix);
|
|
3091
3056
|
if (prefix.format === "u32" && prefix.endian === "le") {
|
|
3092
3057
|
return { decoder: void 0, encoder: void 0 };
|
|
3093
3058
|
}
|
|
3094
|
-
const prefixManifest =
|
|
3059
|
+
const prefixManifest = visit(count.prefix, visitor);
|
|
3095
3060
|
return {
|
|
3096
|
-
decoder:
|
|
3097
|
-
encoder:
|
|
3061
|
+
decoder: pipe(prefixManifest.decoder, (f) => mapFragmentContent(f, (c) => `size: ${c}`)),
|
|
3062
|
+
encoder: pipe(prefixManifest.encoder, (f) => mapFragmentContent(f, (c) => `size: ${c}`))
|
|
3098
3063
|
};
|
|
3099
3064
|
}
|
|
3100
3065
|
|
|
3101
3066
|
// src/visitors/getRenderMapVisitor.ts
|
|
3102
3067
|
function getRenderMapVisitor(options = {}) {
|
|
3103
|
-
const linkables = new
|
|
3104
|
-
const stack = new
|
|
3068
|
+
const linkables = new LinkableDictionary();
|
|
3069
|
+
const stack = new NodeStack();
|
|
3105
3070
|
const customAccountData = parseCustomDataOptions(options.customAccountData ?? [], "AccountData");
|
|
3106
3071
|
const customInstructionData = parseCustomDataOptions(options.customInstructionData ?? [], "InstructionData");
|
|
3107
3072
|
const renderScopeWithTypeManifestVisitor = {
|
|
3108
|
-
asyncResolvers: (options.asyncResolvers ?? []).map(
|
|
3073
|
+
asyncResolvers: (options.asyncResolvers ?? []).map(camelCase),
|
|
3109
3074
|
customAccountData,
|
|
3110
3075
|
customInstructionData,
|
|
3111
3076
|
dependencyMap: options.dependencyMap ?? {},
|
|
3077
|
+
dependencyVersions: options.dependencyVersions ?? {},
|
|
3112
3078
|
getImportFrom: getImportFromFactory(options.linkOverrides ?? {}, customAccountData, customInstructionData),
|
|
3113
3079
|
linkables,
|
|
3114
3080
|
nameApi: getNameApi({ ...DEFAULT_NAME_TRANSFORMERS, ...options.nameTransformers }),
|
|
3115
|
-
nonScalarEnums: (options.nonScalarEnums ?? []).map(
|
|
3081
|
+
nonScalarEnums: (options.nonScalarEnums ?? []).map(camelCase),
|
|
3116
3082
|
renderParentInstructions: options.renderParentInstructions ?? false,
|
|
3117
3083
|
useGranularImports: options.useGranularImports ?? false
|
|
3118
3084
|
};
|
|
3119
3085
|
const typeManifestVisitor = getTypeManifestVisitor({ ...renderScopeWithTypeManifestVisitor, stack });
|
|
3120
3086
|
const renderScope = { ...renderScopeWithTypeManifestVisitor, typeManifestVisitor };
|
|
3121
|
-
const internalNodes = (options.internalNodes ?? []).map(
|
|
3087
|
+
const internalNodes = (options.internalNodes ?? []).map(camelCase);
|
|
3122
3088
|
const resolvedInstructionInputVisitor = getResolvedInstructionInputsVisitor();
|
|
3123
3089
|
const byteSizeVisitor = getByteSizeVisitor(linkables, { stack });
|
|
3124
3090
|
const asPage = (fragment2, dependencyMap = {}) => {
|
|
@@ -3128,47 +3094,47 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3128
3094
|
dependencyMap: { ...renderScope.dependencyMap, ...dependencyMap }
|
|
3129
3095
|
});
|
|
3130
3096
|
};
|
|
3131
|
-
return
|
|
3132
|
-
|
|
3097
|
+
return pipe(
|
|
3098
|
+
staticVisitor(() => createRenderMap(), {
|
|
3133
3099
|
keys: ["rootNode", "programNode", "pdaNode", "accountNode", "definedTypeNode", "instructionNode"]
|
|
3134
3100
|
}),
|
|
3135
|
-
(v) =>
|
|
3101
|
+
(v) => extendVisitor(v, {
|
|
3136
3102
|
visitAccount(node) {
|
|
3137
3103
|
return createRenderMap(
|
|
3138
|
-
`accounts/${
|
|
3104
|
+
`accounts/${camelCase(node.name)}.ts`,
|
|
3139
3105
|
asPage(
|
|
3140
3106
|
getAccountPageFragment({
|
|
3141
3107
|
...renderScope,
|
|
3142
3108
|
accountPath: stack.getPath("accountNode"),
|
|
3143
|
-
size:
|
|
3109
|
+
size: visit(node, byteSizeVisitor)
|
|
3144
3110
|
})
|
|
3145
3111
|
)
|
|
3146
3112
|
);
|
|
3147
3113
|
},
|
|
3148
3114
|
visitDefinedType(node) {
|
|
3149
3115
|
return createRenderMap(
|
|
3150
|
-
`types/${
|
|
3151
|
-
asPage(getTypePageFragment({ ...renderScope, node, size:
|
|
3116
|
+
`types/${camelCase(node.name)}.ts`,
|
|
3117
|
+
asPage(getTypePageFragment({ ...renderScope, node, size: visit(node, byteSizeVisitor) }), {
|
|
3152
3118
|
generatedTypes: "."
|
|
3153
3119
|
})
|
|
3154
3120
|
);
|
|
3155
3121
|
},
|
|
3156
3122
|
visitInstruction(node) {
|
|
3157
3123
|
return createRenderMap(
|
|
3158
|
-
`instructions/${
|
|
3124
|
+
`instructions/${camelCase(node.name)}.ts`,
|
|
3159
3125
|
asPage(
|
|
3160
3126
|
getInstructionPageFragment({
|
|
3161
3127
|
...renderScope,
|
|
3162
3128
|
instructionPath: stack.getPath("instructionNode"),
|
|
3163
|
-
resolvedInputs:
|
|
3164
|
-
size:
|
|
3129
|
+
resolvedInputs: visit(node, resolvedInstructionInputVisitor),
|
|
3130
|
+
size: visit(node, byteSizeVisitor)
|
|
3165
3131
|
})
|
|
3166
3132
|
)
|
|
3167
3133
|
);
|
|
3168
3134
|
},
|
|
3169
3135
|
visitPda(node) {
|
|
3170
3136
|
return createRenderMap(
|
|
3171
|
-
`pdas/${
|
|
3137
|
+
`pdas/${camelCase(node.name)}.ts`,
|
|
3172
3138
|
asPage(getPdaPageFragment({ ...renderScope, pdaPath: stack.getPath("pdaNode") }))
|
|
3173
3139
|
);
|
|
3174
3140
|
},
|
|
@@ -3180,15 +3146,15 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3180
3146
|
const scope = { ...renderScope, programNode: node };
|
|
3181
3147
|
return mergeRenderMaps([
|
|
3182
3148
|
createRenderMap({
|
|
3183
|
-
[`programs/${
|
|
3184
|
-
[`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
|
|
3185
3151
|
}),
|
|
3186
|
-
...node.pdas.map((p) =>
|
|
3187
|
-
...node.accounts.map((a) =>
|
|
3188
|
-
...node.definedTypes.map((t) =>
|
|
3189
|
-
...customDataDefinedType.map((t) =>
|
|
3190
|
-
...
|
|
3191
|
-
(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)
|
|
3192
3158
|
)
|
|
3193
3159
|
]);
|
|
3194
3160
|
},
|
|
@@ -3198,13 +3164,12 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3198
3164
|
const programsWithErrorsToExport = programsToExport.filter((p) => p.errors.length > 0);
|
|
3199
3165
|
const pdasToExport = getAllPdas(node);
|
|
3200
3166
|
const accountsToExport = getAllAccounts(node).filter(isNotInternal);
|
|
3201
|
-
const instructionsToExport =
|
|
3167
|
+
const instructionsToExport = getAllInstructionsWithSubs(node, {
|
|
3202
3168
|
leavesOnly: !renderScope.renderParentInstructions
|
|
3203
3169
|
}).filter(isNotInternal);
|
|
3204
3170
|
const definedTypesToExport = getAllDefinedTypes(node).filter(isNotInternal);
|
|
3205
3171
|
const hasAnythingToExport = programsToExport.length > 0 || accountsToExport.length > 0 || instructionsToExport.length > 0 || definedTypesToExport.length > 0;
|
|
3206
3172
|
const scope = {
|
|
3207
|
-
...renderScope,
|
|
3208
3173
|
accountsToExport,
|
|
3209
3174
|
definedTypesToExport,
|
|
3210
3175
|
instructionsToExport,
|
|
@@ -3222,59 +3187,26 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3222
3187
|
["shared/index.ts"]: hasAnythingToExport ? asPage(getSharedPageFragment()) : void 0,
|
|
3223
3188
|
["types/index.ts"]: asPage(getIndexPageFragment(definedTypesToExport))
|
|
3224
3189
|
}),
|
|
3225
|
-
...getAllPrograms(node).map((p) =>
|
|
3190
|
+
...getAllPrograms(node).map((p) => visit(p, self))
|
|
3226
3191
|
]);
|
|
3227
3192
|
}
|
|
3228
3193
|
}),
|
|
3229
|
-
(v) =>
|
|
3194
|
+
(v) => recordNodeStackVisitor(v, stack),
|
|
3230
3195
|
(v) => recordLinkablesOnFirstVisitVisitor(v, linkables)
|
|
3231
3196
|
);
|
|
3232
3197
|
}
|
|
3233
|
-
|
|
3234
|
-
// src/visitors/renderVisitor.ts
|
|
3235
|
-
import { deleteDirectory, mapRenderMapContentAsync, writeRenderMap } from "@codama/renderers-core";
|
|
3236
|
-
import { rootNodeVisitor, visit as visit10 } from "@codama/visitors-core";
|
|
3237
|
-
import * as estreePlugin from "prettier/plugins/estree";
|
|
3238
|
-
import * as typeScriptPlugin from "prettier/plugins/typescript";
|
|
3239
|
-
import { format } from "prettier/standalone";
|
|
3240
|
-
var DEFAULT_PRETTIER_OPTIONS = {
|
|
3241
|
-
arrowParens: "always",
|
|
3242
|
-
parser: "typescript",
|
|
3243
|
-
plugins: [estreePlugin, typeScriptPlugin],
|
|
3244
|
-
printWidth: 80,
|
|
3245
|
-
semi: true,
|
|
3246
|
-
singleQuote: true,
|
|
3247
|
-
tabWidth: 2,
|
|
3248
|
-
trailingComma: "es5",
|
|
3249
|
-
useTabs: false
|
|
3250
|
-
};
|
|
3251
3198
|
function renderVisitor(path, options = {}) {
|
|
3252
3199
|
return rootNodeVisitor(async (root) => {
|
|
3253
3200
|
if (options.deleteFolderBeforeRendering ?? true) {
|
|
3254
3201
|
deleteDirectory(path);
|
|
3255
3202
|
}
|
|
3256
|
-
let renderMap =
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
renderMap = await mapRenderMapContentAsync(renderMap, (code) => format(code, prettierOptions));
|
|
3260
|
-
}
|
|
3203
|
+
let renderMap = visit(root, getRenderMapVisitor(options));
|
|
3204
|
+
renderMap = await formatCode(renderMap, options);
|
|
3205
|
+
syncPackageJson(renderMap, options);
|
|
3261
3206
|
writeRenderMap(renderMap, path);
|
|
3262
3207
|
});
|
|
3263
3208
|
}
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
createImportMap,
|
|
3268
|
-
renderVisitor as default,
|
|
3269
|
-
getNameApi,
|
|
3270
|
-
getRenderMapVisitor,
|
|
3271
|
-
getTypeManifestVisitor,
|
|
3272
|
-
importMapToString,
|
|
3273
|
-
mergeImportMaps,
|
|
3274
|
-
mergeTypeManifests,
|
|
3275
|
-
parseImportInput,
|
|
3276
|
-
removeFromImportMap,
|
|
3277
|
-
renderVisitor,
|
|
3278
|
-
typeManifest
|
|
3279
|
-
};
|
|
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
|
|
3280
3212
|
//# sourceMappingURL=index.node.mjs.map
|