@codama/renderers-js 1.5.1 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -15
- package/dist/index.browser.cjs +680 -655
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.mjs +492 -559
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.node.cjs +679 -671
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +491 -559
- package/dist/index.node.mjs.map +1 -1
- package/dist/index.react-native.mjs +492 -559
- package/dist/index.react-native.mjs.map +1 -1
- package/dist/types/utils/formatCode.d.ts +7 -0
- package/dist/types/utils/formatCode.d.ts.map +1 -0
- package/dist/types/utils/importMap.d.ts +1 -0
- package/dist/types/utils/importMap.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/options.d.ts +5 -3
- package/dist/types/utils/options.d.ts.map +1 -1
- package/dist/types/utils/packageJson.d.ts +25 -0
- package/dist/types/utils/packageJson.d.ts.map +1 -0
- package/dist/types/visitors/getRenderMapVisitor.d.ts.map +1 -1
- package/dist/types/visitors/renderVisitor.d.ts.map +1 -1
- package/package.json +9 -7
package/dist/index.node.cjs
CHANGED
|
@@ -1,51 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var nodes = require('@codama/nodes');
|
|
6
|
+
var renderersCore = require('@codama/renderers-core');
|
|
7
|
+
var visitorsCore = require('@codama/visitors-core');
|
|
8
|
+
var codecsStrings = require('@solana/codecs-strings');
|
|
9
|
+
var prettier = require('prettier');
|
|
10
|
+
var estreePlugin = require('prettier/plugins/estree');
|
|
11
|
+
var typeScriptPlugin = require('prettier/plugins/typescript');
|
|
12
|
+
var errors = require('@codama/errors');
|
|
13
|
+
var semver = require('semver');
|
|
14
|
+
|
|
15
|
+
function _interopNamespace(e) {
|
|
16
|
+
if (e && e.__esModule) return e;
|
|
17
|
+
var n = Object.create(null);
|
|
18
|
+
if (e) {
|
|
19
|
+
Object.keys(e).forEach(function (k) {
|
|
20
|
+
if (k !== 'default') {
|
|
21
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
22
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () { return e[k]; }
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
17
28
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
n.default = e;
|
|
30
|
+
return Object.freeze(n);
|
|
31
|
+
}
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
var
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
DEFAULT_NAME_TRANSFORMERS: () => DEFAULT_NAME_TRANSFORMERS,
|
|
34
|
-
addToImportMap: () => addToImportMap,
|
|
35
|
-
createImportMap: () => createImportMap,
|
|
36
|
-
default: () => renderVisitor,
|
|
37
|
-
getNameApi: () => getNameApi,
|
|
38
|
-
getRenderMapVisitor: () => getRenderMapVisitor,
|
|
39
|
-
getTypeManifestVisitor: () => getTypeManifestVisitor,
|
|
40
|
-
importMapToString: () => importMapToString,
|
|
41
|
-
mergeImportMaps: () => mergeImportMaps,
|
|
42
|
-
mergeTypeManifests: () => mergeTypeManifests,
|
|
43
|
-
parseImportInput: () => parseImportInput,
|
|
44
|
-
removeFromImportMap: () => removeFromImportMap,
|
|
45
|
-
renderVisitor: () => renderVisitor,
|
|
46
|
-
typeManifest: () => typeManifest
|
|
47
|
-
});
|
|
48
|
-
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
var estreePlugin__namespace = /*#__PURE__*/_interopNamespace(estreePlugin);
|
|
34
|
+
var typeScriptPlugin__namespace = /*#__PURE__*/_interopNamespace(typeScriptPlugin);
|
|
49
35
|
|
|
50
36
|
// src/utils/importMap.ts
|
|
51
37
|
var DEFAULT_EXTERNAL_MODULE_MAP = {
|
|
@@ -102,20 +88,20 @@ function parseImportInput(input) {
|
|
|
102
88
|
usedIdentifier: alias ?? name
|
|
103
89
|
});
|
|
104
90
|
}
|
|
105
|
-
function addToImportMap(importMap,
|
|
91
|
+
function addToImportMap(importMap, module, imports) {
|
|
106
92
|
const parsedImports = imports.map(parseImportInput).map((i) => [i.usedIdentifier, i]);
|
|
107
|
-
return mergeImportMaps([importMap, /* @__PURE__ */ new Map([[
|
|
93
|
+
return mergeImportMaps([importMap, /* @__PURE__ */ new Map([[module, new Map(parsedImports)]])]);
|
|
108
94
|
}
|
|
109
|
-
function removeFromImportMap(importMap,
|
|
95
|
+
function removeFromImportMap(importMap, module, usedIdentifiers) {
|
|
110
96
|
const newMap = new Map(importMap);
|
|
111
|
-
const newModuleMap = new Map(newMap.get(
|
|
97
|
+
const newModuleMap = new Map(newMap.get(module));
|
|
112
98
|
usedIdentifiers.forEach((usedIdentifier) => {
|
|
113
99
|
newModuleMap.delete(usedIdentifier);
|
|
114
100
|
});
|
|
115
101
|
if (newModuleMap.size === 0) {
|
|
116
|
-
newMap.delete(
|
|
102
|
+
newMap.delete(module);
|
|
117
103
|
} else {
|
|
118
|
-
newMap.set(
|
|
104
|
+
newMap.set(module, newModuleMap);
|
|
119
105
|
}
|
|
120
106
|
return Object.freeze(newMap);
|
|
121
107
|
}
|
|
@@ -124,8 +110,8 @@ function mergeImportMaps(importMaps) {
|
|
|
124
110
|
if (importMaps.length === 1) return importMaps[0];
|
|
125
111
|
const mergedMap = new Map(importMaps[0]);
|
|
126
112
|
for (const map of importMaps.slice(1)) {
|
|
127
|
-
for (const [
|
|
128
|
-
const mergedModuleMap = mergedMap.get(
|
|
113
|
+
for (const [module, imports] of map) {
|
|
114
|
+
const mergedModuleMap = mergedMap.get(module) ?? /* @__PURE__ */ new Map();
|
|
129
115
|
for (const [usedIdentifier, importInfo] of imports) {
|
|
130
116
|
const existingImportInfo = mergedModuleMap.get(usedIdentifier);
|
|
131
117
|
const shouldOverwriteTypeOnly = existingImportInfo && existingImportInfo.importedIdentifier === importInfo.importedIdentifier && existingImportInfo.isType && !importInfo.isType;
|
|
@@ -133,7 +119,7 @@ function mergeImportMaps(importMaps) {
|
|
|
133
119
|
mergedModuleMap.set(usedIdentifier, importInfo);
|
|
134
120
|
}
|
|
135
121
|
}
|
|
136
|
-
mergedMap.set(
|
|
122
|
+
mergedMap.set(module, mergedModuleMap);
|
|
137
123
|
}
|
|
138
124
|
}
|
|
139
125
|
return Object.freeze(mergedMap);
|
|
@@ -144,11 +130,15 @@ function importMapToString(importMap, dependencyMap = {}, useGranularImports = f
|
|
|
144
130
|
const relative = Number(a.startsWith(".")) - Number(b.startsWith("."));
|
|
145
131
|
if (relative !== 0) return relative;
|
|
146
132
|
return a.localeCompare(b);
|
|
147
|
-
}).map(([
|
|
133
|
+
}).map(([module, imports]) => {
|
|
148
134
|
const innerImports = [...imports.values()].map(importInfoToString).sort((a, b) => a.localeCompare(b)).join(", ");
|
|
149
|
-
return `import { ${innerImports} } from '${
|
|
135
|
+
return `import { ${innerImports} } from '${module}';`;
|
|
150
136
|
}).join("\n");
|
|
151
137
|
}
|
|
138
|
+
function getExternalDependencies(importMap, dependencyMap, useGranularImports) {
|
|
139
|
+
const resolvedImports = resolveImportMapModules(importMap, dependencyMap, useGranularImports);
|
|
140
|
+
return new Set([...resolvedImports.keys()].filter((module) => !module.startsWith(".")));
|
|
141
|
+
}
|
|
152
142
|
function resolveImportMapModules(importMap, dependencyMap, useGranularImports) {
|
|
153
143
|
const dependencyMapWithDefaults = {
|
|
154
144
|
...useGranularImports ? DEFAULT_GRANULAR_EXTERNAL_MODULE_MAP : DEFAULT_EXTERNAL_MODULE_MAP,
|
|
@@ -156,8 +146,8 @@ function resolveImportMapModules(importMap, dependencyMap, useGranularImports) {
|
|
|
156
146
|
...dependencyMap
|
|
157
147
|
};
|
|
158
148
|
return mergeImportMaps(
|
|
159
|
-
[...importMap.entries()].map(([
|
|
160
|
-
const resolvedModule = dependencyMapWithDefaults[
|
|
149
|
+
[...importMap.entries()].map(([module, imports]) => {
|
|
150
|
+
const resolvedModule = dependencyMapWithDefaults[module] ?? module;
|
|
161
151
|
return /* @__PURE__ */ new Map([[resolvedModule, imports]]);
|
|
162
152
|
})
|
|
163
153
|
);
|
|
@@ -166,73 +156,67 @@ function importInfoToString({ importedIdentifier, isType, usedIdentifier }) {
|
|
|
166
156
|
const alias = importedIdentifier !== usedIdentifier ? ` as ${usedIdentifier}` : "";
|
|
167
157
|
return `${isType ? "type " : ""}${importedIdentifier}${alias}`;
|
|
168
158
|
}
|
|
169
|
-
|
|
170
|
-
// src/utils/nameTransformers.ts
|
|
171
|
-
var import_nodes = require("@codama/nodes");
|
|
172
159
|
function getNameApi(transformers) {
|
|
173
160
|
const helpers = {
|
|
174
|
-
camelCase:
|
|
175
|
-
capitalize:
|
|
176
|
-
kebabCase:
|
|
177
|
-
pascalCase:
|
|
178
|
-
snakeCase:
|
|
179
|
-
titleCase:
|
|
161
|
+
camelCase: nodes.camelCase,
|
|
162
|
+
capitalize: nodes.capitalize,
|
|
163
|
+
kebabCase: nodes.kebabCase,
|
|
164
|
+
pascalCase: nodes.pascalCase,
|
|
165
|
+
snakeCase: nodes.snakeCase,
|
|
166
|
+
titleCase: nodes.titleCase
|
|
180
167
|
};
|
|
181
168
|
return Object.fromEntries(
|
|
182
169
|
Object.entries(transformers).map(([key, transformer]) => [key, (name) => transformer(name, helpers)])
|
|
183
170
|
);
|
|
184
171
|
}
|
|
185
172
|
var DEFAULT_NAME_TRANSFORMERS = {
|
|
186
|
-
accountDecodeFunction: (name) => `decode${
|
|
187
|
-
accountFetchAllFunction: (name) => `fetchAll${
|
|
188
|
-
accountFetchAllMaybeFunction: (name) => `fetchAllMaybe${
|
|
189
|
-
accountFetchFromSeedsFunction: (name) => `fetch${
|
|
190
|
-
accountFetchFunction: (name) => `fetch${
|
|
191
|
-
accountFetchMaybeFromSeedsFunction: (name) => `fetchMaybe${
|
|
192
|
-
accountFetchMaybeFunction: (name) => `fetchMaybe${
|
|
193
|
-
accountGetSizeFunction: (name) => `get${
|
|
194
|
-
codecFunction: (name) => `get${
|
|
195
|
-
constant: (name) =>
|
|
196
|
-
constantFunction: (name) => `get${
|
|
197
|
-
dataArgsType: (name) => `${
|
|
198
|
-
dataType: (name) => `${
|
|
199
|
-
decoderFunction: (name) => `get${
|
|
173
|
+
accountDecodeFunction: (name) => `decode${nodes.pascalCase(name)}`,
|
|
174
|
+
accountFetchAllFunction: (name) => `fetchAll${nodes.pascalCase(name)}`,
|
|
175
|
+
accountFetchAllMaybeFunction: (name) => `fetchAllMaybe${nodes.pascalCase(name)}`,
|
|
176
|
+
accountFetchFromSeedsFunction: (name) => `fetch${nodes.pascalCase(name)}FromSeeds`,
|
|
177
|
+
accountFetchFunction: (name) => `fetch${nodes.pascalCase(name)}`,
|
|
178
|
+
accountFetchMaybeFromSeedsFunction: (name) => `fetchMaybe${nodes.pascalCase(name)}FromSeeds`,
|
|
179
|
+
accountFetchMaybeFunction: (name) => `fetchMaybe${nodes.pascalCase(name)}`,
|
|
180
|
+
accountGetSizeFunction: (name) => `get${nodes.pascalCase(name)}Size`,
|
|
181
|
+
codecFunction: (name) => `get${nodes.pascalCase(name)}Codec`,
|
|
182
|
+
constant: (name) => nodes.snakeCase(name).toUpperCase(),
|
|
183
|
+
constantFunction: (name) => `get${nodes.pascalCase(name)}Bytes`,
|
|
184
|
+
dataArgsType: (name) => `${nodes.pascalCase(name)}Args`,
|
|
185
|
+
dataType: (name) => `${nodes.pascalCase(name)}`,
|
|
186
|
+
decoderFunction: (name) => `get${nodes.pascalCase(name)}Decoder`,
|
|
200
187
|
discriminatedUnionDiscriminator: () => "__kind",
|
|
201
|
-
discriminatedUnionFunction: (name) => `${
|
|
202
|
-
discriminatedUnionVariant: (name) => `${
|
|
203
|
-
encoderFunction: (name) => `get${
|
|
204
|
-
enumVariant: (name) => `${
|
|
205
|
-
instructionAsyncFunction: (name) => `get${
|
|
206
|
-
instructionAsyncInputType: (name) => `${
|
|
207
|
-
instructionDataType: (name) => `${
|
|
208
|
-
instructionExtraType: (name) => `${
|
|
209
|
-
instructionParseFunction: (name) => `parse${
|
|
210
|
-
instructionParsedType: (name) => `Parsed${
|
|
211
|
-
instructionSyncFunction: (name) => `get${
|
|
212
|
-
instructionSyncInputType: (name) => `${
|
|
213
|
-
instructionType: (name) => `${
|
|
214
|
-
isDiscriminatedUnionFunction: (name) => `is${
|
|
215
|
-
pdaFindFunction: (name) => `find${
|
|
216
|
-
pdaSeedsType: (name) => `${
|
|
217
|
-
programAccountsEnum: (name) => `${
|
|
218
|
-
programAccountsEnumVariant: (name) => `${
|
|
219
|
-
programAccountsIdentifierFunction: (name) => `identify${
|
|
220
|
-
programAddressConstant: (name) => `${
|
|
221
|
-
programErrorConstant: (name) =>
|
|
222
|
-
programErrorConstantPrefix: (name) => `${
|
|
223
|
-
programErrorMessagesMap: (name) => `${
|
|
224
|
-
programErrorUnion: (name) => `${
|
|
225
|
-
programGetErrorMessageFunction: (name) => `get${
|
|
226
|
-
programInstructionsEnum: (name) => `${
|
|
227
|
-
programInstructionsEnumVariant: (name) => `${
|
|
228
|
-
programInstructionsIdentifierFunction: (name) => `identify${
|
|
229
|
-
programInstructionsParsedUnionType: (name) => `Parsed${
|
|
230
|
-
programIsErrorFunction: (name) => `is${
|
|
231
|
-
resolverFunction: (name) => `${
|
|
188
|
+
discriminatedUnionFunction: (name) => `${nodes.camelCase(name)}`,
|
|
189
|
+
discriminatedUnionVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
190
|
+
encoderFunction: (name) => `get${nodes.pascalCase(name)}Encoder`,
|
|
191
|
+
enumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
192
|
+
instructionAsyncFunction: (name) => `get${nodes.pascalCase(name)}InstructionAsync`,
|
|
193
|
+
instructionAsyncInputType: (name) => `${nodes.pascalCase(name)}AsyncInput`,
|
|
194
|
+
instructionDataType: (name) => `${nodes.pascalCase(name)}InstructionData`,
|
|
195
|
+
instructionExtraType: (name) => `${nodes.pascalCase(name)}InstructionExtra`,
|
|
196
|
+
instructionParseFunction: (name) => `parse${nodes.pascalCase(name)}Instruction`,
|
|
197
|
+
instructionParsedType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
|
|
198
|
+
instructionSyncFunction: (name) => `get${nodes.pascalCase(name)}Instruction`,
|
|
199
|
+
instructionSyncInputType: (name) => `${nodes.pascalCase(name)}Input`,
|
|
200
|
+
instructionType: (name) => `${nodes.pascalCase(name)}Instruction`,
|
|
201
|
+
isDiscriminatedUnionFunction: (name) => `is${nodes.pascalCase(name)}`,
|
|
202
|
+
pdaFindFunction: (name) => `find${nodes.pascalCase(name)}Pda`,
|
|
203
|
+
pdaSeedsType: (name) => `${nodes.pascalCase(name)}Seeds`,
|
|
204
|
+
programAccountsEnum: (name) => `${nodes.pascalCase(name)}Account`,
|
|
205
|
+
programAccountsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
206
|
+
programAccountsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Account`,
|
|
207
|
+
programAddressConstant: (name) => `${nodes.snakeCase(name).toUpperCase()}_PROGRAM_ADDRESS`,
|
|
208
|
+
programErrorConstant: (name) => nodes.snakeCase(name).toUpperCase(),
|
|
209
|
+
programErrorConstantPrefix: (name) => `${nodes.snakeCase(name).toUpperCase()}_ERROR__`,
|
|
210
|
+
programErrorMessagesMap: (name) => `${nodes.camelCase(name)}ErrorMessages`,
|
|
211
|
+
programErrorUnion: (name) => `${nodes.pascalCase(name)}Error`,
|
|
212
|
+
programGetErrorMessageFunction: (name) => `get${nodes.pascalCase(name)}ErrorMessage`,
|
|
213
|
+
programInstructionsEnum: (name) => `${nodes.pascalCase(name)}Instruction`,
|
|
214
|
+
programInstructionsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
|
|
215
|
+
programInstructionsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Instruction`,
|
|
216
|
+
programInstructionsParsedUnionType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
|
|
217
|
+
programIsErrorFunction: (name) => `is${nodes.pascalCase(name)}Error`,
|
|
218
|
+
resolverFunction: (name) => `${nodes.camelCase(name)}`
|
|
232
219
|
};
|
|
233
|
-
|
|
234
|
-
// src/utils/fragment.ts
|
|
235
|
-
var import_renderers_core = require("@codama/renderers-core");
|
|
236
220
|
function createFragment(content) {
|
|
237
221
|
return Object.freeze({ content, features: /* @__PURE__ */ new Set(), imports: createImportMap() });
|
|
238
222
|
}
|
|
@@ -240,7 +224,7 @@ function isFragment(value) {
|
|
|
240
224
|
return typeof value === "object" && value !== null && "content" in value;
|
|
241
225
|
}
|
|
242
226
|
function fragment(template, ...items) {
|
|
243
|
-
return
|
|
227
|
+
return renderersCore.createFragmentTemplate(template, items, isFragment, mergeFragments);
|
|
244
228
|
}
|
|
245
229
|
function mergeFragments(fragments, mergeContent) {
|
|
246
230
|
const filteredFragments = fragments.filter((f) => f !== void 0);
|
|
@@ -250,24 +234,24 @@ function mergeFragments(fragments, mergeContent) {
|
|
|
250
234
|
imports: mergeImportMaps(filteredFragments.map((f) => f.imports))
|
|
251
235
|
});
|
|
252
236
|
}
|
|
253
|
-
function use(importInput,
|
|
237
|
+
function use(importInput, module) {
|
|
254
238
|
const importInfo = parseImportInput(importInput);
|
|
255
|
-
return addFragmentImports(createFragment(importInfo.usedIdentifier),
|
|
239
|
+
return addFragmentImports(createFragment(importInfo.usedIdentifier), module, [importInput]);
|
|
256
240
|
}
|
|
257
241
|
function mergeFragmentImports(fragment2, importMaps) {
|
|
258
242
|
return Object.freeze({ ...fragment2, imports: mergeImportMaps([fragment2.imports, ...importMaps]) });
|
|
259
243
|
}
|
|
260
|
-
function addFragmentImports(fragment2,
|
|
261
|
-
return Object.freeze({ ...fragment2, imports: addToImportMap(fragment2.imports,
|
|
244
|
+
function addFragmentImports(fragment2, module, importInputs) {
|
|
245
|
+
return Object.freeze({ ...fragment2, imports: addToImportMap(fragment2.imports, module, importInputs) });
|
|
262
246
|
}
|
|
263
|
-
function removeFragmentImports(fragment2,
|
|
264
|
-
return Object.freeze({ ...fragment2, imports: removeFromImportMap(fragment2.imports,
|
|
247
|
+
function removeFragmentImports(fragment2, module, usedIdentifiers) {
|
|
248
|
+
return Object.freeze({ ...fragment2, imports: removeFromImportMap(fragment2.imports, module, usedIdentifiers) });
|
|
265
249
|
}
|
|
266
250
|
function addFragmentFeatures(fragment2, features) {
|
|
267
251
|
return Object.freeze({ ...fragment2, features: /* @__PURE__ */ new Set([...fragment2.features, ...features]) });
|
|
268
252
|
}
|
|
269
|
-
function getExportAllFragment(
|
|
270
|
-
return fragment`export * from '${
|
|
253
|
+
function getExportAllFragment(module) {
|
|
254
|
+
return fragment`export * from '${module}';`;
|
|
271
255
|
}
|
|
272
256
|
function getDocblockFragment(lines, withLineJump = false) {
|
|
273
257
|
const lineJump = withLineJump ? "\n" : "";
|
|
@@ -312,24 +296,12 @@ function mergeTypeManifests(manifests, options = {}) {
|
|
|
312
296
|
value: merge((m) => m.value, mergeValues)
|
|
313
297
|
});
|
|
314
298
|
}
|
|
315
|
-
|
|
316
|
-
// src/visitors/getRenderMapVisitor.ts
|
|
317
|
-
var import_nodes29 = require("@codama/nodes");
|
|
318
|
-
var import_renderers_core13 = require("@codama/renderers-core");
|
|
319
|
-
var import_visitors_core29 = require("@codama/visitors-core");
|
|
320
|
-
|
|
321
|
-
// src/fragments/accountFetchHelpers.ts
|
|
322
|
-
var import_visitors_core2 = require("@codama/visitors-core");
|
|
323
|
-
|
|
324
|
-
// src/utils/async.ts
|
|
325
|
-
var import_nodes2 = require("@codama/nodes");
|
|
326
|
-
var import_visitors_core = require("@codama/visitors-core");
|
|
327
299
|
function hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers) {
|
|
328
300
|
const hasByteDeltasAsync = (instructionNode.byteDeltas ?? []).some(
|
|
329
|
-
({ value }) =>
|
|
301
|
+
({ value }) => nodes.isNode(value, "resolverValueNode") && asyncResolvers.includes(value.name)
|
|
330
302
|
);
|
|
331
303
|
const hasRemainingAccountsAsync = (instructionNode.remainingAccounts ?? []).some(
|
|
332
|
-
({ value }) =>
|
|
304
|
+
({ value }) => nodes.isNode(value, "resolverValueNode") && asyncResolvers.includes(value.name)
|
|
333
305
|
);
|
|
334
306
|
return hasAsyncDefaultValues(resolvedInputs, asyncResolvers) || hasByteDeltasAsync || hasRemainingAccountsAsync;
|
|
335
307
|
}
|
|
@@ -351,8 +323,8 @@ function isAsyncDefaultValue(defaultValue, asyncResolvers) {
|
|
|
351
323
|
}
|
|
352
324
|
}
|
|
353
325
|
function getInstructionDependencies(input, asyncResolvers, useAsync) {
|
|
354
|
-
if (
|
|
355
|
-
return
|
|
326
|
+
if (nodes.isNode(input, "instructionNode")) {
|
|
327
|
+
return visitorsCore.deduplicateInstructionDependencies([
|
|
356
328
|
...input.accounts.flatMap((x) => getInstructionDependencies(x, asyncResolvers, useAsync)),
|
|
357
329
|
...input.arguments.flatMap((x) => getInstructionDependencies(x, asyncResolvers, useAsync)),
|
|
358
330
|
...(input.extraArguments ?? []).flatMap((x) => getInstructionDependencies(x, asyncResolvers, useAsync))
|
|
@@ -363,29 +335,29 @@ function getInstructionDependencies(input, asyncResolvers, useAsync) {
|
|
|
363
335
|
if (!defaultValue) return [];
|
|
364
336
|
return getInstructionDependencies({ ...input, defaultValue }, asyncResolvers, useAsync);
|
|
365
337
|
};
|
|
366
|
-
if (
|
|
367
|
-
return [
|
|
338
|
+
if (nodes.isNode(input.defaultValue, ["accountValueNode", "accountBumpValueNode"])) {
|
|
339
|
+
return [nodes.accountValueNode(input.defaultValue.name)];
|
|
368
340
|
}
|
|
369
|
-
if (
|
|
370
|
-
return [
|
|
341
|
+
if (nodes.isNode(input.defaultValue, ["argumentValueNode"])) {
|
|
342
|
+
return [nodes.argumentValueNode(input.defaultValue.name)];
|
|
371
343
|
}
|
|
372
|
-
if (
|
|
344
|
+
if (nodes.isNode(input.defaultValue, "pdaValueNode")) {
|
|
373
345
|
const dependencies = /* @__PURE__ */ new Map();
|
|
374
346
|
input.defaultValue.seeds.forEach((seed) => {
|
|
375
|
-
if (
|
|
347
|
+
if (nodes.isNode(seed.value, ["accountValueNode", "argumentValueNode"])) {
|
|
376
348
|
dependencies.set(seed.value.name, { ...seed.value });
|
|
377
349
|
}
|
|
378
350
|
});
|
|
379
351
|
return [...dependencies.values()];
|
|
380
352
|
}
|
|
381
|
-
if (
|
|
353
|
+
if (nodes.isNode(input.defaultValue, "resolverValueNode")) {
|
|
382
354
|
const isSynchronousResolver = !asyncResolvers.includes(input.defaultValue.name);
|
|
383
355
|
if (useAsync || isSynchronousResolver) {
|
|
384
356
|
return input.defaultValue.dependsOn ?? [];
|
|
385
357
|
}
|
|
386
358
|
}
|
|
387
|
-
if (
|
|
388
|
-
return
|
|
359
|
+
if (nodes.isNode(input.defaultValue, "conditionalValueNode")) {
|
|
360
|
+
return visitorsCore.deduplicateInstructionDependencies([
|
|
389
361
|
...getNestedDependencies(input.defaultValue.condition),
|
|
390
362
|
...getNestedDependencies(input.defaultValue.ifTrue),
|
|
391
363
|
...getNestedDependencies(input.defaultValue.ifFalse)
|
|
@@ -393,58 +365,74 @@ function getInstructionDependencies(input, asyncResolvers, useAsync) {
|
|
|
393
365
|
}
|
|
394
366
|
return [];
|
|
395
367
|
}
|
|
396
|
-
|
|
397
|
-
// src/utils/codecs.ts
|
|
398
|
-
var import_codecs_strings = require("@solana/codecs-strings");
|
|
399
368
|
function getBytesFromBytesValueNode(node) {
|
|
400
369
|
switch (node.encoding) {
|
|
401
370
|
case "utf8":
|
|
402
|
-
return
|
|
371
|
+
return codecsStrings.getUtf8Encoder().encode(node.data);
|
|
403
372
|
case "base16":
|
|
404
|
-
return
|
|
373
|
+
return codecsStrings.getBase16Encoder().encode(node.data);
|
|
405
374
|
case "base58":
|
|
406
|
-
return
|
|
375
|
+
return codecsStrings.getBase58Encoder().encode(node.data);
|
|
407
376
|
case "base64":
|
|
408
377
|
default:
|
|
409
|
-
return
|
|
378
|
+
return codecsStrings.getBase64Encoder().encode(node.data);
|
|
410
379
|
}
|
|
411
380
|
}
|
|
412
|
-
|
|
413
|
-
// src/utils/customData.ts
|
|
414
|
-
var import_nodes3 = require("@codama/nodes");
|
|
415
381
|
var parseCustomDataOptions = (customDataOptions, defaultSuffix) => new Map(
|
|
416
382
|
customDataOptions.map((o) => {
|
|
417
383
|
const options = typeof o === "string" ? { name: o } : o;
|
|
418
|
-
const importAs =
|
|
384
|
+
const importAs = nodes.camelCase(options.importAs ?? `${options.name}${defaultSuffix}`);
|
|
419
385
|
const importFrom = options.importFrom ?? "hooked";
|
|
420
386
|
return [
|
|
421
|
-
|
|
387
|
+
nodes.camelCase(options.name),
|
|
422
388
|
{
|
|
423
389
|
extract: options.extract ?? false,
|
|
424
|
-
extractAs: options.extractAs ?
|
|
390
|
+
extractAs: options.extractAs ? nodes.camelCase(options.extractAs) : importAs,
|
|
425
391
|
importAs,
|
|
426
392
|
importFrom,
|
|
427
|
-
linkNode:
|
|
393
|
+
linkNode: nodes.definedTypeLinkNode(importAs)
|
|
428
394
|
}
|
|
429
395
|
];
|
|
430
396
|
})
|
|
431
397
|
);
|
|
432
|
-
var getDefinedTypeNodesToExtract = (nodes, parsedCustomDataOptions) => nodes.flatMap((node) => {
|
|
398
|
+
var getDefinedTypeNodesToExtract = (nodes$1, parsedCustomDataOptions) => nodes$1.flatMap((node) => {
|
|
433
399
|
const options = parsedCustomDataOptions.get(node.name);
|
|
434
400
|
if (!options || !options.extract) return [];
|
|
435
|
-
if (
|
|
436
|
-
return [
|
|
401
|
+
if (nodes.isNode(node, "accountNode")) {
|
|
402
|
+
return [nodes.definedTypeNode({ name: options.extractAs, type: { ...node.data } })];
|
|
437
403
|
}
|
|
438
404
|
return [
|
|
439
|
-
|
|
405
|
+
nodes.definedTypeNode({
|
|
440
406
|
name: options.extractAs,
|
|
441
|
-
type:
|
|
407
|
+
type: nodes.structTypeNodeFromInstructionArgumentNodes(node.arguments)
|
|
442
408
|
})
|
|
443
409
|
];
|
|
444
410
|
});
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
411
|
+
var DEFAULT_PRETTIER_OPTIONS = {
|
|
412
|
+
arrowParens: "always",
|
|
413
|
+
parser: "typescript",
|
|
414
|
+
plugins: [estreePlugin__namespace, typeScriptPlugin__namespace],
|
|
415
|
+
printWidth: 80,
|
|
416
|
+
semi: true,
|
|
417
|
+
singleQuote: true,
|
|
418
|
+
tabWidth: 2,
|
|
419
|
+
trailingComma: "es5",
|
|
420
|
+
useTabs: false
|
|
421
|
+
};
|
|
422
|
+
async function formatCode(renderMap, options) {
|
|
423
|
+
const shouldFormatCode = options.formatCode ?? true;
|
|
424
|
+
if (!shouldFormatCode) return renderMap;
|
|
425
|
+
const prettierOptions = {
|
|
426
|
+
...DEFAULT_PRETTIER_OPTIONS,
|
|
427
|
+
...await resolvePrettierOptions(options.packageFolder),
|
|
428
|
+
...options.prettierOptions
|
|
429
|
+
};
|
|
430
|
+
return await renderersCore.mapRenderMapContentAsync(renderMap, (code) => prettier.format(code, prettierOptions));
|
|
431
|
+
}
|
|
432
|
+
async function resolvePrettierOptions(packageFolder) {
|
|
433
|
+
if (!packageFolder) return null;
|
|
434
|
+
return await prettier.resolveConfig(packageFolder);
|
|
435
|
+
}
|
|
448
436
|
function getImportFromFactory(overrides, customAccountData, customInstructionData) {
|
|
449
437
|
const customDataOverrides = Object.fromEntries(
|
|
450
438
|
[...customAccountData.values(), ...customInstructionData.values()].map(({ importFrom, importAs }) => [
|
|
@@ -476,7 +464,7 @@ function getImportFromFactory(overrides, customAccountData, customInstructionDat
|
|
|
476
464
|
case "resolverValueNode":
|
|
477
465
|
return linkOverrides.resolvers[node.name] ?? "hooked";
|
|
478
466
|
default:
|
|
479
|
-
throw new
|
|
467
|
+
throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
|
|
480
468
|
expectedKinds: [
|
|
481
469
|
"AccountLinkNode",
|
|
482
470
|
"DefinedTypeLinkNode",
|
|
@@ -491,11 +479,177 @@ function getImportFromFactory(overrides, customAccountData, customInstructionDat
|
|
|
491
479
|
}
|
|
492
480
|
};
|
|
493
481
|
}
|
|
482
|
+
var DEFAULT_DEPENDENCY_VERSIONS = {
|
|
483
|
+
"@solana/accounts": "^5.0.0",
|
|
484
|
+
"@solana/addresses": "^5.0.0",
|
|
485
|
+
"@solana/codecs": "^5.0.0",
|
|
486
|
+
"@solana/errors": "^5.0.0",
|
|
487
|
+
"@solana/instructions": "^5.0.0",
|
|
488
|
+
"@solana/kit": "^5.0.0",
|
|
489
|
+
"@solana/programs": "^5.0.0",
|
|
490
|
+
"@solana/rpc-types": "^5.0.0",
|
|
491
|
+
"@solana/signers": "^5.0.0"
|
|
492
|
+
};
|
|
493
|
+
function syncPackageJson(renderMap, options) {
|
|
494
|
+
const shouldSyncPackageJson = options.syncPackageJson ?? false;
|
|
495
|
+
const packageFolder = options.packageFolder;
|
|
496
|
+
if (!packageFolder) {
|
|
497
|
+
if (shouldSyncPackageJson) {
|
|
498
|
+
errors.logWarn("Cannot sync package.json. Please provide the 'packageFolder' option.");
|
|
499
|
+
}
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const packageJsonPath = renderersCore.joinPath(packageFolder, "package.json");
|
|
503
|
+
const usedDependencies = getUsedDependencyVersions(
|
|
504
|
+
renderMap,
|
|
505
|
+
options.dependencyMap ?? {},
|
|
506
|
+
options.dependencyVersions ?? {},
|
|
507
|
+
options.useGranularImports ?? false
|
|
508
|
+
);
|
|
509
|
+
if (!shouldSyncPackageJson) {
|
|
510
|
+
if (renderersCore.fileExists(packageJsonPath)) {
|
|
511
|
+
checkExistingPackageJson(renderersCore.readJson(packageJsonPath), usedDependencies);
|
|
512
|
+
}
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (renderersCore.fileExists(packageJsonPath)) {
|
|
516
|
+
const packageJson = updateExistingPackageJson(renderersCore.readJson(packageJsonPath), usedDependencies);
|
|
517
|
+
renderersCore.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
518
|
+
} else {
|
|
519
|
+
const packageJson = createNewPackageJson(usedDependencies);
|
|
520
|
+
renderersCore.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function createNewPackageJson(dependencyVersions) {
|
|
524
|
+
return updateExistingPackageJson(
|
|
525
|
+
{
|
|
526
|
+
name: "js-client",
|
|
527
|
+
version: "1.0.0",
|
|
528
|
+
// eslint-disable-next-line sort-keys-fix/sort-keys-fix
|
|
529
|
+
description: "",
|
|
530
|
+
main: "src/index.ts",
|
|
531
|
+
scripts: { test: 'echo "Error: no test specified" && exit 1' },
|
|
532
|
+
// eslint-disable-next-line sort-keys-fix/sort-keys-fix
|
|
533
|
+
keywords: [],
|
|
534
|
+
// eslint-disable-next-line sort-keys-fix/sort-keys-fix
|
|
535
|
+
author: ""
|
|
536
|
+
},
|
|
537
|
+
dependencyVersions
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
function updateExistingPackageJson(packageJson, dependencyVersions) {
|
|
541
|
+
const updatedDependencies = { ...packageJson.dependencies };
|
|
542
|
+
const updatedPeerDependencies = { ...packageJson.peerDependencies };
|
|
543
|
+
const updatedDevDependencies = { ...packageJson.devDependencies };
|
|
544
|
+
for (const [dependency, requiredRange] of Object.entries(dependencyVersions)) {
|
|
545
|
+
let found = false;
|
|
546
|
+
if (updatedDependencies[dependency]) {
|
|
547
|
+
updateDependency(updatedDependencies, dependency, requiredRange);
|
|
548
|
+
found = true;
|
|
549
|
+
}
|
|
550
|
+
if (updatedPeerDependencies[dependency]) {
|
|
551
|
+
updateDependency(updatedPeerDependencies, dependency, requiredRange);
|
|
552
|
+
found = true;
|
|
553
|
+
}
|
|
554
|
+
if (updatedDevDependencies[dependency]) {
|
|
555
|
+
updateDependency(updatedDevDependencies, dependency, requiredRange);
|
|
556
|
+
found = true;
|
|
557
|
+
}
|
|
558
|
+
if (!found) {
|
|
559
|
+
const dependencyGroupToAdd = dependency === "@solana/kit" ? updatedPeerDependencies : updatedDependencies;
|
|
560
|
+
dependencyGroupToAdd[dependency] = requiredRange;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
...packageJson,
|
|
565
|
+
...Object.entries(updatedPeerDependencies).length > 0 ? { peerDependencies: updatedPeerDependencies } : {},
|
|
566
|
+
...Object.entries(updatedDependencies).length > 0 ? { dependencies: updatedDependencies } : {},
|
|
567
|
+
...Object.entries(updatedDevDependencies).length > 0 ? { devDependencies: updatedDevDependencies } : {}
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
function checkExistingPackageJson(packageJson, dependencyVersions) {
|
|
571
|
+
const missingDependencies = [];
|
|
572
|
+
const dependenciesToUpdate = [];
|
|
573
|
+
const existingDependencies = {
|
|
574
|
+
...packageJson.devDependencies,
|
|
575
|
+
...packageJson.peerDependencies,
|
|
576
|
+
...packageJson.dependencies
|
|
577
|
+
};
|
|
578
|
+
for (const [dependency, requiredRange] of Object.entries(dependencyVersions)) {
|
|
579
|
+
if (!existingDependencies[dependency]) {
|
|
580
|
+
missingDependencies.push(dependency);
|
|
581
|
+
} else if (shouldUpdateRange(dependency, existingDependencies[dependency], requiredRange)) {
|
|
582
|
+
dependenciesToUpdate.push(dependency);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (missingDependencies.length === 0 && dependenciesToUpdate.length === 0) return;
|
|
586
|
+
const missingList = missingDependencies.map((d) => `- ${d} missing: ${dependencyVersions[d]}
|
|
587
|
+
`).join("");
|
|
588
|
+
const outdatedList = dependenciesToUpdate.map((d) => `- ${d} outdated: ${existingDependencies[d]} -> ${dependencyVersions[d]}
|
|
589
|
+
`).join("");
|
|
590
|
+
errors.logWarn(
|
|
591
|
+
`The following dependencies in your \`package.json\` are out-of-date or missing:
|
|
592
|
+
${missingList}${outdatedList}`
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
function getUsedDependencyVersions(renderMap, dependencyMap, dependencyVersions, useGranularImports) {
|
|
596
|
+
const dependencyVersionsWithDefaults = {
|
|
597
|
+
...DEFAULT_DEPENDENCY_VERSIONS,
|
|
598
|
+
...dependencyVersions
|
|
599
|
+
};
|
|
600
|
+
const fragment2 = mergeFragments([...renderMap.values()], () => "");
|
|
601
|
+
const usedDependencies = getExternalDependencies(fragment2.imports, dependencyMap, useGranularImports);
|
|
602
|
+
const [usedDependencyVersion, missingDependencies] = [...usedDependencies].reduce(
|
|
603
|
+
([acc, missingDependencies2], dependency) => {
|
|
604
|
+
const version = dependencyVersionsWithDefaults[dependency];
|
|
605
|
+
if (version) {
|
|
606
|
+
acc[dependency] = version;
|
|
607
|
+
} else {
|
|
608
|
+
missingDependencies2.add(dependency);
|
|
609
|
+
}
|
|
610
|
+
return [acc, missingDependencies2];
|
|
611
|
+
},
|
|
612
|
+
[{}, /* @__PURE__ */ new Set()]
|
|
613
|
+
);
|
|
614
|
+
if (missingDependencies.size > 0) {
|
|
615
|
+
throw new errors.CodamaError(errors.CODAMA_ERROR__RENDERERS__MISSING_DEPENDENCY_VERSIONS, {
|
|
616
|
+
dependencies: [...missingDependencies],
|
|
617
|
+
message: "Please add these dependencies to the `dependencyVersions` option."
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
return usedDependencyVersion;
|
|
621
|
+
}
|
|
622
|
+
function shouldUpdateRange(dependency, currentRange, requiredRange) {
|
|
623
|
+
try {
|
|
624
|
+
if (semver.subset(currentRange, requiredRange)) {
|
|
625
|
+
return false;
|
|
626
|
+
}
|
|
627
|
+
const minRequiredVersion = semver.minVersion(requiredRange);
|
|
628
|
+
const minCurrentVersion = semver.minVersion(currentRange);
|
|
629
|
+
if (!minCurrentVersion || !minRequiredVersion) {
|
|
630
|
+
throw new Error("Could not determine minimum versions.");
|
|
631
|
+
}
|
|
632
|
+
if (semver.lt(minCurrentVersion, minRequiredVersion)) {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
return false;
|
|
636
|
+
} catch (error) {
|
|
637
|
+
console.warn(
|
|
638
|
+
`Could not parse the following ranges for dependency "${dependency}": [${currentRange}] and/or [${requiredRange}]. Caused by: ${error.message}`
|
|
639
|
+
);
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
function updateDependency(dependencyGroup, dependency, requiredRange) {
|
|
644
|
+
const currentRange = dependencyGroup[dependency];
|
|
645
|
+
if (!shouldUpdateRange(dependency, currentRange, requiredRange)) return;
|
|
646
|
+
dependencyGroup[dependency] = requiredRange;
|
|
647
|
+
}
|
|
494
648
|
|
|
495
649
|
// src/fragments/accountFetchHelpers.ts
|
|
496
650
|
function getAccountFetchHelpersFragment(scope) {
|
|
497
651
|
const { accountPath, typeManifest: typeManifest2, nameApi, customAccountData } = scope;
|
|
498
|
-
const accountNode =
|
|
652
|
+
const accountNode = visitorsCore.getLastNodeFromPath(accountPath);
|
|
499
653
|
const decodeFunction = nameApi.accountDecodeFunction(accountNode.name);
|
|
500
654
|
const fetchAllFunction = nameApi.accountFetchAllFunction(accountNode.name);
|
|
501
655
|
const fetchAllMaybeFunction = nameApi.accountFetchAllMaybeFunction(accountNode.name);
|
|
@@ -504,7 +658,7 @@ function getAccountFetchHelpersFragment(scope) {
|
|
|
504
658
|
const hasCustomData = customAccountData.has(accountNode.name);
|
|
505
659
|
const accountType = hasCustomData ? typeManifest2.strictType : nameApi.dataType(accountNode.name);
|
|
506
660
|
const decoderFunction = hasCustomData ? typeManifest2.decoder : `${nameApi.decoderFunction(accountNode.name)}()`;
|
|
507
|
-
return
|
|
661
|
+
return visitorsCore.pipe(
|
|
508
662
|
fragment`export function ${decodeFunction}<TAddress extends string = string>(encodedAccount: EncodedAccount<TAddress>): Account<${accountType}, TAddress>;
|
|
509
663
|
export function ${decodeFunction}<TAddress extends string = string>(encodedAccount: MaybeEncodedAccount<TAddress>): MaybeAccount<${accountType}, TAddress>;
|
|
510
664
|
export function ${decodeFunction}<TAddress extends string = string>(encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>): Account<${accountType}, TAddress> | MaybeAccount<${accountType}, TAddress> {
|
|
@@ -564,28 +718,20 @@ export async function ${fetchAllMaybeFunction}(
|
|
|
564
718
|
])
|
|
565
719
|
);
|
|
566
720
|
}
|
|
567
|
-
|
|
568
|
-
// src/fragments/accountPage.ts
|
|
569
|
-
var import_nodes9 = require("@codama/nodes");
|
|
570
|
-
var import_visitors_core7 = require("@codama/visitors-core");
|
|
571
|
-
|
|
572
|
-
// src/fragments/accountPdaHelpers.ts
|
|
573
|
-
var import_nodes4 = require("@codama/nodes");
|
|
574
|
-
var import_visitors_core3 = require("@codama/visitors-core");
|
|
575
721
|
function getAccountPdaHelpersFragment(scope) {
|
|
576
722
|
const { accountPath, nameApi, linkables, customAccountData, typeManifest: typeManifest2 } = scope;
|
|
577
|
-
const accountNode =
|
|
723
|
+
const accountNode = visitorsCore.getLastNodeFromPath(accountPath);
|
|
578
724
|
const pdaNode = accountNode.pda ? linkables.get([...accountPath, accountNode.pda]) : void 0;
|
|
579
725
|
if (!pdaNode) return;
|
|
580
726
|
const accountType = customAccountData.has(accountNode.name) ? typeManifest2.strictType : nameApi.dataType(accountNode.name);
|
|
581
727
|
const importFrom = "generatedPdas";
|
|
582
728
|
const pdaSeedsType = nameApi.pdaSeedsType(pdaNode.name);
|
|
583
729
|
const findPdaFunction = nameApi.pdaFindFunction(pdaNode.name);
|
|
584
|
-
const hasVariableSeeds = pdaNode.seeds.filter(
|
|
730
|
+
const hasVariableSeeds = pdaNode.seeds.filter(nodes.isNodeFilter("variablePdaSeedNode")).length > 0;
|
|
585
731
|
const fetchFromSeedsFunction = nameApi.accountFetchFromSeedsFunction(accountNode.name);
|
|
586
732
|
const fetchMaybeFromSeedsFunction = nameApi.accountFetchMaybeFromSeedsFunction(accountNode.name);
|
|
587
733
|
const fetchMaybeFunction = nameApi.accountFetchMaybeFunction(accountNode.name);
|
|
588
|
-
return
|
|
734
|
+
return visitorsCore.pipe(
|
|
589
735
|
fragment`export async function ${fetchFromSeedsFunction}(
|
|
590
736
|
rpc: Parameters<typeof fetchEncodedAccount>[0],
|
|
591
737
|
${hasVariableSeeds ? `seeds: ${pdaSeedsType},` : ""}
|
|
@@ -615,12 +761,9 @@ export async function ${fetchMaybeFromSeedsFunction}(
|
|
|
615
761
|
])
|
|
616
762
|
);
|
|
617
763
|
}
|
|
618
|
-
|
|
619
|
-
// src/fragments/accountSizeHelpers.ts
|
|
620
|
-
var import_visitors_core4 = require("@codama/visitors-core");
|
|
621
764
|
function getAccountSizeHelpersFragment(scope) {
|
|
622
765
|
const { accountPath, nameApi } = scope;
|
|
623
|
-
const accountNode =
|
|
766
|
+
const accountNode = visitorsCore.getLastNodeFromPath(accountPath);
|
|
624
767
|
if (accountNode.size == null) return;
|
|
625
768
|
const getSizeFunction = nameApi.accountGetSizeFunction(accountNode.name);
|
|
626
769
|
return fragment`export function ${getSizeFunction}(): number {
|
|
@@ -628,10 +771,6 @@ function getAccountSizeHelpersFragment(scope) {
|
|
|
628
771
|
}`;
|
|
629
772
|
}
|
|
630
773
|
|
|
631
|
-
// src/fragments/accountType.ts
|
|
632
|
-
var import_nodes7 = require("@codama/nodes");
|
|
633
|
-
var import_visitors_core5 = require("@codama/visitors-core");
|
|
634
|
-
|
|
635
774
|
// src/fragments/type.ts
|
|
636
775
|
function getTypeFragment(scope) {
|
|
637
776
|
const { name, manifest, nameApi, docs = [] } = scope;
|
|
@@ -645,9 +784,6 @@ function getTypeFragment(scope) {
|
|
|
645
784
|
const looseExport = manifest.strictType.content === manifest.looseType.content ? aliasedLooseName : fragment`export type ${looseName} = ${manifest.looseType};`;
|
|
646
785
|
return fragment`${docblock}export type ${strictName} = ${manifest.strictType};\n\n${looseExport}`;
|
|
647
786
|
}
|
|
648
|
-
|
|
649
|
-
// src/fragments/typeDecoder.ts
|
|
650
|
-
var import_nodes5 = require("@codama/nodes");
|
|
651
787
|
function getTypeDecoderFragment(scope) {
|
|
652
788
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
653
789
|
const decoderFunction = nameApi.decoderFunction(name);
|
|
@@ -657,15 +793,12 @@ function getTypeDecoderFragment(scope) {
|
|
|
657
793
|
typeof scope.size === "number" ? "type FixedSizeDecoder" : "type Decoder",
|
|
658
794
|
"solanaCodecsCore"
|
|
659
795
|
);
|
|
660
|
-
const useTypeCast =
|
|
796
|
+
const useTypeCast = nodes.isNode(node, "enumTypeNode") && nodes.isDataEnum(node) && typeof scope.size === "number";
|
|
661
797
|
const typeCast = useTypeCast ? fragment` as ${decoderType}<${strictName}>` : "";
|
|
662
798
|
return fragment`${docblock}export function ${decoderFunction}(): ${decoderType}<${strictName}> {
|
|
663
799
|
return ${manifest.decoder}${typeCast};
|
|
664
800
|
}`;
|
|
665
801
|
}
|
|
666
|
-
|
|
667
|
-
// src/fragments/typeEncoder.ts
|
|
668
|
-
var import_nodes6 = require("@codama/nodes");
|
|
669
802
|
function getTypeEncoderFragment(scope) {
|
|
670
803
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
671
804
|
const encoderFunction = nameApi.encoderFunction(name);
|
|
@@ -675,7 +808,7 @@ function getTypeEncoderFragment(scope) {
|
|
|
675
808
|
typeof scope.size === "number" ? "type FixedSizeEncoder" : "type Encoder",
|
|
676
809
|
"solanaCodecsCore"
|
|
677
810
|
);
|
|
678
|
-
const useTypeCast =
|
|
811
|
+
const useTypeCast = nodes.isNode(node, "enumTypeNode") && nodes.isDataEnum(node) && typeof scope.size === "number";
|
|
679
812
|
const typeCast = useTypeCast ? fragment` as ${encoderType}<${looseName}>` : "";
|
|
680
813
|
return fragment`${docblock}export function ${encoderFunction}(): ${encoderType}<${looseName}> {
|
|
681
814
|
return ${manifest.encoder}${typeCast};
|
|
@@ -715,7 +848,7 @@ function getTypeWithCodecFragment(scope) {
|
|
|
715
848
|
// src/fragments/accountType.ts
|
|
716
849
|
function getAccountTypeFragment(scope) {
|
|
717
850
|
const { accountPath, typeManifest: typeManifest2, nameApi, customAccountData } = scope;
|
|
718
|
-
const accountNode =
|
|
851
|
+
const accountNode = visitorsCore.getLastNodeFromPath(accountPath);
|
|
719
852
|
if (customAccountData.has(accountNode.name)) return;
|
|
720
853
|
return getTypeWithCodecFragment({
|
|
721
854
|
codecDocs: [`Gets the codec for {@link ${nameApi.dataType(accountNode.name)}} account data.`],
|
|
@@ -724,15 +857,11 @@ function getAccountTypeFragment(scope) {
|
|
|
724
857
|
manifest: typeManifest2,
|
|
725
858
|
name: accountNode.name,
|
|
726
859
|
nameApi,
|
|
727
|
-
node:
|
|
860
|
+
node: nodes.resolveNestedTypeNode(accountNode.data),
|
|
728
861
|
size: scope.size,
|
|
729
862
|
typeDocs: accountNode.docs
|
|
730
863
|
});
|
|
731
864
|
}
|
|
732
|
-
|
|
733
|
-
// src/fragments/discriminatorConstants.ts
|
|
734
|
-
var import_nodes8 = require("@codama/nodes");
|
|
735
|
-
var import_visitors_core6 = require("@codama/visitors-core");
|
|
736
865
|
function getDiscriminatorConstantsFragment(scope) {
|
|
737
866
|
const fragments = scope.discriminatorNodes.map((node) => getDiscriminatorConstantFragment(node, scope)).filter(Boolean);
|
|
738
867
|
return mergeFragments(fragments, (c) => c.join("\n\n"));
|
|
@@ -749,22 +878,22 @@ function getDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
|
749
878
|
}
|
|
750
879
|
function getConstantDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
751
880
|
const { discriminatorNodes, typeManifestVisitor, prefix } = scope;
|
|
752
|
-
const index = discriminatorNodes.filter(
|
|
881
|
+
const index = discriminatorNodes.filter(nodes.isNodeFilter("constantDiscriminatorNode")).indexOf(discriminatorNode);
|
|
753
882
|
const suffix = index <= 0 ? "" : `_${index + 1}`;
|
|
754
|
-
const name =
|
|
755
|
-
const encoder =
|
|
756
|
-
const value =
|
|
883
|
+
const name = nodes.camelCase(`${prefix}_discriminator${suffix}`);
|
|
884
|
+
const encoder = visitorsCore.visit(discriminatorNode.constant.type, typeManifestVisitor).encoder;
|
|
885
|
+
const value = visitorsCore.visit(discriminatorNode.constant.value, typeManifestVisitor).value;
|
|
757
886
|
return getConstantFragment({ ...scope, encoder, name, value });
|
|
758
887
|
}
|
|
759
888
|
function getFieldDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
760
889
|
const { fields, prefix, typeManifestVisitor } = scope;
|
|
761
890
|
const field = fields.find((f) => f.name === discriminatorNode.name);
|
|
762
|
-
if (!field || !field.defaultValue || !
|
|
891
|
+
if (!field || !field.defaultValue || !nodes.isNode(field.defaultValue, nodes.VALUE_NODES)) {
|
|
763
892
|
return null;
|
|
764
893
|
}
|
|
765
|
-
const name =
|
|
766
|
-
const encoder =
|
|
767
|
-
const value =
|
|
894
|
+
const name = nodes.camelCase(`${prefix}_${discriminatorNode.name}`);
|
|
895
|
+
const encoder = visitorsCore.visit(field.type, typeManifestVisitor).encoder;
|
|
896
|
+
const value = visitorsCore.visit(field.defaultValue, typeManifestVisitor).value;
|
|
768
897
|
return getConstantFragment({ ...scope, encoder, name, value });
|
|
769
898
|
}
|
|
770
899
|
function getConstantFragment(scope) {
|
|
@@ -776,12 +905,12 @@ function getConstantFragment(scope) {
|
|
|
776
905
|
|
|
777
906
|
// src/fragments/accountPage.ts
|
|
778
907
|
function getAccountPageFragment(scope) {
|
|
779
|
-
const node =
|
|
780
|
-
if (!
|
|
908
|
+
const node = visitorsCore.getLastNodeFromPath(scope.accountPath);
|
|
909
|
+
if (!visitorsCore.findProgramNodeFromPath(scope.accountPath)) {
|
|
781
910
|
throw new Error("Account must be visited inside a program.");
|
|
782
911
|
}
|
|
783
|
-
const typeManifest2 =
|
|
784
|
-
const fields =
|
|
912
|
+
const typeManifest2 = visitorsCore.visit(node, scope.typeManifestVisitor);
|
|
913
|
+
const fields = nodes.resolveNestedTypeNode(node.data).fields;
|
|
785
914
|
return mergeFragments(
|
|
786
915
|
[
|
|
787
916
|
getDiscriminatorConstantsFragment({
|
|
@@ -798,30 +927,24 @@ function getAccountPageFragment(scope) {
|
|
|
798
927
|
(cs) => cs.join("\n\n")
|
|
799
928
|
);
|
|
800
929
|
}
|
|
801
|
-
|
|
802
|
-
// src/fragments/discriminatorCondition.ts
|
|
803
|
-
var import_nodes10 = require("@codama/nodes");
|
|
804
|
-
var import_renderers_core2 = require("@codama/renderers-core");
|
|
805
|
-
var import_visitors_core8 = require("@codama/visitors-core");
|
|
806
|
-
var import_codecs_strings2 = require("@solana/codecs-strings");
|
|
807
930
|
function getDiscriminatorConditionFragment(scope) {
|
|
808
|
-
return
|
|
931
|
+
return visitorsCore.pipe(
|
|
809
932
|
mergeFragments(
|
|
810
933
|
scope.discriminators.flatMap((discriminator) => {
|
|
811
|
-
if (
|
|
934
|
+
if (nodes.isNode(discriminator, "sizeDiscriminatorNode")) {
|
|
812
935
|
return [getSizeConditionFragment(discriminator, scope)];
|
|
813
936
|
}
|
|
814
|
-
if (
|
|
937
|
+
if (nodes.isNode(discriminator, "constantDiscriminatorNode")) {
|
|
815
938
|
return [getByteConditionFragment(discriminator, scope)];
|
|
816
939
|
}
|
|
817
|
-
if (
|
|
940
|
+
if (nodes.isNode(discriminator, "fieldDiscriminatorNode")) {
|
|
818
941
|
return [getFieldConditionFragment(discriminator, scope)];
|
|
819
942
|
}
|
|
820
943
|
return [];
|
|
821
944
|
}),
|
|
822
945
|
(c) => c.join(" && ")
|
|
823
946
|
),
|
|
824
|
-
(f) =>
|
|
947
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `if (${c}) { ${scope.ifTrue} }`)
|
|
825
948
|
);
|
|
826
949
|
}
|
|
827
950
|
function getSizeConditionFragment(discriminator, scope) {
|
|
@@ -830,7 +953,7 @@ function getSizeConditionFragment(discriminator, scope) {
|
|
|
830
953
|
}
|
|
831
954
|
function getByteConditionFragment(discriminator, scope) {
|
|
832
955
|
const { dataName, typeManifestVisitor } = scope;
|
|
833
|
-
const constant =
|
|
956
|
+
const constant = visitorsCore.visit(discriminator.constant, typeManifestVisitor).value;
|
|
834
957
|
return fragment`${use("containsBytes", "solanaCodecsCore")}(${dataName}, ${constant}, ${discriminator.offset})`;
|
|
835
958
|
}
|
|
836
959
|
function getFieldConditionFragment(discriminator, scope) {
|
|
@@ -840,17 +963,17 @@ function getFieldConditionFragment(discriminator, scope) {
|
|
|
840
963
|
`Field discriminator "${discriminator.name}" does not have a matching argument with default value.`
|
|
841
964
|
);
|
|
842
965
|
}
|
|
843
|
-
if (
|
|
844
|
-
const base64Bytes =
|
|
966
|
+
if (nodes.isNode(field.type, "arrayTypeNode") && nodes.isNode(field.type.item, "numberTypeNode") && field.type.item.format === "u8" && nodes.isNode(field.type.count, "fixedCountNode") && nodes.isNode(field.defaultValue, "arrayValueNode") && field.defaultValue.items.every(nodes.isNodeFilter("numberValueNode"))) {
|
|
967
|
+
const base64Bytes = codecsStrings.getBase64Decoder().decode(
|
|
845
968
|
new Uint8Array(field.defaultValue.items.map((node) => node.number))
|
|
846
969
|
);
|
|
847
970
|
return getByteConditionFragment(
|
|
848
|
-
|
|
971
|
+
nodes.constantDiscriminatorNode(nodes.constantValueNodeFromBytes("base64", base64Bytes), discriminator.offset),
|
|
849
972
|
scope
|
|
850
973
|
);
|
|
851
974
|
}
|
|
852
975
|
return getByteConditionFragment(
|
|
853
|
-
|
|
976
|
+
nodes.constantDiscriminatorNode(nodes.constantValueNode(field.type, field.defaultValue), discriminator.offset),
|
|
854
977
|
scope
|
|
855
978
|
);
|
|
856
979
|
}
|
|
@@ -930,19 +1053,13 @@ function getIsErrorFunctionFragment(scope) {
|
|
|
930
1053
|
return ${use("isProgramError", "solanaPrograms")}<TProgramErrorCode>(error, transactionMessage, ${programAddressConstant}, code);
|
|
931
1054
|
}`;
|
|
932
1055
|
}
|
|
933
|
-
|
|
934
|
-
// src/fragments/indexPage.ts
|
|
935
|
-
var import_nodes11 = require("@codama/nodes");
|
|
936
1056
|
function getIndexPageFragment(items) {
|
|
937
1057
|
if (items.length === 0) return;
|
|
938
|
-
const names = items.map((item) =>
|
|
1058
|
+
const names = items.map((item) => nodes.camelCase(item.name)).sort((a, b) => a.localeCompare(b)).map((name) => getExportAllFragment(`./${name}`));
|
|
939
1059
|
return mergeFragments(names, (cs) => cs.join("\n"));
|
|
940
1060
|
}
|
|
941
|
-
|
|
942
|
-
// src/fragments/instructionAccountMeta.ts
|
|
943
|
-
var import_nodes12 = require("@codama/nodes");
|
|
944
1061
|
function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
945
|
-
const typeParam = `TAccount${
|
|
1062
|
+
const typeParam = `TAccount${nodes.pascalCase(instructionAccountNode.name)}`;
|
|
946
1063
|
if (instructionAccountNode.isSigner === true && instructionAccountNode.isWritable) {
|
|
947
1064
|
return fragment`${use("type WritableSignerAccount", "solanaInstructions")}<${typeParam}> & ${use("type AccountSignerMeta", "solanaSigners")}<${typeParam}>`;
|
|
948
1065
|
}
|
|
@@ -954,17 +1071,13 @@ function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
|
954
1071
|
}
|
|
955
1072
|
return fragment`${use("type ReadonlyAccount", "solanaInstructions")}<${typeParam}>`;
|
|
956
1073
|
}
|
|
957
|
-
|
|
958
|
-
// src/fragments/instructionAccountTypeParam.ts
|
|
959
|
-
var import_nodes13 = require("@codama/nodes");
|
|
960
|
-
var import_visitors_core9 = require("@codama/visitors-core");
|
|
961
1074
|
function getInstructionAccountTypeParamFragment(scope) {
|
|
962
|
-
const { instructionAccountPath,
|
|
963
|
-
const instructionAccountNode =
|
|
964
|
-
const instructionNode =
|
|
965
|
-
const programNode =
|
|
966
|
-
const typeParam = `TAccount${
|
|
967
|
-
const accountMeta =
|
|
1075
|
+
const { instructionAccountPath, linkables } = scope;
|
|
1076
|
+
const instructionAccountNode = visitorsCore.getLastNodeFromPath(instructionAccountPath);
|
|
1077
|
+
const instructionNode = visitorsCore.findInstructionNodeFromPath(instructionAccountPath);
|
|
1078
|
+
const programNode = visitorsCore.findProgramNodeFromPath(instructionAccountPath);
|
|
1079
|
+
const typeParam = `TAccount${nodes.pascalCase(instructionAccountNode.name)}`;
|
|
1080
|
+
const accountMeta = fragment` | ${use("type AccountMeta", "solanaInstructions")}<string>` ;
|
|
968
1081
|
if (instructionNode.optionalAccountStrategy === "omitted" && instructionAccountNode.isOptional) {
|
|
969
1082
|
return fragment`${typeParam} extends string${accountMeta} | undefined = undefined`;
|
|
970
1083
|
}
|
|
@@ -984,13 +1097,8 @@ function getDefaultAddress(defaultValue, programId, linkables) {
|
|
|
984
1097
|
return "string";
|
|
985
1098
|
}
|
|
986
1099
|
}
|
|
987
|
-
|
|
988
|
-
// src/fragments/instructionByteDelta.ts
|
|
989
|
-
var import_nodes14 = require("@codama/nodes");
|
|
990
|
-
var import_renderers_core3 = require("@codama/renderers-core");
|
|
991
|
-
var import_visitors_core10 = require("@codama/visitors-core");
|
|
992
1100
|
function getInstructionByteDeltaFragment(scope) {
|
|
993
|
-
const { byteDeltas } =
|
|
1101
|
+
const { byteDeltas } = visitorsCore.getLastNodeFromPath(scope.instructionPath);
|
|
994
1102
|
const fragments = (byteDeltas ?? []).flatMap((c) => getByteDeltaFragment(c, scope));
|
|
995
1103
|
if (fragments.length === 0) return;
|
|
996
1104
|
return mergeFragments(
|
|
@@ -1001,16 +1109,16 @@ const byteDelta: number = [${c.join(",")}].reduce((a, b) => a + b, 0);`
|
|
|
1001
1109
|
}
|
|
1002
1110
|
function getByteDeltaFragment(byteDelta, scope) {
|
|
1003
1111
|
let bytesFragment = (() => {
|
|
1004
|
-
if (
|
|
1112
|
+
if (nodes.isNode(byteDelta.value, "numberValueNode")) {
|
|
1005
1113
|
return getNumberValueNodeFragment(byteDelta);
|
|
1006
1114
|
}
|
|
1007
|
-
if (
|
|
1115
|
+
if (nodes.isNode(byteDelta.value, "argumentValueNode")) {
|
|
1008
1116
|
return getArgumentValueNodeFragment(byteDelta);
|
|
1009
1117
|
}
|
|
1010
|
-
if (
|
|
1118
|
+
if (nodes.isNode(byteDelta.value, "accountLinkNode")) {
|
|
1011
1119
|
return getAccountLinkNodeFragment(byteDelta, scope);
|
|
1012
1120
|
}
|
|
1013
|
-
if (
|
|
1121
|
+
if (nodes.isNode(byteDelta.value, "resolverValueNode")) {
|
|
1014
1122
|
return getResolverValueNodeFragment(byteDelta, scope);
|
|
1015
1123
|
}
|
|
1016
1124
|
return null;
|
|
@@ -1020,21 +1128,21 @@ function getByteDeltaFragment(byteDelta, scope) {
|
|
|
1020
1128
|
bytesFragment = fragment`${bytesFragment} + ${use("BASE_ACCOUNT_SIZE", "solanaAccounts")}`;
|
|
1021
1129
|
}
|
|
1022
1130
|
if (byteDelta.subtract) {
|
|
1023
|
-
bytesFragment =
|
|
1131
|
+
bytesFragment = visitorsCore.pipe(bytesFragment, (f) => renderersCore.mapFragmentContent(f, (c) => `- (${c})`));
|
|
1024
1132
|
}
|
|
1025
1133
|
return [bytesFragment];
|
|
1026
1134
|
}
|
|
1027
1135
|
function getNumberValueNodeFragment(byteDelta) {
|
|
1028
|
-
|
|
1136
|
+
nodes.assertIsNode(byteDelta.value, "numberValueNode");
|
|
1029
1137
|
return fragment`${byteDelta.value.number}`;
|
|
1030
1138
|
}
|
|
1031
1139
|
function getArgumentValueNodeFragment(byteDelta) {
|
|
1032
|
-
|
|
1033
|
-
const argumentName =
|
|
1140
|
+
nodes.assertIsNode(byteDelta.value, "argumentValueNode");
|
|
1141
|
+
const argumentName = nodes.camelCase(byteDelta.value.name);
|
|
1034
1142
|
return fragment`Number(args.${argumentName})`;
|
|
1035
1143
|
}
|
|
1036
1144
|
function getAccountLinkNodeFragment(byteDelta, scope) {
|
|
1037
|
-
|
|
1145
|
+
nodes.assertIsNode(byteDelta.value, "accountLinkNode");
|
|
1038
1146
|
const functionName = use(
|
|
1039
1147
|
scope.nameApi.accountGetSizeFunction(byteDelta.value.name),
|
|
1040
1148
|
scope.getImportFrom(byteDelta.value)
|
|
@@ -1042,7 +1150,7 @@ function getAccountLinkNodeFragment(byteDelta, scope) {
|
|
|
1042
1150
|
return fragment`${functionName}()`;
|
|
1043
1151
|
}
|
|
1044
1152
|
function getResolverValueNodeFragment(byteDelta, scope) {
|
|
1045
|
-
|
|
1153
|
+
nodes.assertIsNode(byteDelta.value, "resolverValueNode");
|
|
1046
1154
|
const isAsync = scope.asyncResolvers.includes(byteDelta.value.name);
|
|
1047
1155
|
if (!scope.useAsync && isAsync) return null;
|
|
1048
1156
|
const awaitKeyword = scope.useAsync && isAsync ? "await " : "";
|
|
@@ -1050,55 +1158,32 @@ function getResolverValueNodeFragment(byteDelta, scope) {
|
|
|
1050
1158
|
scope.nameApi.resolverFunction(byteDelta.value.name),
|
|
1051
1159
|
scope.getImportFrom(byteDelta.value)
|
|
1052
1160
|
);
|
|
1053
|
-
return
|
|
1161
|
+
return visitorsCore.pipe(
|
|
1054
1162
|
fragment`${awaitKeyword}${functionName}(resolverScope)`,
|
|
1055
1163
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1056
1164
|
);
|
|
1057
1165
|
}
|
|
1058
|
-
|
|
1059
|
-
// src/fragments/instructionData.ts
|
|
1060
|
-
var import_nodes15 = require("@codama/nodes");
|
|
1061
|
-
var import_visitors_core11 = require("@codama/visitors-core");
|
|
1062
1166
|
function getInstructionDataFragment(scope) {
|
|
1063
1167
|
const { instructionPath, dataArgsManifest, nameApi, customInstructionData } = scope;
|
|
1064
|
-
const instructionNode =
|
|
1168
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1065
1169
|
if (instructionNode.arguments.length === 0 || customInstructionData.has(instructionNode.name)) return;
|
|
1066
1170
|
const instructionDataName = nameApi.instructionDataType(instructionNode.name);
|
|
1067
1171
|
return getTypeWithCodecFragment({
|
|
1068
1172
|
manifest: dataArgsManifest,
|
|
1069
1173
|
name: instructionDataName,
|
|
1070
1174
|
nameApi,
|
|
1071
|
-
node:
|
|
1175
|
+
node: nodes.structTypeNodeFromInstructionArgumentNodes(instructionNode.arguments),
|
|
1072
1176
|
size: scope.size
|
|
1073
1177
|
});
|
|
1074
1178
|
}
|
|
1075
|
-
|
|
1076
|
-
// src/fragments/instructionExtraArgs.ts
|
|
1077
|
-
var import_renderers_core4 = require("@codama/renderers-core");
|
|
1078
|
-
var import_visitors_core12 = require("@codama/visitors-core");
|
|
1079
1179
|
function getInstructionExtraArgsFragment(scope) {
|
|
1080
1180
|
const { instructionPath, extraArgsManifest, nameApi } = scope;
|
|
1081
|
-
const instructionNode =
|
|
1181
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1082
1182
|
if ((instructionNode.extraArguments ?? []).length === 0) return;
|
|
1083
1183
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
1084
1184
|
const looseName = nameApi.dataArgsType(instructionExtraName);
|
|
1085
|
-
return
|
|
1185
|
+
return renderersCore.mapFragmentContent(extraArgsManifest.looseType, (c) => `export type ${looseName} = ${c};`);
|
|
1086
1186
|
}
|
|
1087
|
-
|
|
1088
|
-
// src/fragments/instructionFunction.ts
|
|
1089
|
-
var import_nodes20 = require("@codama/nodes");
|
|
1090
|
-
var import_renderers_core8 = require("@codama/renderers-core");
|
|
1091
|
-
var import_visitors_core17 = require("@codama/visitors-core");
|
|
1092
|
-
|
|
1093
|
-
// src/fragments/instructionInputResolved.ts
|
|
1094
|
-
var import_nodes17 = require("@codama/nodes");
|
|
1095
|
-
var import_renderers_core6 = require("@codama/renderers-core");
|
|
1096
|
-
var import_visitors_core14 = require("@codama/visitors-core");
|
|
1097
|
-
|
|
1098
|
-
// src/fragments/instructionInputDefault.ts
|
|
1099
|
-
var import_nodes16 = require("@codama/nodes");
|
|
1100
|
-
var import_renderers_core5 = require("@codama/renderers-core");
|
|
1101
|
-
var import_visitors_core13 = require("@codama/visitors-core");
|
|
1102
1187
|
function getInstructionInputDefaultFragment(scope) {
|
|
1103
1188
|
const { input, optionalAccountStrategy, asyncResolvers, useAsync, nameApi, typeManifestVisitor, getImportFrom } = scope;
|
|
1104
1189
|
if (!input.defaultValue) {
|
|
@@ -1109,8 +1194,8 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1109
1194
|
}
|
|
1110
1195
|
const { defaultValue } = input;
|
|
1111
1196
|
const defaultFragment = (renderedValue, isWritable) => {
|
|
1112
|
-
const inputName =
|
|
1113
|
-
if (input.kind === "instructionAccountNode" &&
|
|
1197
|
+
const inputName = nodes.camelCase(input.name);
|
|
1198
|
+
if (input.kind === "instructionAccountNode" && nodes.isNode(defaultValue, "resolverValueNode")) {
|
|
1114
1199
|
return fragment`accounts.${inputName} = { ...accounts.${inputName}, ...${renderedValue} };`;
|
|
1115
1200
|
}
|
|
1116
1201
|
if (input.kind === "instructionAccountNode" && isWritable === void 0) {
|
|
@@ -1128,7 +1213,7 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1128
1213
|
const addressType = use("type Address", "solanaAddresses");
|
|
1129
1214
|
switch (defaultValue.kind) {
|
|
1130
1215
|
case "accountValueNode":
|
|
1131
|
-
const name =
|
|
1216
|
+
const name = nodes.camelCase(defaultValue.name);
|
|
1132
1217
|
if (input.kind === "instructionAccountNode" && input.resolvedIsSigner && !input.isSigner) {
|
|
1133
1218
|
return defaultFragment(fragment`${expectTransactionSigner}(accounts.${name}.value).address`);
|
|
1134
1219
|
}
|
|
@@ -1138,13 +1223,13 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1138
1223
|
return defaultFragment(fragment`${expectAddress}(accounts.${name}.value)`);
|
|
1139
1224
|
case "pdaValueNode":
|
|
1140
1225
|
let pdaProgramValue;
|
|
1141
|
-
if (
|
|
1142
|
-
pdaProgramValue = fragment`${expectAddress}(accounts.${
|
|
1226
|
+
if (nodes.isNode(defaultValue.programId, "accountValueNode")) {
|
|
1227
|
+
pdaProgramValue = fragment`${expectAddress}(accounts.${nodes.camelCase(defaultValue.programId.name)}.value)`;
|
|
1143
1228
|
}
|
|
1144
|
-
if (
|
|
1145
|
-
pdaProgramValue = fragment`${expectAddress}(args.${
|
|
1229
|
+
if (nodes.isNode(defaultValue.programId, "argumentValueNode")) {
|
|
1230
|
+
pdaProgramValue = fragment`${expectAddress}(args.${nodes.camelCase(defaultValue.programId.name)})`;
|
|
1146
1231
|
}
|
|
1147
|
-
if (
|
|
1232
|
+
if (nodes.isNode(defaultValue.pda, "pdaNode")) {
|
|
1148
1233
|
let pdaProgram = fragment`programAddress`;
|
|
1149
1234
|
if (pdaProgramValue) {
|
|
1150
1235
|
pdaProgram = pdaProgramValue;
|
|
@@ -1152,29 +1237,29 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1152
1237
|
pdaProgram = fragment`'${defaultValue.pda.programId}' as ${addressType}<'${defaultValue.pda.programId}'>`;
|
|
1153
1238
|
}
|
|
1154
1239
|
const pdaSeeds2 = defaultValue.pda.seeds.flatMap((seed) => {
|
|
1155
|
-
if (
|
|
1240
|
+
if (nodes.isNode(seed, "constantPdaSeedNode") && nodes.isNode(seed.value, "programIdValueNode")) {
|
|
1156
1241
|
return [fragment`${use("getAddressEncoder", "solanaAddresses")}().encode(${pdaProgram})`];
|
|
1157
1242
|
}
|
|
1158
|
-
if (
|
|
1159
|
-
const typeManifest2 =
|
|
1160
|
-
const valueManifest2 =
|
|
1243
|
+
if (nodes.isNode(seed, "constantPdaSeedNode") && !nodes.isNode(seed.value, "programIdValueNode")) {
|
|
1244
|
+
const typeManifest2 = visitorsCore.visit(seed.type, typeManifestVisitor);
|
|
1245
|
+
const valueManifest2 = visitorsCore.visit(seed.value, typeManifestVisitor);
|
|
1161
1246
|
return [fragment`${typeManifest2.encoder}.encode(${valueManifest2.value})`];
|
|
1162
1247
|
}
|
|
1163
|
-
if (
|
|
1164
|
-
const typeManifest2 =
|
|
1248
|
+
if (nodes.isNode(seed, "variablePdaSeedNode")) {
|
|
1249
|
+
const typeManifest2 = visitorsCore.visit(seed.type, typeManifestVisitor);
|
|
1165
1250
|
const valueSeed = defaultValue.seeds.find((s) => s.name === seed.name)?.value;
|
|
1166
1251
|
if (!valueSeed) return [];
|
|
1167
|
-
if (
|
|
1252
|
+
if (nodes.isNode(valueSeed, "accountValueNode")) {
|
|
1168
1253
|
return [
|
|
1169
|
-
fragment`${typeManifest2.encoder}.encode(${expectAddress}(accounts.${
|
|
1254
|
+
fragment`${typeManifest2.encoder}.encode(${expectAddress}(accounts.${nodes.camelCase(valueSeed.name)}.value))`
|
|
1170
1255
|
];
|
|
1171
1256
|
}
|
|
1172
|
-
if (
|
|
1257
|
+
if (nodes.isNode(valueSeed, "argumentValueNode")) {
|
|
1173
1258
|
return [
|
|
1174
|
-
fragment`${typeManifest2.encoder}.encode(${expectSome}(args.${
|
|
1259
|
+
fragment`${typeManifest2.encoder}.encode(${expectSome}(args.${nodes.camelCase(valueSeed.name)}))`
|
|
1175
1260
|
];
|
|
1176
1261
|
}
|
|
1177
|
-
const valueManifest2 =
|
|
1262
|
+
const valueManifest2 = visitorsCore.visit(valueSeed, typeManifestVisitor);
|
|
1178
1263
|
return [fragment`${typeManifest2.encoder}.encode(${valueManifest2.value})`];
|
|
1179
1264
|
}
|
|
1180
1265
|
return [];
|
|
@@ -1189,20 +1274,20 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1189
1274
|
const pdaFunction = use(nameApi.pdaFindFunction(defaultValue.pda.name), getImportFrom(defaultValue.pda));
|
|
1190
1275
|
const pdaArgs = [];
|
|
1191
1276
|
const pdaSeeds = defaultValue.seeds.map((seed) => {
|
|
1192
|
-
if (
|
|
1193
|
-
return fragment`${seed.name}: ${expectAddress}(accounts.${
|
|
1277
|
+
if (nodes.isNode(seed.value, "accountValueNode")) {
|
|
1278
|
+
return fragment`${seed.name}: ${expectAddress}(accounts.${nodes.camelCase(seed.value.name)}.value)`;
|
|
1194
1279
|
}
|
|
1195
|
-
if (
|
|
1196
|
-
return fragment`${seed.name}: ${expectSome}(args.${
|
|
1280
|
+
if (nodes.isNode(seed.value, "argumentValueNode")) {
|
|
1281
|
+
return fragment`${seed.name}: ${expectSome}(args.${nodes.camelCase(seed.value.name)})`;
|
|
1197
1282
|
}
|
|
1198
|
-
return
|
|
1199
|
-
|
|
1200
|
-
(f) =>
|
|
1283
|
+
return visitorsCore.pipe(
|
|
1284
|
+
visitorsCore.visit(seed.value, typeManifestVisitor).value,
|
|
1285
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `${seed.name}: ${c}`)
|
|
1201
1286
|
);
|
|
1202
1287
|
});
|
|
1203
|
-
const pdaSeedsFragment =
|
|
1288
|
+
const pdaSeedsFragment = visitorsCore.pipe(
|
|
1204
1289
|
mergeFragments(pdaSeeds, (renders) => renders.join(", ")),
|
|
1205
|
-
(f) =>
|
|
1290
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `{ ${c} }`)
|
|
1206
1291
|
);
|
|
1207
1292
|
if (pdaSeeds.length > 0) {
|
|
1208
1293
|
pdaArgs.push(pdaSeedsFragment);
|
|
@@ -1228,14 +1313,14 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1228
1313
|
return fragment``;
|
|
1229
1314
|
case "accountBumpValueNode":
|
|
1230
1315
|
return defaultFragment(
|
|
1231
|
-
fragment`${expectProgramDerivedAddress}(accounts.${
|
|
1316
|
+
fragment`${expectProgramDerivedAddress}(accounts.${nodes.camelCase(defaultValue.name)}.value)[1]`
|
|
1232
1317
|
);
|
|
1233
1318
|
case "argumentValueNode":
|
|
1234
|
-
return defaultFragment(fragment`${expectSome}(args.${
|
|
1319
|
+
return defaultFragment(fragment`${expectSome}(args.${nodes.camelCase(defaultValue.name)})`);
|
|
1235
1320
|
case "resolverValueNode":
|
|
1236
1321
|
const resolverFunction = use(nameApi.resolverFunction(defaultValue.name), getImportFrom(defaultValue));
|
|
1237
1322
|
const resolverAwait = useAsync && asyncResolvers.includes(defaultValue.name) ? "await " : "";
|
|
1238
|
-
return
|
|
1323
|
+
return visitorsCore.pipe(
|
|
1239
1324
|
defaultFragment(fragment`${resolverAwait}${resolverFunction}(resolverScope)`),
|
|
1240
1325
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1241
1326
|
);
|
|
@@ -1260,21 +1345,21 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1260
1345
|
}
|
|
1261
1346
|
const negatedCondition = !ifTrueRenderer;
|
|
1262
1347
|
let condition = "true";
|
|
1263
|
-
if (
|
|
1348
|
+
if (nodes.isNode(defaultValue.condition, "resolverValueNode")) {
|
|
1264
1349
|
const conditionalResolverFunction = nameApi.resolverFunction(defaultValue.condition.name);
|
|
1265
|
-
const
|
|
1266
|
-
conditionalFragment =
|
|
1350
|
+
const module = getImportFrom(defaultValue.condition);
|
|
1351
|
+
conditionalFragment = visitorsCore.pipe(
|
|
1267
1352
|
conditionalFragment,
|
|
1268
|
-
(f) => addFragmentImports(f,
|
|
1353
|
+
(f) => addFragmentImports(f, module, [conditionalResolverFunction]),
|
|
1269
1354
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1270
1355
|
);
|
|
1271
1356
|
const conditionalResolverAwait = useAsync && asyncResolvers.includes(defaultValue.condition.name) ? "await " : "";
|
|
1272
1357
|
condition = `${conditionalResolverAwait}${conditionalResolverFunction}(resolverScope)`;
|
|
1273
1358
|
condition = negatedCondition ? `!${condition}` : condition;
|
|
1274
1359
|
} else {
|
|
1275
|
-
const comparedInputName =
|
|
1360
|
+
const comparedInputName = nodes.isNode(defaultValue.condition, "accountValueNode") ? `accounts.${nodes.camelCase(defaultValue.condition.name)}.value` : `args.${nodes.camelCase(defaultValue.condition.name)}`;
|
|
1276
1361
|
if (defaultValue.value) {
|
|
1277
|
-
const comparedValue =
|
|
1362
|
+
const comparedValue = visitorsCore.visit(defaultValue.value, typeManifestVisitor).value;
|
|
1278
1363
|
conditionalFragment = mergeFragments([conditionalFragment, comparedValue], (c) => c[0]);
|
|
1279
1364
|
const operator = negatedCondition ? "!==" : "===";
|
|
1280
1365
|
condition = `${comparedInputName} ${operator} ${comparedValue.content}`;
|
|
@@ -1283,7 +1368,7 @@ function getInstructionInputDefaultFragment(scope) {
|
|
|
1283
1368
|
}
|
|
1284
1369
|
}
|
|
1285
1370
|
if (ifTrueRenderer && ifFalseRenderer) {
|
|
1286
|
-
return
|
|
1371
|
+
return renderersCore.setFragmentContent(
|
|
1287
1372
|
conditionalFragment,
|
|
1288
1373
|
`if (${condition}) {
|
|
1289
1374
|
${ifTrueRenderer.content}
|
|
@@ -1292,14 +1377,14 @@ ${ifFalseRenderer.content}
|
|
|
1292
1377
|
}`
|
|
1293
1378
|
);
|
|
1294
1379
|
}
|
|
1295
|
-
return
|
|
1380
|
+
return renderersCore.setFragmentContent(
|
|
1296
1381
|
conditionalFragment,
|
|
1297
1382
|
`if (${condition}) {
|
|
1298
1383
|
${ifTrueRenderer ? ifTrueRenderer.content : ifFalseRenderer?.content}
|
|
1299
1384
|
}`
|
|
1300
1385
|
);
|
|
1301
1386
|
default:
|
|
1302
|
-
const valueManifest =
|
|
1387
|
+
const valueManifest = visitorsCore.visit(defaultValue, typeManifestVisitor).value;
|
|
1303
1388
|
return defaultFragment(valueManifest);
|
|
1304
1389
|
}
|
|
1305
1390
|
}
|
|
@@ -1314,19 +1399,19 @@ function renderNestedInstructionDefault(scope) {
|
|
|
1314
1399
|
|
|
1315
1400
|
// src/fragments/instructionInputResolved.ts
|
|
1316
1401
|
function getInstructionInputResolvedFragment(scope) {
|
|
1317
|
-
const instructionNode =
|
|
1402
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(scope.instructionPath);
|
|
1318
1403
|
const resolvedInputFragments = scope.resolvedInputs.flatMap((input) => {
|
|
1319
1404
|
const inputFragment = getInstructionInputDefaultFragment({
|
|
1320
1405
|
...scope,
|
|
1321
1406
|
input,
|
|
1322
|
-
optionalAccountStrategy:
|
|
1407
|
+
optionalAccountStrategy: nodes.parseOptionalAccountStrategy(instructionNode.optionalAccountStrategy)
|
|
1323
1408
|
});
|
|
1324
1409
|
if (!inputFragment.content) return [];
|
|
1325
|
-
const camelName =
|
|
1410
|
+
const camelName = nodes.camelCase(input.name);
|
|
1326
1411
|
return [
|
|
1327
|
-
|
|
1412
|
+
renderersCore.mapFragmentContent(
|
|
1328
1413
|
inputFragment,
|
|
1329
|
-
(c) =>
|
|
1414
|
+
(c) => nodes.isNode(input, "instructionArgumentNode") ? `if (!args.${camelName}) {
|
|
1330
1415
|
${c}
|
|
1331
1416
|
}` : `if (!accounts.${camelName}.value) {
|
|
1332
1417
|
${c}
|
|
@@ -1339,19 +1424,14 @@ ${c}
|
|
|
1339
1424
|
}
|
|
1340
1425
|
return mergeFragments([fragment`// Resolve default values.`, ...resolvedInputFragments], (c) => c.join("\n"));
|
|
1341
1426
|
}
|
|
1342
|
-
|
|
1343
|
-
// src/fragments/instructionInputType.ts
|
|
1344
|
-
var import_nodes18 = require("@codama/nodes");
|
|
1345
|
-
var import_renderers_core7 = require("@codama/renderers-core");
|
|
1346
|
-
var import_visitors_core15 = require("@codama/visitors-core");
|
|
1347
1427
|
function getInstructionInputTypeFragment(scope) {
|
|
1348
1428
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
1349
|
-
const instructionNode =
|
|
1429
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1350
1430
|
const instructionInputType = useAsync ? nameApi.instructionAsyncInputType(instructionNode.name) : nameApi.instructionSyncInputType(instructionNode.name);
|
|
1351
1431
|
const [dataArgumentsFragment, customDataArgumentsFragment] = getDataArgumentsFragments(scope);
|
|
1352
1432
|
let accountTypeParams = "";
|
|
1353
1433
|
if (instructionNode.accounts.length > 0) {
|
|
1354
|
-
accountTypeParams = instructionNode.accounts.map((account) => `TAccount${
|
|
1434
|
+
accountTypeParams = instructionNode.accounts.map((account) => `TAccount${nodes.pascalCase(account.name)} extends string = string`).join(", ");
|
|
1355
1435
|
accountTypeParams = `<${accountTypeParams}>`;
|
|
1356
1436
|
}
|
|
1357
1437
|
const typeBodyFragment = mergeFragments(
|
|
@@ -1369,20 +1449,20 @@ function getInstructionInputTypeFragment(scope) {
|
|
|
1369
1449
|
}
|
|
1370
1450
|
function getAccountsFragment(scope) {
|
|
1371
1451
|
const { instructionPath, resolvedInputs, useAsync, asyncResolvers } = scope;
|
|
1372
|
-
const instructionNode =
|
|
1452
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1373
1453
|
const fragments = instructionNode.accounts.map((account) => {
|
|
1374
1454
|
const resolvedAccount = resolvedInputs.find(
|
|
1375
1455
|
(input) => input.kind === "instructionAccountNode" && input.name === account.name
|
|
1376
1456
|
);
|
|
1377
|
-
const hasDefaultValue = !!resolvedAccount.defaultValue && !
|
|
1457
|
+
const hasDefaultValue = !!resolvedAccount.defaultValue && !nodes.isNode(resolvedAccount.defaultValue, ["identityValueNode", "payerValueNode"]) && (useAsync || !isAsyncDefaultValue(resolvedAccount.defaultValue, asyncResolvers));
|
|
1378
1458
|
const docs = getDocblockFragment(account.docs ?? [], true);
|
|
1379
1459
|
const optionalSign = hasDefaultValue || resolvedAccount.isOptional ? "?" : "";
|
|
1380
|
-
return fragment`${docs}${
|
|
1460
|
+
return fragment`${docs}${nodes.camelCase(account.name)}${optionalSign}: ${getAccountTypeFragment2(resolvedAccount)};`;
|
|
1381
1461
|
});
|
|
1382
1462
|
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1383
1463
|
}
|
|
1384
1464
|
function getAccountTypeFragment2(account) {
|
|
1385
|
-
const typeParam = `TAccount${
|
|
1465
|
+
const typeParam = `TAccount${nodes.pascalCase(account.name)}`;
|
|
1386
1466
|
const address = use("type Address", "solanaAddresses");
|
|
1387
1467
|
const signer = use("type TransactionSigner", "solanaSigners");
|
|
1388
1468
|
const pda = use("type ProgramDerivedAddress", "solanaAddresses");
|
|
@@ -1394,15 +1474,15 @@ function getAccountTypeFragment2(account) {
|
|
|
1394
1474
|
}
|
|
1395
1475
|
function getDataArgumentsFragments(scope) {
|
|
1396
1476
|
const { instructionPath, nameApi } = scope;
|
|
1397
|
-
const instructionNode =
|
|
1477
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1398
1478
|
const customData = scope.customInstructionData.get(instructionNode.name);
|
|
1399
1479
|
if (customData) {
|
|
1400
1480
|
return [
|
|
1401
1481
|
void 0,
|
|
1402
|
-
|
|
1482
|
+
visitorsCore.pipe(
|
|
1403
1483
|
fragment`${nameApi.dataArgsType(customData.importAs)}`,
|
|
1404
1484
|
(f) => mergeFragmentImports(f, [scope.dataArgsManifest.looseType.imports]),
|
|
1405
|
-
(f) =>
|
|
1485
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `${c} & `)
|
|
1406
1486
|
)
|
|
1407
1487
|
];
|
|
1408
1488
|
}
|
|
@@ -1416,7 +1496,7 @@ function getDataArgumentsFragments(scope) {
|
|
|
1416
1496
|
}
|
|
1417
1497
|
function getExtraArgumentsFragment(scope) {
|
|
1418
1498
|
const { instructionPath, nameApi } = scope;
|
|
1419
|
-
const instructionNode =
|
|
1499
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1420
1500
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
1421
1501
|
const extraArgsType = nameApi.dataArgsType(instructionExtraName);
|
|
1422
1502
|
const fragments = (instructionNode.extraArguments ?? []).flatMap((arg) => {
|
|
@@ -1428,18 +1508,18 @@ function getExtraArgumentsFragment(scope) {
|
|
|
1428
1508
|
}
|
|
1429
1509
|
function getArgumentFragment(arg, argsType, resolvedInputs, renamedArgs) {
|
|
1430
1510
|
const resolvedArg = resolvedInputs.find(
|
|
1431
|
-
(input) =>
|
|
1511
|
+
(input) => nodes.isNode(input, "instructionArgumentNode") && input.name === arg.name
|
|
1432
1512
|
);
|
|
1433
1513
|
if (arg.defaultValue && arg.defaultValueStrategy === "omitted") return null;
|
|
1434
1514
|
const renamedName = renamedArgs.get(arg.name) ?? arg.name;
|
|
1435
1515
|
const optionalSign = arg.defaultValue || resolvedArg?.defaultValue ? "?" : "";
|
|
1436
|
-
return fragment`${
|
|
1516
|
+
return fragment`${nodes.camelCase(renamedName)}${optionalSign}: ${argsType}["${nodes.camelCase(arg.name)}"];`;
|
|
1437
1517
|
}
|
|
1438
1518
|
function getRemainingAccountsFragment(instructionNode) {
|
|
1439
1519
|
const fragments = (instructionNode.remainingAccounts ?? []).flatMap((remainingAccountsNode) => {
|
|
1440
|
-
if (
|
|
1520
|
+
if (nodes.isNode(remainingAccountsNode.value, "resolverValueNode")) return [];
|
|
1441
1521
|
const { name } = remainingAccountsNode.value;
|
|
1442
|
-
const allArguments =
|
|
1522
|
+
const allArguments = nodes.getAllInstructionArguments(instructionNode);
|
|
1443
1523
|
const argumentExists = allArguments.some((arg) => arg.name === name);
|
|
1444
1524
|
if (argumentExists) return [];
|
|
1445
1525
|
const isSigner = remainingAccountsNode.isSigner ?? false;
|
|
@@ -1450,20 +1530,16 @@ function getRemainingAccountsFragment(instructionNode) {
|
|
|
1450
1530
|
if (isSigner === "either") return fragment`${signerFragment} | ${addressFragment}`;
|
|
1451
1531
|
return isSigner ? signerFragment : addressFragment;
|
|
1452
1532
|
})();
|
|
1453
|
-
return fragment`${
|
|
1533
|
+
return fragment`${nodes.camelCase(name)}${optionalSign}: Array<${typeFragment}>;`;
|
|
1454
1534
|
});
|
|
1455
1535
|
if (fragments.length === 0) return;
|
|
1456
1536
|
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1457
1537
|
}
|
|
1458
|
-
|
|
1459
|
-
// src/fragments/instructionRemainingAccounts.ts
|
|
1460
|
-
var import_nodes19 = require("@codama/nodes");
|
|
1461
|
-
var import_visitors_core16 = require("@codama/visitors-core");
|
|
1462
1538
|
function getInstructionRemainingAccountsFragment(scope) {
|
|
1463
|
-
const { remainingAccounts } =
|
|
1539
|
+
const { remainingAccounts } = visitorsCore.getLastNodeFromPath(scope.instructionPath);
|
|
1464
1540
|
const fragments = (remainingAccounts ?? []).flatMap((a) => getRemainingAccountsFragment2(a, scope));
|
|
1465
1541
|
if (fragments.length === 0) return;
|
|
1466
|
-
return
|
|
1542
|
+
return visitorsCore.pipe(
|
|
1467
1543
|
mergeFragments(
|
|
1468
1544
|
fragments,
|
|
1469
1545
|
(c) => `// Remaining accounts.
|
|
@@ -1474,10 +1550,10 @@ const remainingAccounts: AccountMeta[] = ${c.length === 1 ? c[0] : `[...${c.join
|
|
|
1474
1550
|
}
|
|
1475
1551
|
function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
1476
1552
|
const remainingAccountsFragment = (() => {
|
|
1477
|
-
if (
|
|
1553
|
+
if (nodes.isNode(remainingAccounts.value, "argumentValueNode")) {
|
|
1478
1554
|
return getArgumentValueNodeFragment2(remainingAccounts, scope);
|
|
1479
1555
|
}
|
|
1480
|
-
if (
|
|
1556
|
+
if (nodes.isNode(remainingAccounts.value, "resolverValueNode")) {
|
|
1481
1557
|
return getResolverValueNodeFragment2(remainingAccounts, scope);
|
|
1482
1558
|
}
|
|
1483
1559
|
return null;
|
|
@@ -1486,9 +1562,9 @@ function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
|
1486
1562
|
return [remainingAccountsFragment];
|
|
1487
1563
|
}
|
|
1488
1564
|
function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
1489
|
-
const instructionNode =
|
|
1490
|
-
|
|
1491
|
-
const argumentName =
|
|
1565
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(scope.instructionPath);
|
|
1566
|
+
nodes.assertIsNode(remainingAccounts.value, "argumentValueNode");
|
|
1567
|
+
const argumentName = nodes.camelCase(remainingAccounts.value.name);
|
|
1492
1568
|
const isOptional = remainingAccounts.isOptional ?? false;
|
|
1493
1569
|
const isSigner = remainingAccounts.isSigner ?? false;
|
|
1494
1570
|
const isWritable = remainingAccounts.isWritable ?? false;
|
|
@@ -1497,7 +1573,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1497
1573
|
const signerRole = isWritable ? fragment`${accountRole}.WRITABLE_SIGNER` : fragment`${accountRole}.READONLY_SIGNER`;
|
|
1498
1574
|
const role = isSigner === true ? signerRole : nonSignerRole;
|
|
1499
1575
|
const argumentArray = isOptional ? `(args.${argumentName} ?? [])` : `args.${argumentName}`;
|
|
1500
|
-
const allArguments =
|
|
1576
|
+
const allArguments = nodes.getAllInstructionArguments(instructionNode);
|
|
1501
1577
|
const argumentExists = allArguments.some((arg) => arg.name === remainingAccounts.value.name);
|
|
1502
1578
|
if (argumentExists || isSigner === false) {
|
|
1503
1579
|
return fragment`${argumentArray}.map((address) => ({ address, role: ${role} }))`;
|
|
@@ -1508,7 +1584,7 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1508
1584
|
return fragment`${argumentArray}.map((signer) => ({ address: signer.address, role: ${signerRole}, signer }))`;
|
|
1509
1585
|
}
|
|
1510
1586
|
function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
1511
|
-
|
|
1587
|
+
nodes.assertIsNode(remainingAccounts.value, "resolverValueNode");
|
|
1512
1588
|
const isAsync = scope.asyncResolvers.includes(remainingAccounts.value.name);
|
|
1513
1589
|
if (!scope.useAsync && isAsync) return null;
|
|
1514
1590
|
const awaitKeyword = scope.useAsync && isAsync ? "await " : "";
|
|
@@ -1516,7 +1592,7 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1516
1592
|
scope.nameApi.resolverFunction(remainingAccounts.value.name),
|
|
1517
1593
|
scope.getImportFrom(remainingAccounts.value)
|
|
1518
1594
|
);
|
|
1519
|
-
return
|
|
1595
|
+
return visitorsCore.pipe(
|
|
1520
1596
|
fragment`${awaitKeyword}${functionName}(resolverScope)`,
|
|
1521
1597
|
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1522
1598
|
);
|
|
@@ -1525,13 +1601,13 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1525
1601
|
// src/fragments/instructionFunction.ts
|
|
1526
1602
|
function getInstructionFunctionFragment(scope) {
|
|
1527
1603
|
const { useAsync, instructionPath, resolvedInputs, renamedArgs, asyncResolvers, nameApi, customInstructionData } = scope;
|
|
1528
|
-
const instructionNode =
|
|
1529
|
-
const programNode =
|
|
1604
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1605
|
+
const programNode = visitorsCore.findProgramNodeFromPath(instructionPath);
|
|
1530
1606
|
if (useAsync && !hasAsyncFunction(instructionNode, resolvedInputs, asyncResolvers)) return;
|
|
1531
1607
|
const customData = customInstructionData.get(instructionNode.name);
|
|
1532
1608
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
1533
1609
|
const instructionDependencies = getInstructionDependencies(instructionNode, asyncResolvers, useAsync);
|
|
1534
|
-
const argDependencies = instructionDependencies.filter(
|
|
1610
|
+
const argDependencies = instructionDependencies.filter(nodes.isNodeFilter("argumentValueNode")).map((node) => node.name);
|
|
1535
1611
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
1536
1612
|
const argIsNotOmitted = (arg) => !(arg.defaultValue && arg.defaultValueStrategy === "omitted");
|
|
1537
1613
|
const argIsDependent = (arg) => argDependencies.includes(arg.name);
|
|
@@ -1544,7 +1620,7 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1544
1620
|
const hasExtraArgs = (instructionNode.extraArguments ?? []).filter(
|
|
1545
1621
|
(field) => argIsNotOmitted(field) && (argIsDependent(field) || argHasDefaultValue(field))
|
|
1546
1622
|
).length > 0;
|
|
1547
|
-
const hasRemainingAccountArgs = (instructionNode.remainingAccounts ?? []).filter(({ value }) =>
|
|
1623
|
+
const hasRemainingAccountArgs = (instructionNode.remainingAccounts ?? []).filter(({ value }) => nodes.isNode(value, "argumentValueNode")).length > 0;
|
|
1548
1624
|
const hasAnyArgs = hasDataArgs || hasExtraArgs || hasRemainingAccountArgs;
|
|
1549
1625
|
const hasInput = hasAccounts || hasAnyArgs;
|
|
1550
1626
|
const programAddressConstant = use(nameApi.programAddressConstant(programNode.name), "generatedPrograms");
|
|
@@ -1563,7 +1639,7 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1563
1639
|
const typeParams = getTypeParamsFragment(instructionNode, programAddressConstant);
|
|
1564
1640
|
const returnType = getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync);
|
|
1565
1641
|
const inputType = getInstructionInputTypeFragment(scope);
|
|
1566
|
-
const inputArg =
|
|
1642
|
+
const inputArg = renderersCore.mapFragmentContent(getInputTypeCallFragment(scope), (c) => hasInput ? `input: ${c}, ` : "");
|
|
1567
1643
|
const functionBody = mergeFragments(
|
|
1568
1644
|
[
|
|
1569
1645
|
getProgramAddressInitializationFragment(programAddressConstant),
|
|
@@ -1595,7 +1671,7 @@ function getAccountsInitializationFragment(instructionNode) {
|
|
|
1595
1671
|
if (instructionNode.accounts.length === 0) return;
|
|
1596
1672
|
const accounts = mergeFragments(
|
|
1597
1673
|
instructionNode.accounts.map((account) => {
|
|
1598
|
-
const name =
|
|
1674
|
+
const name = nodes.camelCase(account.name);
|
|
1599
1675
|
const isWritable = account.isWritable ? "true" : "false";
|
|
1600
1676
|
return fragment`${name}: { value: input.${name} ?? null, isWritable: ${isWritable} }`;
|
|
1601
1677
|
}),
|
|
@@ -1630,7 +1706,7 @@ function getReturnStatementFragment(scope) {
|
|
|
1630
1706
|
const hasLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted" && instructionNode.accounts.some((account) => account.isOptional);
|
|
1631
1707
|
const getAccountMeta = hasAccounts ? fragment`const getAccountMeta = ${use("getAccountMetaFactory", "shared")}(programAddress, '${optionalAccountStrategy}');` : "";
|
|
1632
1708
|
const accountItems = [
|
|
1633
|
-
...instructionNode.accounts.map((account) => `getAccountMeta(accounts.${
|
|
1709
|
+
...instructionNode.accounts.map((account) => `getAccountMeta(accounts.${nodes.camelCase(account.name)})`),
|
|
1634
1710
|
...hasRemainingAccounts ? ["...remainingAccounts"] : []
|
|
1635
1711
|
].join(", ");
|
|
1636
1712
|
let accounts;
|
|
@@ -1651,14 +1727,14 @@ function getReturnStatementFragment(scope) {
|
|
|
1651
1727
|
} else if (hasData) {
|
|
1652
1728
|
data = fragment`data: ${encoderFunctionFragment}.encode({})`;
|
|
1653
1729
|
}
|
|
1654
|
-
const instructionAttributes =
|
|
1730
|
+
const instructionAttributes = visitorsCore.pipe(
|
|
1655
1731
|
[accounts, hasByteDeltas ? fragment`byteDelta` : void 0, data, fragment`programAddress`],
|
|
1656
1732
|
(fs) => mergeFragments(fs, (cs) => cs.join(", "))
|
|
1657
1733
|
);
|
|
1658
1734
|
return fragment`${getAccountMeta}\nreturn Object.freeze({ ${instructionAttributes} } as ${scope.syncReturnTypeFragment});`;
|
|
1659
1735
|
}
|
|
1660
1736
|
function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync) {
|
|
1661
|
-
return
|
|
1737
|
+
return visitorsCore.pipe(
|
|
1662
1738
|
instructionTypeFragment,
|
|
1663
1739
|
(f) => hasByteDeltas ? fragment`${f} & ${use("type InstructionWithByteDelta", "shared")}` : f,
|
|
1664
1740
|
(f) => useAsync ? fragment`Promise<${f}>` : f
|
|
@@ -1667,7 +1743,7 @@ function getReturnTypeFragment(instructionTypeFragment, hasByteDeltas, useAsync)
|
|
|
1667
1743
|
function getTypeParamsFragment(instructionNode, programAddressConstant) {
|
|
1668
1744
|
return mergeFragments(
|
|
1669
1745
|
[
|
|
1670
|
-
...instructionNode.accounts.map((account) => fragment`TAccount${
|
|
1746
|
+
...instructionNode.accounts.map((account) => fragment`TAccount${nodes.pascalCase(account.name)} extends string`),
|
|
1671
1747
|
fragment`TProgramAddress extends ${use("type Address", "solanaAddresses")} = typeof ${programAddressConstant}`
|
|
1672
1748
|
],
|
|
1673
1749
|
(cs) => `<${cs.join(", ")}>`
|
|
@@ -1675,48 +1751,39 @@ function getTypeParamsFragment(instructionNode, programAddressConstant) {
|
|
|
1675
1751
|
}
|
|
1676
1752
|
function getInstructionTypeFragment(scope) {
|
|
1677
1753
|
const { instructionPath, nameApi } = scope;
|
|
1678
|
-
const instructionNode =
|
|
1754
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1679
1755
|
const instructionTypeName = nameApi.instructionType(instructionNode.name);
|
|
1680
1756
|
const accountTypeParamsFragments = instructionNode.accounts.map((account) => {
|
|
1681
|
-
const typeParam = fragment`TAccount${
|
|
1682
|
-
const camelName =
|
|
1757
|
+
const typeParam = fragment`TAccount${nodes.pascalCase(account.name)}`;
|
|
1758
|
+
const camelName = nodes.camelCase(account.name);
|
|
1683
1759
|
if (account.isSigner === "either") {
|
|
1684
1760
|
const signerRole = use(
|
|
1685
1761
|
account.isWritable ? "type WritableSignerAccount" : "type ReadonlySignerAccount",
|
|
1686
1762
|
"solanaInstructions"
|
|
1687
1763
|
);
|
|
1688
|
-
return
|
|
1764
|
+
return visitorsCore.pipe(
|
|
1689
1765
|
fragment`typeof input["${camelName}"] extends TransactionSigner<${typeParam}> ? ${signerRole}<${typeParam}> & AccountSignerMeta<${typeParam}> : ${typeParam}`,
|
|
1690
1766
|
(f) => addFragmentImports(f, "solanaSigners", ["type AccountSignerMeta", "type TransactionSigner"])
|
|
1691
1767
|
);
|
|
1692
1768
|
}
|
|
1693
1769
|
return typeParam;
|
|
1694
1770
|
});
|
|
1695
|
-
return
|
|
1771
|
+
return visitorsCore.pipe(
|
|
1696
1772
|
mergeFragments([fragment`TProgramAddress`, ...accountTypeParamsFragments], (c) => c.join(", ")),
|
|
1697
|
-
(f) =>
|
|
1773
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `${instructionTypeName}<${c}>`)
|
|
1698
1774
|
);
|
|
1699
1775
|
}
|
|
1700
1776
|
function getInputTypeCallFragment(scope) {
|
|
1701
1777
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
1702
|
-
const instructionNode =
|
|
1778
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1703
1779
|
const inputTypeName = useAsync ? nameApi.instructionAsyncInputType(instructionNode.name) : nameApi.instructionSyncInputType(instructionNode.name);
|
|
1704
1780
|
if (instructionNode.accounts.length === 0) return fragment`${inputTypeName}`;
|
|
1705
|
-
const accountTypeParams = instructionNode.accounts.map((account) => `TAccount${
|
|
1781
|
+
const accountTypeParams = instructionNode.accounts.map((account) => `TAccount${nodes.pascalCase(account.name)}`).join(", ");
|
|
1706
1782
|
return fragment`${inputTypeName}<${accountTypeParams}>`;
|
|
1707
1783
|
}
|
|
1708
|
-
|
|
1709
|
-
// src/fragments/instructionPage.ts
|
|
1710
|
-
var import_errors2 = require("@codama/errors");
|
|
1711
|
-
var import_nodes23 = require("@codama/nodes");
|
|
1712
|
-
var import_visitors_core20 = require("@codama/visitors-core");
|
|
1713
|
-
|
|
1714
|
-
// src/fragments/instructionParseFunction.ts
|
|
1715
|
-
var import_nodes21 = require("@codama/nodes");
|
|
1716
|
-
var import_visitors_core18 = require("@codama/visitors-core");
|
|
1717
1784
|
function getInstructionParseFunctionFragment(scope) {
|
|
1718
|
-
const instructionNode =
|
|
1719
|
-
const programNode =
|
|
1785
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(scope.instructionPath);
|
|
1786
|
+
const programNode = visitorsCore.findProgramNodeFromPath(scope.instructionPath);
|
|
1720
1787
|
const programAddressConstant = use(scope.nameApi.programAddressConstant(programNode.name), "generatedPrograms");
|
|
1721
1788
|
const childScope = { ...scope, instructionNode, programAddressConstant };
|
|
1722
1789
|
return mergeFragments([getTypeFragment2(childScope), getFunctionFragment(childScope)], (cs) => cs.join("\n\n"));
|
|
@@ -1737,7 +1804,7 @@ function getTypeFragment2(scope) {
|
|
|
1737
1804
|
const accounts = mergeFragments(
|
|
1738
1805
|
scope.instructionNode.accounts.map((account, i) => {
|
|
1739
1806
|
const docs = getDocblockFragment(account.docs ?? [], true);
|
|
1740
|
-
const name =
|
|
1807
|
+
const name = nodes.camelCase(account.name);
|
|
1741
1808
|
return fragment`${docs}${name}${account.isOptional ? "?" : ""}: TAccountMetas[${i}]${account.isOptional ? " | undefined" : ""};`;
|
|
1742
1809
|
}),
|
|
1743
1810
|
(cs) => hasAccounts ? `
|
|
@@ -1771,7 +1838,7 @@ function getFunctionFragment(scope) {
|
|
|
1771
1838
|
[
|
|
1772
1839
|
fragment`${use("type Instruction", "solanaInstructions")}<TProgram>`,
|
|
1773
1840
|
hasAccounts ? fragment`${use("type InstructionWithAccounts", "solanaInstructions")}<TAccountMetas>` : void 0,
|
|
1774
|
-
hasData ?
|
|
1841
|
+
hasData ? visitorsCore.pipe(
|
|
1775
1842
|
fragment`InstructionWithData<ReadonlyUint8Array>`,
|
|
1776
1843
|
(f) => addFragmentImports(f, "solanaInstructions", ["type InstructionWithData"]),
|
|
1777
1844
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
@@ -1809,7 +1876,7 @@ const getNextOptionalAccount = () => {
|
|
|
1809
1876
|
}
|
|
1810
1877
|
const accounts = mergeFragments(
|
|
1811
1878
|
scope.instructionNode.accounts.map(
|
|
1812
|
-
(account) => account.isOptional ? fragment`${
|
|
1879
|
+
(account) => account.isOptional ? fragment`${nodes.camelCase(account.name)}: getNextOptionalAccount()` : fragment`${nodes.camelCase(account.name)}: getNextAccount()`
|
|
1813
1880
|
),
|
|
1814
1881
|
(cs) => hasAccounts ? `, accounts: { ${cs.join(", ")} }` : ""
|
|
1815
1882
|
);
|
|
@@ -1819,15 +1886,10 @@ const getNextOptionalAccount = () => {
|
|
|
1819
1886
|
return { programAddress: instruction.programAddress${accounts}${data} };
|
|
1820
1887
|
}`;
|
|
1821
1888
|
}
|
|
1822
|
-
|
|
1823
|
-
// src/fragments/instructionType.ts
|
|
1824
|
-
var import_nodes22 = require("@codama/nodes");
|
|
1825
|
-
var import_renderers_core9 = require("@codama/renderers-core");
|
|
1826
|
-
var import_visitors_core19 = require("@codama/visitors-core");
|
|
1827
1889
|
function getInstructionTypeFragment2(scope) {
|
|
1828
1890
|
const { instructionPath, nameApi, customInstructionData } = scope;
|
|
1829
|
-
const instructionNode =
|
|
1830
|
-
const programNode =
|
|
1891
|
+
const instructionNode = visitorsCore.getLastNodeFromPath(instructionPath);
|
|
1892
|
+
const programNode = visitorsCore.findProgramNodeFromPath(instructionPath);
|
|
1831
1893
|
const hasAccounts = instructionNode.accounts.length > 0;
|
|
1832
1894
|
const customData = customInstructionData.get(instructionNode.name);
|
|
1833
1895
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
@@ -1837,7 +1899,6 @@ function getInstructionTypeFragment2(scope) {
|
|
|
1837
1899
|
instructionNode.accounts.map(
|
|
1838
1900
|
(account) => getInstructionAccountTypeParamFragment({
|
|
1839
1901
|
...scope,
|
|
1840
|
-
allowAccountMeta: true,
|
|
1841
1902
|
instructionAccountPath: [...instructionPath, account]
|
|
1842
1903
|
})
|
|
1843
1904
|
),
|
|
@@ -1847,8 +1908,8 @@ function getInstructionTypeFragment2(scope) {
|
|
|
1847
1908
|
const usesLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted";
|
|
1848
1909
|
const accountMetasFragment = mergeFragments(
|
|
1849
1910
|
instructionNode.accounts.map(
|
|
1850
|
-
(account) =>
|
|
1851
|
-
const typeParam = `TAccount${
|
|
1911
|
+
(account) => renderersCore.mapFragmentContent(getInstructionAccountMetaFragment(account), (c) => {
|
|
1912
|
+
const typeParam = `TAccount${nodes.pascalCase(account.name)}`;
|
|
1852
1913
|
const isLegacyOptional = account.isOptional && usesLegacyOptionalAccounts;
|
|
1853
1914
|
const type = `${typeParam} extends string ? ${c} : ${typeParam}`;
|
|
1854
1915
|
if (!isLegacyOptional) return type;
|
|
@@ -1865,17 +1926,17 @@ ${use("type Instruction", "solanaInstructions")}<TProgram>${data}${accounts};`;
|
|
|
1865
1926
|
|
|
1866
1927
|
// src/fragments/instructionPage.ts
|
|
1867
1928
|
function getInstructionPageFragment(scope) {
|
|
1868
|
-
const node =
|
|
1869
|
-
if (!
|
|
1929
|
+
const node = visitorsCore.getLastNodeFromPath(scope.instructionPath);
|
|
1930
|
+
if (!visitorsCore.findProgramNodeFromPath(scope.instructionPath)) {
|
|
1870
1931
|
throw new Error("Instruction must be visited inside a program.");
|
|
1871
1932
|
}
|
|
1872
1933
|
const childScope = {
|
|
1873
1934
|
...scope,
|
|
1874
|
-
dataArgsManifest:
|
|
1875
|
-
extraArgsManifest:
|
|
1876
|
-
|
|
1935
|
+
dataArgsManifest: visitorsCore.visit(node, scope.typeManifestVisitor),
|
|
1936
|
+
extraArgsManifest: visitorsCore.visit(
|
|
1937
|
+
nodes.definedTypeNode({
|
|
1877
1938
|
name: scope.nameApi.instructionExtraType(node.name),
|
|
1878
|
-
type:
|
|
1939
|
+
type: nodes.structTypeNodeFromInstructionArgumentNodes(node.extraArguments ?? [])
|
|
1879
1940
|
}),
|
|
1880
1941
|
scope.typeManifestVisitor
|
|
1881
1942
|
),
|
|
@@ -1911,17 +1972,13 @@ function getRenamedArgsMap(instruction) {
|
|
|
1911
1972
|
const allNames = [...instruction.accounts.map((account) => account.name), ...argNames];
|
|
1912
1973
|
const duplicates = allNames.filter((e, i, a) => a.indexOf(e) !== i);
|
|
1913
1974
|
if (duplicates.length === 0) return /* @__PURE__ */ new Map();
|
|
1914
|
-
|
|
1975
|
+
errors.logWarn(
|
|
1915
1976
|
`[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.`
|
|
1916
1977
|
);
|
|
1917
|
-
return new Map(duplicates.map((name) => [
|
|
1978
|
+
return new Map(duplicates.map((name) => [nodes.camelCase(name), nodes.camelCase(`${name}Arg`)]));
|
|
1918
1979
|
}
|
|
1919
|
-
|
|
1920
|
-
// src/fragments/pdaFunction.ts
|
|
1921
|
-
var import_nodes24 = require("@codama/nodes");
|
|
1922
|
-
var import_visitors_core21 = require("@codama/visitors-core");
|
|
1923
1980
|
function getPdaFunctionFragment(scope) {
|
|
1924
|
-
const pdaNode =
|
|
1981
|
+
const pdaNode = visitorsCore.getLastNodeFromPath(scope.pdaPath);
|
|
1925
1982
|
const seeds = parsePdaSeedNodes(pdaNode.seeds, scope);
|
|
1926
1983
|
return mergeFragments(
|
|
1927
1984
|
[getSeedInputTypeFragment(seeds, scope), getFunctionFragment2(seeds, scope)],
|
|
@@ -1929,9 +1986,9 @@ function getPdaFunctionFragment(scope) {
|
|
|
1929
1986
|
);
|
|
1930
1987
|
}
|
|
1931
1988
|
function getSeedInputTypeFragment(seeds, scope) {
|
|
1932
|
-
const variableSeeds = seeds.filter(
|
|
1989
|
+
const variableSeeds = seeds.filter(nodes.isNodeFilter("variablePdaSeedNode"));
|
|
1933
1990
|
if (variableSeeds.length === 0) return;
|
|
1934
|
-
const pdaNode =
|
|
1991
|
+
const pdaNode = visitorsCore.getLastNodeFromPath(scope.pdaPath);
|
|
1935
1992
|
const seedTypeName = scope.nameApi.pdaSeedsType(pdaNode.name);
|
|
1936
1993
|
const seedAttributes = mergeFragments(
|
|
1937
1994
|
variableSeeds.map((seed) => seed.inputAttribute),
|
|
@@ -1940,15 +1997,15 @@ function getSeedInputTypeFragment(seeds, scope) {
|
|
|
1940
1997
|
return fragment`export type ${seedTypeName} = {\n${seedAttributes}\n};`;
|
|
1941
1998
|
}
|
|
1942
1999
|
function getFunctionFragment2(seeds, scope) {
|
|
1943
|
-
const pdaNode =
|
|
1944
|
-
const programNode =
|
|
2000
|
+
const pdaNode = visitorsCore.getLastNodeFromPath(scope.pdaPath);
|
|
2001
|
+
const programNode = visitorsCore.findProgramNodeFromPath(scope.pdaPath);
|
|
1945
2002
|
const addressType = use("type Address", "solanaAddresses");
|
|
1946
2003
|
const pdaType = use("type ProgramDerivedAddress", "solanaAddresses");
|
|
1947
2004
|
const getPdaFunction = use("getProgramDerivedAddress", "solanaAddresses");
|
|
1948
2005
|
const seedTypeName = scope.nameApi.pdaSeedsType(pdaNode.name);
|
|
1949
2006
|
const findPdaFunction = scope.nameApi.pdaFindFunction(pdaNode.name);
|
|
1950
2007
|
const docs = getDocblockFragment(pdaNode.docs ?? [], true);
|
|
1951
|
-
const hasVariableSeeds = seeds.filter(
|
|
2008
|
+
const hasVariableSeeds = seeds.filter(nodes.isNodeFilter("variablePdaSeedNode")).length > 0;
|
|
1952
2009
|
const seedArgument = hasVariableSeeds ? `seeds: ${seedTypeName}, ` : "";
|
|
1953
2010
|
const programAddress = pdaNode.programId ?? programNode.publicKey;
|
|
1954
2011
|
const encodedSeeds = mergeFragments(
|
|
@@ -1962,39 +2019,31 @@ function getFunctionFragment2(seeds, scope) {
|
|
|
1962
2019
|
}
|
|
1963
2020
|
function parsePdaSeedNodes(seeds, scope) {
|
|
1964
2021
|
return seeds.map((seed) => {
|
|
1965
|
-
if (
|
|
1966
|
-
const name =
|
|
2022
|
+
if (nodes.isNode(seed, "variablePdaSeedNode")) {
|
|
2023
|
+
const name = nodes.camelCase(seed.name);
|
|
1967
2024
|
const docs = getDocblockFragment(seed.docs ?? [], true);
|
|
1968
|
-
const { encoder: encoder2, looseType } =
|
|
2025
|
+
const { encoder: encoder2, looseType } = visitorsCore.visit(seed.type, scope.typeManifestVisitor);
|
|
1969
2026
|
return {
|
|
1970
2027
|
...seed,
|
|
1971
2028
|
encodedValue: fragment`${encoder2}.encode(seeds.${name})`,
|
|
1972
2029
|
inputAttribute: fragment`${docs}${name}: ${looseType};`
|
|
1973
2030
|
};
|
|
1974
2031
|
}
|
|
1975
|
-
if (
|
|
2032
|
+
if (nodes.isNode(seed.value, "programIdValueNode")) {
|
|
1976
2033
|
const addressEncoder = use("getAddressEncoder", "solanaAddresses");
|
|
1977
2034
|
return { ...seed, encodedValue: fragment`${addressEncoder}().encode(programAddress)` };
|
|
1978
2035
|
}
|
|
1979
|
-
const { encoder } =
|
|
1980
|
-
const { value } =
|
|
2036
|
+
const { encoder } = visitorsCore.visit(seed.type, scope.typeManifestVisitor);
|
|
2037
|
+
const { value } = visitorsCore.visit(seed.value, scope.typeManifestVisitor);
|
|
1981
2038
|
return { ...seed, encodedValue: fragment`${encoder}.encode(${value})` };
|
|
1982
2039
|
});
|
|
1983
2040
|
}
|
|
1984
|
-
|
|
1985
|
-
// src/fragments/pdaPage.ts
|
|
1986
|
-
var import_visitors_core22 = require("@codama/visitors-core");
|
|
1987
2041
|
function getPdaPageFragment(scope) {
|
|
1988
|
-
if (!
|
|
2042
|
+
if (!visitorsCore.findProgramNodeFromPath(scope.pdaPath)) {
|
|
1989
2043
|
throw new Error("PDA must be visited inside a program.");
|
|
1990
2044
|
}
|
|
1991
2045
|
return getPdaFunctionFragment(scope);
|
|
1992
2046
|
}
|
|
1993
|
-
|
|
1994
|
-
// src/fragments/programAccounts.ts
|
|
1995
|
-
var import_nodes25 = require("@codama/nodes");
|
|
1996
|
-
var import_renderers_core10 = require("@codama/renderers-core");
|
|
1997
|
-
var import_visitors_core23 = require("@codama/visitors-core");
|
|
1998
2047
|
function getProgramAccountsFragment(scope) {
|
|
1999
2048
|
if (scope.programNode.accounts.length === 0) return;
|
|
2000
2049
|
return mergeFragments(
|
|
@@ -2019,7 +2068,7 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2019
2068
|
if (!hasAccountDiscriminators) return;
|
|
2020
2069
|
const programAccountsEnum = nameApi.programAccountsEnum(programNode.name);
|
|
2021
2070
|
const programAccountsIdentifierFunction = nameApi.programAccountsIdentifierFunction(programNode.name);
|
|
2022
|
-
return
|
|
2071
|
+
return visitorsCore.pipe(
|
|
2023
2072
|
mergeFragments(
|
|
2024
2073
|
accountsWithDiscriminators.map((account) => {
|
|
2025
2074
|
const variant = nameApi.programAccountsEnumVariant(account.name);
|
|
@@ -2028,12 +2077,12 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
2028
2077
|
dataName: "data",
|
|
2029
2078
|
discriminators: account.discriminators ?? [],
|
|
2030
2079
|
ifTrue: `return ${programAccountsEnum}.${variant};`,
|
|
2031
|
-
struct:
|
|
2080
|
+
struct: nodes.resolveNestedTypeNode(account.data)
|
|
2032
2081
|
});
|
|
2033
2082
|
}),
|
|
2034
2083
|
(c) => c.join("\n")
|
|
2035
2084
|
),
|
|
2036
|
-
(f) =>
|
|
2085
|
+
(f) => renderersCore.mapFragmentContent(
|
|
2037
2086
|
f,
|
|
2038
2087
|
(discriminators) => `export function ${programAccountsIdentifierFunction}(account: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programAccountsEnum} {
|
|
2039
2088
|
const data = 'data' in account ? account.data : account;
|
|
@@ -2044,25 +2093,17 @@ throw new Error("The provided account could not be identified as a ${programNode
|
|
|
2044
2093
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
2045
2094
|
);
|
|
2046
2095
|
}
|
|
2047
|
-
|
|
2048
|
-
// src/fragments/programConstant.ts
|
|
2049
|
-
var import_visitors_core24 = require("@codama/visitors-core");
|
|
2050
2096
|
function getProgramConstantFragment(scope) {
|
|
2051
2097
|
const { programNode, nameApi } = scope;
|
|
2052
2098
|
const programAddressConstant = nameApi.programAddressConstant(programNode.name);
|
|
2053
|
-
return
|
|
2099
|
+
return visitorsCore.pipe(
|
|
2054
2100
|
fragment`export const ${programAddressConstant} = '${programNode.publicKey}' as Address<'${programNode.publicKey}'>;`,
|
|
2055
2101
|
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
2056
2102
|
);
|
|
2057
2103
|
}
|
|
2058
|
-
|
|
2059
|
-
// src/fragments/programInstructions.ts
|
|
2060
|
-
var import_nodes26 = require("@codama/nodes");
|
|
2061
|
-
var import_renderers_core11 = require("@codama/renderers-core");
|
|
2062
|
-
var import_visitors_core25 = require("@codama/visitors-core");
|
|
2063
2104
|
function getProgramInstructionsFragment(scope) {
|
|
2064
2105
|
if (scope.programNode.instructions.length === 0) return;
|
|
2065
|
-
const allInstructions =
|
|
2106
|
+
const allInstructions = nodes.getAllInstructionsWithSubs(scope.programNode, {
|
|
2066
2107
|
leavesOnly: !scope.renderParentInstructions,
|
|
2067
2108
|
subInstructionsFirst: true
|
|
2068
2109
|
});
|
|
@@ -2101,14 +2142,14 @@ function getProgramInstructionsIdentifierFunctionFragment(scope) {
|
|
|
2101
2142
|
dataName: "data",
|
|
2102
2143
|
discriminators: instruction.discriminators ?? [],
|
|
2103
2144
|
ifTrue: `return ${programInstructionsEnum}.${variant};`,
|
|
2104
|
-
struct:
|
|
2145
|
+
struct: nodes.structTypeNodeFromInstructionArgumentNodes(instruction.arguments)
|
|
2105
2146
|
});
|
|
2106
2147
|
}),
|
|
2107
2148
|
(c) => c.join("\n")
|
|
2108
2149
|
);
|
|
2109
|
-
return
|
|
2150
|
+
return visitorsCore.pipe(
|
|
2110
2151
|
discriminatorsFragment,
|
|
2111
|
-
(f) =>
|
|
2152
|
+
(f) => renderersCore.mapFragmentContent(
|
|
2112
2153
|
f,
|
|
2113
2154
|
(discriminators) => `export function ${programInstructionsIdentifierFunction}(instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programInstructionsEnum} {
|
|
2114
2155
|
const data = 'data' in instruction ? instruction.data : instruction;
|
|
@@ -2168,9 +2209,6 @@ function getRootIndexPageFragment(scope) {
|
|
|
2168
2209
|
(cs) => cs.join("\n")
|
|
2169
2210
|
);
|
|
2170
2211
|
}
|
|
2171
|
-
|
|
2172
|
-
// src/fragments/sharedPage.ts
|
|
2173
|
-
var import_visitors_core26 = require("@codama/visitors-core");
|
|
2174
2212
|
function getSharedPageFragment() {
|
|
2175
2213
|
const sharedPage = fragment`/**
|
|
2176
2214
|
* Asserts that the given value is not null or undefined.
|
|
@@ -2271,7 +2309,7 @@ export function getAccountMetaFactory(
|
|
|
2271
2309
|
export function isTransactionSigner<TAddress extends string = string>(value: Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress>): value is TransactionSigner<TAddress> {
|
|
2272
2310
|
return !!value && typeof value === 'object' && 'address' in value && kitIsTransactionSigner(value);
|
|
2273
2311
|
}`;
|
|
2274
|
-
return
|
|
2312
|
+
return visitorsCore.pipe(
|
|
2275
2313
|
sharedPage,
|
|
2276
2314
|
(f) => addFragmentImports(f, "solanaAddresses", [
|
|
2277
2315
|
"type Address",
|
|
@@ -2286,12 +2324,9 @@ export function isTransactionSigner<TAddress extends string = string>(value: Add
|
|
|
2286
2324
|
])
|
|
2287
2325
|
);
|
|
2288
2326
|
}
|
|
2289
|
-
|
|
2290
|
-
// src/fragments/typeDiscriminatedUnionHelpers.ts
|
|
2291
|
-
var import_nodes27 = require("@codama/nodes");
|
|
2292
2327
|
function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
2293
2328
|
const { name, typeNode, nameApi } = scope;
|
|
2294
|
-
const isDiscriminatedUnion =
|
|
2329
|
+
const isDiscriminatedUnion = nodes.isNode(typeNode, "enumTypeNode") && nodes.isDataEnum(typeNode);
|
|
2295
2330
|
if (!isDiscriminatedUnion) return;
|
|
2296
2331
|
const functionName = nameApi.discriminatedUnionFunction(name);
|
|
2297
2332
|
const isDiscriminatedUnionFunctionName = nameApi.isDiscriminatedUnionFunction(name);
|
|
@@ -2303,10 +2338,10 @@ function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
|
2303
2338
|
const variantSignatures = mergeFragments(
|
|
2304
2339
|
typeNode.variants.map((variant) => {
|
|
2305
2340
|
const variantName = nameApi.discriminatedUnionVariant(variant.name);
|
|
2306
|
-
if (
|
|
2341
|
+
if (nodes.isNode(variant, "enumStructVariantTypeNode")) {
|
|
2307
2342
|
return fragment`export function ${functionName}(kind: '${variantName}', data: ${getVariantContentType}<${looseName}, '${discriminatorName}', '${variantName}'>): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
2308
2343
|
}
|
|
2309
|
-
if (
|
|
2344
|
+
if (nodes.isNode(variant, "enumTupleVariantTypeNode")) {
|
|
2310
2345
|
return fragment`export function ${functionName}(kind: '${variantName}', data: ${getVariantContentType}<${looseName}, '${discriminatorName}', '${variantName}'>['fields']): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
2311
2346
|
}
|
|
2312
2347
|
return fragment`export function ${functionName}(kind: '${variantName}'): ${getVariantType}<${looseName}, '${discriminatorName}', '${variantName}'>;`;
|
|
@@ -2324,13 +2359,10 @@ export function ${isDiscriminatedUnionFunctionName}<K extends ${strictName}['${d
|
|
|
2324
2359
|
};
|
|
2325
2360
|
`;
|
|
2326
2361
|
}
|
|
2327
|
-
|
|
2328
|
-
// src/fragments/typePage.ts
|
|
2329
|
-
var import_visitors_core27 = require("@codama/visitors-core");
|
|
2330
2362
|
function getTypePageFragment(scope) {
|
|
2331
2363
|
const node = scope.node;
|
|
2332
|
-
const manifest =
|
|
2333
|
-
return
|
|
2364
|
+
const manifest = visitorsCore.visit(node, scope.typeManifestVisitor);
|
|
2365
|
+
return visitorsCore.pipe(
|
|
2334
2366
|
mergeFragments(
|
|
2335
2367
|
[
|
|
2336
2368
|
getTypeWithCodecFragment({ ...scope, manifest, name: node.name, node: node.type, typeDocs: node.docs }),
|
|
@@ -2347,42 +2379,37 @@ function getTypePageFragment(scope) {
|
|
|
2347
2379
|
])
|
|
2348
2380
|
);
|
|
2349
2381
|
}
|
|
2350
|
-
|
|
2351
|
-
// src/visitors/getTypeManifestVisitor.ts
|
|
2352
|
-
var import_nodes28 = require("@codama/nodes");
|
|
2353
|
-
var import_renderers_core12 = require("@codama/renderers-core");
|
|
2354
|
-
var import_visitors_core28 = require("@codama/visitors-core");
|
|
2355
2382
|
function getTypeManifestVisitor(input) {
|
|
2356
2383
|
const { nameApi, linkables, nonScalarEnums, customAccountData, customInstructionData, getImportFrom } = input;
|
|
2357
|
-
const stack = input.stack ?? new
|
|
2384
|
+
const stack = input.stack ?? new visitorsCore.NodeStack();
|
|
2358
2385
|
let parentName = null;
|
|
2359
|
-
return
|
|
2360
|
-
|
|
2386
|
+
return visitorsCore.pipe(
|
|
2387
|
+
visitorsCore.staticVisitor(() => typeManifest(), {
|
|
2361
2388
|
keys: [
|
|
2362
|
-
...
|
|
2363
|
-
...
|
|
2389
|
+
...nodes.REGISTERED_TYPE_NODE_KINDS,
|
|
2390
|
+
...nodes.REGISTERED_VALUE_NODE_KINDS,
|
|
2364
2391
|
"definedTypeLinkNode",
|
|
2365
2392
|
"definedTypeNode",
|
|
2366
2393
|
"accountNode",
|
|
2367
2394
|
"instructionNode"
|
|
2368
2395
|
]
|
|
2369
2396
|
}),
|
|
2370
|
-
(visitor) =>
|
|
2397
|
+
(visitor) => visitorsCore.extendVisitor(visitor, {
|
|
2371
2398
|
visitAccount(account, { self }) {
|
|
2372
2399
|
parentName = {
|
|
2373
2400
|
loose: nameApi.dataArgsType(account.name),
|
|
2374
2401
|
strict: nameApi.dataType(account.name)
|
|
2375
2402
|
};
|
|
2376
2403
|
const link = customAccountData.get(account.name)?.linkNode;
|
|
2377
|
-
const manifest = link ?
|
|
2404
|
+
const manifest = link ? visitorsCore.visit(link, self) : visitorsCore.visit(account.data, self);
|
|
2378
2405
|
parentName = null;
|
|
2379
2406
|
return manifest;
|
|
2380
2407
|
},
|
|
2381
2408
|
visitAmountType(amountType, { self }) {
|
|
2382
|
-
return
|
|
2409
|
+
return visitorsCore.visit(amountType.number, self);
|
|
2383
2410
|
},
|
|
2384
2411
|
visitArrayType(arrayType, { self }) {
|
|
2385
|
-
const childManifest =
|
|
2412
|
+
const childManifest = visitorsCore.visit(arrayType.item, self);
|
|
2386
2413
|
const sizeManifest = getArrayLikeSizeOption(arrayType.count, self);
|
|
2387
2414
|
const encoderOptions = sizeManifest.encoder ? fragment`, { ${sizeManifest.encoder} }` : "";
|
|
2388
2415
|
const decoderOptions = sizeManifest.decoder ? fragment`, { ${sizeManifest.decoder} }` : "";
|
|
@@ -2396,16 +2423,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2396
2423
|
},
|
|
2397
2424
|
visitArrayValue(node, { self }) {
|
|
2398
2425
|
return mergeTypeManifests(
|
|
2399
|
-
node.items.map((v) =>
|
|
2426
|
+
node.items.map((v) => visitorsCore.visit(v, self)),
|
|
2400
2427
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
2401
2428
|
);
|
|
2402
2429
|
},
|
|
2403
2430
|
visitBooleanType(booleanType, { self }) {
|
|
2404
2431
|
let sizeEncoder = fragment``;
|
|
2405
2432
|
let sizeDecoder = fragment``;
|
|
2406
|
-
const resolvedSize =
|
|
2433
|
+
const resolvedSize = nodes.resolveNestedTypeNode(booleanType.size);
|
|
2407
2434
|
if (resolvedSize.format !== "u8" || resolvedSize.endian !== "le") {
|
|
2408
|
-
const size =
|
|
2435
|
+
const size = visitorsCore.visit(booleanType.size, self);
|
|
2409
2436
|
sizeEncoder = fragment`{ size: ${size.encoder} }`;
|
|
2410
2437
|
sizeDecoder = fragment`{ size: ${size.decoder} }`;
|
|
2411
2438
|
}
|
|
@@ -2433,22 +2460,22 @@ function getTypeManifestVisitor(input) {
|
|
|
2433
2460
|
return typeManifest({ value: fragment`new Uint8Array([${Array.from(bytes).join(", ")}])` });
|
|
2434
2461
|
},
|
|
2435
2462
|
visitConstantValue(node, { self }) {
|
|
2436
|
-
if (
|
|
2437
|
-
return
|
|
2463
|
+
if (nodes.isNode(node.type, "bytesTypeNode") && nodes.isNode(node.value, "bytesValueNode")) {
|
|
2464
|
+
return visitorsCore.visit(node.value, self);
|
|
2438
2465
|
}
|
|
2439
2466
|
return typeManifest({
|
|
2440
|
-
value: fragment`${
|
|
2467
|
+
value: fragment`${visitorsCore.visit(node.type, self).encoder}.encode(${visitorsCore.visit(node.value, self).value})`
|
|
2441
2468
|
});
|
|
2442
2469
|
},
|
|
2443
2470
|
visitDateTimeType(dateTimeType, { self }) {
|
|
2444
|
-
return
|
|
2471
|
+
return visitorsCore.visit(dateTimeType.number, self);
|
|
2445
2472
|
},
|
|
2446
2473
|
visitDefinedType(definedType, { self }) {
|
|
2447
2474
|
parentName = {
|
|
2448
2475
|
loose: nameApi.dataArgsType(definedType.name),
|
|
2449
2476
|
strict: nameApi.dataType(definedType.name)
|
|
2450
2477
|
};
|
|
2451
|
-
const manifest =
|
|
2478
|
+
const manifest = visitorsCore.visit(definedType.type, self);
|
|
2452
2479
|
parentName = null;
|
|
2453
2480
|
return manifest;
|
|
2454
2481
|
},
|
|
@@ -2466,7 +2493,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2466
2493
|
});
|
|
2467
2494
|
},
|
|
2468
2495
|
visitEnumEmptyVariantType(enumEmptyVariantType) {
|
|
2469
|
-
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2496
|
+
const discriminator = nameApi.discriminatedUnionDiscriminator(nodes.camelCase(parentName?.strict ?? ""));
|
|
2470
2497
|
const name = nameApi.discriminatedUnionVariant(enumEmptyVariantType.name);
|
|
2471
2498
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2472
2499
|
return typeManifest({
|
|
@@ -2479,54 +2506,54 @@ function getTypeManifestVisitor(input) {
|
|
|
2479
2506
|
visitEnumStructVariantType(enumStructVariantType, { self }) {
|
|
2480
2507
|
const currentParentName = parentName;
|
|
2481
2508
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2482
|
-
|
|
2509
|
+
nodes.camelCase(currentParentName?.strict ?? "")
|
|
2483
2510
|
);
|
|
2484
2511
|
const name = nameApi.discriminatedUnionVariant(enumStructVariantType.name);
|
|
2485
2512
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2486
2513
|
parentName = null;
|
|
2487
|
-
const structManifest =
|
|
2514
|
+
const structManifest = visitorsCore.visit(enumStructVariantType.struct, self);
|
|
2488
2515
|
parentName = currentParentName;
|
|
2489
2516
|
return typeManifest({
|
|
2490
2517
|
...structManifest,
|
|
2491
2518
|
decoder: fragment`['${name}', ${structManifest.decoder}]`,
|
|
2492
2519
|
encoder: fragment`['${name}', ${structManifest.encoder}]`,
|
|
2493
|
-
looseType:
|
|
2520
|
+
looseType: visitorsCore.pipe(
|
|
2494
2521
|
structManifest.looseType,
|
|
2495
|
-
(f) =>
|
|
2522
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2496
2523
|
),
|
|
2497
|
-
strictType:
|
|
2524
|
+
strictType: visitorsCore.pipe(
|
|
2498
2525
|
structManifest.strictType,
|
|
2499
|
-
(f) =>
|
|
2526
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2500
2527
|
)
|
|
2501
2528
|
});
|
|
2502
2529
|
},
|
|
2503
2530
|
visitEnumTupleVariantType(enumTupleVariantType, { self }) {
|
|
2504
2531
|
const currentParentName = parentName;
|
|
2505
2532
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2506
|
-
|
|
2533
|
+
nodes.camelCase(currentParentName?.strict ?? "")
|
|
2507
2534
|
);
|
|
2508
2535
|
const name = nameApi.discriminatedUnionVariant(enumTupleVariantType.name);
|
|
2509
2536
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
2510
|
-
const struct =
|
|
2511
|
-
|
|
2537
|
+
const struct = nodes.structTypeNode([
|
|
2538
|
+
nodes.structFieldTypeNode({
|
|
2512
2539
|
name: "fields",
|
|
2513
2540
|
type: enumTupleVariantType.tuple
|
|
2514
2541
|
})
|
|
2515
2542
|
]);
|
|
2516
2543
|
parentName = null;
|
|
2517
|
-
const structManifest =
|
|
2544
|
+
const structManifest = visitorsCore.visit(struct, self);
|
|
2518
2545
|
parentName = currentParentName;
|
|
2519
2546
|
return typeManifest({
|
|
2520
2547
|
...structManifest,
|
|
2521
2548
|
decoder: fragment`['${name}', ${structManifest.decoder}]`,
|
|
2522
2549
|
encoder: fragment`['${name}', ${structManifest.encoder}]`,
|
|
2523
|
-
looseType:
|
|
2550
|
+
looseType: visitorsCore.pipe(
|
|
2524
2551
|
structManifest.looseType,
|
|
2525
|
-
(f) =>
|
|
2552
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2526
2553
|
),
|
|
2527
|
-
strictType:
|
|
2554
|
+
strictType: visitorsCore.pipe(
|
|
2528
2555
|
structManifest.strictType,
|
|
2529
|
-
(f) =>
|
|
2556
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2530
2557
|
)
|
|
2531
2558
|
});
|
|
2532
2559
|
},
|
|
@@ -2534,16 +2561,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2534
2561
|
const currentParentName = parentName;
|
|
2535
2562
|
const encoderOptions = [];
|
|
2536
2563
|
const decoderOptions = [];
|
|
2537
|
-
const enumSize =
|
|
2564
|
+
const enumSize = nodes.resolveNestedTypeNode(enumType.size);
|
|
2538
2565
|
if (enumSize.format !== "u8" || enumSize.endian !== "le") {
|
|
2539
|
-
const sizeManifest =
|
|
2566
|
+
const sizeManifest = visitorsCore.visit(enumType.size, self);
|
|
2540
2567
|
encoderOptions.push(fragment`size: ${sizeManifest.encoder}`);
|
|
2541
2568
|
decoderOptions.push(fragment`size: ${sizeManifest.decoder}`);
|
|
2542
2569
|
}
|
|
2543
2570
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2544
|
-
|
|
2571
|
+
nodes.camelCase(currentParentName?.strict ?? "")
|
|
2545
2572
|
);
|
|
2546
|
-
if (!
|
|
2573
|
+
if (!nodes.isScalarEnum(enumType) && discriminator !== "__kind") {
|
|
2547
2574
|
encoderOptions.push(fragment`discriminator: '${discriminator}'`);
|
|
2548
2575
|
decoderOptions.push(fragment`discriminator: '${discriminator}'`);
|
|
2549
2576
|
}
|
|
@@ -2555,7 +2582,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2555
2582
|
decoderOptions,
|
|
2556
2583
|
(cs) => cs.length > 0 ? `, { ${cs.join(", ")} }` : ""
|
|
2557
2584
|
);
|
|
2558
|
-
if (
|
|
2585
|
+
if (nodes.isScalarEnum(enumType)) {
|
|
2559
2586
|
if (currentParentName === null) {
|
|
2560
2587
|
throw new Error(
|
|
2561
2588
|
"Scalar enums cannot be inlined and must be introduced via a defined type. Ensure you are not inlining a defined type that is a scalar enum through a visitor."
|
|
@@ -2571,7 +2598,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2571
2598
|
});
|
|
2572
2599
|
}
|
|
2573
2600
|
const mergedManifest = mergeTypeManifests(
|
|
2574
|
-
enumType.variants.map((variant) =>
|
|
2601
|
+
enumType.variants.map((variant) => visitorsCore.visit(variant, self)),
|
|
2575
2602
|
{
|
|
2576
2603
|
mergeCodecs: (renders) => renders.join(", "),
|
|
2577
2604
|
mergeTypes: (renders) => renders.join(" | ")
|
|
@@ -2589,14 +2616,14 @@ function getTypeManifestVisitor(input) {
|
|
|
2589
2616
|
const enumFunction = nameApi.discriminatedUnionFunction(node.enum.name);
|
|
2590
2617
|
const importFrom = getImportFrom(node.enum);
|
|
2591
2618
|
const enumNode = linkables.get([...stack.getPath(), node.enum])?.type;
|
|
2592
|
-
const isScalar = enumNode &&
|
|
2619
|
+
const isScalar = enumNode && nodes.isNode(enumNode, "enumTypeNode") ? nodes.isScalarEnum(enumNode) : !nonScalarEnums.includes(node.enum.name);
|
|
2593
2620
|
if (!node.value && isScalar) {
|
|
2594
2621
|
const variantName2 = nameApi.enumVariant(node.variant);
|
|
2595
2622
|
return typeManifest({
|
|
2596
2623
|
...manifest,
|
|
2597
|
-
value:
|
|
2624
|
+
value: visitorsCore.pipe(
|
|
2598
2625
|
manifest.value,
|
|
2599
|
-
(f) =>
|
|
2626
|
+
(f) => renderersCore.setFragmentContent(f, `${enumName}.${variantName2}`),
|
|
2600
2627
|
(f) => addFragmentImports(f, importFrom, [enumName])
|
|
2601
2628
|
)
|
|
2602
2629
|
});
|
|
@@ -2605,24 +2632,24 @@ function getTypeManifestVisitor(input) {
|
|
|
2605
2632
|
if (!node.value) {
|
|
2606
2633
|
return typeManifest({
|
|
2607
2634
|
...manifest,
|
|
2608
|
-
value:
|
|
2635
|
+
value: visitorsCore.pipe(
|
|
2609
2636
|
manifest.value,
|
|
2610
|
-
(f) =>
|
|
2637
|
+
(f) => renderersCore.setFragmentContent(f, `${enumFunction}('${variantName}')`),
|
|
2611
2638
|
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2612
2639
|
)
|
|
2613
2640
|
});
|
|
2614
2641
|
}
|
|
2615
2642
|
return typeManifest({
|
|
2616
2643
|
...manifest,
|
|
2617
|
-
value:
|
|
2618
|
-
|
|
2619
|
-
(f) =>
|
|
2644
|
+
value: visitorsCore.pipe(
|
|
2645
|
+
visitorsCore.visit(node.value, self).value,
|
|
2646
|
+
(f) => renderersCore.mapFragmentContent(f, (c) => `${enumFunction}('${variantName}', ${c})`),
|
|
2620
2647
|
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2621
2648
|
)
|
|
2622
2649
|
});
|
|
2623
2650
|
},
|
|
2624
2651
|
visitFixedSizeType(node, { self }) {
|
|
2625
|
-
const manifest =
|
|
2652
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2626
2653
|
return typeManifest({
|
|
2627
2654
|
...manifest,
|
|
2628
2655
|
decoder: fragment`${use("fixDecoderSize", "solanaCodecsCore")}(${manifest.decoder}, ${node.size})`,
|
|
@@ -2630,13 +2657,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2630
2657
|
});
|
|
2631
2658
|
},
|
|
2632
2659
|
visitHiddenPrefixType(node, { self }) {
|
|
2633
|
-
const manifest =
|
|
2634
|
-
const prefixes = node.prefix.map((c) =>
|
|
2635
|
-
const prefixEncoders =
|
|
2660
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2661
|
+
const prefixes = node.prefix.map((c) => visitorsCore.visit(c, self).value);
|
|
2662
|
+
const prefixEncoders = visitorsCore.pipe(
|
|
2636
2663
|
mergeFragments(prefixes, (cs) => cs.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2637
2664
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2638
2665
|
);
|
|
2639
|
-
const prefixDecoders =
|
|
2666
|
+
const prefixDecoders = visitorsCore.pipe(
|
|
2640
2667
|
mergeFragments(prefixes, (cs) => cs.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2641
2668
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2642
2669
|
);
|
|
@@ -2647,13 +2674,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2647
2674
|
});
|
|
2648
2675
|
},
|
|
2649
2676
|
visitHiddenSuffixType(node, { self }) {
|
|
2650
|
-
const manifest =
|
|
2651
|
-
const suffixes = node.suffix.map((c) =>
|
|
2652
|
-
const suffixEncoders =
|
|
2677
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2678
|
+
const suffixes = node.suffix.map((c) => visitorsCore.visit(c, self).value);
|
|
2679
|
+
const suffixEncoders = visitorsCore.pipe(
|
|
2653
2680
|
mergeFragments(suffixes, (cs) => cs.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2654
2681
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2655
2682
|
);
|
|
2656
|
-
const suffixDecoders =
|
|
2683
|
+
const suffixDecoders = visitorsCore.pipe(
|
|
2657
2684
|
mergeFragments(suffixes, (cs) => cs.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2658
2685
|
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2659
2686
|
);
|
|
@@ -2670,19 +2697,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2670
2697
|
strict: nameApi.dataType(instructionDataName)
|
|
2671
2698
|
};
|
|
2672
2699
|
const link = customInstructionData.get(instruction.name)?.linkNode;
|
|
2673
|
-
const struct =
|
|
2674
|
-
const manifest = link ?
|
|
2700
|
+
const struct = nodes.structTypeNodeFromInstructionArgumentNodes(instruction.arguments);
|
|
2701
|
+
const manifest = link ? visitorsCore.visit(link, self) : visitorsCore.visit(struct, self);
|
|
2675
2702
|
parentName = null;
|
|
2676
2703
|
return manifest;
|
|
2677
2704
|
},
|
|
2678
2705
|
visitMapEntryValue(node, { self }) {
|
|
2679
|
-
return mergeTypeManifests([
|
|
2706
|
+
return mergeTypeManifests([visitorsCore.visit(node.key, self), visitorsCore.visit(node.value, self)], {
|
|
2680
2707
|
mergeValues: (renders) => `[${renders.join(", ")}]`
|
|
2681
2708
|
});
|
|
2682
2709
|
},
|
|
2683
2710
|
visitMapType(mapType, { self }) {
|
|
2684
|
-
const key =
|
|
2685
|
-
const value =
|
|
2711
|
+
const key = visitorsCore.visit(mapType.key, self);
|
|
2712
|
+
const value = visitorsCore.visit(mapType.value, self);
|
|
2686
2713
|
const mergedManifest = mergeTypeManifests([key, value], {
|
|
2687
2714
|
mergeCodecs: ([k, v]) => `${k}, ${v}`,
|
|
2688
2715
|
mergeTypes: ([k, v]) => `Map<${k}, ${v}>`
|
|
@@ -2697,7 +2724,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2697
2724
|
});
|
|
2698
2725
|
},
|
|
2699
2726
|
visitMapValue(node, { self }) {
|
|
2700
|
-
const entryFragments = node.entries.map((entry) =>
|
|
2727
|
+
const entryFragments = node.entries.map((entry) => visitorsCore.visit(entry, self));
|
|
2701
2728
|
return mergeTypeManifests(entryFragments, {
|
|
2702
2729
|
mergeValues: (renders) => `new Map([${renders.join(", ")}])`
|
|
2703
2730
|
});
|
|
@@ -2723,12 +2750,12 @@ function getTypeManifestVisitor(input) {
|
|
|
2723
2750
|
return typeManifest({ value: fragment`${JSON.stringify(node.number)}` });
|
|
2724
2751
|
},
|
|
2725
2752
|
visitOptionType(optionType, { self }) {
|
|
2726
|
-
const childManifest =
|
|
2753
|
+
const childManifest = visitorsCore.visit(optionType.item, self);
|
|
2727
2754
|
const encoderOptions = [];
|
|
2728
2755
|
const decoderOptions = [];
|
|
2729
|
-
const optionPrefix =
|
|
2756
|
+
const optionPrefix = nodes.resolveNestedTypeNode(optionType.prefix);
|
|
2730
2757
|
if (optionPrefix.format !== "u8" || optionPrefix.endian !== "le") {
|
|
2731
|
-
const prefixManifest =
|
|
2758
|
+
const prefixManifest = visitorsCore.visit(optionType.prefix, self);
|
|
2732
2759
|
encoderOptions.push(fragment`prefix: ${prefixManifest.encoder}`);
|
|
2733
2760
|
decoderOptions.push(fragment`prefix: ${prefixManifest.decoder}`);
|
|
2734
2761
|
}
|
|
@@ -2753,7 +2780,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2753
2780
|
});
|
|
2754
2781
|
},
|
|
2755
2782
|
visitPostOffsetType(node, { self }) {
|
|
2756
|
-
const manifest =
|
|
2783
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2757
2784
|
if (node.strategy === "padded") {
|
|
2758
2785
|
return typeManifest({
|
|
2759
2786
|
...manifest,
|
|
@@ -2779,7 +2806,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2779
2806
|
});
|
|
2780
2807
|
},
|
|
2781
2808
|
visitPreOffsetType(node, { self }) {
|
|
2782
|
-
const manifest =
|
|
2809
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2783
2810
|
if (node.strategy === "padded") {
|
|
2784
2811
|
return typeManifest({
|
|
2785
2812
|
...manifest,
|
|
@@ -2816,7 +2843,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2816
2843
|
});
|
|
2817
2844
|
},
|
|
2818
2845
|
visitRemainderOptionType(node, { self }) {
|
|
2819
|
-
const childManifest =
|
|
2846
|
+
const childManifest = visitorsCore.visit(node.item, self);
|
|
2820
2847
|
const encoderOptions = ["prefix: null"];
|
|
2821
2848
|
const decoderOptions = ["prefix: null"];
|
|
2822
2849
|
const encoderOptionsAsString = encoderOptions.length > 0 ? `, { ${encoderOptions.join(", ")} }` : "";
|
|
@@ -2830,8 +2857,8 @@ function getTypeManifestVisitor(input) {
|
|
|
2830
2857
|
});
|
|
2831
2858
|
},
|
|
2832
2859
|
visitSentinelType(node, { self }) {
|
|
2833
|
-
const manifest =
|
|
2834
|
-
const sentinel =
|
|
2860
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2861
|
+
const sentinel = visitorsCore.visit(node.sentinel, self).value;
|
|
2835
2862
|
return typeManifest({
|
|
2836
2863
|
...manifest,
|
|
2837
2864
|
decoder: fragment`${use("addDecoderSentinel", "solanaCodecsCore")}(${manifest.decoder}, ${sentinel})`,
|
|
@@ -2839,7 +2866,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2839
2866
|
});
|
|
2840
2867
|
},
|
|
2841
2868
|
visitSetType(setType, { self }) {
|
|
2842
|
-
const childManifest =
|
|
2869
|
+
const childManifest = visitorsCore.visit(setType.item, self);
|
|
2843
2870
|
const sizeManifest = getArrayLikeSizeOption(setType.count, self);
|
|
2844
2871
|
const encoderOptions = sizeManifest.encoder ? fragment`, { ${sizeManifest.encoder} }` : "";
|
|
2845
2872
|
const decoderOptions = sizeManifest.decoder ? fragment`, { ${sizeManifest.decoder} }` : "";
|
|
@@ -2853,13 +2880,13 @@ function getTypeManifestVisitor(input) {
|
|
|
2853
2880
|
},
|
|
2854
2881
|
visitSetValue(node, { self }) {
|
|
2855
2882
|
return mergeTypeManifests(
|
|
2856
|
-
node.items.map((v) =>
|
|
2883
|
+
node.items.map((v) => visitorsCore.visit(v, self)),
|
|
2857
2884
|
{ mergeValues: (renders) => `new Set([${renders.join(", ")}])` }
|
|
2858
2885
|
);
|
|
2859
2886
|
},
|
|
2860
2887
|
visitSizePrefixType(node, { self }) {
|
|
2861
|
-
const manifest =
|
|
2862
|
-
const prefix =
|
|
2888
|
+
const manifest = visitorsCore.visit(node.type, self);
|
|
2889
|
+
const prefix = visitorsCore.visit(node.prefix, self);
|
|
2863
2890
|
return typeManifest({
|
|
2864
2891
|
...manifest,
|
|
2865
2892
|
decoder: fragment`${use("addDecoderSizePrefix", "solanaCodecsCore")}(${manifest.decoder}, ${prefix.decoder})`,
|
|
@@ -2867,7 +2894,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2867
2894
|
});
|
|
2868
2895
|
},
|
|
2869
2896
|
visitSolAmountType({ number }, { self }) {
|
|
2870
|
-
const numberManifest =
|
|
2897
|
+
const numberManifest = visitorsCore.visit(number, self);
|
|
2871
2898
|
return typeManifest({
|
|
2872
2899
|
...numberManifest,
|
|
2873
2900
|
decoder: fragment`${use("getLamportsDecoder", "solanaRpcTypes")}(${numberManifest.decoder})`,
|
|
@@ -2877,7 +2904,7 @@ function getTypeManifestVisitor(input) {
|
|
|
2877
2904
|
});
|
|
2878
2905
|
},
|
|
2879
2906
|
visitSomeValue(node, { self }) {
|
|
2880
|
-
const innerValue =
|
|
2907
|
+
const innerValue = visitorsCore.visit(node.value, self).value;
|
|
2881
2908
|
return typeManifest({
|
|
2882
2909
|
value: fragment`${use("some", "solanaOptions")}(${innerValue})`
|
|
2883
2910
|
});
|
|
@@ -2910,8 +2937,8 @@ function getTypeManifestVisitor(input) {
|
|
|
2910
2937
|
});
|
|
2911
2938
|
},
|
|
2912
2939
|
visitStructFieldType(structFieldType, { self }) {
|
|
2913
|
-
const name =
|
|
2914
|
-
const originalChildManifest =
|
|
2940
|
+
const name = nodes.camelCase(structFieldType.name);
|
|
2941
|
+
const originalChildManifest = visitorsCore.visit(structFieldType.type, self);
|
|
2915
2942
|
let docs = getDocblockFragment(structFieldType.docs ?? [], true);
|
|
2916
2943
|
docs = docs ? fragment`\n${docs}` : docs;
|
|
2917
2944
|
const childManifest = typeManifest({
|
|
@@ -2933,16 +2960,16 @@ function getTypeManifestVisitor(input) {
|
|
|
2933
2960
|
return typeManifest({ ...childManifest, looseType: fragment`` });
|
|
2934
2961
|
},
|
|
2935
2962
|
visitStructFieldValue(node, { self }) {
|
|
2936
|
-
const innerValue =
|
|
2963
|
+
const innerValue = visitorsCore.visit(node.value, self).value;
|
|
2937
2964
|
return typeManifest({
|
|
2938
2965
|
value: fragment`${node.name}: ${innerValue}`
|
|
2939
2966
|
});
|
|
2940
2967
|
},
|
|
2941
2968
|
visitStructType(structType, { self }) {
|
|
2942
2969
|
const optionalFields = structType.fields.filter((f) => !!f.defaultValue);
|
|
2943
|
-
const mergedManifest =
|
|
2970
|
+
const mergedManifest = visitorsCore.pipe(
|
|
2944
2971
|
mergeTypeManifests(
|
|
2945
|
-
structType.fields.map((field) =>
|
|
2972
|
+
structType.fields.map((field) => visitorsCore.visit(field, self)),
|
|
2946
2973
|
{
|
|
2947
2974
|
mergeCodecs: (renders) => `([${renders.join(", ")}])`,
|
|
2948
2975
|
mergeTypes: (renders) => `{ ${renders.join("")} }`
|
|
@@ -2958,20 +2985,20 @@ function getTypeManifestVisitor(input) {
|
|
|
2958
2985
|
return mergedManifest;
|
|
2959
2986
|
}
|
|
2960
2987
|
const parentPath = stack.getPath();
|
|
2961
|
-
const instructionNode =
|
|
2962
|
-
const accountNode =
|
|
2988
|
+
const instructionNode = visitorsCore.findLastNodeFromPath(parentPath, "instructionNode");
|
|
2989
|
+
const accountNode = visitorsCore.findLastNodeFromPath(parentPath, "accountNode");
|
|
2963
2990
|
const discriminatorPrefix = instructionNode ? instructionNode.name : accountNode?.name;
|
|
2964
2991
|
const discriminators = (instructionNode ? instructionNode.discriminators : accountNode?.discriminators) ?? [];
|
|
2965
|
-
const fieldDiscriminators = discriminators.filter(
|
|
2992
|
+
const fieldDiscriminators = discriminators.filter(nodes.isNodeFilter("fieldDiscriminatorNode"));
|
|
2966
2993
|
const defaultValues = mergeFragments(
|
|
2967
2994
|
optionalFields.map((f) => {
|
|
2968
|
-
const key =
|
|
2995
|
+
const key = nodes.camelCase(f.name);
|
|
2969
2996
|
if (fieldDiscriminators.some((d) => d.name === f.name)) {
|
|
2970
|
-
const constantName = nameApi.constant(
|
|
2997
|
+
const constantName = nameApi.constant(nodes.camelCase(`${discriminatorPrefix}_${f.name}`));
|
|
2971
2998
|
return f.defaultValueStrategy === "omitted" ? fragment`${key}: ${constantName}` : fragment`${key}: value.${key} ?? ${constantName}`;
|
|
2972
2999
|
}
|
|
2973
3000
|
const defaultValue = f.defaultValue;
|
|
2974
|
-
const value =
|
|
3001
|
+
const value = visitorsCore.visit(defaultValue, self).value;
|
|
2975
3002
|
return f.defaultValueStrategy === "omitted" ? fragment`${key}: ${value}` : fragment`${key}: value.${key} ?? ${value}`;
|
|
2976
3003
|
}),
|
|
2977
3004
|
(cs) => cs.join(", ")
|
|
@@ -2983,12 +3010,12 @@ function getTypeManifestVisitor(input) {
|
|
|
2983
3010
|
},
|
|
2984
3011
|
visitStructValue(node, { self }) {
|
|
2985
3012
|
return mergeTypeManifests(
|
|
2986
|
-
node.fields.map((field) =>
|
|
3013
|
+
node.fields.map((field) => visitorsCore.visit(field, self)),
|
|
2987
3014
|
{ mergeValues: (renders) => `{ ${renders.join(", ")} }` }
|
|
2988
3015
|
);
|
|
2989
3016
|
},
|
|
2990
3017
|
visitTupleType(tupleType, { self }) {
|
|
2991
|
-
const items = tupleType.items.map((item) =>
|
|
3018
|
+
const items = tupleType.items.map((item) => visitorsCore.visit(item, self));
|
|
2992
3019
|
const mergedManifest = mergeTypeManifests(items, {
|
|
2993
3020
|
mergeCodecs: (codecs) => `[${codecs.join(", ")}]`,
|
|
2994
3021
|
mergeTypes: (types) => `readonly [${types.join(", ")}]`
|
|
@@ -3001,16 +3028,16 @@ function getTypeManifestVisitor(input) {
|
|
|
3001
3028
|
},
|
|
3002
3029
|
visitTupleValue(node, { self }) {
|
|
3003
3030
|
return mergeTypeManifests(
|
|
3004
|
-
node.items.map((v) =>
|
|
3031
|
+
node.items.map((v) => visitorsCore.visit(v, self)),
|
|
3005
3032
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
3006
3033
|
);
|
|
3007
3034
|
},
|
|
3008
3035
|
visitZeroableOptionType(node, { self }) {
|
|
3009
|
-
const childManifest =
|
|
3036
|
+
const childManifest = visitorsCore.visit(node.item, self);
|
|
3010
3037
|
const encoderOptions = [fragment`prefix: null`];
|
|
3011
3038
|
const decoderOptions = [fragment`prefix: null`];
|
|
3012
3039
|
if (node.zeroValue) {
|
|
3013
|
-
const zeroValueManifest =
|
|
3040
|
+
const zeroValueManifest = visitorsCore.visit(node.zeroValue, self);
|
|
3014
3041
|
encoderOptions.push(fragment`noneValue: ${zeroValueManifest.value}`);
|
|
3015
3042
|
decoderOptions.push(fragment`noneValue: ${zeroValueManifest.value}`);
|
|
3016
3043
|
} else {
|
|
@@ -3034,56 +3061,57 @@ function getTypeManifestVisitor(input) {
|
|
|
3034
3061
|
});
|
|
3035
3062
|
}
|
|
3036
3063
|
}),
|
|
3037
|
-
(visitor) =>
|
|
3064
|
+
(visitor) => visitorsCore.recordNodeStackVisitor(visitor, stack)
|
|
3038
3065
|
);
|
|
3039
3066
|
}
|
|
3040
3067
|
function getArrayLikeSizeOption(count, visitor) {
|
|
3041
|
-
if (
|
|
3068
|
+
if (nodes.isNode(count, "fixedCountNode")) {
|
|
3042
3069
|
return {
|
|
3043
3070
|
decoder: fragment`size: ${count.value}`,
|
|
3044
3071
|
encoder: fragment`size: ${count.value}`
|
|
3045
3072
|
};
|
|
3046
3073
|
}
|
|
3047
|
-
if (
|
|
3074
|
+
if (nodes.isNode(count, "remainderCountNode")) {
|
|
3048
3075
|
return {
|
|
3049
3076
|
decoder: fragment`size: 'remainder'`,
|
|
3050
3077
|
encoder: fragment`size: 'remainder'`
|
|
3051
3078
|
};
|
|
3052
3079
|
}
|
|
3053
|
-
const prefix =
|
|
3080
|
+
const prefix = nodes.resolveNestedTypeNode(count.prefix);
|
|
3054
3081
|
if (prefix.format === "u32" && prefix.endian === "le") {
|
|
3055
3082
|
return { decoder: void 0, encoder: void 0 };
|
|
3056
3083
|
}
|
|
3057
|
-
const prefixManifest =
|
|
3084
|
+
const prefixManifest = visitorsCore.visit(count.prefix, visitor);
|
|
3058
3085
|
return {
|
|
3059
|
-
decoder:
|
|
3060
|
-
encoder:
|
|
3086
|
+
decoder: visitorsCore.pipe(prefixManifest.decoder, (f) => renderersCore.mapFragmentContent(f, (c) => `size: ${c}`)),
|
|
3087
|
+
encoder: visitorsCore.pipe(prefixManifest.encoder, (f) => renderersCore.mapFragmentContent(f, (c) => `size: ${c}`))
|
|
3061
3088
|
};
|
|
3062
3089
|
}
|
|
3063
3090
|
|
|
3064
3091
|
// src/visitors/getRenderMapVisitor.ts
|
|
3065
3092
|
function getRenderMapVisitor(options = {}) {
|
|
3066
|
-
const linkables = new
|
|
3067
|
-
const stack = new
|
|
3093
|
+
const linkables = new visitorsCore.LinkableDictionary();
|
|
3094
|
+
const stack = new visitorsCore.NodeStack();
|
|
3068
3095
|
const customAccountData = parseCustomDataOptions(options.customAccountData ?? [], "AccountData");
|
|
3069
3096
|
const customInstructionData = parseCustomDataOptions(options.customInstructionData ?? [], "InstructionData");
|
|
3070
3097
|
const renderScopeWithTypeManifestVisitor = {
|
|
3071
|
-
asyncResolvers: (options.asyncResolvers ?? []).map(
|
|
3098
|
+
asyncResolvers: (options.asyncResolvers ?? []).map(nodes.camelCase),
|
|
3072
3099
|
customAccountData,
|
|
3073
3100
|
customInstructionData,
|
|
3074
3101
|
dependencyMap: options.dependencyMap ?? {},
|
|
3102
|
+
dependencyVersions: options.dependencyVersions ?? {},
|
|
3075
3103
|
getImportFrom: getImportFromFactory(options.linkOverrides ?? {}, customAccountData, customInstructionData),
|
|
3076
3104
|
linkables,
|
|
3077
3105
|
nameApi: getNameApi({ ...DEFAULT_NAME_TRANSFORMERS, ...options.nameTransformers }),
|
|
3078
|
-
nonScalarEnums: (options.nonScalarEnums ?? []).map(
|
|
3106
|
+
nonScalarEnums: (options.nonScalarEnums ?? []).map(nodes.camelCase),
|
|
3079
3107
|
renderParentInstructions: options.renderParentInstructions ?? false,
|
|
3080
3108
|
useGranularImports: options.useGranularImports ?? false
|
|
3081
3109
|
};
|
|
3082
3110
|
const typeManifestVisitor = getTypeManifestVisitor({ ...renderScopeWithTypeManifestVisitor, stack });
|
|
3083
3111
|
const renderScope = { ...renderScopeWithTypeManifestVisitor, typeManifestVisitor };
|
|
3084
|
-
const internalNodes = (options.internalNodes ?? []).map(
|
|
3085
|
-
const resolvedInstructionInputVisitor =
|
|
3086
|
-
const byteSizeVisitor =
|
|
3112
|
+
const internalNodes = (options.internalNodes ?? []).map(nodes.camelCase);
|
|
3113
|
+
const resolvedInstructionInputVisitor = visitorsCore.getResolvedInstructionInputsVisitor();
|
|
3114
|
+
const byteSizeVisitor = visitorsCore.getByteSizeVisitor(linkables, { stack });
|
|
3087
3115
|
const asPage = (fragment2, dependencyMap = {}) => {
|
|
3088
3116
|
if (!fragment2) return void 0;
|
|
3089
3117
|
return getPageFragment(fragment2, {
|
|
@@ -3091,47 +3119,47 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3091
3119
|
dependencyMap: { ...renderScope.dependencyMap, ...dependencyMap }
|
|
3092
3120
|
});
|
|
3093
3121
|
};
|
|
3094
|
-
return
|
|
3095
|
-
|
|
3122
|
+
return visitorsCore.pipe(
|
|
3123
|
+
visitorsCore.staticVisitor(() => renderersCore.createRenderMap(), {
|
|
3096
3124
|
keys: ["rootNode", "programNode", "pdaNode", "accountNode", "definedTypeNode", "instructionNode"]
|
|
3097
3125
|
}),
|
|
3098
|
-
(v) =>
|
|
3126
|
+
(v) => visitorsCore.extendVisitor(v, {
|
|
3099
3127
|
visitAccount(node) {
|
|
3100
|
-
return
|
|
3101
|
-
`accounts/${
|
|
3128
|
+
return renderersCore.createRenderMap(
|
|
3129
|
+
`accounts/${nodes.camelCase(node.name)}.ts`,
|
|
3102
3130
|
asPage(
|
|
3103
3131
|
getAccountPageFragment({
|
|
3104
3132
|
...renderScope,
|
|
3105
3133
|
accountPath: stack.getPath("accountNode"),
|
|
3106
|
-
size:
|
|
3134
|
+
size: visitorsCore.visit(node, byteSizeVisitor)
|
|
3107
3135
|
})
|
|
3108
3136
|
)
|
|
3109
3137
|
);
|
|
3110
3138
|
},
|
|
3111
3139
|
visitDefinedType(node) {
|
|
3112
|
-
return
|
|
3113
|
-
`types/${
|
|
3114
|
-
asPage(getTypePageFragment({ ...renderScope, node, size:
|
|
3140
|
+
return renderersCore.createRenderMap(
|
|
3141
|
+
`types/${nodes.camelCase(node.name)}.ts`,
|
|
3142
|
+
asPage(getTypePageFragment({ ...renderScope, node, size: visitorsCore.visit(node, byteSizeVisitor) }), {
|
|
3115
3143
|
generatedTypes: "."
|
|
3116
3144
|
})
|
|
3117
3145
|
);
|
|
3118
3146
|
},
|
|
3119
3147
|
visitInstruction(node) {
|
|
3120
|
-
return
|
|
3121
|
-
`instructions/${
|
|
3148
|
+
return renderersCore.createRenderMap(
|
|
3149
|
+
`instructions/${nodes.camelCase(node.name)}.ts`,
|
|
3122
3150
|
asPage(
|
|
3123
3151
|
getInstructionPageFragment({
|
|
3124
3152
|
...renderScope,
|
|
3125
3153
|
instructionPath: stack.getPath("instructionNode"),
|
|
3126
|
-
resolvedInputs:
|
|
3127
|
-
size:
|
|
3154
|
+
resolvedInputs: visitorsCore.visit(node, resolvedInstructionInputVisitor),
|
|
3155
|
+
size: visitorsCore.visit(node, byteSizeVisitor)
|
|
3128
3156
|
})
|
|
3129
3157
|
)
|
|
3130
3158
|
);
|
|
3131
3159
|
},
|
|
3132
3160
|
visitPda(node) {
|
|
3133
|
-
return
|
|
3134
|
-
`pdas/${
|
|
3161
|
+
return renderersCore.createRenderMap(
|
|
3162
|
+
`pdas/${nodes.camelCase(node.name)}.ts`,
|
|
3135
3163
|
asPage(getPdaPageFragment({ ...renderScope, pdaPath: stack.getPath("pdaNode") }))
|
|
3136
3164
|
);
|
|
3137
3165
|
},
|
|
@@ -3141,41 +3169,40 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3141
3169
|
...getDefinedTypeNodesToExtract(node.instructions, customInstructionData)
|
|
3142
3170
|
];
|
|
3143
3171
|
const scope = { ...renderScope, programNode: node };
|
|
3144
|
-
return
|
|
3145
|
-
|
|
3146
|
-
[`programs/${
|
|
3147
|
-
[`errors/${
|
|
3172
|
+
return renderersCore.mergeRenderMaps([
|
|
3173
|
+
renderersCore.createRenderMap({
|
|
3174
|
+
[`programs/${nodes.camelCase(node.name)}.ts`]: asPage(getProgramPageFragment(scope)),
|
|
3175
|
+
[`errors/${nodes.camelCase(node.name)}.ts`]: node.errors.length > 0 ? asPage(getErrorPageFragment(scope)) : void 0
|
|
3148
3176
|
}),
|
|
3149
|
-
...node.pdas.map((p) =>
|
|
3150
|
-
...node.accounts.map((a) =>
|
|
3151
|
-
...node.definedTypes.map((t) =>
|
|
3152
|
-
...customDataDefinedType.map((t) =>
|
|
3153
|
-
...
|
|
3154
|
-
(i) =>
|
|
3177
|
+
...node.pdas.map((p) => visitorsCore.visit(p, self)),
|
|
3178
|
+
...node.accounts.map((a) => visitorsCore.visit(a, self)),
|
|
3179
|
+
...node.definedTypes.map((t) => visitorsCore.visit(t, self)),
|
|
3180
|
+
...customDataDefinedType.map((t) => visitorsCore.visit(t, self)),
|
|
3181
|
+
...nodes.getAllInstructionsWithSubs(node, { leavesOnly: !renderScope.renderParentInstructions }).map(
|
|
3182
|
+
(i) => visitorsCore.visit(i, self)
|
|
3155
3183
|
)
|
|
3156
3184
|
]);
|
|
3157
3185
|
},
|
|
3158
3186
|
visitRoot(node, { self }) {
|
|
3159
3187
|
const isNotInternal = (n) => !internalNodes.includes(n.name);
|
|
3160
|
-
const programsToExport =
|
|
3188
|
+
const programsToExport = nodes.getAllPrograms(node).filter(isNotInternal);
|
|
3161
3189
|
const programsWithErrorsToExport = programsToExport.filter((p) => p.errors.length > 0);
|
|
3162
|
-
const pdasToExport =
|
|
3163
|
-
const accountsToExport =
|
|
3164
|
-
const instructionsToExport =
|
|
3190
|
+
const pdasToExport = nodes.getAllPdas(node);
|
|
3191
|
+
const accountsToExport = nodes.getAllAccounts(node).filter(isNotInternal);
|
|
3192
|
+
const instructionsToExport = nodes.getAllInstructionsWithSubs(node, {
|
|
3165
3193
|
leavesOnly: !renderScope.renderParentInstructions
|
|
3166
3194
|
}).filter(isNotInternal);
|
|
3167
|
-
const definedTypesToExport =
|
|
3195
|
+
const definedTypesToExport = nodes.getAllDefinedTypes(node).filter(isNotInternal);
|
|
3168
3196
|
const hasAnythingToExport = programsToExport.length > 0 || accountsToExport.length > 0 || instructionsToExport.length > 0 || definedTypesToExport.length > 0;
|
|
3169
3197
|
const scope = {
|
|
3170
|
-
...renderScope,
|
|
3171
3198
|
accountsToExport,
|
|
3172
3199
|
definedTypesToExport,
|
|
3173
3200
|
instructionsToExport,
|
|
3174
3201
|
pdasToExport,
|
|
3175
3202
|
programsToExport
|
|
3176
3203
|
};
|
|
3177
|
-
return
|
|
3178
|
-
|
|
3204
|
+
return renderersCore.mergeRenderMaps([
|
|
3205
|
+
renderersCore.createRenderMap({
|
|
3179
3206
|
["accounts/index.ts"]: asPage(getIndexPageFragment(accountsToExport)),
|
|
3180
3207
|
["errors/index.ts"]: asPage(getIndexPageFragment(programsWithErrorsToExport)),
|
|
3181
3208
|
["index.ts"]: asPage(getRootIndexPageFragment(scope)),
|
|
@@ -3185,59 +3212,40 @@ function getRenderMapVisitor(options = {}) {
|
|
|
3185
3212
|
["shared/index.ts"]: hasAnythingToExport ? asPage(getSharedPageFragment()) : void 0,
|
|
3186
3213
|
["types/index.ts"]: asPage(getIndexPageFragment(definedTypesToExport))
|
|
3187
3214
|
}),
|
|
3188
|
-
...
|
|
3215
|
+
...nodes.getAllPrograms(node).map((p) => visitorsCore.visit(p, self))
|
|
3189
3216
|
]);
|
|
3190
3217
|
}
|
|
3191
3218
|
}),
|
|
3192
|
-
(v) =>
|
|
3193
|
-
(v) =>
|
|
3219
|
+
(v) => visitorsCore.recordNodeStackVisitor(v, stack),
|
|
3220
|
+
(v) => visitorsCore.recordLinkablesOnFirstVisitVisitor(v, linkables)
|
|
3194
3221
|
);
|
|
3195
3222
|
}
|
|
3196
|
-
|
|
3197
|
-
// src/visitors/renderVisitor.ts
|
|
3198
|
-
var import_renderers_core14 = require("@codama/renderers-core");
|
|
3199
|
-
var import_visitors_core30 = require("@codama/visitors-core");
|
|
3200
|
-
var estreePlugin = __toESM(require("prettier/plugins/estree"));
|
|
3201
|
-
var typeScriptPlugin = __toESM(require("prettier/plugins/typescript"));
|
|
3202
|
-
var import_standalone = require("prettier/standalone");
|
|
3203
|
-
var DEFAULT_PRETTIER_OPTIONS = {
|
|
3204
|
-
arrowParens: "always",
|
|
3205
|
-
parser: "typescript",
|
|
3206
|
-
plugins: [estreePlugin, typeScriptPlugin],
|
|
3207
|
-
printWidth: 80,
|
|
3208
|
-
semi: true,
|
|
3209
|
-
singleQuote: true,
|
|
3210
|
-
tabWidth: 2,
|
|
3211
|
-
trailingComma: "es5",
|
|
3212
|
-
useTabs: false
|
|
3213
|
-
};
|
|
3214
3223
|
function renderVisitor(path, options = {}) {
|
|
3215
|
-
return
|
|
3224
|
+
return visitorsCore.rootNodeVisitor(async (root) => {
|
|
3216
3225
|
if (options.deleteFolderBeforeRendering ?? true) {
|
|
3217
|
-
|
|
3226
|
+
renderersCore.deleteDirectory(path);
|
|
3218
3227
|
}
|
|
3219
|
-
let renderMap =
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
}
|
|
3224
|
-
(0, import_renderers_core14.writeRenderMap)(renderMap, path);
|
|
3228
|
+
let renderMap = visitorsCore.visit(root, getRenderMapVisitor(options));
|
|
3229
|
+
renderMap = await formatCode(renderMap, options);
|
|
3230
|
+
syncPackageJson(renderMap, options);
|
|
3231
|
+
renderersCore.writeRenderMap(renderMap, path);
|
|
3225
3232
|
});
|
|
3226
3233
|
}
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3234
|
+
|
|
3235
|
+
exports.DEFAULT_NAME_TRANSFORMERS = DEFAULT_NAME_TRANSFORMERS;
|
|
3236
|
+
exports.addToImportMap = addToImportMap;
|
|
3237
|
+
exports.createImportMap = createImportMap;
|
|
3238
|
+
exports.default = renderVisitor;
|
|
3239
|
+
exports.getExternalDependencies = getExternalDependencies;
|
|
3240
|
+
exports.getNameApi = getNameApi;
|
|
3241
|
+
exports.getRenderMapVisitor = getRenderMapVisitor;
|
|
3242
|
+
exports.getTypeManifestVisitor = getTypeManifestVisitor;
|
|
3243
|
+
exports.importMapToString = importMapToString;
|
|
3244
|
+
exports.mergeImportMaps = mergeImportMaps;
|
|
3245
|
+
exports.mergeTypeManifests = mergeTypeManifests;
|
|
3246
|
+
exports.parseImportInput = parseImportInput;
|
|
3247
|
+
exports.removeFromImportMap = removeFromImportMap;
|
|
3248
|
+
exports.renderVisitor = renderVisitor;
|
|
3249
|
+
exports.typeManifest = typeManifest;
|
|
3250
|
+
//# sourceMappingURL=index.node.cjs.map
|
|
3243
3251
|
//# sourceMappingURL=index.node.cjs.map
|