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