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