@codama/renderers-js 1.6.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -19
- package/dist/index.browser.cjs +568 -472
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.mjs +568 -472
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.node.cjs +568 -472
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +568 -472
- package/dist/index.node.mjs.map +1 -1
- package/dist/index.react-native.mjs +568 -472
- package/dist/index.react-native.mjs.map +1 -1
- package/dist/types/fragments/index.d.ts +1 -1
- package/dist/types/fragments/index.d.ts.map +1 -1
- package/dist/types/fragments/instructionInputDefault.d.ts.map +1 -1
- package/dist/types/fragments/instructionPage.d.ts +1 -0
- package/dist/types/fragments/instructionPage.d.ts.map +1 -1
- package/dist/types/fragments/programAccounts.d.ts.map +1 -1
- package/dist/types/fragments/programInstructions.d.ts.map +1 -1
- package/dist/types/fragments/programPage.d.ts +1 -1
- package/dist/types/fragments/programPage.d.ts.map +1 -1
- package/dist/types/fragments/programPlugin.d.ts +6 -0
- package/dist/types/fragments/programPlugin.d.ts.map +1 -0
- package/dist/types/utils/formatCode.d.ts +1 -1
- package/dist/types/utils/formatCode.d.ts.map +1 -1
- package/dist/types/utils/fragment.d.ts +1 -1
- package/dist/types/utils/fragment.d.ts.map +1 -1
- package/dist/types/utils/importMap.d.ts +3 -2
- package/dist/types/utils/importMap.d.ts.map +1 -1
- package/dist/types/utils/nameTransformers.d.ts +1 -1
- package/dist/types/utils/nameTransformers.d.ts.map +1 -1
- package/dist/types/utils/options.d.ts +28 -3
- package/dist/types/utils/options.d.ts.map +1 -1
- package/dist/types/utils/packageJson.d.ts +3 -3
- package/dist/types/utils/packageJson.d.ts.map +1 -1
- package/dist/types/visitors/getRenderMapVisitor.d.ts.map +1 -1
- package/dist/types/visitors/renderVisitor.d.ts +1 -1
- package/dist/types/visitors/renderVisitor.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/types/fragments/sharedPage.d.ts +0 -3
- package/dist/types/fragments/sharedPage.d.ts.map +0 -1
package/dist/index.browser.cjs
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var nodes = require('@codama/nodes');
|
|
6
|
-
var renderersCore = require('@codama/renderers-core');
|
|
7
6
|
var visitorsCore = require('@codama/visitors-core');
|
|
8
7
|
var codecsStrings = require('@solana/codecs-strings');
|
|
8
|
+
var renderersCore = require('@codama/renderers-core');
|
|
9
9
|
require('prettier');
|
|
10
10
|
var babelPlugin = require('prettier/plugins/babel');
|
|
11
11
|
var estreePlugin = require('prettier/plugins/estree');
|
|
@@ -36,270 +36,7 @@ var babelPlugin__namespace = /*#__PURE__*/_interopNamespace(babelPlugin);
|
|
|
36
36
|
var estreePlugin__namespace = /*#__PURE__*/_interopNamespace(estreePlugin);
|
|
37
37
|
var typeScriptPlugin__namespace = /*#__PURE__*/_interopNamespace(typeScriptPlugin);
|
|
38
38
|
|
|
39
|
-
// src/utils/
|
|
40
|
-
var DEFAULT_EXTERNAL_MODULE_MAP = {
|
|
41
|
-
solanaAccounts: "@solana/kit",
|
|
42
|
-
solanaAddresses: "@solana/kit",
|
|
43
|
-
solanaCodecsCore: "@solana/kit",
|
|
44
|
-
solanaCodecsDataStructures: "@solana/kit",
|
|
45
|
-
solanaCodecsNumbers: "@solana/kit",
|
|
46
|
-
solanaCodecsStrings: "@solana/kit",
|
|
47
|
-
solanaErrors: "@solana/kit",
|
|
48
|
-
solanaInstructions: "@solana/kit",
|
|
49
|
-
solanaOptions: "@solana/kit",
|
|
50
|
-
solanaPrograms: "@solana/kit",
|
|
51
|
-
solanaRpcTypes: "@solana/kit",
|
|
52
|
-
solanaSigners: "@solana/kit"
|
|
53
|
-
};
|
|
54
|
-
var DEFAULT_GRANULAR_EXTERNAL_MODULE_MAP = {
|
|
55
|
-
solanaAccounts: "@solana/accounts",
|
|
56
|
-
solanaAddresses: "@solana/addresses",
|
|
57
|
-
solanaCodecsCore: "@solana/codecs",
|
|
58
|
-
solanaCodecsDataStructures: "@solana/codecs",
|
|
59
|
-
solanaCodecsNumbers: "@solana/codecs",
|
|
60
|
-
solanaCodecsStrings: "@solana/codecs",
|
|
61
|
-
solanaErrors: "@solana/errors",
|
|
62
|
-
solanaInstructions: "@solana/instructions",
|
|
63
|
-
solanaOptions: "@solana/codecs",
|
|
64
|
-
solanaPrograms: "@solana/programs",
|
|
65
|
-
solanaRpcTypes: "@solana/rpc-types",
|
|
66
|
-
solanaSigners: "@solana/signers"
|
|
67
|
-
};
|
|
68
|
-
var DEFAULT_INTERNAL_MODULE_MAP = {
|
|
69
|
-
errors: "../errors",
|
|
70
|
-
generated: "..",
|
|
71
|
-
generatedAccounts: "../accounts",
|
|
72
|
-
generatedErrors: "../errors",
|
|
73
|
-
generatedInstructions: "../instructions",
|
|
74
|
-
generatedPdas: "../pdas",
|
|
75
|
-
generatedPrograms: "../programs",
|
|
76
|
-
generatedTypes: "../types",
|
|
77
|
-
hooked: "../../hooked",
|
|
78
|
-
shared: "../shared",
|
|
79
|
-
types: "../types"
|
|
80
|
-
};
|
|
81
|
-
function createImportMap() {
|
|
82
|
-
return Object.freeze(/* @__PURE__ */ new Map());
|
|
83
|
-
}
|
|
84
|
-
function parseImportInput(input) {
|
|
85
|
-
const matches = input.match(/^(type )?([^ ]+)(?: as (.+))?$/);
|
|
86
|
-
if (!matches) return Object.freeze({ importedIdentifier: input, isType: false, usedIdentifier: input });
|
|
87
|
-
const [_, isType, name, alias] = matches;
|
|
88
|
-
return Object.freeze({
|
|
89
|
-
importedIdentifier: name,
|
|
90
|
-
isType: !!isType,
|
|
91
|
-
usedIdentifier: alias ?? name
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
function addToImportMap(importMap, module, imports) {
|
|
95
|
-
const parsedImports = imports.map(parseImportInput).map((i) => [i.usedIdentifier, i]);
|
|
96
|
-
return mergeImportMaps([importMap, /* @__PURE__ */ new Map([[module, new Map(parsedImports)]])]);
|
|
97
|
-
}
|
|
98
|
-
function removeFromImportMap(importMap, module, usedIdentifiers) {
|
|
99
|
-
const newMap = new Map(importMap);
|
|
100
|
-
const newModuleMap = new Map(newMap.get(module));
|
|
101
|
-
usedIdentifiers.forEach((usedIdentifier) => {
|
|
102
|
-
newModuleMap.delete(usedIdentifier);
|
|
103
|
-
});
|
|
104
|
-
if (newModuleMap.size === 0) {
|
|
105
|
-
newMap.delete(module);
|
|
106
|
-
} else {
|
|
107
|
-
newMap.set(module, newModuleMap);
|
|
108
|
-
}
|
|
109
|
-
return Object.freeze(newMap);
|
|
110
|
-
}
|
|
111
|
-
function mergeImportMaps(importMaps) {
|
|
112
|
-
if (importMaps.length === 0) return createImportMap();
|
|
113
|
-
if (importMaps.length === 1) return importMaps[0];
|
|
114
|
-
const mergedMap = new Map(importMaps[0]);
|
|
115
|
-
for (const map of importMaps.slice(1)) {
|
|
116
|
-
for (const [module, imports] of map) {
|
|
117
|
-
const mergedModuleMap = mergedMap.get(module) ?? /* @__PURE__ */ new Map();
|
|
118
|
-
for (const [usedIdentifier, importInfo] of imports) {
|
|
119
|
-
const existingImportInfo = mergedModuleMap.get(usedIdentifier);
|
|
120
|
-
const shouldOverwriteTypeOnly = existingImportInfo && existingImportInfo.importedIdentifier === importInfo.importedIdentifier && existingImportInfo.isType && !importInfo.isType;
|
|
121
|
-
if (!existingImportInfo || shouldOverwriteTypeOnly) {
|
|
122
|
-
mergedModuleMap.set(usedIdentifier, importInfo);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
mergedMap.set(module, mergedModuleMap);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return Object.freeze(mergedMap);
|
|
129
|
-
}
|
|
130
|
-
function importMapToString(importMap, dependencyMap = {}, useGranularImports = false) {
|
|
131
|
-
const resolvedMap = resolveImportMapModules(importMap, dependencyMap, useGranularImports);
|
|
132
|
-
return [...resolvedMap.entries()].sort(([a], [b]) => {
|
|
133
|
-
const relative = Number(a.startsWith(".")) - Number(b.startsWith("."));
|
|
134
|
-
if (relative !== 0) return relative;
|
|
135
|
-
return a.localeCompare(b);
|
|
136
|
-
}).map(([module, imports]) => {
|
|
137
|
-
const innerImports = [...imports.values()].map(importInfoToString).sort((a, b) => a.localeCompare(b)).join(", ");
|
|
138
|
-
return `import { ${innerImports} } from '${module}';`;
|
|
139
|
-
}).join("\n");
|
|
140
|
-
}
|
|
141
|
-
function getExternalDependencies(importMap, dependencyMap, useGranularImports) {
|
|
142
|
-
const resolvedImports = resolveImportMapModules(importMap, dependencyMap, useGranularImports);
|
|
143
|
-
return new Set([...resolvedImports.keys()].filter((module) => !module.startsWith(".")));
|
|
144
|
-
}
|
|
145
|
-
function resolveImportMapModules(importMap, dependencyMap, useGranularImports) {
|
|
146
|
-
const dependencyMapWithDefaults = {
|
|
147
|
-
...useGranularImports ? DEFAULT_GRANULAR_EXTERNAL_MODULE_MAP : DEFAULT_EXTERNAL_MODULE_MAP,
|
|
148
|
-
...DEFAULT_INTERNAL_MODULE_MAP,
|
|
149
|
-
...dependencyMap
|
|
150
|
-
};
|
|
151
|
-
return mergeImportMaps(
|
|
152
|
-
[...importMap.entries()].map(([module, imports]) => {
|
|
153
|
-
const resolvedModule = dependencyMapWithDefaults[module] ?? module;
|
|
154
|
-
return /* @__PURE__ */ new Map([[resolvedModule, imports]]);
|
|
155
|
-
})
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
function importInfoToString({ importedIdentifier, isType, usedIdentifier }) {
|
|
159
|
-
const alias = importedIdentifier !== usedIdentifier ? ` as ${usedIdentifier}` : "";
|
|
160
|
-
return `${isType ? "type " : ""}${importedIdentifier}${alias}`;
|
|
161
|
-
}
|
|
162
|
-
function getNameApi(transformers) {
|
|
163
|
-
const helpers = {
|
|
164
|
-
camelCase: nodes.camelCase,
|
|
165
|
-
capitalize: nodes.capitalize,
|
|
166
|
-
kebabCase: nodes.kebabCase,
|
|
167
|
-
pascalCase: nodes.pascalCase,
|
|
168
|
-
snakeCase: nodes.snakeCase,
|
|
169
|
-
titleCase: nodes.titleCase
|
|
170
|
-
};
|
|
171
|
-
return Object.fromEntries(
|
|
172
|
-
Object.entries(transformers).map(([key, transformer]) => [key, (name) => transformer(name, helpers)])
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
var DEFAULT_NAME_TRANSFORMERS = {
|
|
176
|
-
accountDecodeFunction: (name) => `decode${nodes.pascalCase(name)}`,
|
|
177
|
-
accountFetchAllFunction: (name) => `fetchAll${nodes.pascalCase(name)}`,
|
|
178
|
-
accountFetchAllMaybeFunction: (name) => `fetchAllMaybe${nodes.pascalCase(name)}`,
|
|
179
|
-
accountFetchFromSeedsFunction: (name) => `fetch${nodes.pascalCase(name)}FromSeeds`,
|
|
180
|
-
accountFetchFunction: (name) => `fetch${nodes.pascalCase(name)}`,
|
|
181
|
-
accountFetchMaybeFromSeedsFunction: (name) => `fetchMaybe${nodes.pascalCase(name)}FromSeeds`,
|
|
182
|
-
accountFetchMaybeFunction: (name) => `fetchMaybe${nodes.pascalCase(name)}`,
|
|
183
|
-
accountGetSizeFunction: (name) => `get${nodes.pascalCase(name)}Size`,
|
|
184
|
-
codecFunction: (name) => `get${nodes.pascalCase(name)}Codec`,
|
|
185
|
-
constant: (name) => nodes.snakeCase(name).toUpperCase(),
|
|
186
|
-
constantFunction: (name) => `get${nodes.pascalCase(name)}Bytes`,
|
|
187
|
-
dataArgsType: (name) => `${nodes.pascalCase(name)}Args`,
|
|
188
|
-
dataType: (name) => `${nodes.pascalCase(name)}`,
|
|
189
|
-
decoderFunction: (name) => `get${nodes.pascalCase(name)}Decoder`,
|
|
190
|
-
discriminatedUnionDiscriminator: () => "__kind",
|
|
191
|
-
discriminatedUnionFunction: (name) => `${nodes.camelCase(name)}`,
|
|
192
|
-
discriminatedUnionVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
193
|
-
encoderFunction: (name) => `get${nodes.pascalCase(name)}Encoder`,
|
|
194
|
-
enumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
195
|
-
instructionAsyncFunction: (name) => `get${nodes.pascalCase(name)}InstructionAsync`,
|
|
196
|
-
instructionAsyncInputType: (name) => `${nodes.pascalCase(name)}AsyncInput`,
|
|
197
|
-
instructionDataType: (name) => `${nodes.pascalCase(name)}InstructionData`,
|
|
198
|
-
instructionExtraType: (name) => `${nodes.pascalCase(name)}InstructionExtra`,
|
|
199
|
-
instructionParseFunction: (name) => `parse${nodes.pascalCase(name)}Instruction`,
|
|
200
|
-
instructionParsedType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
|
|
201
|
-
instructionSyncFunction: (name) => `get${nodes.pascalCase(name)}Instruction`,
|
|
202
|
-
instructionSyncInputType: (name) => `${nodes.pascalCase(name)}Input`,
|
|
203
|
-
instructionType: (name) => `${nodes.pascalCase(name)}Instruction`,
|
|
204
|
-
isDiscriminatedUnionFunction: (name) => `is${nodes.pascalCase(name)}`,
|
|
205
|
-
pdaFindFunction: (name) => `find${nodes.pascalCase(name)}Pda`,
|
|
206
|
-
pdaSeedsType: (name) => `${nodes.pascalCase(name)}Seeds`,
|
|
207
|
-
programAccountsEnum: (name) => `${nodes.pascalCase(name)}Account`,
|
|
208
|
-
programAccountsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
209
|
-
programAccountsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Account`,
|
|
210
|
-
programAddressConstant: (name) => `${nodes.snakeCase(name).toUpperCase()}_PROGRAM_ADDRESS`,
|
|
211
|
-
programErrorConstant: (name) => nodes.snakeCase(name).toUpperCase(),
|
|
212
|
-
programErrorConstantPrefix: (name) => `${nodes.snakeCase(name).toUpperCase()}_ERROR__`,
|
|
213
|
-
programErrorMessagesMap: (name) => `${nodes.camelCase(name)}ErrorMessages`,
|
|
214
|
-
programErrorUnion: (name) => `${nodes.pascalCase(name)}Error`,
|
|
215
|
-
programGetErrorMessageFunction: (name) => `get${nodes.pascalCase(name)}ErrorMessage`,
|
|
216
|
-
programInstructionsEnum: (name) => `${nodes.pascalCase(name)}Instruction`,
|
|
217
|
-
programInstructionsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
218
|
-
programInstructionsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Instruction`,
|
|
219
|
-
programInstructionsParseFunction: (name) => `parse${nodes.pascalCase(name)}Instruction`,
|
|
220
|
-
programInstructionsParsedUnionType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
|
|
221
|
-
programIsErrorFunction: (name) => `is${nodes.pascalCase(name)}Error`,
|
|
222
|
-
resolverFunction: (name) => `${nodes.camelCase(name)}`
|
|
223
|
-
};
|
|
224
|
-
function createFragment(content) {
|
|
225
|
-
return Object.freeze({ content, features: /* @__PURE__ */ new Set(), imports: createImportMap() });
|
|
226
|
-
}
|
|
227
|
-
function isFragment(value) {
|
|
228
|
-
return typeof value === "object" && value !== null && "content" in value;
|
|
229
|
-
}
|
|
230
|
-
function fragment(template, ...items) {
|
|
231
|
-
return renderersCore.createFragmentTemplate(template, items, isFragment, mergeFragments);
|
|
232
|
-
}
|
|
233
|
-
function mergeFragments(fragments, mergeContent) {
|
|
234
|
-
const filteredFragments = fragments.filter((f) => f !== void 0);
|
|
235
|
-
return Object.freeze({
|
|
236
|
-
content: mergeContent(filteredFragments.map((fragment2) => fragment2.content)),
|
|
237
|
-
features: new Set(filteredFragments.flatMap((f) => [...f.features])),
|
|
238
|
-
imports: mergeImportMaps(filteredFragments.map((f) => f.imports))
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
function use(importInput, module) {
|
|
242
|
-
const importInfo = parseImportInput(importInput);
|
|
243
|
-
return addFragmentImports(createFragment(importInfo.usedIdentifier), module, [importInput]);
|
|
244
|
-
}
|
|
245
|
-
function mergeFragmentImports(fragment2, importMaps) {
|
|
246
|
-
return Object.freeze({ ...fragment2, imports: mergeImportMaps([fragment2.imports, ...importMaps]) });
|
|
247
|
-
}
|
|
248
|
-
function addFragmentImports(fragment2, module, importInputs) {
|
|
249
|
-
return Object.freeze({ ...fragment2, imports: addToImportMap(fragment2.imports, module, importInputs) });
|
|
250
|
-
}
|
|
251
|
-
function removeFragmentImports(fragment2, module, usedIdentifiers) {
|
|
252
|
-
return Object.freeze({ ...fragment2, imports: removeFromImportMap(fragment2.imports, module, usedIdentifiers) });
|
|
253
|
-
}
|
|
254
|
-
function addFragmentFeatures(fragment2, features) {
|
|
255
|
-
return Object.freeze({ ...fragment2, features: /* @__PURE__ */ new Set([...fragment2.features, ...features]) });
|
|
256
|
-
}
|
|
257
|
-
function getExportAllFragment(module) {
|
|
258
|
-
return fragment`export * from '${module}';`;
|
|
259
|
-
}
|
|
260
|
-
function getDocblockFragment(lines, withLineJump = false) {
|
|
261
|
-
const lineJump = withLineJump ? "\n" : "";
|
|
262
|
-
if (lines.length === 0) return;
|
|
263
|
-
if (lines.length === 1) return fragment`/** ${lines[0]} */${lineJump}`;
|
|
264
|
-
const prefixedLines = lines.map((line) => line ? ` * ${line}` : " *");
|
|
265
|
-
return fragment`/**\n${prefixedLines.join("\n")}\n */${lineJump}`;
|
|
266
|
-
}
|
|
267
|
-
function getPageFragment(page, scope) {
|
|
268
|
-
const header = getDocblockFragment([
|
|
269
|
-
"This code was AUTOGENERATED using the Codama library.",
|
|
270
|
-
"Please DO NOT EDIT THIS FILE, instead use visitors",
|
|
271
|
-
"to add features, then rerun Codama to update it.",
|
|
272
|
-
"",
|
|
273
|
-
"@see https://github.com/codama-idl/codama"
|
|
274
|
-
]);
|
|
275
|
-
const imports = page.imports.size === 0 ? void 0 : fragment`${importMapToString(page.imports, scope.dependencyMap, scope.useGranularImports)}`;
|
|
276
|
-
return mergeFragments([header, imports, page], (cs) => cs.join("\n\n"));
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// src/utils/typeManifest.ts
|
|
280
|
-
function typeManifest(input = {}) {
|
|
281
|
-
return Object.freeze({
|
|
282
|
-
decoder: fragment``,
|
|
283
|
-
encoder: fragment``,
|
|
284
|
-
isEnum: false,
|
|
285
|
-
looseType: fragment``,
|
|
286
|
-
strictType: fragment``,
|
|
287
|
-
value: fragment``,
|
|
288
|
-
...input
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
function mergeTypeManifests(manifests, options = {}) {
|
|
292
|
-
const { mergeTypes, mergeCodecs, mergeValues } = options;
|
|
293
|
-
const merge = (fragmentFn, mergeFn) => mergeFn ? mergeFragments(manifests.map(fragmentFn), mergeFn) : fragment``;
|
|
294
|
-
return Object.freeze({
|
|
295
|
-
decoder: merge((m) => m.decoder, mergeCodecs),
|
|
296
|
-
encoder: merge((m) => m.encoder, mergeCodecs),
|
|
297
|
-
isEnum: false,
|
|
298
|
-
looseType: merge((m) => m.looseType, mergeTypes),
|
|
299
|
-
strictType: merge((m) => m.strictType, mergeTypes),
|
|
300
|
-
value: merge((m) => m.value, mergeValues)
|
|
301
|
-
});
|
|
302
|
-
}
|
|
39
|
+
// src/utils/async.ts
|
|
303
40
|
function hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers) {
|
|
304
41
|
const hasByteDeltasAsync = (instructionNode.byteDeltas ?? []).some(
|
|
305
42
|
({ value }) => nodes.isNode(value, "resolverValueNode") && asyncResolvers.includes(value.name)
|
|
@@ -415,12 +152,12 @@ var getDefinedTypeNodesToExtract = (nodes$1, parsedCustomDataOptions) => nodes$1
|
|
|
415
152
|
var DEFAULT_PRETTIER_OPTIONS = {
|
|
416
153
|
plugins: [estreePlugin__namespace, typeScriptPlugin__namespace, babelPlugin__namespace]
|
|
417
154
|
};
|
|
418
|
-
async function getCodeFormatter(options) {
|
|
155
|
+
async function getCodeFormatter(packageFolder, options) {
|
|
419
156
|
const shouldFormatCode = options.formatCode ?? true;
|
|
420
157
|
if (!shouldFormatCode) return (code) => Promise.resolve(code);
|
|
421
158
|
const prettierOptions = {
|
|
422
159
|
...DEFAULT_PRETTIER_OPTIONS,
|
|
423
|
-
...await resolvePrettierOptions(
|
|
160
|
+
...await resolvePrettierOptions(),
|
|
424
161
|
...options.prettierOptions
|
|
425
162
|
};
|
|
426
163
|
return (code, filepath) => standalone.format(code, { ...prettierOptions, filepath });
|
|
@@ -430,6 +167,60 @@ async function resolvePrettierOptions(packageFolder) {
|
|
|
430
167
|
return null;
|
|
431
168
|
}
|
|
432
169
|
}
|
|
170
|
+
function createFragment(content) {
|
|
171
|
+
return Object.freeze({ content, features: /* @__PURE__ */ new Set(), imports: createImportMap() });
|
|
172
|
+
}
|
|
173
|
+
function isFragment(value) {
|
|
174
|
+
return typeof value === "object" && value !== null && "content" in value;
|
|
175
|
+
}
|
|
176
|
+
function fragment(template, ...items) {
|
|
177
|
+
return renderersCore.createFragmentTemplate(template, items, isFragment, mergeFragments);
|
|
178
|
+
}
|
|
179
|
+
function mergeFragments(fragments, mergeContent) {
|
|
180
|
+
const filteredFragments = fragments.filter((f) => f !== void 0);
|
|
181
|
+
return Object.freeze({
|
|
182
|
+
content: mergeContent(filteredFragments.map((fragment2) => fragment2.content)),
|
|
183
|
+
features: new Set(filteredFragments.flatMap((f) => [...f.features])),
|
|
184
|
+
imports: mergeImportMaps(filteredFragments.map((f) => f.imports))
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function use(importInput, module) {
|
|
188
|
+
const importInfo = parseImportInput(importInput);
|
|
189
|
+
return addFragmentImports(createFragment(importInfo.usedIdentifier), module, [importInput]);
|
|
190
|
+
}
|
|
191
|
+
function mergeFragmentImports(fragment2, importMaps) {
|
|
192
|
+
return Object.freeze({ ...fragment2, imports: mergeImportMaps([fragment2.imports, ...importMaps]) });
|
|
193
|
+
}
|
|
194
|
+
function addFragmentImports(fragment2, module, importInputs) {
|
|
195
|
+
return Object.freeze({ ...fragment2, imports: addToImportMap(fragment2.imports, module, importInputs) });
|
|
196
|
+
}
|
|
197
|
+
function removeFragmentImports(fragment2, module, usedIdentifiers) {
|
|
198
|
+
return Object.freeze({ ...fragment2, imports: removeFromImportMap(fragment2.imports, module, usedIdentifiers) });
|
|
199
|
+
}
|
|
200
|
+
function addFragmentFeatures(fragment2, features) {
|
|
201
|
+
return Object.freeze({ ...fragment2, features: /* @__PURE__ */ new Set([...fragment2.features, ...features]) });
|
|
202
|
+
}
|
|
203
|
+
function getExportAllFragment(module) {
|
|
204
|
+
return fragment`export * from '${module}';`;
|
|
205
|
+
}
|
|
206
|
+
function getDocblockFragment(lines, withLineJump = false) {
|
|
207
|
+
const lineJump = withLineJump ? "\n" : "";
|
|
208
|
+
if (lines.length === 0) return;
|
|
209
|
+
if (lines.length === 1) return fragment`/** ${lines[0]} */${lineJump}`;
|
|
210
|
+
const prefixedLines = lines.map((line) => line ? ` * ${line}` : " *");
|
|
211
|
+
return fragment`/**\n${prefixedLines.join("\n")}\n */${lineJump}`;
|
|
212
|
+
}
|
|
213
|
+
function getPageFragment(page, scope) {
|
|
214
|
+
const header = getDocblockFragment([
|
|
215
|
+
"This code was AUTOGENERATED using the Codama library.",
|
|
216
|
+
"Please DO NOT EDIT THIS FILE, instead use visitors",
|
|
217
|
+
"to add features, then rerun Codama to update it.",
|
|
218
|
+
"",
|
|
219
|
+
"@see https://github.com/codama-idl/codama"
|
|
220
|
+
]);
|
|
221
|
+
const imports = page.imports.size === 0 ? void 0 : fragment`${importMapToString(page.imports, scope.dependencyMap, scope.kitImportStrategy)}`;
|
|
222
|
+
return mergeFragments([header, imports, page], (cs) => cs.join("\n\n"));
|
|
223
|
+
}
|
|
433
224
|
function getImportFromFactory(overrides, customAccountData, customInstructionData) {
|
|
434
225
|
const customDataOverrides = Object.fromEntries(
|
|
435
226
|
[...customAccountData.values(), ...customInstructionData.values()].map(({ importFrom, importAs }) => [
|
|
@@ -476,32 +267,98 @@ function getImportFromFactory(overrides, customAccountData, customInstructionDat
|
|
|
476
267
|
}
|
|
477
268
|
};
|
|
478
269
|
}
|
|
270
|
+
function getNameApi(transformers) {
|
|
271
|
+
const helpers = {
|
|
272
|
+
camelCase: nodes.camelCase,
|
|
273
|
+
capitalize: nodes.capitalize,
|
|
274
|
+
kebabCase: nodes.kebabCase,
|
|
275
|
+
pascalCase: nodes.pascalCase,
|
|
276
|
+
snakeCase: nodes.snakeCase,
|
|
277
|
+
titleCase: nodes.titleCase
|
|
278
|
+
};
|
|
279
|
+
return Object.fromEntries(
|
|
280
|
+
Object.entries(transformers).map(([key, transformer]) => [key, (name) => transformer(name, helpers)])
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
var DEFAULT_NAME_TRANSFORMERS = {
|
|
284
|
+
accountDecodeFunction: (name) => `decode${nodes.pascalCase(name)}`,
|
|
285
|
+
accountFetchAllFunction: (name) => `fetchAll${nodes.pascalCase(name)}`,
|
|
286
|
+
accountFetchAllMaybeFunction: (name) => `fetchAllMaybe${nodes.pascalCase(name)}`,
|
|
287
|
+
accountFetchFromSeedsFunction: (name) => `fetch${nodes.pascalCase(name)}FromSeeds`,
|
|
288
|
+
accountFetchFunction: (name) => `fetch${nodes.pascalCase(name)}`,
|
|
289
|
+
accountFetchMaybeFromSeedsFunction: (name) => `fetchMaybe${nodes.pascalCase(name)}FromSeeds`,
|
|
290
|
+
accountFetchMaybeFunction: (name) => `fetchMaybe${nodes.pascalCase(name)}`,
|
|
291
|
+
accountGetSizeFunction: (name) => `get${nodes.pascalCase(name)}Size`,
|
|
292
|
+
codecFunction: (name) => `get${nodes.pascalCase(name)}Codec`,
|
|
293
|
+
constant: (name) => nodes.snakeCase(name).toUpperCase(),
|
|
294
|
+
constantFunction: (name) => `get${nodes.pascalCase(name)}Bytes`,
|
|
295
|
+
dataArgsType: (name) => `${nodes.pascalCase(name)}Args`,
|
|
296
|
+
dataType: (name) => `${nodes.pascalCase(name)}`,
|
|
297
|
+
decoderFunction: (name) => `get${nodes.pascalCase(name)}Decoder`,
|
|
298
|
+
discriminatedUnionDiscriminator: () => "__kind",
|
|
299
|
+
discriminatedUnionFunction: (name) => `${nodes.camelCase(name)}`,
|
|
300
|
+
discriminatedUnionVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
301
|
+
encoderFunction: (name) => `get${nodes.pascalCase(name)}Encoder`,
|
|
302
|
+
enumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
303
|
+
instructionAsyncFunction: (name) => `get${nodes.pascalCase(name)}InstructionAsync`,
|
|
304
|
+
instructionAsyncInputType: (name) => `${nodes.pascalCase(name)}AsyncInput`,
|
|
305
|
+
instructionDataType: (name) => `${nodes.pascalCase(name)}InstructionData`,
|
|
306
|
+
instructionExtraType: (name) => `${nodes.pascalCase(name)}InstructionExtra`,
|
|
307
|
+
instructionParseFunction: (name) => `parse${nodes.pascalCase(name)}Instruction`,
|
|
308
|
+
instructionParsedType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
|
|
309
|
+
instructionSyncFunction: (name) => `get${nodes.pascalCase(name)}Instruction`,
|
|
310
|
+
instructionSyncInputType: (name) => `${nodes.pascalCase(name)}Input`,
|
|
311
|
+
instructionType: (name) => `${nodes.pascalCase(name)}Instruction`,
|
|
312
|
+
isDiscriminatedUnionFunction: (name) => `is${nodes.pascalCase(name)}`,
|
|
313
|
+
pdaFindFunction: (name) => `find${nodes.pascalCase(name)}Pda`,
|
|
314
|
+
pdaSeedsType: (name) => `${nodes.pascalCase(name)}Seeds`,
|
|
315
|
+
programAccountsEnum: (name) => `${nodes.pascalCase(name)}Account`,
|
|
316
|
+
programAccountsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
317
|
+
programAccountsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Account`,
|
|
318
|
+
programAddressConstant: (name) => `${nodes.snakeCase(name).toUpperCase()}_PROGRAM_ADDRESS`,
|
|
319
|
+
programErrorConstant: (name) => nodes.snakeCase(name).toUpperCase(),
|
|
320
|
+
programErrorConstantPrefix: (name) => `${nodes.snakeCase(name).toUpperCase()}_ERROR__`,
|
|
321
|
+
programErrorMessagesMap: (name) => `${nodes.camelCase(name)}ErrorMessages`,
|
|
322
|
+
programErrorUnion: (name) => `${nodes.pascalCase(name)}Error`,
|
|
323
|
+
programGetErrorMessageFunction: (name) => `get${nodes.pascalCase(name)}ErrorMessage`,
|
|
324
|
+
programInstructionsEnum: (name) => `${nodes.pascalCase(name)}Instruction`,
|
|
325
|
+
programInstructionsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
326
|
+
programInstructionsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Instruction`,
|
|
327
|
+
programInstructionsParseFunction: (name) => `parse${nodes.pascalCase(name)}Instruction`,
|
|
328
|
+
programInstructionsParsedUnionType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
|
|
329
|
+
programIsErrorFunction: (name) => `is${nodes.pascalCase(name)}Error`,
|
|
330
|
+
programPluginAccountKey: (name) => `${nodes.camelCase(name)}`,
|
|
331
|
+
programPluginAccountsType: (name) => `${nodes.pascalCase(name)}PluginAccounts`,
|
|
332
|
+
programPluginFunction: (name) => `${nodes.camelCase(name)}Program`,
|
|
333
|
+
programPluginInstructionKey: (name) => `${nodes.camelCase(name)}`,
|
|
334
|
+
programPluginInstructionsType: (name) => `${nodes.pascalCase(name)}PluginInstructions`,
|
|
335
|
+
programPluginKey: (name) => `${nodes.camelCase(name)}`,
|
|
336
|
+
programPluginRequirementsType: (name) => `${nodes.pascalCase(name)}PluginRequirements`,
|
|
337
|
+
programPluginType: (name) => `${nodes.pascalCase(name)}Plugin`,
|
|
338
|
+
resolverFunction: (name) => `${nodes.camelCase(name)}`
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// src/utils/options.ts
|
|
342
|
+
var DEFAULT_KIT_IMPORT_STRATEGY = "preferRoot";
|
|
479
343
|
var DEFAULT_DEPENDENCY_VERSIONS = {
|
|
480
|
-
"@solana/accounts": "^
|
|
481
|
-
"@solana/addresses": "^
|
|
482
|
-
"@solana/codecs": "^
|
|
483
|
-
"@solana/errors": "^
|
|
484
|
-
"@solana/instructions": "^
|
|
485
|
-
"@solana/kit": "^
|
|
486
|
-
"@solana/programs": "^
|
|
487
|
-
"@solana/rpc-types": "^
|
|
488
|
-
"@solana/signers": "^
|
|
344
|
+
"@solana/accounts": "^6.0.0",
|
|
345
|
+
"@solana/addresses": "^6.0.0",
|
|
346
|
+
"@solana/codecs": "^6.0.0",
|
|
347
|
+
"@solana/errors": "^6.0.0",
|
|
348
|
+
"@solana/instructions": "^6.0.0",
|
|
349
|
+
"@solana/kit": "^6.0.0",
|
|
350
|
+
"@solana/programs": "^6.0.0",
|
|
351
|
+
"@solana/rpc-types": "^6.0.0",
|
|
352
|
+
"@solana/signers": "^6.0.0"
|
|
489
353
|
};
|
|
490
|
-
async function syncPackageJson(renderMap, formatCode, options) {
|
|
491
|
-
const shouldSyncPackageJson = options.syncPackageJson ??
|
|
492
|
-
const packageFolder = options.packageFolder;
|
|
493
|
-
if (!packageFolder) {
|
|
494
|
-
if (shouldSyncPackageJson) {
|
|
495
|
-
errors.logWarn("Cannot sync package.json. Please provide the 'packageFolder' option.");
|
|
496
|
-
}
|
|
497
|
-
return;
|
|
498
|
-
}
|
|
354
|
+
async function syncPackageJson(renderMap, formatCode, packageFolder, options) {
|
|
355
|
+
const shouldSyncPackageJson = options.syncPackageJson ?? true;
|
|
499
356
|
const packageJsonPath = renderersCore.joinPath(packageFolder, "package.json");
|
|
500
357
|
const usedDependencies = getUsedDependencyVersions(
|
|
501
358
|
renderMap,
|
|
502
359
|
options.dependencyMap ?? {},
|
|
503
360
|
options.dependencyVersions ?? {},
|
|
504
|
-
options.
|
|
361
|
+
options.kitImportStrategy ?? DEFAULT_KIT_IMPORT_STRATEGY
|
|
505
362
|
);
|
|
506
363
|
if (!shouldSyncPackageJson) {
|
|
507
364
|
if (renderersCore.fileExists(packageJsonPath)) {
|
|
@@ -589,13 +446,13 @@ function checkExistingPackageJson(packageJson, dependencyVersions) {
|
|
|
589
446
|
${missingList}${outdatedList}`
|
|
590
447
|
);
|
|
591
448
|
}
|
|
592
|
-
function getUsedDependencyVersions(renderMap, dependencyMap, dependencyVersions,
|
|
449
|
+
function getUsedDependencyVersions(renderMap, dependencyMap, dependencyVersions, kitImportStrategy) {
|
|
593
450
|
const dependencyVersionsWithDefaults = {
|
|
594
451
|
...DEFAULT_DEPENDENCY_VERSIONS,
|
|
595
452
|
...dependencyVersions
|
|
596
453
|
};
|
|
597
454
|
const fragment2 = mergeFragments([...renderMap.values()], () => "");
|
|
598
|
-
const usedDependencies = getExternalDependencies(fragment2.imports, dependencyMap,
|
|
455
|
+
const usedDependencies = getExternalDependencies(fragment2.imports, dependencyMap, kitImportStrategy);
|
|
599
456
|
const [usedDependencyVersion, missingDependencies] = [...usedDependencies].reduce(
|
|
600
457
|
([acc, missingDependencies2], dependency) => {
|
|
601
458
|
const version = dependencyVersionsWithDefaults[dependency];
|
|
@@ -648,7 +505,173 @@ async function writePackageJson(packageJson, packageJsonPath, formatCode) {
|
|
|
648
505
|
renderersCore.writeFile(packageJsonPath, formattedContent);
|
|
649
506
|
}
|
|
650
507
|
|
|
651
|
-
// src/
|
|
508
|
+
// src/utils/typeManifest.ts
|
|
509
|
+
function typeManifest(input = {}) {
|
|
510
|
+
return Object.freeze({
|
|
511
|
+
decoder: fragment``,
|
|
512
|
+
encoder: fragment``,
|
|
513
|
+
isEnum: false,
|
|
514
|
+
looseType: fragment``,
|
|
515
|
+
strictType: fragment``,
|
|
516
|
+
value: fragment``,
|
|
517
|
+
...input
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
function mergeTypeManifests(manifests, options = {}) {
|
|
521
|
+
const { mergeTypes, mergeCodecs, mergeValues } = options;
|
|
522
|
+
const merge = (fragmentFn, mergeFn) => mergeFn ? mergeFragments(manifests.map(fragmentFn), mergeFn) : fragment``;
|
|
523
|
+
return Object.freeze({
|
|
524
|
+
decoder: merge((m) => m.decoder, mergeCodecs),
|
|
525
|
+
encoder: merge((m) => m.encoder, mergeCodecs),
|
|
526
|
+
isEnum: false,
|
|
527
|
+
looseType: merge((m) => m.looseType, mergeTypes),
|
|
528
|
+
strictType: merge((m) => m.strictType, mergeTypes),
|
|
529
|
+
value: merge((m) => m.value, mergeValues)
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// src/utils/importMap.ts
|
|
534
|
+
var DEFAULT_EXTERNAL_MODULE_MAP = {
|
|
535
|
+
solanaAccounts: "@solana/kit",
|
|
536
|
+
solanaAddresses: "@solana/kit",
|
|
537
|
+
solanaCodecsCore: "@solana/kit",
|
|
538
|
+
solanaCodecsDataStructures: "@solana/kit",
|
|
539
|
+
solanaCodecsNumbers: "@solana/kit",
|
|
540
|
+
solanaCodecsStrings: "@solana/kit",
|
|
541
|
+
solanaErrors: "@solana/kit",
|
|
542
|
+
solanaInstructionPlans: "@solana/kit",
|
|
543
|
+
solanaInstructions: "@solana/kit",
|
|
544
|
+
solanaOptions: "@solana/kit",
|
|
545
|
+
solanaPluginCore: "@solana/kit",
|
|
546
|
+
solanaPluginInterfaces: "@solana/kit",
|
|
547
|
+
solanaProgramClientCore: "@solana/kit/program-client-core",
|
|
548
|
+
solanaPrograms: "@solana/kit",
|
|
549
|
+
solanaRpcApi: "@solana/kit",
|
|
550
|
+
solanaRpcTypes: "@solana/kit",
|
|
551
|
+
solanaSigners: "@solana/kit"
|
|
552
|
+
};
|
|
553
|
+
var DEFAULT_GRANULAR_EXTERNAL_MODULE_MAP = {
|
|
554
|
+
solanaAccounts: "@solana/accounts",
|
|
555
|
+
solanaAddresses: "@solana/addresses",
|
|
556
|
+
solanaCodecsCore: "@solana/codecs",
|
|
557
|
+
solanaCodecsDataStructures: "@solana/codecs",
|
|
558
|
+
solanaCodecsNumbers: "@solana/codecs",
|
|
559
|
+
solanaCodecsStrings: "@solana/codecs",
|
|
560
|
+
solanaErrors: "@solana/errors",
|
|
561
|
+
solanaInstructionPlans: "@solana/instruction-plans",
|
|
562
|
+
solanaInstructions: "@solana/instructions",
|
|
563
|
+
solanaOptions: "@solana/codecs",
|
|
564
|
+
solanaPluginCore: "@solana/plugin-core",
|
|
565
|
+
solanaPluginInterfaces: "@solana/plugin-interfaces",
|
|
566
|
+
solanaProgramClientCore: "@solana/program-client-core",
|
|
567
|
+
solanaPrograms: "@solana/programs",
|
|
568
|
+
solanaRpcApi: "@solana/rpc-api",
|
|
569
|
+
solanaRpcTypes: "@solana/rpc-types",
|
|
570
|
+
solanaSigners: "@solana/signers"
|
|
571
|
+
};
|
|
572
|
+
var DEFAULT_INTERNAL_MODULE_MAP = {
|
|
573
|
+
errors: "../errors",
|
|
574
|
+
generated: "..",
|
|
575
|
+
generatedAccounts: "../accounts",
|
|
576
|
+
generatedErrors: "../errors",
|
|
577
|
+
generatedInstructions: "../instructions",
|
|
578
|
+
generatedPdas: "../pdas",
|
|
579
|
+
generatedPrograms: "../programs",
|
|
580
|
+
generatedTypes: "../types",
|
|
581
|
+
hooked: "../../hooked",
|
|
582
|
+
shared: "../shared",
|
|
583
|
+
types: "../types"
|
|
584
|
+
};
|
|
585
|
+
function createImportMap() {
|
|
586
|
+
return Object.freeze(/* @__PURE__ */ new Map());
|
|
587
|
+
}
|
|
588
|
+
function parseImportInput(input) {
|
|
589
|
+
const matches = input.match(/^(type )?([^ ]+)(?: as (.+))?$/);
|
|
590
|
+
if (!matches) return Object.freeze({ importedIdentifier: input, isType: false, usedIdentifier: input });
|
|
591
|
+
const [_, isType, name, alias] = matches;
|
|
592
|
+
return Object.freeze({
|
|
593
|
+
importedIdentifier: name,
|
|
594
|
+
isType: !!isType,
|
|
595
|
+
usedIdentifier: alias ?? name
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
function addToImportMap(importMap, module, imports) {
|
|
599
|
+
const parsedImports = imports.map(parseImportInput).map((i) => [i.usedIdentifier, i]);
|
|
600
|
+
return mergeImportMaps([importMap, /* @__PURE__ */ new Map([[module, new Map(parsedImports)]])]);
|
|
601
|
+
}
|
|
602
|
+
function removeFromImportMap(importMap, module, usedIdentifiers) {
|
|
603
|
+
const newMap = new Map(importMap);
|
|
604
|
+
const newModuleMap = new Map(newMap.get(module));
|
|
605
|
+
usedIdentifiers.forEach((usedIdentifier) => {
|
|
606
|
+
newModuleMap.delete(usedIdentifier);
|
|
607
|
+
});
|
|
608
|
+
if (newModuleMap.size === 0) {
|
|
609
|
+
newMap.delete(module);
|
|
610
|
+
} else {
|
|
611
|
+
newMap.set(module, newModuleMap);
|
|
612
|
+
}
|
|
613
|
+
return Object.freeze(newMap);
|
|
614
|
+
}
|
|
615
|
+
function mergeImportMaps(importMaps) {
|
|
616
|
+
if (importMaps.length === 0) return createImportMap();
|
|
617
|
+
if (importMaps.length === 1) return importMaps[0];
|
|
618
|
+
const mergedMap = new Map(importMaps[0]);
|
|
619
|
+
for (const map of importMaps.slice(1)) {
|
|
620
|
+
for (const [module, imports] of map) {
|
|
621
|
+
const mergedModuleMap = mergedMap.get(module) ?? /* @__PURE__ */ new Map();
|
|
622
|
+
for (const [usedIdentifier, importInfo] of imports) {
|
|
623
|
+
const existingImportInfo = mergedModuleMap.get(usedIdentifier);
|
|
624
|
+
const shouldOverwriteTypeOnly = existingImportInfo && existingImportInfo.importedIdentifier === importInfo.importedIdentifier && existingImportInfo.isType && !importInfo.isType;
|
|
625
|
+
if (!existingImportInfo || shouldOverwriteTypeOnly) {
|
|
626
|
+
mergedModuleMap.set(usedIdentifier, importInfo);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
mergedMap.set(module, mergedModuleMap);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return Object.freeze(mergedMap);
|
|
633
|
+
}
|
|
634
|
+
function importMapToString(importMap, dependencyMap = {}, kitImportStrategy = DEFAULT_KIT_IMPORT_STRATEGY) {
|
|
635
|
+
const resolvedMap = resolveImportMapModules(importMap, dependencyMap, kitImportStrategy);
|
|
636
|
+
return [...resolvedMap.entries()].sort(([a], [b]) => {
|
|
637
|
+
const relative = Number(a.startsWith(".")) - Number(b.startsWith("."));
|
|
638
|
+
if (relative !== 0) return relative;
|
|
639
|
+
return a.localeCompare(b);
|
|
640
|
+
}).map(([module, imports]) => {
|
|
641
|
+
const innerImports = [...imports.values()].map(importInfoToString).sort((a, b) => a.localeCompare(b)).join(", ");
|
|
642
|
+
return `import { ${innerImports} } from '${module}';`;
|
|
643
|
+
}).join("\n");
|
|
644
|
+
}
|
|
645
|
+
function getExternalDependencies(importMap, dependencyMap, kitImportStrategy) {
|
|
646
|
+
const resolvedImports = resolveImportMapModules(importMap, dependencyMap, kitImportStrategy);
|
|
647
|
+
return new Set(
|
|
648
|
+
[...resolvedImports.keys()].filter((module) => !module.startsWith(".")).map((module) => {
|
|
649
|
+
const subPathExportIndex = module.startsWith("@") ? 2 : 1;
|
|
650
|
+
return module.split("/").slice(0, subPathExportIndex).join("/");
|
|
651
|
+
})
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
function resolveImportMapModules(importMap, dependencyMap, kitImportStrategy) {
|
|
655
|
+
const defaultExternalModuleMap = kitImportStrategy === "granular" ? DEFAULT_GRANULAR_EXTERNAL_MODULE_MAP : DEFAULT_EXTERNAL_MODULE_MAP;
|
|
656
|
+
if (kitImportStrategy === "preferRoot") {
|
|
657
|
+
defaultExternalModuleMap["solanaProgramClientCore"] = "@solana/program-client-core";
|
|
658
|
+
}
|
|
659
|
+
const dependencyMapWithDefaults = {
|
|
660
|
+
...defaultExternalModuleMap,
|
|
661
|
+
...DEFAULT_INTERNAL_MODULE_MAP,
|
|
662
|
+
...dependencyMap
|
|
663
|
+
};
|
|
664
|
+
return mergeImportMaps(
|
|
665
|
+
[...importMap.entries()].map(([module, imports]) => {
|
|
666
|
+
const resolvedModule = dependencyMapWithDefaults[module] ?? module;
|
|
667
|
+
return /* @__PURE__ */ new Map([[resolvedModule, imports]]);
|
|
668
|
+
})
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
function importInfoToString({ importedIdentifier, isType, usedIdentifier }) {
|
|
672
|
+
const alias = importedIdentifier !== usedIdentifier ? ` as ${usedIdentifier}` : "";
|
|
673
|
+
return `${isType ? "type " : ""}${importedIdentifier}${alias}`;
|
|
674
|
+
}
|
|
652
675
|
function getAccountFetchHelpersFragment(scope) {
|
|
653
676
|
const { accountPath, typeManifest: typeManifest2, nameApi, customAccountData } = scope;
|
|
654
677
|
const accountNode = visitorsCore.getLastNodeFromPath(accountPath);
|
|
@@ -1208,28 +1231,45 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1208
1231
|
}
|
|
1209
1232
|
return fragment`args.${inputName} = ${renderedValue};`;
|
|
1210
1233
|
};
|
|
1211
|
-
const
|
|
1212
|
-
const
|
|
1213
|
-
|
|
1214
|
-
|
|
1234
|
+
const getNonNullResolvedInstructionInput = use("getNonNullResolvedInstructionInput", "solanaProgramClientCore");
|
|
1235
|
+
const getResolvedInstructionAccountAsTransactionSigner = use(
|
|
1236
|
+
"getResolvedInstructionAccountAsTransactionSigner",
|
|
1237
|
+
"solanaProgramClientCore"
|
|
1238
|
+
);
|
|
1239
|
+
const getAddressFromResolvedInstructionAccount = use(
|
|
1240
|
+
"getAddressFromResolvedInstructionAccount",
|
|
1241
|
+
"solanaProgramClientCore"
|
|
1242
|
+
);
|
|
1243
|
+
const getResolvedInstructionAccountAsProgramDerivedAddress = use(
|
|
1244
|
+
"getResolvedInstructionAccountAsProgramDerivedAddress",
|
|
1245
|
+
"solanaProgramClientCore"
|
|
1246
|
+
);
|
|
1215
1247
|
const addressType = use("type Address", "solanaAddresses");
|
|
1216
1248
|
switch (defaultValue.kind) {
|
|
1217
1249
|
case "accountValueNode":
|
|
1218
1250
|
const name = nodes.camelCase(defaultValue.name);
|
|
1219
1251
|
if (input.kind === "instructionAccountNode" && input.resolvedIsSigner && !input.isSigner) {
|
|
1220
|
-
return defaultFragment(
|
|
1252
|
+
return defaultFragment(
|
|
1253
|
+
fragment`${getResolvedInstructionAccountAsTransactionSigner}("${name}", accounts.${name}.value).address`
|
|
1254
|
+
);
|
|
1221
1255
|
}
|
|
1222
1256
|
if (input.kind === "instructionAccountNode") {
|
|
1223
|
-
return defaultFragment(
|
|
1257
|
+
return defaultFragment(
|
|
1258
|
+
fragment`${getNonNullResolvedInstructionInput}("${name}", accounts.${name}.value)`
|
|
1259
|
+
);
|
|
1224
1260
|
}
|
|
1225
|
-
return defaultFragment(
|
|
1261
|
+
return defaultFragment(
|
|
1262
|
+
fragment`${getAddressFromResolvedInstructionAccount}("${name}", accounts.${name}.value)`
|
|
1263
|
+
);
|
|
1226
1264
|
case "pdaValueNode":
|
|
1227
1265
|
let pdaProgramValue;
|
|
1228
1266
|
if (nodes.isNode(defaultValue.programId, "accountValueNode")) {
|
|
1229
|
-
|
|
1267
|
+
const name2 = nodes.camelCase(defaultValue.programId.name);
|
|
1268
|
+
pdaProgramValue = fragment`${getAddressFromResolvedInstructionAccount}("${name2}", accounts.${name2}.value)`;
|
|
1230
1269
|
}
|
|
1231
1270
|
if (nodes.isNode(defaultValue.programId, "argumentValueNode")) {
|
|
1232
|
-
|
|
1271
|
+
const name2 = nodes.camelCase(defaultValue.programId.name);
|
|
1272
|
+
pdaProgramValue = fragment`${getAddressFromResolvedInstructionAccount}("${name2}", args.${name2})`;
|
|
1233
1273
|
}
|
|
1234
1274
|
if (nodes.isNode(defaultValue.pda, "pdaNode")) {
|
|
1235
1275
|
let pdaProgram = fragment`programAddress`;
|
|
@@ -1252,13 +1292,15 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1252
1292
|
const valueSeed = defaultValue.seeds.find((s) => s.name === seed.name)?.value;
|
|
1253
1293
|
if (!valueSeed) return [];
|
|
1254
1294
|
if (nodes.isNode(valueSeed, "accountValueNode")) {
|
|
1295
|
+
const name2 = nodes.camelCase(valueSeed.name);
|
|
1255
1296
|
return [
|
|
1256
|
-
fragment`${typeManifest2.encoder}.encode(${
|
|
1297
|
+
fragment`${typeManifest2.encoder}.encode(${getAddressFromResolvedInstructionAccount}("${name2}", accounts.${name2}.value))`
|
|
1257
1298
|
];
|
|
1258
1299
|
}
|
|
1259
1300
|
if (nodes.isNode(valueSeed, "argumentValueNode")) {
|
|
1301
|
+
const name2 = nodes.camelCase(valueSeed.name);
|
|
1260
1302
|
return [
|
|
1261
|
-
fragment`${typeManifest2.encoder}.encode(${
|
|
1303
|
+
fragment`${typeManifest2.encoder}.encode(${getNonNullResolvedInstructionInput}("${name2}", args.${name2}))`
|
|
1262
1304
|
];
|
|
1263
1305
|
}
|
|
1264
1306
|
const valueManifest2 = visitorsCore.visit(valueSeed, typeManifestVisitor);
|
|
@@ -1277,10 +1319,12 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1277
1319
|
const pdaArgs = [];
|
|
1278
1320
|
const pdaSeeds = defaultValue.seeds.map((seed) => {
|
|
1279
1321
|
if (nodes.isNode(seed.value, "accountValueNode")) {
|
|
1280
|
-
|
|
1322
|
+
const name2 = nodes.camelCase(seed.value.name);
|
|
1323
|
+
return fragment`${seed.name}: ${getAddressFromResolvedInstructionAccount}("${name2}", accounts.${name2}.value)`;
|
|
1281
1324
|
}
|
|
1282
1325
|
if (nodes.isNode(seed.value, "argumentValueNode")) {
|
|
1283
|
-
|
|
1326
|
+
const name2 = nodes.camelCase(seed.value.name);
|
|
1327
|
+
return fragment`${seed.name}: ${getNonNullResolvedInstructionInput}("${name2}", args.${name2})`;
|
|
1284
1328
|
}
|
|
1285
1329
|
return visitorsCore.pipe(
|
|
1286
1330
|
visitorsCore.visit(seed.value, typeManifestVisitor).value,
|
|
@@ -1315,10 +1359,12 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1315
1359
|
return fragment``;
|
|
1316
1360
|
case "accountBumpValueNode":
|
|
1317
1361
|
return defaultFragment(
|
|
1318
|
-
fragment`${
|
|
1362
|
+
fragment`${getResolvedInstructionAccountAsProgramDerivedAddress}("${nodes.camelCase(defaultValue.name)}", accounts.${nodes.camelCase(defaultValue.name)}.value)[1]`
|
|
1319
1363
|
);
|
|
1320
1364
|
case "argumentValueNode":
|
|
1321
|
-
return defaultFragment(
|
|
1365
|
+
return defaultFragment(
|
|
1366
|
+
fragment`${getNonNullResolvedInstructionInput}("${nodes.camelCase(defaultValue.name)}", args.${nodes.camelCase(defaultValue.name)})`
|
|
1367
|
+
);
|
|
1322
1368
|
case "resolverValueNode":
|
|
1323
1369
|
const resolverFunction = use(nameApi.resolverFunction(defaultValue.name), getImportFrom(defaultValue));
|
|
1324
1370
|
const resolverAwait = useAsync && asyncResolvers.includes(defaultValue.name) ? "await " : "";
|
|
@@ -1581,7 +1627,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1581
1627
|
return fragment`${argumentArray}.map((address) => ({ address, role: ${role} }))`;
|
|
1582
1628
|
}
|
|
1583
1629
|
if (isSigner === "either") {
|
|
1584
|
-
return fragment`${argumentArray}.map((addressOrSigner) => (
|
|
1630
|
+
return fragment`${argumentArray}.map((addressOrSigner) => (typeof addressOrSigner === "string" ? { address: addressOrSigner, role: ${role} } : { address: addressOrSigner.address, role: ${role}, signer: addressOrSigner }))`;
|
|
1585
1631
|
}
|
|
1586
1632
|
return fragment`${argumentArray}.map((signer) => ({ address: signer.address, role: ${signerRole}, signer }))`;
|
|
1587
1633
|
}
|
|
@@ -1681,7 +1727,7 @@ function getAccountsInitializationFragment(instructionNode) {
|
|
|
1681
1727
|
);
|
|
1682
1728
|
return fragment` // Original accounts.
|
|
1683
1729
|
const originalAccounts = { ${accounts} }
|
|
1684
|
-
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ${use("type
|
|
1730
|
+
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ${use("type ResolvedInstructionAccount", "solanaProgramClientCore")}>;
|
|
1685
1731
|
`;
|
|
1686
1732
|
}
|
|
1687
1733
|
function getArgumentsInitializationFragment(hasAnyArgs, renamedArgs) {
|
|
@@ -1706,9 +1752,11 @@ function getReturnStatementFragment(scope) {
|
|
|
1706
1752
|
const optionalAccountStrategy = instructionNode.optionalAccountStrategy ?? "programId";
|
|
1707
1753
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
1708
1754
|
const hasLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted" && instructionNode.accounts.some((account) => account.isOptional);
|
|
1709
|
-
const getAccountMeta = hasAccounts ? fragment`const getAccountMeta = ${use("getAccountMetaFactory", "
|
|
1755
|
+
const getAccountMeta = hasAccounts ? fragment`const getAccountMeta = ${use("getAccountMetaFactory", "solanaProgramClientCore")}(programAddress, '${optionalAccountStrategy}');` : "";
|
|
1710
1756
|
const accountItems = [
|
|
1711
|
-
...instructionNode.accounts.map(
|
|
1757
|
+
...instructionNode.accounts.map(
|
|
1758
|
+
(account) => `getAccountMeta("${nodes.camelCase(account.name)}", accounts.${nodes.camelCase(account.name)})`
|
|
1759
|
+
),
|
|
1712
1760
|
...hasRemainingAccounts ? ["...remainingAccounts"] : []
|
|
1713
1761
|
].join(", ");
|
|
1714
1762
|
let accounts;
|
|
@@ -1738,7 +1786,7 @@ function getReturnStatementFragment(scope) {
|
|
|
1738
1786
|
function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync) {
|
|
1739
1787
|
return visitorsCore.pipe(
|
|
1740
1788
|
instructionTypeFragment,
|
|
1741
|
-
(f) => hasByteDeltas ? fragment`${f} & ${use("type InstructionWithByteDelta", "
|
|
1789
|
+
(f) => hasByteDeltas ? fragment`${f} & ${use("type InstructionWithByteDelta", "solanaProgramClientCore")}` : f,
|
|
1742
1790
|
(f) => useAsync ? fragment`Promise<${f}>` : f
|
|
1743
1791
|
);
|
|
1744
1792
|
}
|
|
@@ -1850,9 +1898,10 @@ function getFunctionFragment(scope) {
|
|
|
1850
1898
|
);
|
|
1851
1899
|
let accountHelpers;
|
|
1852
1900
|
if (hasAccounts) {
|
|
1901
|
+
const solanaError = use("SolanaError", "solanaErrors");
|
|
1902
|
+
const solanaErrorCode = use("SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS", "solanaErrors");
|
|
1853
1903
|
accountHelpers = fragment`if (instruction.accounts.length < ${minimumNumberOfAccounts}) {
|
|
1854
|
-
|
|
1855
|
-
throw new Error('Not enough accounts');
|
|
1904
|
+
throw new ${solanaError}(${solanaErrorCode}, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: ${minimumNumberOfAccounts} });
|
|
1856
1905
|
}
|
|
1857
1906
|
let accountIndex = 0;
|
|
1858
1907
|
const getNextAccount = () => {
|
|
@@ -2070,30 +2119,27 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2070
2119
|
if (!hasAccountDiscriminators) return;
|
|
2071
2120
|
const programAccountsEnum = nameApi.programAccountsEnum(programNode.name);
|
|
2072
2121
|
const programAccountsIdentifierFunction = nameApi.programAccountsIdentifierFunction(programNode.name);
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
(c) => c.join("\n")
|
|
2086
|
-
),
|
|
2087
|
-
(f) => renderersCore.mapFragmentContent(
|
|
2088
|
-
f,
|
|
2089
|
-
(discriminators) => `export function ${programAccountsIdentifierFunction}(account: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programAccountsEnum} {
|
|
2090
|
-
const data = 'data' in account ? account.data : account;
|
|
2091
|
-
${discriminators}
|
|
2092
|
-
throw new Error("The provided account could not be identified as a ${programNode.name} account.")
|
|
2093
|
-
}`
|
|
2094
|
-
),
|
|
2095
|
-
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
2122
|
+
const discriminatorsFragment = mergeFragments(
|
|
2123
|
+
accountsWithDiscriminators.map((account) => {
|
|
2124
|
+
const variant = nameApi.programAccountsEnumVariant(account.name);
|
|
2125
|
+
return getDiscriminatorConditionFragment({
|
|
2126
|
+
...scope,
|
|
2127
|
+
dataName: "data",
|
|
2128
|
+
discriminators: account.discriminators ?? [],
|
|
2129
|
+
ifTrue: `return ${programAccountsEnum}.${variant};`,
|
|
2130
|
+
struct: nodes.resolveNestedTypeNode(account.data)
|
|
2131
|
+
});
|
|
2132
|
+
}),
|
|
2133
|
+
(c) => c.join("\n")
|
|
2096
2134
|
);
|
|
2135
|
+
const readonlyUint8Array = use("type ReadonlyUint8Array", "solanaCodecsCore");
|
|
2136
|
+
const solanaError = use("SolanaError", "solanaErrors");
|
|
2137
|
+
const solanaErrorCode = use("SOLANA_ERROR__PROGRAM_CLIENTS__FAILED_TO_IDENTIFY_ACCOUNT", "solanaErrors");
|
|
2138
|
+
return fragment`export function ${programAccountsIdentifierFunction}(account: { data: ${readonlyUint8Array} } | ${readonlyUint8Array}): ${programAccountsEnum} {
|
|
2139
|
+
const data = 'data' in account ? account.data : account;
|
|
2140
|
+
${discriminatorsFragment}
|
|
2141
|
+
throw new ${solanaError}(${solanaErrorCode}, { accountData: data, programName: "${programNode.name}" });
|
|
2142
|
+
}`;
|
|
2097
2143
|
}
|
|
2098
2144
|
function getProgramConstantFragment(scope) {
|
|
2099
2145
|
const { programNode, nameApi } = scope;
|
|
@@ -2150,18 +2196,14 @@ function getProgramInstructionsIdentifierFunctionFragment(scope) {
|
|
|
2150
2196
|
}),
|
|
2151
2197
|
(c) => c.join("\n")
|
|
2152
2198
|
);
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
${
|
|
2160
|
-
|
|
2161
|
-
}`
|
|
2162
|
-
),
|
|
2163
|
-
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
2164
|
-
);
|
|
2199
|
+
const readonlyUint8Array = use("type ReadonlyUint8Array", "solanaCodecsCore");
|
|
2200
|
+
const solanaError = use("SolanaError", "solanaErrors");
|
|
2201
|
+
const solanaErrorCode = use("SOLANA_ERROR__PROGRAM_CLIENTS__FAILED_TO_IDENTIFY_INSTRUCTION", "solanaErrors");
|
|
2202
|
+
return fragment`export function ${programInstructionsIdentifierFunction}(instruction: { data: ${readonlyUint8Array} } | ${readonlyUint8Array}): ${programInstructionsEnum} {
|
|
2203
|
+
const data = 'data' in instruction ? instruction.data : instruction;
|
|
2204
|
+
${discriminatorsFragment}
|
|
2205
|
+
throw new ${solanaError}(${solanaErrorCode}, { instructionData: data, programName: "${programNode.name}" });
|
|
2206
|
+
}`;
|
|
2165
2207
|
}
|
|
2166
2208
|
function getProgramInstructionsParsedUnionTypeFragment(scope) {
|
|
2167
2209
|
const { programNode, allInstructions, nameApi } = scope;
|
|
@@ -2205,6 +2247,8 @@ function getProgramInstructionsParseFunctionFragment(scope) {
|
|
|
2205
2247
|
}),
|
|
2206
2248
|
(c) => c.join("\n")
|
|
2207
2249
|
);
|
|
2250
|
+
const solanaError = use("SolanaError", "solanaErrors");
|
|
2251
|
+
const solanaErrorCode = use("SOLANA_ERROR__PROGRAM_CLIENTS__UNRECOGNIZED_INSTRUCTION_TYPE", "solanaErrors");
|
|
2208
2252
|
return fragment`
|
|
2209
2253
|
export function ${parseFunction}<TProgram extends string>(
|
|
2210
2254
|
instruction: ${use("type Instruction", "solanaInstructions")}<TProgram>
|
|
@@ -2213,15 +2257,183 @@ function getProgramInstructionsParseFunctionFragment(scope) {
|
|
|
2213
2257
|
const instructionType = ${programInstructionsIdentifierFunction}(instruction);
|
|
2214
2258
|
switch (instructionType) {
|
|
2215
2259
|
${switchCases}
|
|
2216
|
-
default: throw new
|
|
2260
|
+
default: throw new ${solanaError}(${solanaErrorCode}, { instructionType: instructionType as string, programName: "${programNode.name}" });
|
|
2217
2261
|
}
|
|
2218
2262
|
}`;
|
|
2219
2263
|
}
|
|
2264
|
+
function getProgramPluginFragment(scope) {
|
|
2265
|
+
if (scope.programNode.accounts.length === 0 && scope.programNode.instructions.length === 0) return;
|
|
2266
|
+
const resolvedInstructionInputVisitor = visitorsCore.getResolvedInstructionInputsVisitor();
|
|
2267
|
+
const asyncInstructions = scope.programNode.instructions.filter(
|
|
2268
|
+
(instruction) => hasAsyncFunction(instruction, visitorsCore.visit(instruction, resolvedInstructionInputVisitor), scope.asyncResolvers)
|
|
2269
|
+
).map((i) => i.name);
|
|
2270
|
+
return mergeFragments(
|
|
2271
|
+
[
|
|
2272
|
+
getProgramPluginTypeFragment(scope),
|
|
2273
|
+
getProgramPluginAccountsTypeFragment(scope),
|
|
2274
|
+
getProgramPluginInstructionsTypeFragment({ ...scope, asyncInstructions }),
|
|
2275
|
+
getProgramPluginRequirementsTypeFragment(scope),
|
|
2276
|
+
getProgramPluginFunctionFragment({ ...scope, asyncInstructions }),
|
|
2277
|
+
getMakeOptionalHelperTypeFragment(scope)
|
|
2278
|
+
],
|
|
2279
|
+
(c) => c.join("\n\n")
|
|
2280
|
+
);
|
|
2281
|
+
}
|
|
2282
|
+
function getProgramPluginTypeFragment(scope) {
|
|
2283
|
+
const { programNode, nameApi } = scope;
|
|
2284
|
+
const programPluginType = nameApi.programPluginType(programNode.name);
|
|
2285
|
+
const programPluginAccountsType = nameApi.programPluginAccountsType(programNode.name);
|
|
2286
|
+
const programPluginInstructionsType = nameApi.programPluginInstructionsType(programNode.name);
|
|
2287
|
+
const fields = mergeFragments(
|
|
2288
|
+
[
|
|
2289
|
+
programNode.accounts.length > 0 ? fragment`accounts: ${programPluginAccountsType};` : void 0,
|
|
2290
|
+
programNode.instructions.length > 0 ? fragment`instructions: ${programPluginInstructionsType};` : void 0
|
|
2291
|
+
],
|
|
2292
|
+
(c) => c.join(" ")
|
|
2293
|
+
);
|
|
2294
|
+
return fragment`export type ${programPluginType} = { ${fields} }`;
|
|
2295
|
+
}
|
|
2296
|
+
function getProgramPluginAccountsTypeFragment(scope) {
|
|
2297
|
+
const { programNode, nameApi } = scope;
|
|
2298
|
+
if (programNode.accounts.length === 0) return;
|
|
2299
|
+
const programPluginAccountsType = nameApi.programPluginAccountsType(programNode.name);
|
|
2300
|
+
const selfFetchFunctions = use("type SelfFetchFunctions", "solanaProgramClientCore");
|
|
2301
|
+
const fields = mergeFragments(
|
|
2302
|
+
programNode.accounts.map((account) => {
|
|
2303
|
+
const name = nameApi.programPluginAccountKey(account.name);
|
|
2304
|
+
const codecFunction = use("type " + nameApi.codecFunction(account.name), "generatedAccounts");
|
|
2305
|
+
const fromType = use("type " + nameApi.dataArgsType(account.name), "generatedAccounts");
|
|
2306
|
+
const toType = use("type " + nameApi.dataType(account.name), "generatedAccounts");
|
|
2307
|
+
return fragment`${name}: ReturnType<typeof ${codecFunction}> & ${selfFetchFunctions}<${fromType}, ${toType}>;`;
|
|
2308
|
+
}),
|
|
2309
|
+
(c) => c.join(" ")
|
|
2310
|
+
);
|
|
2311
|
+
return fragment`export type ${programPluginAccountsType} = { ${fields} }`;
|
|
2312
|
+
}
|
|
2313
|
+
function getProgramPluginInstructionsTypeFragment(scope) {
|
|
2314
|
+
const { programNode, asyncInstructions, nameApi } = scope;
|
|
2315
|
+
if (programNode.instructions.length === 0) return;
|
|
2316
|
+
const programPluginInstructionsType = nameApi.programPluginInstructionsType(programNode.name);
|
|
2317
|
+
const selfPlanAndSendFunctions = use("type SelfPlanAndSendFunctions", "solanaProgramClientCore");
|
|
2318
|
+
const fields = mergeFragments(
|
|
2319
|
+
programNode.instructions.map((instruction) => {
|
|
2320
|
+
const name = nameApi.programPluginInstructionKey(instruction.name);
|
|
2321
|
+
const isAsync = asyncInstructions.includes(instruction.name);
|
|
2322
|
+
const instructionType = use("type " + nameApi.instructionType(instruction.name), "generatedInstructions");
|
|
2323
|
+
const instructionInputType = isAsync ? use("type " + nameApi.instructionAsyncInputType(instruction.name), "generatedInstructions") : use("type " + nameApi.instructionSyncInputType(instruction.name), "generatedInstructions");
|
|
2324
|
+
const instructionTypeWithPromise = isAsync ? fragment`Promise<${instructionType}>` : instructionType;
|
|
2325
|
+
return fragment`${name}: (input: ${instructionInputType}) => ${instructionTypeWithPromise} & ${selfPlanAndSendFunctions};`;
|
|
2326
|
+
}),
|
|
2327
|
+
(c) => c.join(" ")
|
|
2328
|
+
);
|
|
2329
|
+
return fragment`export type ${programPluginInstructionsType} = { ${fields} }`;
|
|
2330
|
+
}
|
|
2331
|
+
function getProgramPluginRequirementsTypeFragment(scope) {
|
|
2332
|
+
const { programNode, nameApi } = scope;
|
|
2333
|
+
const programRequirementsType = nameApi.programPluginRequirementsType(programNode.name);
|
|
2334
|
+
const clientWithRpc = fragment`${use("type ClientWithRpc", "solanaPluginInterfaces")}<${use("type GetAccountInfoApi", "solanaRpcApi")} & ${use("type GetMultipleAccountsApi", "solanaRpcApi")}>`;
|
|
2335
|
+
const clientWithPayer = use("type ClientWithPayer", "solanaPluginInterfaces");
|
|
2336
|
+
const clientWithTransactionPlanning = use("type ClientWithTransactionPlanning", "solanaPluginInterfaces");
|
|
2337
|
+
const clientWithTransactionSending = use("type ClientWithTransactionSending", "solanaPluginInterfaces");
|
|
2338
|
+
const hasAccounts = programNode.accounts.length > 0;
|
|
2339
|
+
const hasInstructions = programNode.instructions.length > 0;
|
|
2340
|
+
const requirements = mergeFragments(
|
|
2341
|
+
[
|
|
2342
|
+
hasAccounts ? clientWithRpc : void 0,
|
|
2343
|
+
hasNonOptionalPayerDefaultValueInputs(programNode) ? clientWithPayer : void 0,
|
|
2344
|
+
hasInstructions ? clientWithTransactionPlanning : void 0,
|
|
2345
|
+
hasInstructions ? clientWithTransactionSending : void 0
|
|
2346
|
+
],
|
|
2347
|
+
(c) => c.join(" & ")
|
|
2348
|
+
);
|
|
2349
|
+
return fragment`export type ${programRequirementsType} = ${requirements}`;
|
|
2350
|
+
}
|
|
2351
|
+
function getProgramPluginFunctionFragment(scope) {
|
|
2352
|
+
const { programNode, nameApi } = scope;
|
|
2353
|
+
const programPluginFunction = nameApi.programPluginFunction(programNode.name);
|
|
2354
|
+
const programPluginType = nameApi.programPluginType(programNode.name);
|
|
2355
|
+
const programPluginRequirementsType = nameApi.programPluginRequirementsType(programNode.name);
|
|
2356
|
+
const programPluginKey = nameApi.programPluginKey(programNode.name);
|
|
2357
|
+
const fields = mergeFragments(
|
|
2358
|
+
[getProgramPluginAccountsObjectFragment(scope), getProgramPluginInstructionsObjectFragment(scope)],
|
|
2359
|
+
(c) => c.join(", ")
|
|
2360
|
+
);
|
|
2361
|
+
return fragment`export function ${programPluginFunction}() {
|
|
2362
|
+
return <T extends ${programPluginRequirementsType}>(client: T) => {
|
|
2363
|
+
return { ...client, ${programPluginKey}: { ${fields}} as ${programPluginType} };
|
|
2364
|
+
};
|
|
2365
|
+
}`;
|
|
2366
|
+
}
|
|
2367
|
+
function getProgramPluginAccountsObjectFragment(scope) {
|
|
2368
|
+
const { programNode, nameApi } = scope;
|
|
2369
|
+
if (programNode.accounts.length === 0) return;
|
|
2370
|
+
const fields = mergeFragments(
|
|
2371
|
+
programNode.accounts.map((account) => {
|
|
2372
|
+
const name = nameApi.programPluginAccountKey(account.name);
|
|
2373
|
+
const addSelfFetchFunctions = use("addSelfFetchFunctions", "solanaProgramClientCore");
|
|
2374
|
+
const codecFunction = use(nameApi.codecFunction(account.name), "generatedAccounts");
|
|
2375
|
+
return fragment`${name}: ${addSelfFetchFunctions}(client, ${codecFunction}())`;
|
|
2376
|
+
}),
|
|
2377
|
+
(c) => c.join(", ")
|
|
2378
|
+
);
|
|
2379
|
+
return fragment`accounts: { ${fields} }`;
|
|
2380
|
+
}
|
|
2381
|
+
function getProgramPluginInstructionsObjectFragment(scope) {
|
|
2382
|
+
const { programNode, nameApi, asyncInstructions } = scope;
|
|
2383
|
+
if (programNode.instructions.length === 0) return;
|
|
2384
|
+
const fields = mergeFragments(
|
|
2385
|
+
programNode.instructions.map((instruction) => {
|
|
2386
|
+
const name = nameApi.programPluginInstructionKey(instruction.name);
|
|
2387
|
+
const isAsync = asyncInstructions.includes(instruction.name);
|
|
2388
|
+
const instructionInputType = isAsync ? use("type " + nameApi.instructionAsyncInputType(instruction.name), "generatedInstructions") : use("type " + nameApi.instructionSyncInputType(instruction.name), "generatedInstructions");
|
|
2389
|
+
const instructionFunction = isAsync ? use(nameApi.instructionAsyncFunction(instruction.name), "generatedInstructions") : use(nameApi.instructionSyncFunction(instruction.name), "generatedInstructions");
|
|
2390
|
+
const addSelfPlanAndSendFunctions = use("addSelfPlanAndSendFunctions", "solanaProgramClientCore");
|
|
2391
|
+
const inputWithDefaultPayerValues = getNonOptionalPayerDefaultValueInputs(instruction);
|
|
2392
|
+
if (inputWithDefaultPayerValues.length === 0) {
|
|
2393
|
+
return fragment`${name}: (input: ${instructionInputType}) => ${addSelfPlanAndSendFunctions}(client, ${instructionFunction}(input))`;
|
|
2394
|
+
}
|
|
2395
|
+
const renamedArgs = getRenamedArgsMap(instruction);
|
|
2396
|
+
const processedInputWithDefaultPayerValues = inputWithDefaultPayerValues.map((inputNode) => {
|
|
2397
|
+
return nodes.isNode(inputNode, "instructionAccountNode") ? { name: inputNode.name, signer: inputNode.isSigner !== false } : { name: renamedArgs.get(inputNode.name) ?? inputNode.name, signer: false };
|
|
2398
|
+
});
|
|
2399
|
+
const fieldStringUnion = processedInputWithDefaultPayerValues.map(({ name: name2 }) => `"${name2}"`).join(" | ");
|
|
2400
|
+
const instructionInputTypeWithPayer = fragment`MakeOptional<${instructionInputType}, ${fieldStringUnion}>`;
|
|
2401
|
+
const fieldOverrides = mergeFragments(
|
|
2402
|
+
processedInputWithDefaultPayerValues.map(
|
|
2403
|
+
({ name: name2, signer }) => fragment`${name2}: input.${name2} ?? ${signer ? "client.payer" : "client.payer.address"}`
|
|
2404
|
+
),
|
|
2405
|
+
(c) => c.join(", ")
|
|
2406
|
+
);
|
|
2407
|
+
const inputWithPayer = fragment`{ ...input, ${fieldOverrides} }`;
|
|
2408
|
+
return fragment`${name}: (input: ${instructionInputTypeWithPayer}) => ${addSelfPlanAndSendFunctions}(client, ${instructionFunction}(${inputWithPayer}))`;
|
|
2409
|
+
}),
|
|
2410
|
+
(c) => c.join(", ")
|
|
2411
|
+
);
|
|
2412
|
+
return fragment`instructions: { ${fields} }`;
|
|
2413
|
+
}
|
|
2414
|
+
function getMakeOptionalHelperTypeFragment(scope) {
|
|
2415
|
+
if (!hasNonOptionalPayerDefaultValueInputs(scope.programNode)) return;
|
|
2416
|
+
return fragment`type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;`;
|
|
2417
|
+
}
|
|
2418
|
+
function hasNonOptionalPayerDefaultValueInputs(programNode) {
|
|
2419
|
+
return programNode.instructions.some((instruction) => getNonOptionalPayerDefaultValueInputs(instruction).length > 0);
|
|
2420
|
+
}
|
|
2421
|
+
function getNonOptionalPayerDefaultValueInputs(instructionNode) {
|
|
2422
|
+
return [
|
|
2423
|
+
...instructionNode.accounts.filter((a) => !a.isOptional && nodes.isNode(a.defaultValue, "payerValueNode")),
|
|
2424
|
+
...instructionNode.arguments.filter((a) => nodes.isNode(a.defaultValue, "payerValueNode"))
|
|
2425
|
+
];
|
|
2426
|
+
}
|
|
2220
2427
|
|
|
2221
2428
|
// src/fragments/programPage.ts
|
|
2222
2429
|
function getProgramPageFragment(scope) {
|
|
2223
2430
|
return mergeFragments(
|
|
2224
|
-
[
|
|
2431
|
+
[
|
|
2432
|
+
getProgramConstantFragment(scope),
|
|
2433
|
+
getProgramAccountsFragment(scope),
|
|
2434
|
+
getProgramInstructionsFragment(scope),
|
|
2435
|
+
getProgramPluginFragment(scope)
|
|
2436
|
+
],
|
|
2225
2437
|
(cs) => cs.join("\n\n")
|
|
2226
2438
|
);
|
|
2227
2439
|
}
|
|
@@ -2245,121 +2457,6 @@ function getRootIndexPageFragment(scope) {
|
|
|
2245
2457
|
(cs) => cs.join("\n")
|
|
2246
2458
|
);
|
|
2247
2459
|
}
|
|
2248
|
-
function getSharedPageFragment() {
|
|
2249
|
-
const sharedPage = fragment`/**
|
|
2250
|
-
* Asserts that the given value is not null or undefined.
|
|
2251
|
-
* @internal
|
|
2252
|
-
*/
|
|
2253
|
-
export function expectSome<T>(value: T | null | undefined): T {
|
|
2254
|
-
if (value === null || value === undefined) {
|
|
2255
|
-
throw new Error('Expected a value but received null or undefined.');
|
|
2256
|
-
}
|
|
2257
|
-
return value;
|
|
2258
|
-
}
|
|
2259
|
-
|
|
2260
|
-
/**
|
|
2261
|
-
* Asserts that the given value is a PublicKey.
|
|
2262
|
-
* @internal
|
|
2263
|
-
*/
|
|
2264
|
-
export function expectAddress<T extends string = string>(
|
|
2265
|
-
value: Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null | undefined
|
|
2266
|
-
): Address<T> {
|
|
2267
|
-
if (!value) {
|
|
2268
|
-
throw new Error('Expected a Address.');
|
|
2269
|
-
}
|
|
2270
|
-
if (typeof value === 'object' && "address" in value) {
|
|
2271
|
-
return value.address;
|
|
2272
|
-
}
|
|
2273
|
-
if (Array.isArray(value)) {
|
|
2274
|
-
return value[0] as Address<T>;
|
|
2275
|
-
}
|
|
2276
|
-
return value as Address<T>;
|
|
2277
|
-
}
|
|
2278
|
-
|
|
2279
|
-
/**
|
|
2280
|
-
* Asserts that the given value is a PDA.
|
|
2281
|
-
* @internal
|
|
2282
|
-
*/
|
|
2283
|
-
export function expectProgramDerivedAddress<T extends string = string>(
|
|
2284
|
-
value: Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null | undefined
|
|
2285
|
-
): ProgramDerivedAddress<T> {
|
|
2286
|
-
if (!value || !Array.isArray(value) || !isProgramDerivedAddress(value)) {
|
|
2287
|
-
throw new Error('Expected a ProgramDerivedAddress.');
|
|
2288
|
-
}
|
|
2289
|
-
return value;
|
|
2290
|
-
}
|
|
2291
|
-
|
|
2292
|
-
/**
|
|
2293
|
-
* Asserts that the given value is a TransactionSigner.
|
|
2294
|
-
* @internal
|
|
2295
|
-
*/
|
|
2296
|
-
export function expectTransactionSigner<T extends string = string>(
|
|
2297
|
-
value: Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null | undefined
|
|
2298
|
-
): TransactionSigner<T> {
|
|
2299
|
-
if (!value || !isTransactionSigner(value)) {
|
|
2300
|
-
throw new Error('Expected a TransactionSigner.');
|
|
2301
|
-
}
|
|
2302
|
-
return value;
|
|
2303
|
-
}
|
|
2304
|
-
|
|
2305
|
-
/**
|
|
2306
|
-
* Defines an instruction account to resolve.
|
|
2307
|
-
* @internal
|
|
2308
|
-
*/
|
|
2309
|
-
export type ResolvedAccount<T extends string = string, U extends Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null = Address<T> | ProgramDerivedAddress<T> | TransactionSigner<T> | null> = {
|
|
2310
|
-
isWritable: boolean;
|
|
2311
|
-
value: U;
|
|
2312
|
-
};
|
|
2313
|
-
|
|
2314
|
-
/**
|
|
2315
|
-
* Defines an instruction that stores additional bytes on-chain.
|
|
2316
|
-
* @internal
|
|
2317
|
-
*/
|
|
2318
|
-
export type InstructionWithByteDelta = {
|
|
2319
|
-
byteDelta: number;
|
|
2320
|
-
};
|
|
2321
|
-
|
|
2322
|
-
/**
|
|
2323
|
-
* Get account metas and signers from resolved accounts.
|
|
2324
|
-
* @internal
|
|
2325
|
-
*/
|
|
2326
|
-
export function getAccountMetaFactory(
|
|
2327
|
-
programAddress: Address,
|
|
2328
|
-
optionalAccountStrategy: 'omitted' | 'programId',
|
|
2329
|
-
) {
|
|
2330
|
-
return (account: ResolvedAccount): AccountMeta | AccountSignerMeta | undefined => {
|
|
2331
|
-
if (!account.value) {
|
|
2332
|
-
if (optionalAccountStrategy === 'omitted') return;
|
|
2333
|
-
return Object.freeze({ address: programAddress, role: AccountRole.READONLY });
|
|
2334
|
-
}
|
|
2335
|
-
|
|
2336
|
-
const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;
|
|
2337
|
-
return Object.freeze({
|
|
2338
|
-
address: expectAddress(account.value),
|
|
2339
|
-
role: isTransactionSigner(account.value) ? upgradeRoleToSigner(writableRole) : writableRole,
|
|
2340
|
-
...(isTransactionSigner(account.value) ? { signer: account.value } : {})
|
|
2341
|
-
});
|
|
2342
|
-
};
|
|
2343
|
-
}
|
|
2344
|
-
|
|
2345
|
-
export function isTransactionSigner<TAddress extends string = string>(value: Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress>): value is TransactionSigner<TAddress> {
|
|
2346
|
-
return !!value && typeof value === 'object' && 'address' in value && kitIsTransactionSigner(value);
|
|
2347
|
-
}`;
|
|
2348
|
-
return visitorsCore.pipe(
|
|
2349
|
-
sharedPage,
|
|
2350
|
-
(f) => addFragmentImports(f, "solanaAddresses", [
|
|
2351
|
-
"type Address",
|
|
2352
|
-
"isProgramDerivedAddress",
|
|
2353
|
-
"type ProgramDerivedAddress"
|
|
2354
|
-
]),
|
|
2355
|
-
(f) => addFragmentImports(f, "solanaInstructions", ["AccountRole", "type AccountMeta", "upgradeRoleToSigner"]),
|
|
2356
|
-
(f) => addFragmentImports(f, "solanaSigners", [
|
|
2357
|
-
"type AccountSignerMeta",
|
|
2358
|
-
"isTransactionSigner as kitIsTransactionSigner",
|
|
2359
|
-
"type TransactionSigner"
|
|
2360
|
-
])
|
|
2361
|
-
);
|
|
2362
|
-
}
|
|
2363
2460
|
function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
2364
2461
|
const { name, typeNode, nameApi } = scope;
|
|
2365
2462
|
const isDiscriminatedUnion = nodes.isNode(typeNode, "enumTypeNode") && nodes.isDataEnum(typeNode);
|
|
@@ -3137,11 +3234,11 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3137
3234
|
dependencyMap: options.dependencyMap ?? {},
|
|
3138
3235
|
dependencyVersions: options.dependencyVersions ?? {},
|
|
3139
3236
|
getImportFrom: getImportFromFactory(options.linkOverrides ?? {}, customAccountData, customInstructionData),
|
|
3237
|
+
kitImportStrategy: options.kitImportStrategy ?? DEFAULT_KIT_IMPORT_STRATEGY,
|
|
3140
3238
|
linkables,
|
|
3141
3239
|
nameApi: getNameApi({ ...DEFAULT_NAME_TRANSFORMERS, ...options.nameTransformers }),
|
|
3142
3240
|
nonScalarEnums: (options.nonScalarEnums ?? []).map(nodes.camelCase),
|
|
3143
|
-
renderParentInstructions: options.renderParentInstructions ?? false
|
|
3144
|
-
useGranularImports: options.useGranularImports ?? false
|
|
3241
|
+
renderParentInstructions: options.renderParentInstructions ?? false
|
|
3145
3242
|
};
|
|
3146
3243
|
const typeManifestVisitor = getTypeManifestVisitor({ ...renderScopeWithTypeManifestVisitor, stack });
|
|
3147
3244
|
const renderScope = { ...renderScopeWithTypeManifestVisitor, typeManifestVisitor };
|
|
@@ -3229,7 +3326,6 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3229
3326
|
leavesOnly: !renderScope.renderParentInstructions
|
|
3230
3327
|
}).filter(isNotInternal);
|
|
3231
3328
|
const definedTypesToExport = nodes.getAllDefinedTypes(node).filter(isNotInternal);
|
|
3232
|
-
const hasAnythingToExport = programsToExport.length > 0 || accountsToExport.length > 0 || instructionsToExport.length > 0 || definedTypesToExport.length > 0;
|
|
3233
3329
|
const scope = {
|
|
3234
3330
|
accountsToExport,
|
|
3235
3331
|
definedTypesToExport,
|
|
@@ -3245,7 +3341,6 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3245
3341
|
["instructions/index.ts"]: asPage(getIndexPageFragment(instructionsToExport)),
|
|
3246
3342
|
["pdas/index.ts"]: asPage(getIndexPageFragment(pdasToExport)),
|
|
3247
3343
|
["programs/index.ts"]: asPage(getIndexPageFragment(programsToExport)),
|
|
3248
|
-
["shared/index.ts"]: hasAnythingToExport ? asPage(getSharedPageFragment()) : void 0,
|
|
3249
3344
|
["types/index.ts"]: asPage(getIndexPageFragment(definedTypesToExport))
|
|
3250
3345
|
}),
|
|
3251
3346
|
...nodes.getAllPrograms(node).map((p) => visitorsCore.visit(p, self))
|
|
@@ -3256,16 +3351,17 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3256
3351
|
(v) => visitorsCore.recordLinkablesOnFirstVisitVisitor(v, linkables)
|
|
3257
3352
|
);
|
|
3258
3353
|
}
|
|
3259
|
-
function renderVisitor(
|
|
3354
|
+
function renderVisitor(packageFolder, options = {}) {
|
|
3260
3355
|
return visitorsCore.rootNodeVisitor(async (root) => {
|
|
3356
|
+
const generatedFolder = renderersCore.joinPath(packageFolder, options.generatedFolder ?? "src/generated");
|
|
3261
3357
|
if (options.deleteFolderBeforeRendering ?? true) {
|
|
3262
|
-
renderersCore.deleteDirectory(
|
|
3358
|
+
renderersCore.deleteDirectory(generatedFolder);
|
|
3263
3359
|
}
|
|
3264
3360
|
let renderMap = visitorsCore.visit(root, getRenderMapVisitor(options));
|
|
3265
|
-
const formatCode = await getCodeFormatter(options);
|
|
3361
|
+
const formatCode = await getCodeFormatter(packageFolder, options);
|
|
3266
3362
|
renderMap = await renderersCore.mapRenderMapContentAsync(renderMap, formatCode);
|
|
3267
|
-
await syncPackageJson(renderMap, formatCode, options);
|
|
3268
|
-
renderersCore.writeRenderMap(renderMap,
|
|
3363
|
+
await syncPackageJson(renderMap, formatCode, packageFolder, options);
|
|
3364
|
+
renderersCore.writeRenderMap(renderMap, generatedFolder);
|
|
3269
3365
|
});
|
|
3270
3366
|
}
|
|
3271
3367
|
|