@codama/renderers-js 1.3.3 → 1.3.5
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/dist/index.node.cjs +1543 -965
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +1459 -878
- package/dist/index.node.mjs.map +1 -1
- package/dist/templates/fragments/instructionExtraArgs.njk +1 -1
- package/dist/templates/fragments/pdaFunction.njk +4 -3
- package/dist/templates/fragments/type.njk +3 -3
- package/dist/templates/fragments/typeDecoder.njk +1 -1
- package/dist/templates/fragments/typeEncoder.njk +1 -1
- package/dist/types/ImportMap.d.ts +1 -1
- package/dist/types/ImportMap.d.ts.map +1 -1
- package/dist/types/TypeManifest.d.ts +5 -5
- package/dist/types/TypeManifest.d.ts.map +1 -1
- package/dist/types/fragments/accountFetchHelpers.d.ts +1 -1
- package/dist/types/fragments/accountFetchHelpers.d.ts.map +1 -1
- package/dist/types/fragments/accountPdaHelpers.d.ts +1 -1
- package/dist/types/fragments/accountPdaHelpers.d.ts.map +1 -1
- package/dist/types/fragments/accountSizeHelpers.d.ts +1 -1
- package/dist/types/fragments/accountType.d.ts +1 -1
- package/dist/types/fragments/discriminatorCondition.d.ts +1 -1
- package/dist/types/fragments/discriminatorCondition.d.ts.map +1 -1
- package/dist/types/fragments/discriminatorConstants.d.ts +1 -1
- package/dist/types/fragments/discriminatorConstants.d.ts.map +1 -1
- package/dist/types/fragments/index.d.ts +0 -1
- package/dist/types/fragments/index.d.ts.map +1 -1
- package/dist/types/fragments/instructionAccountMeta.d.ts +1 -1
- package/dist/types/fragments/instructionAccountMeta.d.ts.map +1 -1
- package/dist/types/fragments/instructionAccountTypeParam.d.ts +1 -1
- package/dist/types/fragments/instructionAccountTypeParam.d.ts.map +1 -1
- package/dist/types/fragments/instructionByteDelta.d.ts +1 -1
- package/dist/types/fragments/instructionByteDelta.d.ts.map +1 -1
- package/dist/types/fragments/instructionData.d.ts +1 -1
- package/dist/types/fragments/instructionExtraArgs.d.ts +1 -1
- package/dist/types/fragments/instructionExtraArgs.d.ts.map +1 -1
- package/dist/types/fragments/instructionFunction.d.ts +1 -1
- package/dist/types/fragments/instructionFunction.d.ts.map +1 -1
- package/dist/types/fragments/instructionInputDefault.d.ts +1 -1
- package/dist/types/fragments/instructionInputDefault.d.ts.map +1 -1
- package/dist/types/fragments/instructionInputResolved.d.ts +1 -1
- package/dist/types/fragments/instructionInputResolved.d.ts.map +1 -1
- package/dist/types/fragments/instructionInputType.d.ts +1 -1
- package/dist/types/fragments/instructionInputType.d.ts.map +1 -1
- package/dist/types/fragments/instructionParseFunction.d.ts +1 -1
- package/dist/types/fragments/instructionParseFunction.d.ts.map +1 -1
- package/dist/types/fragments/instructionRemainingAccounts.d.ts +1 -1
- package/dist/types/fragments/instructionRemainingAccounts.d.ts.map +1 -1
- package/dist/types/fragments/instructionType.d.ts +1 -1
- package/dist/types/fragments/instructionType.d.ts.map +1 -1
- package/dist/types/fragments/pdaFunction.d.ts +1 -1
- package/dist/types/fragments/pdaFunction.d.ts.map +1 -1
- package/dist/types/fragments/program.d.ts +1 -1
- package/dist/types/fragments/program.d.ts.map +1 -1
- package/dist/types/fragments/programAccounts.d.ts +1 -1
- package/dist/types/fragments/programAccounts.d.ts.map +1 -1
- package/dist/types/fragments/programErrors.d.ts +1 -1
- package/dist/types/fragments/programErrors.d.ts.map +1 -1
- package/dist/types/fragments/programInstructions.d.ts +1 -1
- package/dist/types/fragments/programInstructions.d.ts.map +1 -1
- package/dist/types/fragments/type.d.ts +1 -1
- package/dist/types/fragments/type.d.ts.map +1 -1
- package/dist/types/fragments/typeCodec.d.ts +1 -1
- package/dist/types/fragments/typeCodec.d.ts.map +1 -1
- package/dist/types/fragments/typeDecoder.d.ts +1 -1
- package/dist/types/fragments/typeDecoder.d.ts.map +1 -1
- package/dist/types/fragments/typeDiscriminatedUnionHelpers.d.ts +1 -1
- package/dist/types/fragments/typeDiscriminatedUnionHelpers.d.ts.map +1 -1
- package/dist/types/fragments/typeEncoder.d.ts +1 -1
- package/dist/types/fragments/typeEncoder.d.ts.map +1 -1
- package/dist/types/fragments/typeWithCodec.d.ts +1 -1
- package/dist/types/getRenderMapVisitor.d.ts +1 -2
- package/dist/types/getRenderMapVisitor.d.ts.map +1 -1
- package/dist/types/getTypeManifestVisitor.d.ts +9 -3
- package/dist/types/getTypeManifestVisitor.d.ts.map +1 -1
- package/dist/types/utils/fragment.d.ts +20 -0
- package/dist/types/utils/fragment.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/types/fragments/common.d.ts +0 -25
- package/dist/types/fragments/common.d.ts.map +0 -1
package/dist/index.node.mjs
CHANGED
|
@@ -131,12 +131,6 @@ var ImportMap = class {
|
|
|
131
131
|
}
|
|
132
132
|
};
|
|
133
133
|
|
|
134
|
-
// src/fragments/accountFetchHelpers.ts
|
|
135
|
-
import { getLastNodeFromPath } from "@codama/visitors-core";
|
|
136
|
-
|
|
137
|
-
// src/fragments/common.ts
|
|
138
|
-
import { join as join2 } from "path";
|
|
139
|
-
|
|
140
134
|
// src/utils/async.ts
|
|
141
135
|
import {
|
|
142
136
|
accountValueNode,
|
|
@@ -269,6 +263,74 @@ var getDefinedTypeNodesToExtract = (nodes, parsedCustomDataOptions) => nodes.fla
|
|
|
269
263
|
];
|
|
270
264
|
});
|
|
271
265
|
|
|
266
|
+
// src/utils/fragment.ts
|
|
267
|
+
import { joinPath } from "@codama/renderers-core";
|
|
268
|
+
|
|
269
|
+
// src/utils/render.ts
|
|
270
|
+
import { dirname as pathDirname, join } from "path";
|
|
271
|
+
import { fileURLToPath } from "url";
|
|
272
|
+
import { camelCase as camelCase2, kebabCase, pascalCase, snakeCase, titleCase } from "@codama/nodes";
|
|
273
|
+
import nunjucks from "nunjucks";
|
|
274
|
+
function jsDocblock(docs) {
|
|
275
|
+
if (docs.length <= 0) return "";
|
|
276
|
+
if (docs.length === 1) return `/** ${docs[0]} */
|
|
277
|
+
`;
|
|
278
|
+
const lines = docs.map((doc) => ` * ${doc}`);
|
|
279
|
+
return `/**
|
|
280
|
+
${lines.join("\n")}
|
|
281
|
+
*/
|
|
282
|
+
`;
|
|
283
|
+
}
|
|
284
|
+
var render = (template, context, options) => {
|
|
285
|
+
const dirname = true ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
|
|
286
|
+
const templates = false ? join(dirname, "..", "..", "public", "templates") : join(dirname, "templates");
|
|
287
|
+
const env = nunjucks.configure(templates, { autoescape: false, trimBlocks: true, ...options });
|
|
288
|
+
env.addFilter("pascalCase", pascalCase);
|
|
289
|
+
env.addFilter("camelCase", camelCase2);
|
|
290
|
+
env.addFilter("snakeCase", snakeCase);
|
|
291
|
+
env.addFilter("kebabCase", kebabCase);
|
|
292
|
+
env.addFilter("titleCase", titleCase);
|
|
293
|
+
env.addFilter("jsDocblock", jsDocblock);
|
|
294
|
+
return env.render(template, context);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/utils/fragment.ts
|
|
298
|
+
function fragment(content) {
|
|
299
|
+
return Object.freeze({
|
|
300
|
+
content,
|
|
301
|
+
features: /* @__PURE__ */ new Set(),
|
|
302
|
+
imports: new ImportMap()
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
function fragmentFromTemplate(fragmentFile, context, options) {
|
|
306
|
+
return fragment(render(joinPath("fragments", fragmentFile), context, options));
|
|
307
|
+
}
|
|
308
|
+
function mergeFragments(fragments, mergeContent) {
|
|
309
|
+
return Object.freeze({
|
|
310
|
+
content: mergeContent(fragments.map((fragment2) => fragment2.content)),
|
|
311
|
+
features: new Set(fragments.flatMap((f) => [...f.features])),
|
|
312
|
+
imports: new ImportMap().mergeWith(...fragments.map((f) => f.imports))
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
function mergeFragmentImports(fragment2, importMaps) {
|
|
316
|
+
return Object.freeze({
|
|
317
|
+
...fragment2,
|
|
318
|
+
imports: new ImportMap().mergeWith(fragment2.imports, ...importMaps)
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
function addFragmentImports(fragment2, module, imports) {
|
|
322
|
+
return Object.freeze({
|
|
323
|
+
...fragment2,
|
|
324
|
+
imports: new ImportMap().mergeWith(fragment2.imports).add(module, imports)
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
function addFragmentFeatures(fragment2, features) {
|
|
328
|
+
return Object.freeze({
|
|
329
|
+
...fragment2,
|
|
330
|
+
features: /* @__PURE__ */ new Set([...fragment2.features, ...features])
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
272
334
|
// src/utils/linkOverrides.ts
|
|
273
335
|
import { CODAMA_ERROR__UNEXPECTED_NODE_KIND, CodamaError } from "@codama/errors";
|
|
274
336
|
function getImportFromFactory(overrides, customAccountData, customInstructionData) {
|
|
@@ -318,140 +380,99 @@ function getImportFromFactory(overrides, customAccountData, customInstructionDat
|
|
|
318
380
|
};
|
|
319
381
|
}
|
|
320
382
|
|
|
321
|
-
// src/
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
${lines.join("\n")}
|
|
333
|
-
*/
|
|
334
|
-
`;
|
|
335
|
-
}
|
|
336
|
-
var render = (template, context, options) => {
|
|
337
|
-
const dirname = true ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
|
|
338
|
-
const templates = false ? join(dirname, "..", "..", "public", "templates") : join(dirname, "templates");
|
|
339
|
-
const env = nunjucks.configure(templates, { autoescape: false, trimBlocks: true, ...options });
|
|
340
|
-
env.addFilter("pascalCase", pascalCase);
|
|
341
|
-
env.addFilter("camelCase", camelCase2);
|
|
342
|
-
env.addFilter("snakeCase", snakeCase);
|
|
343
|
-
env.addFilter("kebabCase", kebabCase);
|
|
344
|
-
env.addFilter("titleCase", titleCase);
|
|
345
|
-
env.addFilter("jsDocblock", jsDocblock);
|
|
346
|
-
return env.render(template, context);
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
// src/fragments/common.ts
|
|
350
|
-
function fragment(render2, imports) {
|
|
351
|
-
return new Fragment(render2, imports);
|
|
352
|
-
}
|
|
353
|
-
function fragmentFromTemplate(fragmentFile, context, options) {
|
|
354
|
-
return fragment(render(join2("fragments", fragmentFile), context, options));
|
|
383
|
+
// src/TypeManifest.ts
|
|
384
|
+
function typeManifest(input = {}) {
|
|
385
|
+
return Object.freeze({
|
|
386
|
+
decoder: fragment(""),
|
|
387
|
+
encoder: fragment(""),
|
|
388
|
+
isEnum: false,
|
|
389
|
+
looseType: fragment(""),
|
|
390
|
+
strictType: fragment(""),
|
|
391
|
+
value: fragment(""),
|
|
392
|
+
...input
|
|
393
|
+
});
|
|
355
394
|
}
|
|
356
|
-
function
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
395
|
+
function mergeTypeManifests(manifests, options = {}) {
|
|
396
|
+
const { mergeTypes, mergeCodecs, mergeValues } = options;
|
|
397
|
+
const merge = (fragmentFn, mergeFn) => mergeFn ? mergeFragments(manifests.map(fragmentFn), mergeFn) : fragment("");
|
|
398
|
+
return Object.freeze({
|
|
399
|
+
decoder: merge((m) => m.decoder, mergeCodecs),
|
|
400
|
+
encoder: merge((m) => m.encoder, mergeCodecs),
|
|
401
|
+
isEnum: false,
|
|
402
|
+
looseType: merge((m) => m.looseType, mergeTypes),
|
|
403
|
+
strictType: merge((m) => m.strictType, mergeTypes),
|
|
404
|
+
value: merge((m) => m.value, mergeValues)
|
|
405
|
+
});
|
|
362
406
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
addImportAlias(module, name, alias) {
|
|
393
|
-
this.imports.addAlias(module, name, alias);
|
|
394
|
-
return this;
|
|
395
|
-
}
|
|
396
|
-
addFeatures(features) {
|
|
397
|
-
const featureArray = typeof features === "string" ? [features] : features;
|
|
398
|
-
featureArray.forEach((f) => this.features.add(f));
|
|
399
|
-
return this;
|
|
400
|
-
}
|
|
401
|
-
removeFeatures(features) {
|
|
402
|
-
const featureArray = typeof features === "string" ? [features] : features;
|
|
403
|
-
featureArray.forEach((f) => this.features.delete(f));
|
|
404
|
-
return this;
|
|
405
|
-
}
|
|
406
|
-
hasFeatures(features) {
|
|
407
|
-
const featureArray = typeof features === "string" ? [features] : features;
|
|
408
|
-
return featureArray.every((f) => this.features.has(f));
|
|
409
|
-
}
|
|
410
|
-
mergeFeaturesWith(...others) {
|
|
411
|
-
others.forEach((f) => this.addFeatures([...f.features]));
|
|
412
|
-
return this;
|
|
413
|
-
}
|
|
414
|
-
clone() {
|
|
415
|
-
return new _Fragment(this.render).mergeImportsWith(this.imports);
|
|
416
|
-
}
|
|
417
|
-
toString() {
|
|
418
|
-
return this.render;
|
|
419
|
-
}
|
|
420
|
-
};
|
|
407
|
+
|
|
408
|
+
// src/getRenderMapVisitor.ts
|
|
409
|
+
import { join as join2 } from "path";
|
|
410
|
+
import { logWarn } from "@codama/errors";
|
|
411
|
+
import {
|
|
412
|
+
camelCase as camelCase12,
|
|
413
|
+
definedTypeNode as definedTypeNode2,
|
|
414
|
+
getAllAccounts,
|
|
415
|
+
getAllDefinedTypes,
|
|
416
|
+
getAllInstructionsWithSubs as getAllInstructionsWithSubs2,
|
|
417
|
+
getAllPdas,
|
|
418
|
+
getAllPrograms,
|
|
419
|
+
resolveNestedTypeNode as resolveNestedTypeNode4,
|
|
420
|
+
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes5
|
|
421
|
+
} from "@codama/nodes";
|
|
422
|
+
import { addToRenderMap, mergeRenderMaps, renderMap } from "@codama/renderers-core";
|
|
423
|
+
import {
|
|
424
|
+
extendVisitor as extendVisitor2,
|
|
425
|
+
findProgramNodeFromPath as findProgramNodeFromPath7,
|
|
426
|
+
getByteSizeVisitor,
|
|
427
|
+
getResolvedInstructionInputsVisitor,
|
|
428
|
+
LinkableDictionary as LinkableDictionary3,
|
|
429
|
+
NodeStack as NodeStack2,
|
|
430
|
+
pipe as pipe25,
|
|
431
|
+
recordLinkablesOnFirstVisitVisitor,
|
|
432
|
+
recordNodeStackVisitor as recordNodeStackVisitor2,
|
|
433
|
+
staticVisitor as staticVisitor2,
|
|
434
|
+
visit as visit6
|
|
435
|
+
} from "@codama/visitors-core";
|
|
421
436
|
|
|
422
437
|
// src/fragments/accountFetchHelpers.ts
|
|
438
|
+
import { getLastNodeFromPath, pipe } from "@codama/visitors-core";
|
|
423
439
|
function getAccountFetchHelpersFragment(scope) {
|
|
424
440
|
const { accountPath, typeManifest: typeManifest2, nameApi, customAccountData } = scope;
|
|
425
441
|
const accountNode = getLastNodeFromPath(accountPath);
|
|
426
442
|
const hasCustomData = customAccountData.has(accountNode.name);
|
|
427
|
-
const accountTypeFragment = hasCustomData ? typeManifest2.strictType
|
|
428
|
-
const decoderFunctionFragment = hasCustomData ? typeManifest2.decoder
|
|
429
|
-
return
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
"
|
|
441
|
-
"
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
443
|
+
const accountTypeFragment = hasCustomData ? typeManifest2.strictType : fragment(nameApi.dataType(accountNode.name));
|
|
444
|
+
const decoderFunctionFragment = hasCustomData ? typeManifest2.decoder : fragment(`${nameApi.decoderFunction(accountNode.name)}()`);
|
|
445
|
+
return pipe(
|
|
446
|
+
fragmentFromTemplate("accountFetchHelpers.njk", {
|
|
447
|
+
accountType: accountTypeFragment.content,
|
|
448
|
+
decodeFunction: nameApi.accountDecodeFunction(accountNode.name),
|
|
449
|
+
decoderFunction: decoderFunctionFragment.content,
|
|
450
|
+
fetchAllFunction: nameApi.accountFetchAllFunction(accountNode.name),
|
|
451
|
+
fetchAllMaybeFunction: nameApi.accountFetchAllMaybeFunction(accountNode.name),
|
|
452
|
+
fetchFunction: nameApi.accountFetchFunction(accountNode.name),
|
|
453
|
+
fetchMaybeFunction: nameApi.accountFetchMaybeFunction(accountNode.name)
|
|
454
|
+
}),
|
|
455
|
+
(f) => mergeFragmentImports(f, [accountTypeFragment.imports, decoderFunctionFragment.imports]),
|
|
456
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"]),
|
|
457
|
+
(f) => addFragmentImports(f, "solanaAccounts", [
|
|
458
|
+
"type Account",
|
|
459
|
+
"assertAccountExists",
|
|
460
|
+
"assertAccountsExist",
|
|
461
|
+
"decodeAccount",
|
|
462
|
+
"type EncodedAccount",
|
|
463
|
+
"fetchEncodedAccount",
|
|
464
|
+
"fetchEncodedAccounts",
|
|
465
|
+
"type FetchAccountConfig",
|
|
466
|
+
"type FetchAccountsConfig",
|
|
467
|
+
"type MaybeAccount",
|
|
468
|
+
"type MaybeEncodedAccount"
|
|
469
|
+
])
|
|
470
|
+
);
|
|
450
471
|
}
|
|
451
472
|
|
|
452
473
|
// src/fragments/accountPdaHelpers.ts
|
|
453
474
|
import { isNodeFilter } from "@codama/nodes";
|
|
454
|
-
import { findProgramNodeFromPath, getLastNodeFromPath as getLastNodeFromPath2 } from "@codama/visitors-core";
|
|
475
|
+
import { findProgramNodeFromPath, getLastNodeFromPath as getLastNodeFromPath2, pipe as pipe2 } from "@codama/visitors-core";
|
|
455
476
|
function getAccountPdaHelpersFragment(scope) {
|
|
456
477
|
const { accountPath, nameApi, linkables, customAccountData, typeManifest: typeManifest2 } = scope;
|
|
457
478
|
const accountNode = getLastNodeFromPath2(accountPath);
|
|
@@ -460,27 +481,33 @@ function getAccountPdaHelpersFragment(scope) {
|
|
|
460
481
|
if (!pdaNode) {
|
|
461
482
|
return fragment("");
|
|
462
483
|
}
|
|
463
|
-
const accountTypeFragment = customAccountData.has(accountNode.name) ? typeManifest2.strictType
|
|
484
|
+
const accountTypeFragment = customAccountData.has(accountNode.name) ? typeManifest2.strictType : fragment(nameApi.dataType(accountNode.name));
|
|
464
485
|
const importFrom = "generatedPdas";
|
|
465
486
|
const pdaSeedsType = nameApi.pdaSeedsType(pdaNode.name);
|
|
466
487
|
const findPdaFunction = nameApi.pdaFindFunction(pdaNode.name);
|
|
467
488
|
const hasVariableSeeds = pdaNode.seeds.filter(isNodeFilter("variablePdaSeedNode")).length > 0;
|
|
468
|
-
return
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
"type
|
|
483
|
-
|
|
489
|
+
return pipe2(
|
|
490
|
+
fragmentFromTemplate("accountPdaHelpers.njk", {
|
|
491
|
+
accountType: accountTypeFragment.content,
|
|
492
|
+
fetchFromSeedsFunction: nameApi.accountFetchFromSeedsFunction(accountNode.name),
|
|
493
|
+
fetchFunction: nameApi.accountFetchFunction(accountNode.name),
|
|
494
|
+
fetchMaybeFromSeedsFunction: nameApi.accountFetchMaybeFromSeedsFunction(accountNode.name),
|
|
495
|
+
fetchMaybeFunction: nameApi.accountFetchMaybeFunction(accountNode.name),
|
|
496
|
+
findPdaFunction,
|
|
497
|
+
hasVariableSeeds,
|
|
498
|
+
pdaSeedsType,
|
|
499
|
+
program: programNode
|
|
500
|
+
}),
|
|
501
|
+
(f) => mergeFragmentImports(f, [accountTypeFragment.imports]),
|
|
502
|
+
(f) => addFragmentImports(f, importFrom, hasVariableSeeds ? [pdaSeedsType, findPdaFunction] : [findPdaFunction]),
|
|
503
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"]),
|
|
504
|
+
(f) => addFragmentImports(f, "solanaAccounts", [
|
|
505
|
+
"type Account",
|
|
506
|
+
"assertAccountExists",
|
|
507
|
+
"type FetchAccountConfig",
|
|
508
|
+
"type MaybeAccount"
|
|
509
|
+
])
|
|
510
|
+
);
|
|
484
511
|
}
|
|
485
512
|
|
|
486
513
|
// src/fragments/accountSizeHelpers.ts
|
|
@@ -502,52 +529,65 @@ import { resolveNestedTypeNode } from "@codama/nodes";
|
|
|
502
529
|
import { getLastNodeFromPath as getLastNodeFromPath4 } from "@codama/visitors-core";
|
|
503
530
|
|
|
504
531
|
// src/fragments/type.ts
|
|
532
|
+
import { pipe as pipe3 } from "@codama/visitors-core";
|
|
505
533
|
function getTypeFragment(scope) {
|
|
506
534
|
const { name, manifest, nameApi, docs = [] } = scope;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
return typeFragment;
|
|
535
|
+
return pipe3(
|
|
536
|
+
fragmentFromTemplate("type.njk", {
|
|
537
|
+
docs,
|
|
538
|
+
looseName: nameApi.dataArgsType(name),
|
|
539
|
+
manifest,
|
|
540
|
+
strictName: nameApi.dataType(name)
|
|
541
|
+
}),
|
|
542
|
+
(f) => !manifest.isEnum ? mergeFragmentImports(f, [manifest.strictType.imports, manifest.looseType.imports]) : f
|
|
543
|
+
);
|
|
517
544
|
}
|
|
518
545
|
|
|
546
|
+
// src/fragments/typeCodec.ts
|
|
547
|
+
import { pipe as pipe6 } from "@codama/visitors-core";
|
|
548
|
+
|
|
519
549
|
// src/fragments/typeDecoder.ts
|
|
520
550
|
import { isDataEnum, isNode as isNode3 } from "@codama/nodes";
|
|
551
|
+
import { pipe as pipe4 } from "@codama/visitors-core";
|
|
521
552
|
function getTypeDecoderFragment(scope) {
|
|
522
553
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
523
554
|
const decoderType = typeof scope.size === "number" ? "FixedSizeDecoder" : "Decoder";
|
|
524
555
|
const useTypeCast = isNode3(node, "enumTypeNode") && isDataEnum(node) && typeof scope.size === "number";
|
|
525
|
-
return
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
556
|
+
return pipe4(
|
|
557
|
+
fragmentFromTemplate("typeDecoder.njk", {
|
|
558
|
+
decoderFunction: nameApi.decoderFunction(name),
|
|
559
|
+
decoderType,
|
|
560
|
+
docs,
|
|
561
|
+
looseName: nameApi.dataArgsType(name),
|
|
562
|
+
manifest,
|
|
563
|
+
strictName: nameApi.dataType(name),
|
|
564
|
+
useTypeCast
|
|
565
|
+
}),
|
|
566
|
+
(f) => mergeFragmentImports(f, [manifest.decoder.imports]),
|
|
567
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", [`type ${decoderType}`])
|
|
568
|
+
);
|
|
534
569
|
}
|
|
535
570
|
|
|
536
571
|
// src/fragments/typeEncoder.ts
|
|
537
572
|
import { isDataEnum as isDataEnum2, isNode as isNode4 } from "@codama/nodes";
|
|
573
|
+
import { pipe as pipe5 } from "@codama/visitors-core";
|
|
538
574
|
function getTypeEncoderFragment(scope) {
|
|
539
575
|
const { name, node, manifest, nameApi, docs = [] } = scope;
|
|
540
576
|
const encoderType = typeof scope.size === "number" ? "FixedSizeEncoder" : "Encoder";
|
|
541
577
|
const useTypeCast = isNode4(node, "enumTypeNode") && isDataEnum2(node) && typeof scope.size === "number";
|
|
542
|
-
return
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
578
|
+
return pipe5(
|
|
579
|
+
fragmentFromTemplate("typeEncoder.njk", {
|
|
580
|
+
docs,
|
|
581
|
+
encoderFunction: nameApi.encoderFunction(name),
|
|
582
|
+
encoderType,
|
|
583
|
+
looseName: nameApi.dataArgsType(name),
|
|
584
|
+
manifest,
|
|
585
|
+
strictName: nameApi.dataType(name),
|
|
586
|
+
useTypeCast
|
|
587
|
+
}),
|
|
588
|
+
(f) => mergeFragmentImports(f, [manifest.encoder.imports]),
|
|
589
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", [`type ${encoderType}`])
|
|
590
|
+
);
|
|
551
591
|
}
|
|
552
592
|
|
|
553
593
|
// src/fragments/typeCodec.ts
|
|
@@ -558,16 +598,19 @@ function getTypeCodecFragment(scope) {
|
|
|
558
598
|
[
|
|
559
599
|
getTypeEncoderFragment({ ...scope, docs: scope.encoderDocs }),
|
|
560
600
|
getTypeDecoderFragment({ ...scope, docs: scope.decoderDocs }),
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
601
|
+
pipe6(
|
|
602
|
+
fragmentFromTemplate("typeCodec.njk", {
|
|
603
|
+
codecFunction: nameApi.codecFunction(name),
|
|
604
|
+
codecType,
|
|
605
|
+
decoderFunction: nameApi.decoderFunction(name),
|
|
606
|
+
docs: scope.codecDocs,
|
|
607
|
+
encoderFunction: nameApi.encoderFunction(name),
|
|
608
|
+
looseName: nameApi.dataArgsType(name),
|
|
609
|
+
manifest,
|
|
610
|
+
strictName: nameApi.dataType(name)
|
|
611
|
+
}),
|
|
612
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", [`type ${codecType}`, "combineCodec"])
|
|
613
|
+
)
|
|
571
614
|
],
|
|
572
615
|
(renders) => renders.join("\n\n")
|
|
573
616
|
);
|
|
@@ -605,24 +648,28 @@ import {
|
|
|
605
648
|
isNode as isNode5,
|
|
606
649
|
isNodeFilter as isNodeFilter2
|
|
607
650
|
} from "@codama/nodes";
|
|
608
|
-
import {
|
|
651
|
+
import { mapFragmentContent } from "@codama/renderers-core";
|
|
652
|
+
import { pipe as pipe7, visit } from "@codama/visitors-core";
|
|
609
653
|
import { getBase64Decoder } from "@solana/codecs-strings";
|
|
610
654
|
function getDiscriminatorConditionFragment(scope) {
|
|
611
|
-
return
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
655
|
+
return pipe7(
|
|
656
|
+
mergeFragments(
|
|
657
|
+
scope.discriminators.flatMap((discriminator) => {
|
|
658
|
+
if (isNode5(discriminator, "sizeDiscriminatorNode")) {
|
|
659
|
+
return [getSizeConditionFragment(discriminator, scope)];
|
|
660
|
+
}
|
|
661
|
+
if (isNode5(discriminator, "constantDiscriminatorNode")) {
|
|
662
|
+
return [getByteConditionFragment(discriminator, scope)];
|
|
663
|
+
}
|
|
664
|
+
if (isNode5(discriminator, "fieldDiscriminatorNode")) {
|
|
665
|
+
return [getFieldConditionFragment(discriminator, scope)];
|
|
666
|
+
}
|
|
667
|
+
return [];
|
|
668
|
+
}),
|
|
669
|
+
(c) => c.join(" && ")
|
|
670
|
+
),
|
|
671
|
+
(f) => mapFragmentContent(f, (c) => `if (${c}) { ${scope.ifTrue}; }`)
|
|
672
|
+
);
|
|
626
673
|
}
|
|
627
674
|
function getSizeConditionFragment(discriminator, scope) {
|
|
628
675
|
const { dataName } = scope;
|
|
@@ -631,7 +678,11 @@ function getSizeConditionFragment(discriminator, scope) {
|
|
|
631
678
|
function getByteConditionFragment(discriminator, scope) {
|
|
632
679
|
const { dataName, typeManifestVisitor } = scope;
|
|
633
680
|
const constant = visit(discriminator.constant, typeManifestVisitor).value;
|
|
634
|
-
return
|
|
681
|
+
return pipe7(
|
|
682
|
+
constant,
|
|
683
|
+
(f) => mapFragmentContent(f, (c) => `containsBytes(${dataName}, ${c}, ${discriminator.offset})`),
|
|
684
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["containsBytes"])
|
|
685
|
+
);
|
|
635
686
|
}
|
|
636
687
|
function getFieldConditionFragment(discriminator, scope) {
|
|
637
688
|
const field = scope.struct.fields.find((f) => f.name === discriminator.name);
|
|
@@ -657,18 +708,33 @@ function getFieldConditionFragment(discriminator, scope) {
|
|
|
657
708
|
|
|
658
709
|
// src/fragments/instructionAccountMeta.ts
|
|
659
710
|
import { pascalCase as pascalCase2 } from "@codama/nodes";
|
|
711
|
+
import { pipe as pipe8 } from "@codama/visitors-core";
|
|
660
712
|
function getInstructionAccountMetaFragment(instructionAccountNode) {
|
|
661
713
|
const typeParam = `TAccount${pascalCase2(instructionAccountNode.name)}`;
|
|
662
714
|
if (instructionAccountNode.isSigner === true && instructionAccountNode.isWritable) {
|
|
663
|
-
return
|
|
715
|
+
return pipe8(
|
|
716
|
+
fragment(`WritableSignerAccount<${typeParam}> & AccountSignerMeta<${typeParam}>`),
|
|
717
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["type WritableSignerAccount"]),
|
|
718
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type AccountSignerMeta"])
|
|
719
|
+
);
|
|
664
720
|
}
|
|
665
721
|
if (instructionAccountNode.isSigner === true) {
|
|
666
|
-
return
|
|
722
|
+
return pipe8(
|
|
723
|
+
fragment(`ReadonlySignerAccount<${typeParam}> & AccountSignerMeta<${typeParam}>`),
|
|
724
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["type ReadonlySignerAccount"]),
|
|
725
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type AccountSignerMeta"])
|
|
726
|
+
);
|
|
667
727
|
}
|
|
668
728
|
if (instructionAccountNode.isWritable) {
|
|
669
|
-
return
|
|
729
|
+
return pipe8(
|
|
730
|
+
fragment(`WritableAccount<${typeParam}>`),
|
|
731
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["type WritableAccount"])
|
|
732
|
+
);
|
|
670
733
|
}
|
|
671
|
-
return
|
|
734
|
+
return pipe8(
|
|
735
|
+
fragment(`ReadonlyAccount<${typeParam}>`),
|
|
736
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["type ReadonlyAccount"])
|
|
737
|
+
);
|
|
672
738
|
}
|
|
673
739
|
|
|
674
740
|
// src/fragments/instructionAccountTypeParam.ts
|
|
@@ -676,7 +742,8 @@ import { pascalCase as pascalCase3 } from "@codama/nodes";
|
|
|
676
742
|
import {
|
|
677
743
|
findInstructionNodeFromPath,
|
|
678
744
|
findProgramNodeFromPath as findProgramNodeFromPath2,
|
|
679
|
-
getLastNodeFromPath as getLastNodeFromPath5
|
|
745
|
+
getLastNodeFromPath as getLastNodeFromPath5,
|
|
746
|
+
pipe as pipe9
|
|
680
747
|
} from "@codama/visitors-core";
|
|
681
748
|
function getInstructionAccountTypeParamFragment(scope) {
|
|
682
749
|
const { instructionAccountPath, allowAccountMeta, linkables } = scope;
|
|
@@ -690,10 +757,16 @@ function getInstructionAccountTypeParamFragment(scope) {
|
|
|
690
757
|
imports.add("solanaInstructions", "type AccountMeta");
|
|
691
758
|
}
|
|
692
759
|
if (instructionNode.optionalAccountStrategy === "omitted" && instructionAccountNode.isOptional) {
|
|
693
|
-
return
|
|
760
|
+
return pipe9(
|
|
761
|
+
fragment(`${typeParam} extends string${accountMeta} | undefined = undefined`),
|
|
762
|
+
(f) => mergeFragmentImports(f, [imports])
|
|
763
|
+
);
|
|
694
764
|
}
|
|
695
765
|
const defaultAddress = getDefaultAddress(instructionAccountNode.defaultValue, programNode.publicKey, linkables);
|
|
696
|
-
return
|
|
766
|
+
return pipe9(
|
|
767
|
+
fragment(`${typeParam} extends string${accountMeta} = ${defaultAddress}`),
|
|
768
|
+
(f) => mergeFragmentImports(f, [imports])
|
|
769
|
+
);
|
|
697
770
|
}
|
|
698
771
|
function getDefaultAddress(defaultValue, programId, linkables) {
|
|
699
772
|
switch (defaultValue?.kind) {
|
|
@@ -711,19 +784,20 @@ function getDefaultAddress(defaultValue, programId, linkables) {
|
|
|
711
784
|
|
|
712
785
|
// src/fragments/instructionByteDelta.ts
|
|
713
786
|
import { assertIsNode, camelCase as camelCase3, isNode as isNode6 } from "@codama/nodes";
|
|
714
|
-
import {
|
|
787
|
+
import { mapFragmentContent as mapFragmentContent2 } from "@codama/renderers-core";
|
|
788
|
+
import { getLastNodeFromPath as getLastNodeFromPath6, pipe as pipe10 } from "@codama/visitors-core";
|
|
715
789
|
function getInstructionByteDeltaFragment(scope) {
|
|
716
790
|
const { byteDeltas } = getLastNodeFromPath6(scope.instructionPath);
|
|
717
|
-
const fragments = (byteDeltas ?? []).flatMap((
|
|
791
|
+
const fragments = (byteDeltas ?? []).flatMap((c) => getByteDeltaFragment(c, scope));
|
|
718
792
|
if (fragments.length === 0) return fragment("");
|
|
719
793
|
return mergeFragments(
|
|
720
794
|
fragments,
|
|
721
|
-
(
|
|
722
|
-
const byteDelta: number = [${
|
|
795
|
+
(c) => `// Bytes created or reallocated by the instruction.
|
|
796
|
+
const byteDelta: number = [${c.join(",")}].reduce((a, b) => a + b, 0);`
|
|
723
797
|
);
|
|
724
798
|
}
|
|
725
799
|
function getByteDeltaFragment(byteDelta, scope) {
|
|
726
|
-
|
|
800
|
+
let bytesFragment = (() => {
|
|
727
801
|
if (isNode6(byteDelta.value, "numberValueNode")) {
|
|
728
802
|
return getNumberValueNodeFragment(byteDelta);
|
|
729
803
|
}
|
|
@@ -740,10 +814,14 @@ function getByteDeltaFragment(byteDelta, scope) {
|
|
|
740
814
|
})();
|
|
741
815
|
if (bytesFragment === null) return [];
|
|
742
816
|
if (byteDelta.withHeader) {
|
|
743
|
-
bytesFragment
|
|
817
|
+
bytesFragment = pipe10(
|
|
818
|
+
bytesFragment,
|
|
819
|
+
(f) => mapFragmentContent2(f, (c) => `${c} + BASE_ACCOUNT_SIZE`),
|
|
820
|
+
(f) => addFragmentImports(f, "solanaAccounts", ["BASE_ACCOUNT_SIZE"])
|
|
821
|
+
);
|
|
744
822
|
}
|
|
745
823
|
if (byteDelta.subtract) {
|
|
746
|
-
bytesFragment
|
|
824
|
+
bytesFragment = pipe10(bytesFragment, (f) => mapFragmentContent2(f, (c) => `- (${c})`));
|
|
747
825
|
}
|
|
748
826
|
return [bytesFragment];
|
|
749
827
|
}
|
|
@@ -759,7 +837,8 @@ function getArgumentValueNodeFragment(byteDelta) {
|
|
|
759
837
|
function getAccountLinkNodeFragment(byteDelta, scope) {
|
|
760
838
|
assertIsNode(byteDelta.value, "accountLinkNode");
|
|
761
839
|
const functionName = scope.nameApi.accountGetSizeFunction(byteDelta.value.name);
|
|
762
|
-
|
|
840
|
+
const module = scope.getImportFrom(byteDelta.value);
|
|
841
|
+
return pipe10(fragment(`${functionName}()`), (f) => addFragmentImports(f, module, [functionName]));
|
|
763
842
|
}
|
|
764
843
|
function getResolverValueNodeFragment(byteDelta, scope) {
|
|
765
844
|
assertIsNode(byteDelta.value, "resolverValueNode");
|
|
@@ -767,7 +846,12 @@ function getResolverValueNodeFragment(byteDelta, scope) {
|
|
|
767
846
|
if (!scope.useAsync && isAsync) return null;
|
|
768
847
|
const awaitKeyword = scope.useAsync && isAsync ? "await " : "";
|
|
769
848
|
const functionName = scope.nameApi.resolverFunction(byteDelta.value.name);
|
|
770
|
-
|
|
849
|
+
const module = scope.getImportFrom(byteDelta.value);
|
|
850
|
+
return pipe10(
|
|
851
|
+
fragment(`${awaitKeyword}${functionName}(resolverScope)`),
|
|
852
|
+
(f) => addFragmentImports(f, module, [functionName]),
|
|
853
|
+
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
854
|
+
);
|
|
771
855
|
}
|
|
772
856
|
|
|
773
857
|
// src/fragments/instructionData.ts
|
|
@@ -796,10 +880,11 @@ import {
|
|
|
796
880
|
isNodeFilter as isNodeFilter3,
|
|
797
881
|
VALUE_NODES
|
|
798
882
|
} from "@codama/nodes";
|
|
883
|
+
import { mapFragmentContent as mapFragmentContent3 } from "@codama/renderers-core";
|
|
799
884
|
import { visit as visit2 } from "@codama/visitors-core";
|
|
800
885
|
function getDiscriminatorConstantsFragment(scope) {
|
|
801
886
|
const fragments = scope.discriminatorNodes.map((node) => getDiscriminatorConstantFragment(node, scope)).filter(Boolean);
|
|
802
|
-
return mergeFragments(fragments, (
|
|
887
|
+
return mergeFragments(fragments, (c) => c.join("\n\n"));
|
|
803
888
|
}
|
|
804
889
|
function getDiscriminatorConstantFragment(discriminatorNode, scope) {
|
|
805
890
|
switch (discriminatorNode.kind) {
|
|
@@ -837,15 +922,18 @@ function getConstantFragment(scope) {
|
|
|
837
922
|
const constantFunction = nameApi.constantFunction(name);
|
|
838
923
|
return mergeFragments(
|
|
839
924
|
[
|
|
840
|
-
value
|
|
841
|
-
|
|
925
|
+
mapFragmentContent3(value, (c) => `export const ${constantName} = ${c};`),
|
|
926
|
+
mapFragmentContent3(
|
|
927
|
+
encoder,
|
|
928
|
+
(c) => `export function ${constantFunction}() { return ${c}.encode(${constantName}); }`
|
|
929
|
+
)
|
|
842
930
|
],
|
|
843
|
-
(
|
|
931
|
+
(c) => c.join("\n\n")
|
|
844
932
|
);
|
|
845
933
|
}
|
|
846
934
|
|
|
847
935
|
// src/fragments/instructionExtraArgs.ts
|
|
848
|
-
import { getLastNodeFromPath as getLastNodeFromPath8 } from "@codama/visitors-core";
|
|
936
|
+
import { getLastNodeFromPath as getLastNodeFromPath8, pipe as pipe11 } from "@codama/visitors-core";
|
|
849
937
|
function getInstructionExtraArgsFragment(scope) {
|
|
850
938
|
const { instructionPath, extraArgsManifest, nameApi } = scope;
|
|
851
939
|
const instructionNode = getLastNodeFromPath8(instructionPath);
|
|
@@ -853,27 +941,34 @@ function getInstructionExtraArgsFragment(scope) {
|
|
|
853
941
|
return fragment("");
|
|
854
942
|
}
|
|
855
943
|
const instructionExtraName = nameApi.instructionExtraType(instructionNode.name);
|
|
856
|
-
return
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
944
|
+
return pipe11(
|
|
945
|
+
fragmentFromTemplate("instructionExtraArgs.njk", {
|
|
946
|
+
looseName: nameApi.dataArgsType(instructionExtraName),
|
|
947
|
+
manifest: extraArgsManifest,
|
|
948
|
+
strictName: nameApi.dataType(instructionExtraName)
|
|
949
|
+
}),
|
|
950
|
+
(f) => mergeFragmentImports(f, [extraArgsManifest.looseType.imports])
|
|
951
|
+
);
|
|
861
952
|
}
|
|
862
953
|
|
|
863
954
|
// src/fragments/instructionFunction.ts
|
|
864
955
|
import { camelCase as camelCase9, isNode as isNode12, isNodeFilter as isNodeFilter4, pascalCase as pascalCase5 } from "@codama/nodes";
|
|
956
|
+
import { mapFragmentContent as mapFragmentContent7 } from "@codama/renderers-core";
|
|
865
957
|
import {
|
|
866
958
|
findProgramNodeFromPath as findProgramNodeFromPath3,
|
|
867
|
-
getLastNodeFromPath as getLastNodeFromPath12
|
|
959
|
+
getLastNodeFromPath as getLastNodeFromPath12,
|
|
960
|
+
pipe as pipe15
|
|
868
961
|
} from "@codama/visitors-core";
|
|
869
962
|
|
|
870
963
|
// src/fragments/instructionInputResolved.ts
|
|
871
964
|
import { camelCase as camelCase6, isNode as isNode9, parseOptionalAccountStrategy } from "@codama/nodes";
|
|
965
|
+
import { mapFragmentContent as mapFragmentContent5 } from "@codama/renderers-core";
|
|
872
966
|
import { getLastNodeFromPath as getLastNodeFromPath9 } from "@codama/visitors-core";
|
|
873
967
|
|
|
874
968
|
// src/fragments/instructionInputDefault.ts
|
|
875
969
|
import { camelCase as camelCase5, isNode as isNode8 } from "@codama/nodes";
|
|
876
|
-
import {
|
|
970
|
+
import { mapFragmentContent as mapFragmentContent4, setFragmentContent } from "@codama/renderers-core";
|
|
971
|
+
import { pipe as pipe12, visit as visit3 } from "@codama/visitors-core";
|
|
877
972
|
function getInstructionInputDefaultFragment(scope) {
|
|
878
973
|
const { input, optionalAccountStrategy, asyncResolvers, useAsync, nameApi, typeManifestVisitor, getImportFrom } = scope;
|
|
879
974
|
if (!input.defaultValue) {
|
|
@@ -903,33 +998,48 @@ accounts.${inputName}.isWritable = ${isWritable ? "true" : "false"}`
|
|
|
903
998
|
case "accountValueNode":
|
|
904
999
|
const name = camelCase5(defaultValue.name);
|
|
905
1000
|
if (input.kind === "instructionAccountNode" && input.resolvedIsSigner && !input.isSigner) {
|
|
906
|
-
return
|
|
907
|
-
|
|
908
|
-
"expectTransactionSigner"
|
|
1001
|
+
return pipe12(
|
|
1002
|
+
defaultFragment(`expectTransactionSigner(accounts.${name}.value).address`),
|
|
1003
|
+
(f) => addFragmentImports(f, "shared", ["expectTransactionSigner"])
|
|
909
1004
|
);
|
|
910
1005
|
}
|
|
911
1006
|
if (input.kind === "instructionAccountNode") {
|
|
912
|
-
return
|
|
1007
|
+
return pipe12(
|
|
1008
|
+
defaultFragment(`expectSome(accounts.${name}.value)`),
|
|
1009
|
+
(f) => addFragmentImports(f, "shared", ["expectSome"])
|
|
1010
|
+
);
|
|
913
1011
|
}
|
|
914
|
-
return
|
|
1012
|
+
return pipe12(
|
|
1013
|
+
defaultFragment(`expectAddress(accounts.${name}.value)`),
|
|
1014
|
+
(f) => addFragmentImports(f, "shared", ["expectAddress"])
|
|
1015
|
+
);
|
|
915
1016
|
case "pdaValueNode":
|
|
916
1017
|
if (isNode8(defaultValue.pda, "pdaNode")) {
|
|
917
|
-
const pdaProgram = defaultValue.pda.programId ?
|
|
918
|
-
`'${defaultValue.pda.programId}' as Address<'${defaultValue.pda.programId}'>`
|
|
919
|
-
|
|
1018
|
+
const pdaProgram = defaultValue.pda.programId ? pipe12(
|
|
1019
|
+
fragment(`'${defaultValue.pda.programId}' as Address<'${defaultValue.pda.programId}'>`),
|
|
1020
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
1021
|
+
) : fragment("programAddress");
|
|
920
1022
|
const pdaSeeds2 = defaultValue.pda.seeds.flatMap((seed) => {
|
|
921
1023
|
if (isNode8(seed, "constantPdaSeedNode") && isNode8(seed.value, "programIdValueNode")) {
|
|
922
1024
|
return [
|
|
923
|
-
|
|
1025
|
+
pipe12(
|
|
1026
|
+
fragment(`getAddressEncoder().encode(${pdaProgram.content})`),
|
|
1027
|
+
(f) => mergeFragmentImports(f, [pdaProgram.imports]),
|
|
1028
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["getAddressEncoder"])
|
|
1029
|
+
)
|
|
924
1030
|
];
|
|
925
1031
|
}
|
|
926
1032
|
if (isNode8(seed, "constantPdaSeedNode") && !isNode8(seed.value, "programIdValueNode")) {
|
|
927
1033
|
const typeManifest2 = visit3(seed.type, typeManifestVisitor);
|
|
928
1034
|
const valueManifest2 = visit3(seed.value, typeManifestVisitor);
|
|
929
1035
|
return [
|
|
930
|
-
|
|
931
|
-
`${typeManifest2.encoder.
|
|
932
|
-
|
|
1036
|
+
pipe12(
|
|
1037
|
+
fragment(`${typeManifest2.encoder.content}.encode(${valueManifest2.value.content})`),
|
|
1038
|
+
(f) => mergeFragmentImports(f, [
|
|
1039
|
+
typeManifest2.encoder.imports,
|
|
1040
|
+
valueManifest2.value.imports
|
|
1041
|
+
])
|
|
1042
|
+
)
|
|
933
1043
|
];
|
|
934
1044
|
}
|
|
935
1045
|
if (isNode8(seed, "variablePdaSeedNode")) {
|
|
@@ -938,62 +1048,92 @@ accounts.${inputName}.isWritable = ${isWritable ? "true" : "false"}`
|
|
|
938
1048
|
if (!valueSeed) return [];
|
|
939
1049
|
if (isNode8(valueSeed, "accountValueNode")) {
|
|
940
1050
|
return [
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1051
|
+
pipe12(
|
|
1052
|
+
fragment(
|
|
1053
|
+
`${typeManifest2.encoder.content}.encode(expectAddress(accounts.${camelCase5(valueSeed.name)}.value))`
|
|
1054
|
+
),
|
|
1055
|
+
(f) => mergeFragmentImports(f, [typeManifest2.encoder.imports]),
|
|
1056
|
+
(f) => addFragmentImports(f, "shared", ["expectAddress"])
|
|
1057
|
+
)
|
|
944
1058
|
];
|
|
945
1059
|
}
|
|
946
1060
|
if (isNode8(valueSeed, "argumentValueNode")) {
|
|
947
1061
|
return [
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
1062
|
+
pipe12(
|
|
1063
|
+
fragment(
|
|
1064
|
+
`${typeManifest2.encoder.content}.encode(expectSome(args.${camelCase5(valueSeed.name)}))`
|
|
1065
|
+
),
|
|
1066
|
+
(f) => mergeFragmentImports(f, [typeManifest2.encoder.imports]),
|
|
1067
|
+
(f) => addFragmentImports(f, "shared", ["expectSome"])
|
|
1068
|
+
)
|
|
951
1069
|
];
|
|
952
1070
|
}
|
|
953
1071
|
const valueManifest2 = visit3(valueSeed, typeManifestVisitor);
|
|
954
1072
|
return [
|
|
955
|
-
|
|
956
|
-
`${typeManifest2.encoder.
|
|
957
|
-
|
|
1073
|
+
pipe12(
|
|
1074
|
+
fragment(`${typeManifest2.encoder.content}.encode(${valueManifest2.value.content})`),
|
|
1075
|
+
(f) => mergeFragmentImports(f, [
|
|
1076
|
+
typeManifest2.encoder.imports,
|
|
1077
|
+
valueManifest2.value.imports
|
|
1078
|
+
])
|
|
1079
|
+
)
|
|
958
1080
|
];
|
|
959
1081
|
}
|
|
960
1082
|
return [];
|
|
961
1083
|
});
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
1084
|
+
return pipe12(
|
|
1085
|
+
mergeFragments([pdaProgram, ...pdaSeeds2], ([p, ...s]) => {
|
|
1086
|
+
const programAddress2 = p === "programAddress" ? p : `programAddress: ${p}`;
|
|
1087
|
+
return `await getProgramDerivedAddress({ ${programAddress2}, seeds: [${s.join(", ")}] })`;
|
|
1088
|
+
}),
|
|
1089
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["getProgramDerivedAddress"]),
|
|
1090
|
+
(f) => mapFragmentContent4(f, (c) => defaultFragment(c).content)
|
|
1091
|
+
);
|
|
967
1092
|
}
|
|
968
1093
|
const pdaFunction = nameApi.pdaFindFunction(defaultValue.pda.name);
|
|
969
1094
|
const pdaArgs = [];
|
|
970
1095
|
const pdaSeeds = defaultValue.seeds.map((seed) => {
|
|
971
1096
|
if (isNode8(seed.value, "accountValueNode")) {
|
|
972
|
-
return
|
|
973
|
-
`${seed.name}: expectAddress(accounts.${camelCase5(seed.value.name)}.value)`
|
|
974
|
-
|
|
1097
|
+
return pipe12(
|
|
1098
|
+
fragment(`${seed.name}: expectAddress(accounts.${camelCase5(seed.value.name)}.value)`),
|
|
1099
|
+
(f) => addFragmentImports(f, "shared", ["expectAddress"])
|
|
1100
|
+
);
|
|
975
1101
|
}
|
|
976
1102
|
if (isNode8(seed.value, "argumentValueNode")) {
|
|
977
|
-
return
|
|
978
|
-
|
|
979
|
-
"expectSome"
|
|
1103
|
+
return pipe12(
|
|
1104
|
+
fragment(`${seed.name}: expectSome(args.${camelCase5(seed.value.name)})`),
|
|
1105
|
+
(f) => addFragmentImports(f, "shared", ["expectSome"])
|
|
980
1106
|
);
|
|
981
1107
|
}
|
|
982
|
-
return
|
|
1108
|
+
return pipe12(
|
|
1109
|
+
visit3(seed.value, typeManifestVisitor).value,
|
|
1110
|
+
(f) => mapFragmentContent4(f, (c) => `${seed.name}: ${c}`)
|
|
1111
|
+
);
|
|
983
1112
|
});
|
|
984
|
-
const pdaSeedsFragment =
|
|
1113
|
+
const pdaSeedsFragment = pipe12(
|
|
1114
|
+
mergeFragments(pdaSeeds, (renders) => renders.join(", ")),
|
|
1115
|
+
(f) => mapFragmentContent4(f, (c) => `{ ${c} }`)
|
|
1116
|
+
);
|
|
985
1117
|
if (pdaSeeds.length > 0) {
|
|
986
|
-
pdaArgs.push(pdaSeedsFragment.
|
|
1118
|
+
pdaArgs.push(pdaSeedsFragment.content);
|
|
987
1119
|
}
|
|
988
|
-
|
|
1120
|
+
const module = getImportFrom(defaultValue.pda);
|
|
1121
|
+
return pipe12(
|
|
1122
|
+
defaultFragment(`await ${pdaFunction}(${pdaArgs.join(", ")})`),
|
|
1123
|
+
(f) => mergeFragmentImports(f, [pdaSeedsFragment.imports]),
|
|
1124
|
+
(f) => addFragmentImports(f, module, [pdaFunction])
|
|
1125
|
+
);
|
|
989
1126
|
case "publicKeyValueNode":
|
|
990
|
-
return
|
|
991
|
-
|
|
992
|
-
"type Address"
|
|
1127
|
+
return pipe12(
|
|
1128
|
+
defaultFragment(`'${defaultValue.publicKey}' as Address<'${defaultValue.publicKey}'>`),
|
|
1129
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
993
1130
|
);
|
|
994
1131
|
case "programLinkNode":
|
|
995
1132
|
const programAddress = nameApi.programAddressConstant(defaultValue.name);
|
|
996
|
-
return
|
|
1133
|
+
return pipe12(
|
|
1134
|
+
defaultFragment(programAddress, false),
|
|
1135
|
+
(f) => addFragmentImports(f, getImportFrom(defaultValue), [programAddress])
|
|
1136
|
+
);
|
|
997
1137
|
case "programIdValueNode":
|
|
998
1138
|
if (optionalAccountStrategy === "programId" && input.kind === "instructionAccountNode" && input.isOptional) {
|
|
999
1139
|
return fragment("");
|
|
@@ -1003,18 +1143,23 @@ accounts.${inputName}.isWritable = ${isWritable ? "true" : "false"}`
|
|
|
1003
1143
|
case "payerValueNode":
|
|
1004
1144
|
return fragment("");
|
|
1005
1145
|
case "accountBumpValueNode":
|
|
1006
|
-
return
|
|
1007
|
-
`expectProgramDerivedAddress(accounts.${camelCase5(defaultValue.name)}.value)[1]`
|
|
1008
|
-
|
|
1146
|
+
return pipe12(
|
|
1147
|
+
defaultFragment(`expectProgramDerivedAddress(accounts.${camelCase5(defaultValue.name)}.value)[1]`),
|
|
1148
|
+
(f) => addFragmentImports(f, "shared", ["expectProgramDerivedAddress"])
|
|
1149
|
+
);
|
|
1009
1150
|
case "argumentValueNode":
|
|
1010
|
-
return
|
|
1011
|
-
|
|
1012
|
-
"expectSome"
|
|
1151
|
+
return pipe12(
|
|
1152
|
+
defaultFragment(`expectSome(args.${camelCase5(defaultValue.name)})`),
|
|
1153
|
+
(f) => addFragmentImports(f, "shared", ["expectSome"])
|
|
1013
1154
|
);
|
|
1014
1155
|
case "resolverValueNode":
|
|
1015
1156
|
const resolverFunction = nameApi.resolverFunction(defaultValue.name);
|
|
1016
1157
|
const resolverAwait = useAsync && asyncResolvers.includes(defaultValue.name) ? "await " : "";
|
|
1017
|
-
return
|
|
1158
|
+
return pipe12(
|
|
1159
|
+
defaultFragment(`${resolverAwait}${resolverFunction}(resolverScope)`),
|
|
1160
|
+
(f) => addFragmentImports(f, getImportFrom(defaultValue), [resolverFunction]),
|
|
1161
|
+
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1162
|
+
);
|
|
1018
1163
|
case "conditionalValueNode":
|
|
1019
1164
|
const ifTrueRenderer = renderNestedInstructionDefault({
|
|
1020
1165
|
...scope,
|
|
@@ -1027,18 +1172,23 @@ accounts.${inputName}.isWritable = ${isWritable ? "true" : "false"}`
|
|
|
1027
1172
|
if (!ifTrueRenderer && !ifFalseRenderer) {
|
|
1028
1173
|
return fragment("");
|
|
1029
1174
|
}
|
|
1030
|
-
|
|
1175
|
+
let conditionalFragment = fragment("");
|
|
1031
1176
|
if (ifTrueRenderer) {
|
|
1032
|
-
conditionalFragment
|
|
1177
|
+
conditionalFragment = mergeFragments([conditionalFragment, ifTrueRenderer], (c) => c[0]);
|
|
1033
1178
|
}
|
|
1034
1179
|
if (ifFalseRenderer) {
|
|
1035
|
-
conditionalFragment
|
|
1180
|
+
conditionalFragment = mergeFragments([conditionalFragment, ifFalseRenderer], (c) => c[0]);
|
|
1036
1181
|
}
|
|
1037
1182
|
const negatedCondition = !ifTrueRenderer;
|
|
1038
1183
|
let condition = "true";
|
|
1039
1184
|
if (isNode8(defaultValue.condition, "resolverValueNode")) {
|
|
1040
1185
|
const conditionalResolverFunction = nameApi.resolverFunction(defaultValue.condition.name);
|
|
1041
|
-
|
|
1186
|
+
const module2 = getImportFrom(defaultValue.condition);
|
|
1187
|
+
conditionalFragment = pipe12(
|
|
1188
|
+
conditionalFragment,
|
|
1189
|
+
(f) => addFragmentImports(f, module2, [conditionalResolverFunction]),
|
|
1190
|
+
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1191
|
+
);
|
|
1042
1192
|
const conditionalResolverAwait = useAsync && asyncResolvers.includes(defaultValue.condition.name) ? "await " : "";
|
|
1043
1193
|
condition = `${conditionalResolverAwait}${conditionalResolverFunction}(resolverScope)`;
|
|
1044
1194
|
condition = negatedCondition ? `!${condition}` : condition;
|
|
@@ -1046,30 +1196,32 @@ accounts.${inputName}.isWritable = ${isWritable ? "true" : "false"}`
|
|
|
1046
1196
|
const comparedInputName = isNode8(defaultValue.condition, "accountValueNode") ? `accounts.${camelCase5(defaultValue.condition.name)}.value` : `args.${camelCase5(defaultValue.condition.name)}`;
|
|
1047
1197
|
if (defaultValue.value) {
|
|
1048
1198
|
const comparedValue = visit3(defaultValue.value, typeManifestVisitor).value;
|
|
1049
|
-
conditionalFragment
|
|
1199
|
+
conditionalFragment = mergeFragments([conditionalFragment, comparedValue], (c) => c[0]);
|
|
1050
1200
|
const operator = negatedCondition ? "!==" : "===";
|
|
1051
|
-
condition = `${comparedInputName} ${operator} ${comparedValue.
|
|
1201
|
+
condition = `${comparedInputName} ${operator} ${comparedValue.content}`;
|
|
1052
1202
|
} else {
|
|
1053
1203
|
condition = negatedCondition ? `!${comparedInputName}` : comparedInputName;
|
|
1054
1204
|
}
|
|
1055
1205
|
}
|
|
1056
1206
|
if (ifTrueRenderer && ifFalseRenderer) {
|
|
1057
|
-
return
|
|
1207
|
+
return setFragmentContent(
|
|
1208
|
+
conditionalFragment,
|
|
1058
1209
|
`if (${condition}) {
|
|
1059
|
-
${ifTrueRenderer.
|
|
1210
|
+
${ifTrueRenderer.content}
|
|
1060
1211
|
} else {
|
|
1061
|
-
${ifFalseRenderer.
|
|
1212
|
+
${ifFalseRenderer.content}
|
|
1062
1213
|
}`
|
|
1063
1214
|
);
|
|
1064
1215
|
}
|
|
1065
|
-
return
|
|
1216
|
+
return setFragmentContent(
|
|
1217
|
+
conditionalFragment,
|
|
1066
1218
|
`if (${condition}) {
|
|
1067
|
-
${ifTrueRenderer ? ifTrueRenderer.
|
|
1219
|
+
${ifTrueRenderer ? ifTrueRenderer.content : ifFalseRenderer?.content}
|
|
1068
1220
|
}`
|
|
1069
1221
|
);
|
|
1070
1222
|
default:
|
|
1071
1223
|
const valueManifest = visit3(defaultValue, typeManifestVisitor).value;
|
|
1072
|
-
return
|
|
1224
|
+
return pipe12(valueManifest, (f) => mapFragmentContent4(f, (c) => defaultFragment(c).content));
|
|
1073
1225
|
}
|
|
1074
1226
|
}
|
|
1075
1227
|
function renderNestedInstructionDefault(scope) {
|
|
@@ -1090,14 +1242,15 @@ function getInstructionInputResolvedFragment(scope) {
|
|
|
1090
1242
|
input,
|
|
1091
1243
|
optionalAccountStrategy: parseOptionalAccountStrategy(instructionNode.optionalAccountStrategy)
|
|
1092
1244
|
});
|
|
1093
|
-
if (!inputFragment.
|
|
1245
|
+
if (!inputFragment.content) return [];
|
|
1094
1246
|
const camelName = camelCase6(input.name);
|
|
1095
1247
|
return [
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1248
|
+
mapFragmentContent5(
|
|
1249
|
+
inputFragment,
|
|
1250
|
+
(c) => isNode9(input, "instructionArgumentNode") ? `if (!args.${camelName}) {
|
|
1251
|
+
${c}
|
|
1099
1252
|
}` : `if (!accounts.${camelName}.value) {
|
|
1100
|
-
${
|
|
1253
|
+
${c}
|
|
1101
1254
|
}`
|
|
1102
1255
|
)
|
|
1103
1256
|
];
|
|
@@ -1105,10 +1258,7 @@ ${r}
|
|
|
1105
1258
|
if (resolvedInputFragments.length === 0) {
|
|
1106
1259
|
return fragment("");
|
|
1107
1260
|
}
|
|
1108
|
-
return mergeFragments(
|
|
1109
|
-
[fragment("// Resolve default values."), ...resolvedInputFragments],
|
|
1110
|
-
(renders) => renders.join("\n")
|
|
1111
|
-
);
|
|
1261
|
+
return mergeFragments([fragment("// Resolve default values."), ...resolvedInputFragments], (c) => c.join("\n"));
|
|
1112
1262
|
}
|
|
1113
1263
|
|
|
1114
1264
|
// src/fragments/instructionInputType.ts
|
|
@@ -1119,8 +1269,10 @@ import {
|
|
|
1119
1269
|
parseDocs,
|
|
1120
1270
|
pascalCase as pascalCase4
|
|
1121
1271
|
} from "@codama/nodes";
|
|
1272
|
+
import { mapFragmentContent as mapFragmentContent6 } from "@codama/renderers-core";
|
|
1122
1273
|
import {
|
|
1123
|
-
getLastNodeFromPath as getLastNodeFromPath10
|
|
1274
|
+
getLastNodeFromPath as getLastNodeFromPath10,
|
|
1275
|
+
pipe as pipe13
|
|
1124
1276
|
} from "@codama/visitors-core";
|
|
1125
1277
|
function getInstructionInputTypeFragment(scope) {
|
|
1126
1278
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
@@ -1130,21 +1282,25 @@ function getInstructionInputTypeFragment(scope) {
|
|
|
1130
1282
|
const [dataArgumentsFragment, customDataArgumentsFragment] = getDataArgumentsFragments(scope);
|
|
1131
1283
|
const extraArgumentsFragment = getExtraArgumentsFragment(scope);
|
|
1132
1284
|
const remainingAccountsFragment = getRemainingAccountsFragment(instructionNode);
|
|
1133
|
-
return
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1285
|
+
return pipe13(
|
|
1286
|
+
fragmentFromTemplate("instructionInputType.njk", {
|
|
1287
|
+
accountsFragment: accountsFragment.content,
|
|
1288
|
+
customDataArgumentsFragment: customDataArgumentsFragment.content,
|
|
1289
|
+
dataArgumentsFragment: dataArgumentsFragment.content,
|
|
1290
|
+
extraArgumentsFragment: extraArgumentsFragment.content,
|
|
1291
|
+
instruction: instructionNode,
|
|
1292
|
+
instructionInputType,
|
|
1293
|
+
remainingAccountsFragment: remainingAccountsFragment.content
|
|
1294
|
+
}),
|
|
1295
|
+
(f) => mergeFragmentImports(f, [
|
|
1296
|
+
accountsFragment.imports,
|
|
1297
|
+
dataArgumentsFragment.imports,
|
|
1298
|
+
customDataArgumentsFragment.imports,
|
|
1299
|
+
extraArgumentsFragment.imports,
|
|
1300
|
+
remainingAccountsFragment.imports
|
|
1301
|
+
]),
|
|
1302
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
1303
|
+
);
|
|
1148
1304
|
}
|
|
1149
1305
|
function getAccountsFragment(scope) {
|
|
1150
1306
|
const { instructionPath, resolvedInputs, useAsync, asyncResolvers } = scope;
|
|
@@ -1157,29 +1313,42 @@ function getAccountsFragment(scope) {
|
|
|
1157
1313
|
const accountDocs = parseDocs(account.docs);
|
|
1158
1314
|
const docblock = accountDocs.length > 0 ? jsDocblock(accountDocs) : "";
|
|
1159
1315
|
const optionalSign = hasDefaultValue || resolvedAccount.isOptional ? "?" : "";
|
|
1160
|
-
return
|
|
1161
|
-
(
|
|
1316
|
+
return mapFragmentContent6(
|
|
1317
|
+
getAccountTypeFragment2(resolvedAccount),
|
|
1318
|
+
(c) => `${docblock}${camelCase7(account.name)}${optionalSign}: ${c};`
|
|
1162
1319
|
);
|
|
1163
1320
|
});
|
|
1164
|
-
return mergeFragments(fragments, (
|
|
1321
|
+
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1165
1322
|
}
|
|
1166
1323
|
function getAccountTypeFragment2(account) {
|
|
1167
1324
|
const typeParam = `TAccount${pascalCase4(account.name)}`;
|
|
1168
1325
|
if (account.isPda && account.isSigner === false) {
|
|
1169
|
-
return
|
|
1170
|
-
|
|
1171
|
-
|
|
1326
|
+
return pipe13(
|
|
1327
|
+
fragment(`ProgramDerivedAddress<${typeParam}>`),
|
|
1328
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type ProgramDerivedAddress"])
|
|
1329
|
+
);
|
|
1172
1330
|
}
|
|
1173
1331
|
if (account.isPda && account.isSigner === "either") {
|
|
1174
|
-
return
|
|
1332
|
+
return pipe13(
|
|
1333
|
+
fragment(`ProgramDerivedAddress<${typeParam}> | TransactionSigner<${typeParam}>`),
|
|
1334
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type ProgramDerivedAddress"]),
|
|
1335
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type TransactionSigner"])
|
|
1336
|
+
);
|
|
1175
1337
|
}
|
|
1176
1338
|
if (account.isSigner === "either") {
|
|
1177
|
-
return
|
|
1339
|
+
return pipe13(
|
|
1340
|
+
fragment(`Address<${typeParam}> | TransactionSigner<${typeParam}>`),
|
|
1341
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"]),
|
|
1342
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type TransactionSigner"])
|
|
1343
|
+
);
|
|
1178
1344
|
}
|
|
1179
1345
|
if (account.isSigner) {
|
|
1180
|
-
return
|
|
1346
|
+
return pipe13(
|
|
1347
|
+
fragment(`TransactionSigner<${typeParam}>`),
|
|
1348
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type TransactionSigner"])
|
|
1349
|
+
);
|
|
1181
1350
|
}
|
|
1182
|
-
return fragment(`Address<${typeParam}>`)
|
|
1351
|
+
return pipe13(fragment(`Address<${typeParam}>`), (f) => addFragmentImports(f, "solanaAddresses", ["type Address"]));
|
|
1183
1352
|
}
|
|
1184
1353
|
function getDataArgumentsFragments(scope) {
|
|
1185
1354
|
const { instructionPath, nameApi } = scope;
|
|
@@ -1188,7 +1357,11 @@ function getDataArgumentsFragments(scope) {
|
|
|
1188
1357
|
if (customData) {
|
|
1189
1358
|
return [
|
|
1190
1359
|
fragment(""),
|
|
1191
|
-
|
|
1360
|
+
pipe13(
|
|
1361
|
+
fragment(nameApi.dataArgsType(customData.importAs)),
|
|
1362
|
+
(f) => mergeFragmentImports(f, [scope.dataArgsManifest.looseType.imports]),
|
|
1363
|
+
(f) => mapFragmentContent6(f, (c) => `${c} & `)
|
|
1364
|
+
)
|
|
1192
1365
|
];
|
|
1193
1366
|
}
|
|
1194
1367
|
const instructionDataName = nameApi.instructionDataType(instructionNode.name);
|
|
@@ -1197,7 +1370,7 @@ function getDataArgumentsFragments(scope) {
|
|
|
1197
1370
|
const argFragment = getArgumentFragment(arg, fragment(dataArgsType), scope.resolvedInputs, scope.renamedArgs);
|
|
1198
1371
|
return argFragment ? [argFragment] : [];
|
|
1199
1372
|
});
|
|
1200
|
-
return [mergeFragments(fragments, (
|
|
1373
|
+
return [mergeFragments(fragments, (c) => c.join("\n")), fragment("")];
|
|
1201
1374
|
}
|
|
1202
1375
|
function getExtraArgumentsFragment(scope) {
|
|
1203
1376
|
const { instructionPath, nameApi } = scope;
|
|
@@ -1208,7 +1381,7 @@ function getExtraArgumentsFragment(scope) {
|
|
|
1208
1381
|
const argFragment = getArgumentFragment(arg, fragment(extraArgsType), scope.resolvedInputs, scope.renamedArgs);
|
|
1209
1382
|
return argFragment ? [argFragment] : [];
|
|
1210
1383
|
});
|
|
1211
|
-
return mergeFragments(fragments, (
|
|
1384
|
+
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1212
1385
|
}
|
|
1213
1386
|
function getArgumentFragment(arg, argsType, resolvedInputs, renamedArgs) {
|
|
1214
1387
|
const resolvedArg = resolvedInputs.find(
|
|
@@ -1217,7 +1390,10 @@ function getArgumentFragment(arg, argsType, resolvedInputs, renamedArgs) {
|
|
|
1217
1390
|
if (arg.defaultValue && arg.defaultValueStrategy === "omitted") return null;
|
|
1218
1391
|
const renamedName = renamedArgs.get(arg.name) ?? arg.name;
|
|
1219
1392
|
const optionalSign = arg.defaultValue || resolvedArg?.defaultValue ? "?" : "";
|
|
1220
|
-
return
|
|
1393
|
+
return mapFragmentContent6(
|
|
1394
|
+
argsType,
|
|
1395
|
+
(c) => `${camelCase7(renamedName)}${optionalSign}: ${c}["${camelCase7(arg.name)}"];`
|
|
1396
|
+
);
|
|
1221
1397
|
}
|
|
1222
1398
|
function getRemainingAccountsFragment(instructionNode) {
|
|
1223
1399
|
const fragments = (instructionNode.remainingAccounts ?? []).flatMap((remainingAccountsNode) => {
|
|
@@ -1228,16 +1404,25 @@ function getRemainingAccountsFragment(instructionNode) {
|
|
|
1228
1404
|
if (argumentExists) return [];
|
|
1229
1405
|
const isSigner = remainingAccountsNode.isSigner ?? false;
|
|
1230
1406
|
const optionalSign = remainingAccountsNode.isOptional ?? false ? "?" : "";
|
|
1231
|
-
const signerFragment =
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1407
|
+
const signerFragment = pipe13(
|
|
1408
|
+
fragment(`TransactionSigner`),
|
|
1409
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type TransactionSigner"])
|
|
1410
|
+
);
|
|
1411
|
+
const addressFragment = pipe13(
|
|
1412
|
+
fragment(`Address`),
|
|
1413
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
1414
|
+
);
|
|
1415
|
+
return mapFragmentContent6(
|
|
1416
|
+
(() => {
|
|
1417
|
+
if (isSigner === "either") {
|
|
1418
|
+
return mergeFragments([signerFragment, addressFragment], (c) => c.join(" | "));
|
|
1419
|
+
}
|
|
1420
|
+
return isSigner ? signerFragment : addressFragment;
|
|
1421
|
+
})(),
|
|
1422
|
+
(c) => `${camelCase7(name)}${optionalSign}: Array<${c}>;`
|
|
1423
|
+
);
|
|
1239
1424
|
});
|
|
1240
|
-
return mergeFragments(fragments, (
|
|
1425
|
+
return mergeFragments(fragments, (c) => c.join("\n"));
|
|
1241
1426
|
}
|
|
1242
1427
|
|
|
1243
1428
|
// src/fragments/instructionRemainingAccounts.ts
|
|
@@ -1247,16 +1432,19 @@ import {
|
|
|
1247
1432
|
getAllInstructionArguments as getAllInstructionArguments2,
|
|
1248
1433
|
isNode as isNode11
|
|
1249
1434
|
} from "@codama/nodes";
|
|
1250
|
-
import { getLastNodeFromPath as getLastNodeFromPath11 } from "@codama/visitors-core";
|
|
1435
|
+
import { getLastNodeFromPath as getLastNodeFromPath11, pipe as pipe14 } from "@codama/visitors-core";
|
|
1251
1436
|
function getInstructionRemainingAccountsFragment(scope) {
|
|
1252
1437
|
const { remainingAccounts } = getLastNodeFromPath11(scope.instructionPath);
|
|
1253
|
-
const fragments = (remainingAccounts ?? []).flatMap((
|
|
1438
|
+
const fragments = (remainingAccounts ?? []).flatMap((a) => getRemainingAccountsFragment2(a, scope));
|
|
1254
1439
|
if (fragments.length === 0) return fragment("");
|
|
1255
|
-
return
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1440
|
+
return pipe14(
|
|
1441
|
+
mergeFragments(
|
|
1442
|
+
fragments,
|
|
1443
|
+
(c) => `// Remaining accounts.
|
|
1444
|
+
const remainingAccounts: AccountMeta[] = ${c.length === 1 ? c[0] : `[...${c.join(", ...")}]`}`
|
|
1445
|
+
),
|
|
1446
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["type AccountMeta"])
|
|
1447
|
+
);
|
|
1260
1448
|
}
|
|
1261
1449
|
function getRemainingAccountsFragment2(remainingAccounts, scope) {
|
|
1262
1450
|
const remainingAccountsFragment = (() => {
|
|
@@ -1285,22 +1473,27 @@ function getArgumentValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1285
1473
|
const allArguments = getAllInstructionArguments2(instructionNode);
|
|
1286
1474
|
const argumentExists = allArguments.some((arg) => arg.name === remainingAccounts.value.name);
|
|
1287
1475
|
if (argumentExists || isSigner === false) {
|
|
1288
|
-
return
|
|
1289
|
-
|
|
1290
|
-
["AccountRole"]
|
|
1476
|
+
return pipe14(
|
|
1477
|
+
fragment(`${argumentArray}.map((address) => ({ address, role: ${role} }))`),
|
|
1478
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["AccountRole"])
|
|
1291
1479
|
);
|
|
1292
1480
|
}
|
|
1293
1481
|
if (isSigner === "either") {
|
|
1294
|
-
return
|
|
1295
|
-
|
|
1482
|
+
return pipe14(
|
|
1483
|
+
fragment(
|
|
1484
|
+
`${argumentArray}.map((addressOrSigner) => (isTransactionSigner(addressOrSigner)
|
|
1296
1485
|
? { address: addressOrSigner.address, role: ${role}, signer: addressOrSigner }
|
|
1297
1486
|
: { address: addressOrSigner, role: ${role} }
|
|
1298
1487
|
))`
|
|
1299
|
-
|
|
1488
|
+
),
|
|
1489
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["AccountRole"]),
|
|
1490
|
+
(f) => addFragmentImports(f, "shared", ["isTransactionSigner"])
|
|
1491
|
+
);
|
|
1300
1492
|
}
|
|
1301
|
-
return
|
|
1302
|
-
`${argumentArray}.map((signer) => ({ address: signer.address, role: ${signerRole}, signer }))`
|
|
1303
|
-
|
|
1493
|
+
return pipe14(
|
|
1494
|
+
fragment(`${argumentArray}.map((signer) => ({ address: signer.address, role: ${signerRole}, signer }))`),
|
|
1495
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["AccountRole"])
|
|
1496
|
+
);
|
|
1304
1497
|
}
|
|
1305
1498
|
function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
1306
1499
|
assertIsNode2(remainingAccounts.value, "resolverValueNode");
|
|
@@ -1308,7 +1501,12 @@ function getResolverValueNodeFragment2(remainingAccounts, scope) {
|
|
|
1308
1501
|
if (!scope.useAsync && isAsync) return null;
|
|
1309
1502
|
const awaitKeyword = scope.useAsync && isAsync ? "await " : "";
|
|
1310
1503
|
const functionName = scope.nameApi.resolverFunction(remainingAccounts.value.name);
|
|
1311
|
-
|
|
1504
|
+
const module = scope.getImportFrom(remainingAccounts.value);
|
|
1505
|
+
return pipe14(
|
|
1506
|
+
fragment(`${awaitKeyword}${functionName}(resolverScope)`),
|
|
1507
|
+
(f) => addFragmentImports(f, module, [functionName]),
|
|
1508
|
+
(f) => addFragmentFeatures(f, ["instruction:resolverScopeVariable"])
|
|
1509
|
+
);
|
|
1312
1510
|
}
|
|
1313
1511
|
|
|
1314
1512
|
// src/fragments/instructionFunction.ts
|
|
@@ -1350,13 +1548,11 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1350
1548
|
const hasInput = hasAccounts || hasAnyArgs;
|
|
1351
1549
|
const instructionDataName = nameApi.instructionDataType(instructionNode.name);
|
|
1352
1550
|
const programAddressConstant = nameApi.programAddressConstant(programNode.name);
|
|
1353
|
-
const encoderFunction = customData ? dataArgsManifest.encoder.
|
|
1354
|
-
const argsTypeFragment =
|
|
1355
|
-
customData ? dataArgsManifest.looseType.
|
|
1551
|
+
const encoderFunction = customData ? dataArgsManifest.encoder.content : `${nameApi.encoderFunction(instructionDataName)}()`;
|
|
1552
|
+
const argsTypeFragment = pipe15(
|
|
1553
|
+
fragment(customData ? dataArgsManifest.looseType.content : nameApi.dataArgsType(instructionDataName)),
|
|
1554
|
+
customData ? (f) => mergeFragmentImports(f, [dataArgsManifest.looseType.imports, dataArgsManifest.encoder.imports]) : (f) => f
|
|
1356
1555
|
);
|
|
1357
|
-
if (customData) {
|
|
1358
|
-
argsTypeFragment.mergeImportsWith(dataArgsManifest.looseType, dataArgsManifest.encoder);
|
|
1359
|
-
}
|
|
1360
1556
|
const functionName = useAsync ? nameApi.instructionAsyncFunction(instructionNode.name) : nameApi.instructionSyncFunction(instructionNode.name);
|
|
1361
1557
|
const typeParamsFragment = getTypeParams(instructionNode, programAddressConstant);
|
|
1362
1558
|
const instructionTypeFragment = getInstructionType(scope);
|
|
@@ -1368,11 +1564,11 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1368
1564
|
const byteDeltaFragment = getInstructionByteDeltaFragment(scope);
|
|
1369
1565
|
const resolvedFragment = mergeFragments(
|
|
1370
1566
|
[resolvedInputsFragment, remainingAccountsFragment, byteDeltaFragment],
|
|
1371
|
-
(
|
|
1567
|
+
(content) => content.join("\n\n")
|
|
1372
1568
|
);
|
|
1373
|
-
const hasRemainingAccounts = remainingAccountsFragment.
|
|
1374
|
-
const hasByteDeltas = byteDeltaFragment.
|
|
1375
|
-
const hasResolver = resolvedFragment.
|
|
1569
|
+
const hasRemainingAccounts = remainingAccountsFragment.content !== "";
|
|
1570
|
+
const hasByteDeltas = byteDeltaFragment.content !== "";
|
|
1571
|
+
const hasResolver = resolvedFragment.features.has("instruction:resolverScopeVariable");
|
|
1376
1572
|
const getReturnType = (instructionType) => {
|
|
1377
1573
|
let returnType = instructionType;
|
|
1378
1574
|
if (hasByteDeltas) {
|
|
@@ -1380,50 +1576,55 @@ function getInstructionFunctionFragment(scope) {
|
|
|
1380
1576
|
}
|
|
1381
1577
|
return useAsync ? `Promise<${returnType}>` : returnType;
|
|
1382
1578
|
};
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1579
|
+
return pipe15(
|
|
1580
|
+
fragmentFromTemplate("instructionFunction.njk", {
|
|
1581
|
+
argsTypeFragment: argsTypeFragment.content,
|
|
1582
|
+
encoderFunction,
|
|
1583
|
+
functionName,
|
|
1584
|
+
getReturnType,
|
|
1585
|
+
hasAccounts,
|
|
1586
|
+
hasAnyArgs,
|
|
1587
|
+
hasByteDeltas,
|
|
1588
|
+
hasData,
|
|
1589
|
+
hasDataArgs,
|
|
1590
|
+
hasExtraArgs,
|
|
1591
|
+
hasInput,
|
|
1592
|
+
hasLegacyOptionalAccounts,
|
|
1593
|
+
hasRemainingAccounts,
|
|
1594
|
+
hasResolver,
|
|
1595
|
+
inputTypeCallFragment: inputTypeCallFragment.content,
|
|
1596
|
+
inputTypeFragment: inputTypeFragment.content,
|
|
1597
|
+
instruction: instructionNode,
|
|
1598
|
+
instructionTypeFragment: instructionTypeFragment.content,
|
|
1599
|
+
programAddressConstant,
|
|
1600
|
+
renamedArgs: renamedArgsText,
|
|
1601
|
+
resolvedFragment: resolvedFragment.content,
|
|
1602
|
+
typeParamsFragment: typeParamsFragment.content,
|
|
1603
|
+
useAsync
|
|
1604
|
+
}),
|
|
1605
|
+
(f) => mergeFragmentImports(f, [
|
|
1606
|
+
typeParamsFragment.imports,
|
|
1607
|
+
instructionTypeFragment.imports,
|
|
1608
|
+
inputTypeFragment.imports,
|
|
1609
|
+
inputTypeCallFragment.imports,
|
|
1610
|
+
resolvedFragment.imports,
|
|
1611
|
+
argsTypeFragment.imports
|
|
1612
|
+
]),
|
|
1613
|
+
(f) => addFragmentImports(f, "generatedPrograms", [programAddressConstant]),
|
|
1614
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"]),
|
|
1615
|
+
(f) => hasAccounts ? addFragmentImports(f, "solanaInstructions", ["type AccountMeta"]) : f,
|
|
1616
|
+
(f) => hasAccounts ? addFragmentImports(f, "shared", ["getAccountMetaFactory", "type ResolvedAccount"]) : f,
|
|
1617
|
+
(f) => hasByteDeltas ? addFragmentImports(f, "shared", ["type InstructionWithByteDelta"]) : f
|
|
1618
|
+
);
|
|
1422
1619
|
}
|
|
1423
1620
|
function getTypeParams(instructionNode, programAddressConstant) {
|
|
1424
1621
|
const typeParams = instructionNode.accounts.map((account) => `TAccount${pascalCase5(account.name)} extends string`);
|
|
1425
1622
|
typeParams.push(`TProgramAddress extends Address = typeof ${programAddressConstant}`);
|
|
1426
|
-
return
|
|
1623
|
+
return pipe15(
|
|
1624
|
+
fragment(typeParams.filter((x) => !!x).join(", ")),
|
|
1625
|
+
(f) => mapFragmentContent7(f, (c) => `<${c}>`),
|
|
1626
|
+
(f) => addFragmentImports(f, "generatedPrograms", [programAddressConstant])
|
|
1627
|
+
);
|
|
1427
1628
|
}
|
|
1428
1629
|
function getInstructionType(scope) {
|
|
1429
1630
|
const { instructionPath, nameApi } = scope;
|
|
@@ -1435,16 +1636,20 @@ function getInstructionType(scope) {
|
|
|
1435
1636
|
const camelName = camelCase9(account.name);
|
|
1436
1637
|
if (account.isSigner === "either") {
|
|
1437
1638
|
const signerRole = account.isWritable ? "WritableSignerAccount" : "ReadonlySignerAccount";
|
|
1438
|
-
return
|
|
1439
|
-
|
|
1440
|
-
|
|
1639
|
+
return pipe15(
|
|
1640
|
+
fragment(
|
|
1641
|
+
`typeof input["${camelName}"] extends TransactionSigner<${typeParam}> ? ${signerRole}<${typeParam}> & AccountSignerMeta<${typeParam}> : ${typeParam}`
|
|
1642
|
+
),
|
|
1643
|
+
(f) => addFragmentImports(f, "solanaInstructions", [`type ${signerRole}`]),
|
|
1644
|
+
(f) => addFragmentImports(f, "solanaSigners", ["type AccountSignerMeta"])
|
|
1645
|
+
);
|
|
1441
1646
|
}
|
|
1442
1647
|
return fragment(typeParam);
|
|
1443
1648
|
});
|
|
1444
|
-
return
|
|
1445
|
-
[programAddressFragment, ...accountTypeParamsFragments],
|
|
1446
|
-
(
|
|
1447
|
-
)
|
|
1649
|
+
return pipe15(
|
|
1650
|
+
mergeFragments([programAddressFragment, ...accountTypeParamsFragments], (c) => c.join(", ")),
|
|
1651
|
+
(f) => mapFragmentContent7(f, (c) => `${instructionTypeName}<${c}>`)
|
|
1652
|
+
);
|
|
1448
1653
|
}
|
|
1449
1654
|
function getInputTypeCall(scope) {
|
|
1450
1655
|
const { instructionPath, useAsync, nameApi } = scope;
|
|
@@ -1456,7 +1661,7 @@ function getInputTypeCall(scope) {
|
|
|
1456
1661
|
}
|
|
1457
1662
|
|
|
1458
1663
|
// src/fragments/instructionParseFunction.ts
|
|
1459
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath4, getLastNodeFromPath as getLastNodeFromPath13 } from "@codama/visitors-core";
|
|
1664
|
+
import { findProgramNodeFromPath as findProgramNodeFromPath4, getLastNodeFromPath as getLastNodeFromPath13, pipe as pipe16 } from "@codama/visitors-core";
|
|
1460
1665
|
function getInstructionParseFunctionFragment(scope) {
|
|
1461
1666
|
const { instructionPath, dataArgsManifest, nameApi, customInstructionData } = scope;
|
|
1462
1667
|
const instructionNode = getLastNodeFromPath13(instructionPath);
|
|
@@ -1468,30 +1673,40 @@ function getInstructionParseFunctionFragment(scope) {
|
|
|
1468
1673
|
const hasData = !!customData || instructionNode.arguments.length > 0;
|
|
1469
1674
|
const instructionDataName = nameApi.instructionDataType(instructionNode.name);
|
|
1470
1675
|
const programAddressConstant = nameApi.programAddressConstant(programNode.name);
|
|
1471
|
-
const dataTypeFragment =
|
|
1472
|
-
|
|
1676
|
+
const dataTypeFragment = customData ? pipe16(
|
|
1677
|
+
fragment(dataArgsManifest.strictType.content),
|
|
1678
|
+
(f) => mergeFragmentImports(f, [dataArgsManifest.strictType.imports, dataArgsManifest.decoder.imports])
|
|
1679
|
+
) : fragment(nameApi.dataType(instructionDataName));
|
|
1680
|
+
const decoderFunction = customData ? dataArgsManifest.decoder.content : `${nameApi.decoderFunction(instructionDataName)}()`;
|
|
1681
|
+
return pipe16(
|
|
1682
|
+
fragmentFromTemplate("instructionParseFunction.njk", {
|
|
1683
|
+
dataTypeFragment: dataTypeFragment.content,
|
|
1684
|
+
decoderFunction,
|
|
1685
|
+
hasAccounts,
|
|
1686
|
+
hasData,
|
|
1687
|
+
hasOptionalAccounts,
|
|
1688
|
+
instruction: instructionNode,
|
|
1689
|
+
instructionParseFunction: nameApi.instructionParseFunction(instructionNode.name),
|
|
1690
|
+
instructionParsedType: nameApi.instructionParsedType(instructionNode.name),
|
|
1691
|
+
minimumNumberOfAccounts,
|
|
1692
|
+
programAddressConstant
|
|
1693
|
+
}),
|
|
1694
|
+
(f) => mergeFragmentImports(f, [dataTypeFragment.imports]),
|
|
1695
|
+
(f) => addFragmentImports(f, "generatedPrograms", [programAddressConstant]),
|
|
1696
|
+
(f) => addFragmentImports(f, "solanaInstructions", ["type Instruction"]),
|
|
1697
|
+
hasAccounts ? (f) => addFragmentImports(f, "solanaInstructions", ["type InstructionWithAccounts", "type AccountMeta"]) : (f) => f,
|
|
1698
|
+
hasData ? (f) => addFragmentImports(
|
|
1699
|
+
addFragmentImports(f, "solanaInstructions", ["type InstructionWithData"]),
|
|
1700
|
+
"solanaCodecsCore",
|
|
1701
|
+
["type ReadonlyUint8Array"]
|
|
1702
|
+
) : (f) => f
|
|
1473
1703
|
);
|
|
1474
|
-
const decoderFunction = customData ? dataArgsManifest.decoder.render : `${nameApi.decoderFunction(instructionDataName)}()`;
|
|
1475
|
-
if (customData) {
|
|
1476
|
-
dataTypeFragment.mergeImportsWith(dataArgsManifest.strictType, dataArgsManifest.decoder);
|
|
1477
|
-
}
|
|
1478
|
-
return fragmentFromTemplate("instructionParseFunction.njk", {
|
|
1479
|
-
dataTypeFragment,
|
|
1480
|
-
decoderFunction,
|
|
1481
|
-
hasAccounts,
|
|
1482
|
-
hasData,
|
|
1483
|
-
hasOptionalAccounts,
|
|
1484
|
-
instruction: instructionNode,
|
|
1485
|
-
instructionParseFunction: nameApi.instructionParseFunction(instructionNode.name),
|
|
1486
|
-
instructionParsedType: nameApi.instructionParsedType(instructionNode.name),
|
|
1487
|
-
minimumNumberOfAccounts,
|
|
1488
|
-
programAddressConstant
|
|
1489
|
-
}).mergeImportsWith(dataTypeFragment).addImports("generatedPrograms", [programAddressConstant]).addImports("solanaInstructions", ["type Instruction"]).addImports("solanaInstructions", hasAccounts ? ["type InstructionWithAccounts", "type AccountMeta"] : []).addImports("solanaInstructions", hasData ? ["type InstructionWithData"] : []).addImports("solanaCodecsCore", hasData ? ["type ReadonlyUint8Array"] : []);
|
|
1490
1704
|
}
|
|
1491
1705
|
|
|
1492
1706
|
// src/fragments/instructionType.ts
|
|
1493
1707
|
import { pascalCase as pascalCase6 } from "@codama/nodes";
|
|
1494
|
-
import {
|
|
1708
|
+
import { mapFragmentContent as mapFragmentContent8 } from "@codama/renderers-core";
|
|
1709
|
+
import { findProgramNodeFromPath as findProgramNodeFromPath5, getLastNodeFromPath as getLastNodeFromPath14, pipe as pipe17 } from "@codama/visitors-core";
|
|
1495
1710
|
function getInstructionTypeFragment(scope) {
|
|
1496
1711
|
const { instructionPath, nameApi, customInstructionData } = scope;
|
|
1497
1712
|
const instructionNode = getLastNodeFromPath14(instructionPath);
|
|
@@ -1515,37 +1730,42 @@ function getInstructionTypeFragment(scope) {
|
|
|
1515
1730
|
const usesLegacyOptionalAccounts = instructionNode.optionalAccountStrategy === "omitted";
|
|
1516
1731
|
const accountMetasFragment = mergeFragments(
|
|
1517
1732
|
instructionNode.accounts.map(
|
|
1518
|
-
(account) => getInstructionAccountMetaFragment(account)
|
|
1733
|
+
(account) => mapFragmentContent8(getInstructionAccountMetaFragment(account), (c) => {
|
|
1519
1734
|
const typeParam = `TAccount${pascalCase6(account.name)}`;
|
|
1520
1735
|
const isLegacyOptional = account.isOptional && usesLegacyOptionalAccounts;
|
|
1521
|
-
const type = `${typeParam} extends string ? ${
|
|
1736
|
+
const type = `${typeParam} extends string ? ${c} : ${typeParam}`;
|
|
1522
1737
|
if (!isLegacyOptional) return type;
|
|
1523
1738
|
return `...(${typeParam} extends undefined ? [] : [${type}])`;
|
|
1524
1739
|
})
|
|
1525
1740
|
),
|
|
1526
|
-
(
|
|
1741
|
+
(c) => c.join(", ")
|
|
1742
|
+
);
|
|
1743
|
+
const fragment2 = pipe17(
|
|
1744
|
+
fragmentFromTemplate("instructionType.njk", {
|
|
1745
|
+
accountMetas: accountMetasFragment.content,
|
|
1746
|
+
accountTypeParams: accountTypeParamsFragment.content,
|
|
1747
|
+
dataType,
|
|
1748
|
+
hasAccounts,
|
|
1749
|
+
hasData,
|
|
1750
|
+
instruction: instructionNode,
|
|
1751
|
+
instructionType: nameApi.instructionType(instructionNode.name),
|
|
1752
|
+
programAddressConstant
|
|
1753
|
+
}),
|
|
1754
|
+
(f) => mergeFragmentImports(f, [accountTypeParamsFragment.imports, accountMetasFragment.imports]),
|
|
1755
|
+
(f) => addFragmentImports(f, "generatedPrograms", [programAddressConstant]),
|
|
1756
|
+
(f) => addFragmentImports(f, "solanaInstructions", [
|
|
1757
|
+
"type AccountMeta",
|
|
1758
|
+
"type Instruction",
|
|
1759
|
+
"type InstructionWithAccounts",
|
|
1760
|
+
...hasData ? ["type InstructionWithData"] : []
|
|
1761
|
+
])
|
|
1527
1762
|
);
|
|
1528
|
-
const fragment2 = fragmentFromTemplate("instructionType.njk", {
|
|
1529
|
-
accountMetas: accountMetasFragment.render,
|
|
1530
|
-
accountTypeParams: accountTypeParamsFragment.render,
|
|
1531
|
-
dataType,
|
|
1532
|
-
hasAccounts,
|
|
1533
|
-
hasData,
|
|
1534
|
-
instruction: instructionNode,
|
|
1535
|
-
instructionType: nameApi.instructionType(instructionNode.name),
|
|
1536
|
-
programAddressConstant
|
|
1537
|
-
}).mergeImportsWith(accountTypeParamsFragment, accountMetasFragment).addImports("generatedPrograms", [programAddressConstant]).addImports("solanaCodecsCore", hasData ? ["type ReadonlyUint8Array"] : []).addImports("solanaInstructions", [
|
|
1538
|
-
"type AccountMeta",
|
|
1539
|
-
"type Instruction",
|
|
1540
|
-
"type InstructionWithAccounts",
|
|
1541
|
-
...hasData ? ["type InstructionWithData"] : []
|
|
1542
|
-
]);
|
|
1543
1763
|
return fragment2;
|
|
1544
1764
|
}
|
|
1545
1765
|
|
|
1546
1766
|
// src/fragments/pdaFunction.ts
|
|
1547
1767
|
import { isNode as isNode13, isNodeFilter as isNodeFilter5 } from "@codama/nodes";
|
|
1548
|
-
import { findProgramNodeFromPath as findProgramNodeFromPath6, getLastNodeFromPath as getLastNodeFromPath15, visit as visit4 } from "@codama/visitors-core";
|
|
1768
|
+
import { findProgramNodeFromPath as findProgramNodeFromPath6, getLastNodeFromPath as getLastNodeFromPath15, pipe as pipe18, visit as visit4 } from "@codama/visitors-core";
|
|
1549
1769
|
function getPdaFunctionFragment(scope) {
|
|
1550
1770
|
const { pdaPath, typeManifestVisitor, nameApi } = scope;
|
|
1551
1771
|
const pdaNode = getLastNodeFromPath15(pdaPath);
|
|
@@ -1568,31 +1788,46 @@ function getPdaFunctionFragment(scope) {
|
|
|
1568
1788
|
return { ...seed, typeManifest: seedManifest, valueManifest };
|
|
1569
1789
|
});
|
|
1570
1790
|
const hasVariableSeeds = pdaNode.seeds.filter(isNodeFilter5("variablePdaSeedNode")).length > 0;
|
|
1571
|
-
return
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1791
|
+
return pipe18(
|
|
1792
|
+
fragmentFromTemplate("pdaFunction.njk", {
|
|
1793
|
+
findPdaFunction: nameApi.pdaFindFunction(pdaNode.name),
|
|
1794
|
+
hasVariableSeeds,
|
|
1795
|
+
pdaDocs: pdaNode.docs,
|
|
1796
|
+
pdaSeedsType: nameApi.pdaSeedsType(pdaNode.name),
|
|
1797
|
+
programAddress: pdaNode.programId ?? programNode.publicKey,
|
|
1798
|
+
seeds
|
|
1799
|
+
}),
|
|
1800
|
+
(f) => mergeFragmentImports(f, [imports]),
|
|
1801
|
+
(f) => addFragmentImports(f, "solanaAddresses", [
|
|
1802
|
+
"type Address",
|
|
1803
|
+
"getProgramDerivedAddress",
|
|
1804
|
+
"type ProgramDerivedAddress"
|
|
1805
|
+
])
|
|
1806
|
+
);
|
|
1578
1807
|
}
|
|
1579
1808
|
|
|
1580
1809
|
// src/fragments/program.ts
|
|
1810
|
+
import { pipe as pipe19 } from "@codama/visitors-core";
|
|
1581
1811
|
function getProgramFragment(scope) {
|
|
1582
1812
|
const { programNode, nameApi } = scope;
|
|
1583
|
-
return
|
|
1584
|
-
program
|
|
1585
|
-
|
|
1586
|
-
|
|
1813
|
+
return pipe19(
|
|
1814
|
+
fragmentFromTemplate("program.njk", {
|
|
1815
|
+
program: programNode,
|
|
1816
|
+
programAddressConstant: nameApi.programAddressConstant(programNode.name)
|
|
1817
|
+
}),
|
|
1818
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
1819
|
+
);
|
|
1587
1820
|
}
|
|
1588
1821
|
|
|
1589
1822
|
// src/fragments/programAccounts.ts
|
|
1590
1823
|
import { resolveNestedTypeNode as resolveNestedTypeNode2 } from "@codama/nodes";
|
|
1824
|
+
import { mapFragmentContent as mapFragmentContent9 } from "@codama/renderers-core";
|
|
1825
|
+
import { pipe as pipe20 } from "@codama/visitors-core";
|
|
1591
1826
|
function getProgramAccountsFragment(scope) {
|
|
1592
1827
|
if (scope.programNode.accounts.length === 0) return fragment("");
|
|
1593
1828
|
return mergeFragments(
|
|
1594
1829
|
[getProgramAccountsEnumFragment(scope), getProgramAccountsIdentifierFunctionFragment(scope)],
|
|
1595
|
-
(
|
|
1830
|
+
(c) => `${c.join("\n\n")}
|
|
1596
1831
|
`
|
|
1597
1832
|
);
|
|
1598
1833
|
}
|
|
@@ -1613,42 +1848,53 @@ function getProgramAccountsIdentifierFunctionFragment(scope) {
|
|
|
1613
1848
|
if (!hasAccountDiscriminators) return fragment("");
|
|
1614
1849
|
const programAccountsEnum = nameApi.programAccountsEnum(programNode.name);
|
|
1615
1850
|
const programAccountsIdentifierFunction = nameApi.programAccountsIdentifierFunction(programNode.name);
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
(
|
|
1851
|
+
return pipe20(
|
|
1852
|
+
mergeFragments(
|
|
1853
|
+
accountsWithDiscriminators.map((account) => {
|
|
1854
|
+
const variant = nameApi.programAccountsEnumVariant(account.name);
|
|
1855
|
+
return getDiscriminatorConditionFragment({
|
|
1856
|
+
...scope,
|
|
1857
|
+
dataName: "data",
|
|
1858
|
+
discriminators: account.discriminators ?? [],
|
|
1859
|
+
ifTrue: `return ${programAccountsEnum}.${variant};`,
|
|
1860
|
+
struct: resolveNestedTypeNode2(account.data)
|
|
1861
|
+
});
|
|
1862
|
+
}),
|
|
1863
|
+
(c) => c.join("\n")
|
|
1864
|
+
),
|
|
1865
|
+
(f) => mapFragmentContent9(
|
|
1866
|
+
f,
|
|
1867
|
+
(discriminators) => `export function ${programAccountsIdentifierFunction}(account: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programAccountsEnum} {
|
|
1631
1868
|
const data = 'data' in account ? account.data : account;
|
|
1632
1869
|
${discriminators}
|
|
1633
1870
|
throw new Error("The provided account could not be identified as a ${programNode.name} account.")
|
|
1634
1871
|
}`
|
|
1635
|
-
|
|
1872
|
+
),
|
|
1873
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
1874
|
+
);
|
|
1636
1875
|
}
|
|
1637
1876
|
|
|
1638
1877
|
// src/fragments/programErrors.ts
|
|
1878
|
+
import { pipe as pipe21 } from "@codama/visitors-core";
|
|
1639
1879
|
function getProgramErrorsFragment(scope) {
|
|
1640
1880
|
const { programNode, nameApi } = scope;
|
|
1641
1881
|
const programAddressConstant = nameApi.programAddressConstant(programNode.name);
|
|
1642
|
-
return
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1882
|
+
return pipe21(
|
|
1883
|
+
fragmentFromTemplate("programErrors.njk", {
|
|
1884
|
+
errors: programNode.errors,
|
|
1885
|
+
escapeProgramErrorMessage: (message) => message.replace(/`/g, "\\`"),
|
|
1886
|
+
getProgramErrorConstant: (name) => nameApi.programErrorConstantPrefix(programNode.name) + nameApi.programErrorConstant(name),
|
|
1887
|
+
programAddressConstant,
|
|
1888
|
+
programErrorMessagesMap: nameApi.programErrorMessagesMap(programNode.name),
|
|
1889
|
+
programErrorUnion: nameApi.programErrorUnion(programNode.name),
|
|
1890
|
+
programGetErrorMessageFunction: nameApi.programGetErrorMessageFunction(programNode.name),
|
|
1891
|
+
programIsErrorFunction: nameApi.programIsErrorFunction(programNode.name)
|
|
1892
|
+
}),
|
|
1893
|
+
(f) => addFragmentImports(f, "generatedPrograms", [programAddressConstant]),
|
|
1894
|
+
(f) => addFragmentImports(f, "solanaPrograms", ["isProgramError"]),
|
|
1895
|
+
(f) => addFragmentImports(f, "solanaErrors", ["type SolanaError", "type SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM"]),
|
|
1896
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
1897
|
+
);
|
|
1652
1898
|
}
|
|
1653
1899
|
|
|
1654
1900
|
// src/fragments/programInstructions.ts
|
|
@@ -1656,6 +1902,8 @@ import {
|
|
|
1656
1902
|
getAllInstructionsWithSubs,
|
|
1657
1903
|
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes3
|
|
1658
1904
|
} from "@codama/nodes";
|
|
1905
|
+
import { mapFragmentContent as mapFragmentContent10 } from "@codama/renderers-core";
|
|
1906
|
+
import { pipe as pipe22 } from "@codama/visitors-core";
|
|
1659
1907
|
function getProgramInstructionsFragment(scope) {
|
|
1660
1908
|
if (scope.programNode.instructions.length === 0) return fragment("");
|
|
1661
1909
|
const allInstructions = getAllInstructionsWithSubs(scope.programNode, {
|
|
@@ -1669,7 +1917,7 @@ function getProgramInstructionsFragment(scope) {
|
|
|
1669
1917
|
getProgramInstructionsIdentifierFunctionFragment(scopeWithInstructions),
|
|
1670
1918
|
getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions)
|
|
1671
1919
|
],
|
|
1672
|
-
(
|
|
1920
|
+
(c) => `${c.join("\n\n")}
|
|
1673
1921
|
`
|
|
1674
1922
|
);
|
|
1675
1923
|
}
|
|
@@ -1703,15 +1951,20 @@ function getProgramInstructionsIdentifierFunctionFragment(scope) {
|
|
|
1703
1951
|
struct: structTypeNodeFromInstructionArgumentNodes3(instruction.arguments)
|
|
1704
1952
|
});
|
|
1705
1953
|
}),
|
|
1706
|
-
(
|
|
1954
|
+
(c) => c.join("\n")
|
|
1707
1955
|
);
|
|
1708
|
-
return
|
|
1709
|
-
|
|
1956
|
+
return pipe22(
|
|
1957
|
+
discriminatorsFragment,
|
|
1958
|
+
(f) => mapFragmentContent10(
|
|
1959
|
+
f,
|
|
1960
|
+
(discriminators) => `export function ${programInstructionsIdentifierFunction}(instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array): ${programInstructionsEnum} {
|
|
1710
1961
|
const data = 'data' in instruction ? instruction.data : instruction;
|
|
1711
1962
|
${discriminators}
|
|
1712
1963
|
throw new Error("The provided instruction could not be identified as a ${programNode.name} instruction.")
|
|
1713
1964
|
}`
|
|
1714
|
-
|
|
1965
|
+
),
|
|
1966
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
1967
|
+
);
|
|
1715
1968
|
}
|
|
1716
1969
|
function getProgramInstructionsParsedUnionTypeFragment(scope) {
|
|
1717
1970
|
const { programNode, allInstructions, nameApi } = scope;
|
|
@@ -1721,94 +1974,48 @@ function getProgramInstructionsParsedUnionTypeFragment(scope) {
|
|
|
1721
1974
|
const typeVariants = allInstructions.map((instruction) => {
|
|
1722
1975
|
const instructionEnumVariant = nameApi.programInstructionsEnumVariant(instruction.name);
|
|
1723
1976
|
const parsedInstructionType = nameApi.instructionParsedType(instruction.name);
|
|
1724
|
-
return
|
|
1725
|
-
|
|
1726
|
-
|
|
1977
|
+
return pipe22(
|
|
1978
|
+
fragment(
|
|
1979
|
+
`| { instructionType: ${programInstructionsEnum}.${instructionEnumVariant} } & ${parsedInstructionType}<TProgram>`
|
|
1980
|
+
),
|
|
1981
|
+
(f) => addFragmentImports(f, "generatedInstructions", [`type ${parsedInstructionType}`])
|
|
1982
|
+
);
|
|
1727
1983
|
});
|
|
1728
1984
|
return mergeFragments(
|
|
1729
1985
|
[
|
|
1730
1986
|
fragment(`export type ${programInstructionsType}<TProgram extends string = '${programAddress}'> =`),
|
|
1731
1987
|
...typeVariants
|
|
1732
1988
|
],
|
|
1733
|
-
(
|
|
1989
|
+
(c) => c.join("\n")
|
|
1734
1990
|
);
|
|
1735
1991
|
}
|
|
1736
1992
|
|
|
1737
1993
|
// src/fragments/typeDiscriminatedUnionHelpers.ts
|
|
1738
1994
|
import { isDataEnum as isDataEnum3, isNode as isNode14 } from "@codama/nodes";
|
|
1995
|
+
import { pipe as pipe23 } from "@codama/visitors-core";
|
|
1739
1996
|
function getTypeDiscriminatedUnionHelpersFragment(scope) {
|
|
1740
1997
|
const { name, typeNode, nameApi } = scope;
|
|
1741
1998
|
const isDiscriminatedUnion = isNode14(typeNode, "enumTypeNode") && isDataEnum3(typeNode);
|
|
1742
1999
|
if (!isDiscriminatedUnion) {
|
|
1743
2000
|
return fragment("");
|
|
1744
2001
|
}
|
|
1745
|
-
return
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
"
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
function typeManifest() {
|
|
1761
|
-
return {
|
|
1762
|
-
decoder: fragment(""),
|
|
1763
|
-
encoder: fragment(""),
|
|
1764
|
-
isEnum: false,
|
|
1765
|
-
looseType: fragment(""),
|
|
1766
|
-
strictType: fragment(""),
|
|
1767
|
-
value: fragment("")
|
|
1768
|
-
};
|
|
1769
|
-
}
|
|
1770
|
-
function mergeManifests(manifests, options = {}) {
|
|
1771
|
-
const { mergeTypes, mergeCodecs, mergeValues } = options;
|
|
1772
|
-
const merge = (fragmentFn, mergeFn) => mergeFn ? mergeFragments(manifests.map(fragmentFn), mergeFn) : fragment("");
|
|
1773
|
-
return {
|
|
1774
|
-
decoder: merge((m) => m.decoder, mergeCodecs),
|
|
1775
|
-
encoder: merge((m) => m.encoder, mergeCodecs),
|
|
1776
|
-
isEnum: false,
|
|
1777
|
-
looseType: merge((m) => m.looseType, mergeTypes),
|
|
1778
|
-
strictType: merge((m) => m.strictType, mergeTypes),
|
|
1779
|
-
value: merge((m) => m.value, mergeValues)
|
|
1780
|
-
};
|
|
2002
|
+
return pipe23(
|
|
2003
|
+
fragmentFromTemplate("typeDiscriminatedUnionHelpers.njk", {
|
|
2004
|
+
discriminatedUnionDiscriminator: nameApi.discriminatedUnionDiscriminator(name),
|
|
2005
|
+
discriminatedUnionFunction: nameApi.discriminatedUnionFunction(name),
|
|
2006
|
+
getVariant: (variant) => nameApi.discriminatedUnionVariant(variant),
|
|
2007
|
+
isDiscriminatedUnionFunction: nameApi.isDiscriminatedUnionFunction(name),
|
|
2008
|
+
looseName: nameApi.dataArgsType(name),
|
|
2009
|
+
strictName: nameApi.dataType(name),
|
|
2010
|
+
typeNode
|
|
2011
|
+
}),
|
|
2012
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", [
|
|
2013
|
+
"type GetDiscriminatedUnionVariantContent",
|
|
2014
|
+
"type GetDiscriminatedUnionVariant"
|
|
2015
|
+
])
|
|
2016
|
+
);
|
|
1781
2017
|
}
|
|
1782
2018
|
|
|
1783
|
-
// src/getRenderMapVisitor.ts
|
|
1784
|
-
import { join as join3 } from "path";
|
|
1785
|
-
import { logWarn } from "@codama/errors";
|
|
1786
|
-
import {
|
|
1787
|
-
camelCase as camelCase12,
|
|
1788
|
-
definedTypeNode as definedTypeNode2,
|
|
1789
|
-
getAllAccounts,
|
|
1790
|
-
getAllDefinedTypes,
|
|
1791
|
-
getAllInstructionsWithSubs as getAllInstructionsWithSubs2,
|
|
1792
|
-
getAllPdas,
|
|
1793
|
-
getAllPrograms,
|
|
1794
|
-
resolveNestedTypeNode as resolveNestedTypeNode4,
|
|
1795
|
-
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes5
|
|
1796
|
-
} from "@codama/nodes";
|
|
1797
|
-
import { RenderMap } from "@codama/renderers-core";
|
|
1798
|
-
import {
|
|
1799
|
-
extendVisitor as extendVisitor2,
|
|
1800
|
-
findProgramNodeFromPath as findProgramNodeFromPath7,
|
|
1801
|
-
getByteSizeVisitor,
|
|
1802
|
-
getResolvedInstructionInputsVisitor,
|
|
1803
|
-
LinkableDictionary as LinkableDictionary3,
|
|
1804
|
-
NodeStack as NodeStack2,
|
|
1805
|
-
pipe as pipe2,
|
|
1806
|
-
recordLinkablesOnFirstVisitVisitor,
|
|
1807
|
-
recordNodeStackVisitor as recordNodeStackVisitor2,
|
|
1808
|
-
staticVisitor as staticVisitor2,
|
|
1809
|
-
visit as visit6
|
|
1810
|
-
} from "@codama/visitors-core";
|
|
1811
|
-
|
|
1812
2019
|
// src/getTypeManifestVisitor.ts
|
|
1813
2020
|
import {
|
|
1814
2021
|
camelCase as camelCase10,
|
|
@@ -1823,11 +2030,12 @@ import {
|
|
|
1823
2030
|
structTypeNode,
|
|
1824
2031
|
structTypeNodeFromInstructionArgumentNodes as structTypeNodeFromInstructionArgumentNodes4
|
|
1825
2032
|
} from "@codama/nodes";
|
|
2033
|
+
import { mapFragmentContent as mapFragmentContent11, setFragmentContent as setFragmentContent2 } from "@codama/renderers-core";
|
|
1826
2034
|
import {
|
|
1827
2035
|
extendVisitor,
|
|
1828
2036
|
findLastNodeFromPath,
|
|
1829
2037
|
NodeStack,
|
|
1830
|
-
pipe,
|
|
2038
|
+
pipe as pipe24,
|
|
1831
2039
|
recordNodeStackVisitor,
|
|
1832
2040
|
staticVisitor,
|
|
1833
2041
|
visit as visit5
|
|
@@ -1836,27 +2044,17 @@ function getTypeManifestVisitor(input) {
|
|
|
1836
2044
|
const { nameApi, linkables, nonScalarEnums, customAccountData, customInstructionData, getImportFrom } = input;
|
|
1837
2045
|
const stack = input.stack ?? new NodeStack();
|
|
1838
2046
|
let parentName = null;
|
|
1839
|
-
return
|
|
1840
|
-
staticVisitor(
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
keys: [
|
|
1851
|
-
...REGISTERED_TYPE_NODE_KINDS,
|
|
1852
|
-
...REGISTERED_VALUE_NODE_KINDS,
|
|
1853
|
-
"definedTypeLinkNode",
|
|
1854
|
-
"definedTypeNode",
|
|
1855
|
-
"accountNode",
|
|
1856
|
-
"instructionNode"
|
|
1857
|
-
]
|
|
1858
|
-
}
|
|
1859
|
-
),
|
|
2047
|
+
return pipe24(
|
|
2048
|
+
staticVisitor(() => typeManifest(), {
|
|
2049
|
+
keys: [
|
|
2050
|
+
...REGISTERED_TYPE_NODE_KINDS,
|
|
2051
|
+
...REGISTERED_VALUE_NODE_KINDS,
|
|
2052
|
+
"definedTypeLinkNode",
|
|
2053
|
+
"definedTypeNode",
|
|
2054
|
+
"accountNode",
|
|
2055
|
+
"instructionNode"
|
|
2056
|
+
]
|
|
2057
|
+
}),
|
|
1860
2058
|
(visitor) => extendVisitor(visitor, {
|
|
1861
2059
|
visitAccount(account, { self }) {
|
|
1862
2060
|
parentName = {
|
|
@@ -1873,17 +2071,29 @@ function getTypeManifestVisitor(input) {
|
|
|
1873
2071
|
},
|
|
1874
2072
|
visitArrayType(arrayType, { self }) {
|
|
1875
2073
|
const childManifest = visit5(arrayType.item, self);
|
|
1876
|
-
childManifest.looseType.mapRender((r) => `Array<${r}>`);
|
|
1877
|
-
childManifest.strictType.mapRender((r) => `Array<${r}>`);
|
|
1878
2074
|
const sizeManifest = getArrayLikeSizeOption(arrayType.count, self);
|
|
1879
|
-
const encoderOptions = sizeManifest.encoder.
|
|
1880
|
-
const decoderOptions = sizeManifest.decoder.
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
2075
|
+
const encoderOptions = sizeManifest.encoder.content ? `, { ${sizeManifest.encoder.content} }` : "";
|
|
2076
|
+
const decoderOptions = sizeManifest.decoder.content ? `, { ${sizeManifest.decoder.content} }` : "";
|
|
2077
|
+
return typeManifest({
|
|
2078
|
+
...childManifest,
|
|
2079
|
+
decoder: pipe24(
|
|
2080
|
+
childManifest.decoder,
|
|
2081
|
+
(f) => mapFragmentContent11(f, (c) => `getArrayDecoder(${c + decoderOptions})`),
|
|
2082
|
+
(f) => mergeFragmentImports(f, [sizeManifest.decoder.imports]),
|
|
2083
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getArrayDecoder"])
|
|
2084
|
+
),
|
|
2085
|
+
encoder: pipe24(
|
|
2086
|
+
childManifest.encoder,
|
|
2087
|
+
(f) => mapFragmentContent11(f, (c) => `getArrayEncoder(${c + encoderOptions})`),
|
|
2088
|
+
(f) => mergeFragmentImports(f, [sizeManifest.encoder.imports]),
|
|
2089
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getArrayEncoder"])
|
|
2090
|
+
),
|
|
2091
|
+
looseType: mapFragmentContent11(childManifest.looseType, (c) => `Array<${c}>`),
|
|
2092
|
+
strictType: mapFragmentContent11(childManifest.strictType, (c) => `Array<${c}>`)
|
|
2093
|
+
});
|
|
1884
2094
|
},
|
|
1885
2095
|
visitArrayValue(node, { self }) {
|
|
1886
|
-
return
|
|
2096
|
+
return mergeTypeManifests(
|
|
1887
2097
|
node.items.map((v) => visit5(v, self)),
|
|
1888
2098
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
1889
2099
|
);
|
|
@@ -1898,62 +2108,61 @@ function getTypeManifestVisitor(input) {
|
|
|
1898
2108
|
const size = visit5(booleanType.size, self);
|
|
1899
2109
|
encoderImports.mergeWith(size.encoder);
|
|
1900
2110
|
decoderImports.mergeWith(size.decoder);
|
|
1901
|
-
sizeEncoder = `{ size: ${size.encoder.
|
|
1902
|
-
sizeDecoder = `{ size: ${size.decoder.
|
|
2111
|
+
sizeEncoder = `{ size: ${size.encoder.content} }`;
|
|
2112
|
+
sizeDecoder = `{ size: ${size.decoder.content} }`;
|
|
1903
2113
|
}
|
|
1904
|
-
return {
|
|
1905
|
-
decoder:
|
|
1906
|
-
|
|
1907
|
-
|
|
2114
|
+
return typeManifest({
|
|
2115
|
+
decoder: pipe24(
|
|
2116
|
+
fragment(`getBooleanDecoder(${sizeDecoder})`),
|
|
2117
|
+
(f) => mergeFragmentImports(f, [decoderImports])
|
|
2118
|
+
),
|
|
2119
|
+
encoder: pipe24(
|
|
2120
|
+
fragment(`getBooleanEncoder(${sizeEncoder})`),
|
|
2121
|
+
(f) => mergeFragmentImports(f, [encoderImports])
|
|
2122
|
+
),
|
|
1908
2123
|
looseType: fragment("boolean"),
|
|
1909
|
-
strictType: fragment("boolean")
|
|
1910
|
-
|
|
1911
|
-
};
|
|
2124
|
+
strictType: fragment("boolean")
|
|
2125
|
+
});
|
|
1912
2126
|
},
|
|
1913
2127
|
visitBooleanValue(node) {
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
2128
|
+
return typeManifest({
|
|
2129
|
+
value: fragment(JSON.stringify(node.boolean))
|
|
2130
|
+
});
|
|
1917
2131
|
},
|
|
1918
2132
|
visitBytesType() {
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
"
|
|
1927
|
-
),
|
|
1928
|
-
isEnum: false,
|
|
1929
|
-
looseType: fragment("ReadonlyUint8Array").addImports(
|
|
1930
|
-
"solanaCodecsCore",
|
|
1931
|
-
"type ReadonlyUint8Array"
|
|
2133
|
+
const readonlyUint8Array = pipe24(
|
|
2134
|
+
fragment("ReadonlyUint8Array"),
|
|
2135
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["type ReadonlyUint8Array"])
|
|
2136
|
+
);
|
|
2137
|
+
return typeManifest({
|
|
2138
|
+
decoder: pipe24(
|
|
2139
|
+
fragment(`getBytesDecoder()`),
|
|
2140
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getBytesDecoder"])
|
|
1932
2141
|
),
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
"
|
|
2142
|
+
encoder: pipe24(
|
|
2143
|
+
fragment(`getBytesEncoder()`),
|
|
2144
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getBytesEncoder"])
|
|
1936
2145
|
),
|
|
1937
|
-
|
|
1938
|
-
|
|
2146
|
+
looseType: readonlyUint8Array,
|
|
2147
|
+
strictType: readonlyUint8Array
|
|
2148
|
+
});
|
|
1939
2149
|
},
|
|
1940
2150
|
visitBytesValue(node) {
|
|
1941
|
-
const manifest = typeManifest();
|
|
1942
2151
|
const bytes = getBytesFromBytesValueNode(node);
|
|
1943
|
-
|
|
1944
|
-
|
|
2152
|
+
return typeManifest({
|
|
2153
|
+
value: fragment(`new Uint8Array([${Array.from(bytes).join(", ")}])`)
|
|
2154
|
+
});
|
|
1945
2155
|
},
|
|
1946
2156
|
visitConstantValue(node, { self }) {
|
|
1947
2157
|
if (isNode15(node.type, "bytesTypeNode") && isNode15(node.value, "bytesValueNode")) {
|
|
1948
2158
|
return visit5(node.value, self);
|
|
1949
2159
|
}
|
|
1950
|
-
return {
|
|
1951
|
-
...typeManifest(),
|
|
2160
|
+
return typeManifest({
|
|
1952
2161
|
value: mergeFragments(
|
|
1953
2162
|
[visit5(node.type, self).encoder, visit5(node.value, self).value],
|
|
1954
2163
|
([encoderFunction, value]) => `${encoderFunction}.encode(${value})`
|
|
1955
2164
|
)
|
|
1956
|
-
};
|
|
2165
|
+
});
|
|
1957
2166
|
},
|
|
1958
2167
|
visitDateTimeType(dateTimeType, { self }) {
|
|
1959
2168
|
return visit5(dateTimeType.number, self);
|
|
@@ -1973,33 +2182,41 @@ function getTypeManifestVisitor(input) {
|
|
|
1973
2182
|
const encoderFunction = nameApi.encoderFunction(node.name);
|
|
1974
2183
|
const decoderFunction = nameApi.decoderFunction(node.name);
|
|
1975
2184
|
const importFrom = getImportFrom(node);
|
|
1976
|
-
return {
|
|
1977
|
-
decoder:
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
2185
|
+
return typeManifest({
|
|
2186
|
+
decoder: pipe24(
|
|
2187
|
+
fragment(`${decoderFunction}()`),
|
|
2188
|
+
(f) => addFragmentImports(f, importFrom, [decoderFunction])
|
|
2189
|
+
),
|
|
2190
|
+
encoder: pipe24(
|
|
2191
|
+
fragment(`${encoderFunction}()`),
|
|
2192
|
+
(f) => addFragmentImports(f, importFrom, [encoderFunction])
|
|
2193
|
+
),
|
|
2194
|
+
looseType: pipe24(
|
|
2195
|
+
fragment(looseName),
|
|
2196
|
+
(f) => addFragmentImports(f, importFrom, [`type ${looseName}`])
|
|
2197
|
+
),
|
|
2198
|
+
strictType: pipe24(
|
|
2199
|
+
fragment(strictName),
|
|
2200
|
+
(f) => addFragmentImports(f, importFrom, [`type ${strictName}`])
|
|
2201
|
+
)
|
|
2202
|
+
});
|
|
1984
2203
|
},
|
|
1985
2204
|
visitEnumEmptyVariantType(enumEmptyVariantType) {
|
|
1986
2205
|
const discriminator = nameApi.discriminatedUnionDiscriminator(camelCase10(parentName?.strict ?? ""));
|
|
1987
2206
|
const name = nameApi.discriminatedUnionVariant(enumEmptyVariantType.name);
|
|
1988
2207
|
const kindAttribute = `${discriminator}: "${name}"`;
|
|
1989
|
-
return {
|
|
1990
|
-
decoder:
|
|
1991
|
-
|
|
1992
|
-
"getUnitDecoder"
|
|
2208
|
+
return typeManifest({
|
|
2209
|
+
decoder: pipe24(
|
|
2210
|
+
fragment(`['${name}', getUnitDecoder()]`),
|
|
2211
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getUnitDecoder"])
|
|
1993
2212
|
),
|
|
1994
|
-
encoder:
|
|
1995
|
-
|
|
1996
|
-
"getUnitEncoder"
|
|
2213
|
+
encoder: pipe24(
|
|
2214
|
+
fragment(`['${name}', getUnitEncoder()]`),
|
|
2215
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getUnitEncoder"])
|
|
1997
2216
|
),
|
|
1998
|
-
isEnum: false,
|
|
1999
2217
|
looseType: fragment(`{ ${kindAttribute} }`),
|
|
2000
|
-
strictType: fragment(`{ ${kindAttribute} }`)
|
|
2001
|
-
|
|
2002
|
-
};
|
|
2218
|
+
strictType: fragment(`{ ${kindAttribute} }`)
|
|
2219
|
+
});
|
|
2003
2220
|
},
|
|
2004
2221
|
visitEnumStructVariantType(enumStructVariantType, { self }) {
|
|
2005
2222
|
const currentParentName = parentName;
|
|
@@ -2011,11 +2228,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2011
2228
|
parentName = null;
|
|
2012
2229
|
const structManifest = visit5(enumStructVariantType.struct, self);
|
|
2013
2230
|
parentName = currentParentName;
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2231
|
+
return typeManifest({
|
|
2232
|
+
...structManifest,
|
|
2233
|
+
decoder: pipe24(structManifest.decoder, (f) => mapFragmentContent11(f, (c) => `['${name}', ${c}]`)),
|
|
2234
|
+
encoder: pipe24(structManifest.encoder, (f) => mapFragmentContent11(f, (c) => `['${name}', ${c}]`)),
|
|
2235
|
+
looseType: pipe24(
|
|
2236
|
+
structManifest.looseType,
|
|
2237
|
+
(f) => mapFragmentContent11(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2238
|
+
),
|
|
2239
|
+
strictType: pipe24(
|
|
2240
|
+
structManifest.strictType,
|
|
2241
|
+
(f) => mapFragmentContent11(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2242
|
+
)
|
|
2243
|
+
});
|
|
2019
2244
|
},
|
|
2020
2245
|
visitEnumTupleVariantType(enumTupleVariantType, { self }) {
|
|
2021
2246
|
const currentParentName = parentName;
|
|
@@ -2033,11 +2258,19 @@ function getTypeManifestVisitor(input) {
|
|
|
2033
2258
|
parentName = null;
|
|
2034
2259
|
const structManifest = visit5(struct, self);
|
|
2035
2260
|
parentName = currentParentName;
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2261
|
+
return typeManifest({
|
|
2262
|
+
...structManifest,
|
|
2263
|
+
decoder: pipe24(structManifest.decoder, (f) => mapFragmentContent11(f, (c) => `['${name}', ${c}]`)),
|
|
2264
|
+
encoder: pipe24(structManifest.encoder, (f) => mapFragmentContent11(f, (c) => `['${name}', ${c}]`)),
|
|
2265
|
+
looseType: pipe24(
|
|
2266
|
+
structManifest.looseType,
|
|
2267
|
+
(f) => mapFragmentContent11(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2268
|
+
),
|
|
2269
|
+
strictType: pipe24(
|
|
2270
|
+
structManifest.strictType,
|
|
2271
|
+
(f) => mapFragmentContent11(f, (c) => `{ ${kindAttribute},${c.slice(1, -1)}}`)
|
|
2272
|
+
)
|
|
2273
|
+
});
|
|
2041
2274
|
},
|
|
2042
2275
|
visitEnumType(enumType, { self }) {
|
|
2043
2276
|
const currentParentName = parentName;
|
|
@@ -2050,8 +2283,8 @@ function getTypeManifestVisitor(input) {
|
|
|
2050
2283
|
const sizeManifest = visit5(enumType.size, self);
|
|
2051
2284
|
encoderImports.mergeWith(sizeManifest.encoder);
|
|
2052
2285
|
decoderImports.mergeWith(sizeManifest.decoder);
|
|
2053
|
-
encoderOptions.push(`size: ${sizeManifest.encoder.
|
|
2054
|
-
decoderOptions.push(`size: ${sizeManifest.decoder.
|
|
2286
|
+
encoderOptions.push(`size: ${sizeManifest.encoder.content}`);
|
|
2287
|
+
decoderOptions.push(`size: ${sizeManifest.decoder.content}`);
|
|
2055
2288
|
}
|
|
2056
2289
|
const discriminator = nameApi.discriminatedUnionDiscriminator(
|
|
2057
2290
|
camelCase10(currentParentName?.strict ?? "")
|
|
@@ -2069,31 +2302,50 @@ function getTypeManifestVisitor(input) {
|
|
|
2069
2302
|
);
|
|
2070
2303
|
}
|
|
2071
2304
|
const variantNames = enumType.variants.map(({ name }) => nameApi.enumVariant(name));
|
|
2072
|
-
return {
|
|
2073
|
-
decoder:
|
|
2074
|
-
`getEnumDecoder(${currentParentName.strict + decoderOptionsAsString})
|
|
2075
|
-
|
|
2305
|
+
return typeManifest({
|
|
2306
|
+
decoder: pipe24(
|
|
2307
|
+
fragment(`getEnumDecoder(${currentParentName.strict + decoderOptionsAsString})`),
|
|
2308
|
+
(f) => mergeFragmentImports(f, [decoderImports]),
|
|
2309
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getEnumDecoder"])
|
|
2076
2310
|
),
|
|
2077
|
-
encoder:
|
|
2078
|
-
`getEnumEncoder(${currentParentName.strict + encoderOptionsAsString})
|
|
2079
|
-
|
|
2311
|
+
encoder: pipe24(
|
|
2312
|
+
fragment(`getEnumEncoder(${currentParentName.strict + encoderOptionsAsString})`),
|
|
2313
|
+
(f) => mergeFragmentImports(f, [encoderImports]),
|
|
2314
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getEnumEncoder"])
|
|
2080
2315
|
),
|
|
2081
2316
|
isEnum: true,
|
|
2082
2317
|
looseType: fragment(`{ ${variantNames.join(", ")} }`),
|
|
2083
|
-
strictType: fragment(`{ ${variantNames.join(", ")} }`)
|
|
2084
|
-
|
|
2085
|
-
};
|
|
2318
|
+
strictType: fragment(`{ ${variantNames.join(", ")} }`)
|
|
2319
|
+
});
|
|
2086
2320
|
}
|
|
2087
|
-
const mergedManifest =
|
|
2321
|
+
const mergedManifest = mergeTypeManifests(
|
|
2088
2322
|
enumType.variants.map((variant) => visit5(variant, self)),
|
|
2089
2323
|
{
|
|
2090
2324
|
mergeCodecs: (renders) => renders.join(", "),
|
|
2091
2325
|
mergeTypes: (renders) => renders.join(" | ")
|
|
2092
2326
|
}
|
|
2093
2327
|
);
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2328
|
+
return typeManifest({
|
|
2329
|
+
...mergedManifest,
|
|
2330
|
+
decoder: pipe24(
|
|
2331
|
+
mergedManifest.decoder,
|
|
2332
|
+
(f) => mapFragmentContent11(
|
|
2333
|
+
f,
|
|
2334
|
+
(c) => `getDiscriminatedUnionDecoder([${c}]${decoderOptionsAsString})`
|
|
2335
|
+
),
|
|
2336
|
+
(f) => mergeFragmentImports(f, [decoderImports]),
|
|
2337
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getDiscriminatedUnionDecoder"])
|
|
2338
|
+
),
|
|
2339
|
+
encoder: pipe24(
|
|
2340
|
+
mergedManifest.encoder,
|
|
2341
|
+
(f) => mapFragmentContent11(
|
|
2342
|
+
f,
|
|
2343
|
+
(c) => `getDiscriminatedUnionEncoder([${c}]${encoderOptionsAsString})`
|
|
2344
|
+
),
|
|
2345
|
+
(f) => mergeFragmentImports(f, [encoderImports]),
|
|
2346
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getDiscriminatedUnionEncoder"])
|
|
2347
|
+
)
|
|
2348
|
+
});
|
|
2097
2349
|
},
|
|
2098
2350
|
visitEnumValue(node, { self }) {
|
|
2099
2351
|
const manifest = typeManifest();
|
|
@@ -2104,40 +2356,108 @@ function getTypeManifestVisitor(input) {
|
|
|
2104
2356
|
const isScalar = enumNode && isNode15(enumNode, "enumTypeNode") ? isScalarEnum(enumNode) : !nonScalarEnums.includes(node.enum.name);
|
|
2105
2357
|
if (!node.value && isScalar) {
|
|
2106
2358
|
const variantName2 = nameApi.enumVariant(node.variant);
|
|
2107
|
-
|
|
2108
|
-
|
|
2359
|
+
return typeManifest({
|
|
2360
|
+
...manifest,
|
|
2361
|
+
value: pipe24(
|
|
2362
|
+
manifest.value,
|
|
2363
|
+
(f) => setFragmentContent2(f, `${enumName}.${variantName2}`),
|
|
2364
|
+
(f) => addFragmentImports(f, importFrom, [enumName])
|
|
2365
|
+
)
|
|
2366
|
+
});
|
|
2109
2367
|
}
|
|
2110
2368
|
const variantName = nameApi.discriminatedUnionVariant(node.variant);
|
|
2111
2369
|
if (!node.value) {
|
|
2112
|
-
|
|
2113
|
-
|
|
2370
|
+
return typeManifest({
|
|
2371
|
+
...manifest,
|
|
2372
|
+
value: pipe24(
|
|
2373
|
+
manifest.value,
|
|
2374
|
+
(f) => setFragmentContent2(f, `${enumFunction}('${variantName}')`),
|
|
2375
|
+
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2376
|
+
)
|
|
2377
|
+
});
|
|
2114
2378
|
}
|
|
2115
|
-
|
|
2116
|
-
|
|
2379
|
+
return typeManifest({
|
|
2380
|
+
...manifest,
|
|
2381
|
+
value: pipe24(
|
|
2382
|
+
visit5(node.value, self).value,
|
|
2383
|
+
(f) => mapFragmentContent11(f, (c) => `${enumFunction}('${variantName}', ${c})`),
|
|
2384
|
+
(f) => addFragmentImports(f, importFrom, [enumFunction])
|
|
2385
|
+
)
|
|
2386
|
+
});
|
|
2117
2387
|
},
|
|
2118
2388
|
visitFixedSizeType(node, { self }) {
|
|
2119
2389
|
const manifest = visit5(node.type, self);
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2390
|
+
return typeManifest({
|
|
2391
|
+
...manifest,
|
|
2392
|
+
decoder: pipe24(
|
|
2393
|
+
manifest.decoder,
|
|
2394
|
+
(f) => mapFragmentContent11(f, (c) => `fixDecoderSize(${c}, ${node.size})`),
|
|
2395
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["fixDecoderSize"])
|
|
2396
|
+
),
|
|
2397
|
+
encoder: pipe24(
|
|
2398
|
+
manifest.encoder,
|
|
2399
|
+
(f) => mapFragmentContent11(f, (c) => `fixEncoderSize(${c}, ${node.size})`),
|
|
2400
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["fixEncoderSize"])
|
|
2401
|
+
)
|
|
2402
|
+
});
|
|
2123
2403
|
},
|
|
2124
2404
|
visitHiddenPrefixType(node, { self }) {
|
|
2125
2405
|
const manifest = visit5(node.type, self);
|
|
2126
2406
|
const prefixes = node.prefix.map((c) => visit5(c, self).value);
|
|
2127
|
-
const prefixEncoders =
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2407
|
+
const prefixEncoders = pipe24(
|
|
2408
|
+
mergeFragments(prefixes, (contents) => contents.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2409
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2410
|
+
);
|
|
2411
|
+
const prefixDecoders = pipe24(
|
|
2412
|
+
mergeFragments(prefixes, (contents) => contents.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2413
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2414
|
+
);
|
|
2415
|
+
return typeManifest({
|
|
2416
|
+
...manifest,
|
|
2417
|
+
decoder: pipe24(
|
|
2418
|
+
mergeFragments(
|
|
2419
|
+
[manifest.decoder, prefixDecoders],
|
|
2420
|
+
([child, prefixes2]) => `getHiddenPrefixDecoder(${child}, [${prefixes2}])`
|
|
2421
|
+
),
|
|
2422
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getHiddenPrefixDecoder"])
|
|
2423
|
+
),
|
|
2424
|
+
encoder: pipe24(
|
|
2425
|
+
mergeFragments(
|
|
2426
|
+
[manifest.encoder, prefixEncoders],
|
|
2427
|
+
([child, prefixes2]) => `getHiddenPrefixEncoder(${child}, [${prefixes2}])`
|
|
2428
|
+
),
|
|
2429
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getHiddenPrefixEncoder"])
|
|
2430
|
+
)
|
|
2431
|
+
});
|
|
2132
2432
|
},
|
|
2133
2433
|
visitHiddenSuffixType(node, { self }) {
|
|
2134
2434
|
const manifest = visit5(node.type, self);
|
|
2135
2435
|
const suffixes = node.suffix.map((c) => visit5(c, self).value);
|
|
2136
|
-
const suffixEncoders =
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2436
|
+
const suffixEncoders = pipe24(
|
|
2437
|
+
mergeFragments(suffixes, (contents) => contents.map((c) => `getConstantEncoder(${c})`).join(", ")),
|
|
2438
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantEncoder"])
|
|
2439
|
+
);
|
|
2440
|
+
const suffixDecoders = pipe24(
|
|
2441
|
+
mergeFragments(suffixes, (contents) => contents.map((c) => `getConstantDecoder(${c})`).join(", ")),
|
|
2442
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["getConstantDecoder"])
|
|
2443
|
+
);
|
|
2444
|
+
return typeManifest({
|
|
2445
|
+
...manifest,
|
|
2446
|
+
decoder: pipe24(
|
|
2447
|
+
mergeFragments(
|
|
2448
|
+
[manifest.decoder, suffixDecoders],
|
|
2449
|
+
([child, suffixes2]) => `getHiddenSuffixDecoder(${child}, [${suffixes2}])`
|
|
2450
|
+
),
|
|
2451
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getHiddenSuffixDecoder"])
|
|
2452
|
+
),
|
|
2453
|
+
encoder: pipe24(
|
|
2454
|
+
mergeFragments(
|
|
2455
|
+
[manifest.encoder, suffixEncoders],
|
|
2456
|
+
([child, suffixes2]) => `getHiddenSuffixEncoder(${child}, [${suffixes2}])`
|
|
2457
|
+
),
|
|
2458
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getHiddenSuffixEncoder"])
|
|
2459
|
+
)
|
|
2460
|
+
});
|
|
2141
2461
|
},
|
|
2142
2462
|
visitInstruction(instruction, { self }) {
|
|
2143
2463
|
const instructionDataName = nameApi.instructionDataType(instruction.name);
|
|
@@ -2152,34 +2472,44 @@ function getTypeManifestVisitor(input) {
|
|
|
2152
2472
|
return manifest;
|
|
2153
2473
|
},
|
|
2154
2474
|
visitMapEntryValue(node, { self }) {
|
|
2155
|
-
return
|
|
2475
|
+
return mergeTypeManifests([visit5(node.key, self), visit5(node.value, self)], {
|
|
2156
2476
|
mergeValues: (renders) => `[${renders.join(", ")}]`
|
|
2157
2477
|
});
|
|
2158
2478
|
},
|
|
2159
2479
|
visitMapType(mapType, { self }) {
|
|
2160
2480
|
const key = visit5(mapType.key, self);
|
|
2161
2481
|
const value = visit5(mapType.value, self);
|
|
2162
|
-
const mergedManifest =
|
|
2482
|
+
const mergedManifest = mergeTypeManifests([key, value], {
|
|
2163
2483
|
mergeCodecs: ([k, v]) => `${k}, ${v}`,
|
|
2164
2484
|
mergeTypes: ([k, v]) => `Map<${k}, ${v}>`
|
|
2165
2485
|
});
|
|
2166
2486
|
const sizeManifest = getArrayLikeSizeOption(mapType.count, self);
|
|
2167
|
-
const encoderOptions = sizeManifest.encoder.
|
|
2168
|
-
const decoderOptions = sizeManifest.decoder.
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2487
|
+
const encoderOptions = sizeManifest.encoder.content ? `, { ${sizeManifest.encoder.content} }` : "";
|
|
2488
|
+
const decoderOptions = sizeManifest.decoder.content ? `, { ${sizeManifest.decoder.content} }` : "";
|
|
2489
|
+
return typeManifest({
|
|
2490
|
+
...mergedManifest,
|
|
2491
|
+
decoder: pipe24(
|
|
2492
|
+
mergedManifest.decoder,
|
|
2493
|
+
(f) => mapFragmentContent11(f, (c) => `getMapDecoder(${c}${decoderOptions})`),
|
|
2494
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getMapDecoder"])
|
|
2495
|
+
),
|
|
2496
|
+
encoder: pipe24(
|
|
2497
|
+
mergedManifest.encoder,
|
|
2498
|
+
(f) => mapFragmentContent11(f, (c) => `getMapEncoder(${c}${encoderOptions})`),
|
|
2499
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getMapEncoder"])
|
|
2500
|
+
)
|
|
2501
|
+
});
|
|
2172
2502
|
},
|
|
2173
2503
|
visitMapValue(node, { self }) {
|
|
2174
2504
|
const entryFragments = node.entries.map((entry) => visit5(entry, self));
|
|
2175
|
-
return
|
|
2505
|
+
return mergeTypeManifests(entryFragments, {
|
|
2176
2506
|
mergeValues: (renders) => `new Map([${renders.join(", ")}])`
|
|
2177
2507
|
});
|
|
2178
2508
|
},
|
|
2179
2509
|
visitNoneValue() {
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2510
|
+
return typeManifest({
|
|
2511
|
+
value: pipe24(fragment("none()"), (f) => addFragmentImports(f, "solanaOptions", ["none"]))
|
|
2512
|
+
});
|
|
2183
2513
|
},
|
|
2184
2514
|
visitNumberType(numberType) {
|
|
2185
2515
|
const encoderFunction = nameApi.encoderFunction(numberType.format);
|
|
@@ -2193,33 +2523,37 @@ function getTypeManifestVisitor(input) {
|
|
|
2193
2523
|
decoderImports.add("solanaCodecsNumbers", "Endian");
|
|
2194
2524
|
endianness = "{ endian: Endian.Big }";
|
|
2195
2525
|
}
|
|
2196
|
-
return {
|
|
2197
|
-
decoder:
|
|
2198
|
-
|
|
2199
|
-
|
|
2526
|
+
return typeManifest({
|
|
2527
|
+
decoder: pipe24(
|
|
2528
|
+
fragment(`${decoderFunction}(${endianness})`),
|
|
2529
|
+
(f) => mergeFragmentImports(f, [decoderImports])
|
|
2530
|
+
),
|
|
2531
|
+
encoder: pipe24(
|
|
2532
|
+
fragment(`${encoderFunction}(${endianness})`),
|
|
2533
|
+
(f) => mergeFragmentImports(f, [encoderImports])
|
|
2534
|
+
),
|
|
2200
2535
|
looseType: fragment(isBigNumber ? "number | bigint" : "number"),
|
|
2201
|
-
strictType: fragment(isBigNumber ? "bigint" : "number")
|
|
2202
|
-
|
|
2203
|
-
};
|
|
2536
|
+
strictType: fragment(isBigNumber ? "bigint" : "number")
|
|
2537
|
+
});
|
|
2204
2538
|
},
|
|
2205
2539
|
visitNumberValue(node) {
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2540
|
+
return typeManifest({
|
|
2541
|
+
value: fragment(JSON.stringify(node.number))
|
|
2542
|
+
});
|
|
2209
2543
|
},
|
|
2210
2544
|
visitOptionType(optionType, { self }) {
|
|
2211
2545
|
const childManifest = visit5(optionType.item, self);
|
|
2212
|
-
childManifest.strictType.mapRender((r) => `Option<${r}>`).addImports("solanaOptions", "type Option");
|
|
2213
|
-
childManifest.looseType.mapRender((r) => `OptionOrNullable<${r}>`).addImports("solanaOptions", "type OptionOrNullable");
|
|
2214
2546
|
const encoderOptions = [];
|
|
2215
2547
|
const decoderOptions = [];
|
|
2548
|
+
const encoderImports = new ImportMap();
|
|
2549
|
+
const decoderImports = new ImportMap();
|
|
2216
2550
|
const optionPrefix = resolveNestedTypeNode3(optionType.prefix);
|
|
2217
2551
|
if (optionPrefix.format !== "u8" || optionPrefix.endian !== "le") {
|
|
2218
2552
|
const prefixManifest = visit5(optionType.prefix, self);
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
encoderOptions.push(`prefix: ${prefixManifest.encoder.
|
|
2222
|
-
decoderOptions.push(`prefix: ${prefixManifest.decoder.
|
|
2553
|
+
encoderImports.mergeWith(prefixManifest.encoder);
|
|
2554
|
+
decoderImports.mergeWith(prefixManifest.decoder);
|
|
2555
|
+
encoderOptions.push(`prefix: ${prefixManifest.encoder.content}`);
|
|
2556
|
+
decoderOptions.push(`prefix: ${prefixManifest.decoder.content}`);
|
|
2223
2557
|
}
|
|
2224
2558
|
if (optionType.fixed) {
|
|
2225
2559
|
encoderOptions.push(`noneValue: "zeroes"`);
|
|
@@ -2227,16 +2561,48 @@ function getTypeManifestVisitor(input) {
|
|
|
2227
2561
|
}
|
|
2228
2562
|
const encoderOptionsAsString = encoderOptions.length > 0 ? `, { ${encoderOptions.join(", ")} }` : "";
|
|
2229
2563
|
const decoderOptionsAsString = decoderOptions.length > 0 ? `, { ${decoderOptions.join(", ")} }` : "";
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2564
|
+
return typeManifest({
|
|
2565
|
+
...childManifest,
|
|
2566
|
+
decoder: pipe24(
|
|
2567
|
+
childManifest.decoder,
|
|
2568
|
+
(f) => mapFragmentContent11(f, (c) => `getOptionDecoder(${c + decoderOptionsAsString})`),
|
|
2569
|
+
(f) => addFragmentImports(f, "solanaOptions", ["getOptionDecoder"]),
|
|
2570
|
+
(f) => mergeFragmentImports(f, [decoderImports])
|
|
2571
|
+
),
|
|
2572
|
+
encoder: pipe24(
|
|
2573
|
+
childManifest.encoder,
|
|
2574
|
+
(f) => mapFragmentContent11(f, (c) => `getOptionEncoder(${c + encoderOptionsAsString})`),
|
|
2575
|
+
(f) => addFragmentImports(f, "solanaOptions", ["getOptionEncoder"]),
|
|
2576
|
+
(f) => mergeFragmentImports(f, [encoderImports])
|
|
2577
|
+
),
|
|
2578
|
+
looseType: pipe24(
|
|
2579
|
+
childManifest.looseType,
|
|
2580
|
+
(f) => mapFragmentContent11(f, (c) => `OptionOrNullable<${c}>`),
|
|
2581
|
+
(f) => addFragmentImports(f, "solanaOptions", ["type OptionOrNullable"])
|
|
2582
|
+
),
|
|
2583
|
+
strictType: pipe24(
|
|
2584
|
+
childManifest.strictType,
|
|
2585
|
+
(f) => mapFragmentContent11(f, (c) => `Option<${c}>`),
|
|
2586
|
+
(f) => addFragmentImports(f, "solanaOptions", ["type Option"])
|
|
2587
|
+
)
|
|
2588
|
+
});
|
|
2233
2589
|
},
|
|
2234
2590
|
visitPostOffsetType(node, { self }) {
|
|
2235
2591
|
const manifest = visit5(node.type, self);
|
|
2236
2592
|
if (node.strategy === "padded") {
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2593
|
+
return typeManifest({
|
|
2594
|
+
...manifest,
|
|
2595
|
+
decoder: pipe24(
|
|
2596
|
+
manifest.decoder,
|
|
2597
|
+
(f) => mapFragmentContent11(f, (c) => `padRightDecoder(${c}, ${node.offset})`),
|
|
2598
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["padRightDecoder"])
|
|
2599
|
+
),
|
|
2600
|
+
encoder: pipe24(
|
|
2601
|
+
manifest.encoder,
|
|
2602
|
+
(f) => mapFragmentContent11(f, (c) => `padRightEncoder(${c}, ${node.offset})`),
|
|
2603
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["padRightEncoder"])
|
|
2604
|
+
)
|
|
2605
|
+
});
|
|
2240
2606
|
}
|
|
2241
2607
|
const fn = (() => {
|
|
2242
2608
|
switch (node.strategy) {
|
|
@@ -2249,16 +2615,36 @@ function getTypeManifestVisitor(input) {
|
|
|
2249
2615
|
return node.offset < 0 ? `({ postOffset }) => postOffset ${node.offset}` : `({ postOffset }) => postOffset + ${node.offset}`;
|
|
2250
2616
|
}
|
|
2251
2617
|
})();
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2618
|
+
return typeManifest({
|
|
2619
|
+
...manifest,
|
|
2620
|
+
decoder: pipe24(
|
|
2621
|
+
manifest.decoder,
|
|
2622
|
+
(f) => mapFragmentContent11(f, (c) => `offsetDecoder(${c}, { postOffset: ${fn} })`),
|
|
2623
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["offsetDecoder"])
|
|
2624
|
+
),
|
|
2625
|
+
encoder: pipe24(
|
|
2626
|
+
manifest.encoder,
|
|
2627
|
+
(f) => mapFragmentContent11(f, (c) => `offsetEncoder(${c}, { postOffset: ${fn} })`),
|
|
2628
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["offsetEncoder"])
|
|
2629
|
+
)
|
|
2630
|
+
});
|
|
2255
2631
|
},
|
|
2256
2632
|
visitPreOffsetType(node, { self }) {
|
|
2257
2633
|
const manifest = visit5(node.type, self);
|
|
2258
2634
|
if (node.strategy === "padded") {
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2635
|
+
return typeManifest({
|
|
2636
|
+
...manifest,
|
|
2637
|
+
decoder: pipe24(
|
|
2638
|
+
manifest.decoder,
|
|
2639
|
+
(f) => mapFragmentContent11(f, (c) => `padLeftDecoder(${c}, ${node.offset})`),
|
|
2640
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["padLeftDecoder"])
|
|
2641
|
+
),
|
|
2642
|
+
encoder: pipe24(
|
|
2643
|
+
manifest.encoder,
|
|
2644
|
+
(f) => mapFragmentContent11(f, (c) => `padLeftEncoder(${c}, ${node.offset})`),
|
|
2645
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["padLeftEncoder"])
|
|
2646
|
+
)
|
|
2647
|
+
});
|
|
2262
2648
|
}
|
|
2263
2649
|
const fn = (() => {
|
|
2264
2650
|
switch (node.strategy) {
|
|
@@ -2269,58 +2655,122 @@ function getTypeManifestVisitor(input) {
|
|
|
2269
2655
|
return node.offset < 0 ? `({ preOffset }) => preOffset ${node.offset}` : `({ preOffset }) => preOffset + ${node.offset}`;
|
|
2270
2656
|
}
|
|
2271
2657
|
})();
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2658
|
+
return typeManifest({
|
|
2659
|
+
...manifest,
|
|
2660
|
+
decoder: pipe24(
|
|
2661
|
+
manifest.decoder,
|
|
2662
|
+
(f) => mapFragmentContent11(f, (c) => `offsetDecoder(${c}, { preOffset: ${fn} })`),
|
|
2663
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["offsetDecoder"])
|
|
2664
|
+
),
|
|
2665
|
+
encoder: pipe24(
|
|
2666
|
+
manifest.encoder,
|
|
2667
|
+
(f) => mapFragmentContent11(f, (c) => `offsetEncoder(${c}, { preOffset: ${fn} })`),
|
|
2668
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["offsetEncoder"])
|
|
2669
|
+
)
|
|
2670
|
+
});
|
|
2275
2671
|
},
|
|
2276
2672
|
visitPublicKeyType() {
|
|
2277
|
-
const
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2673
|
+
const addressFragment = pipe24(
|
|
2674
|
+
fragment("Address"),
|
|
2675
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["type Address"])
|
|
2676
|
+
);
|
|
2677
|
+
return typeManifest({
|
|
2678
|
+
decoder: pipe24(
|
|
2679
|
+
fragment("getAddressDecoder()"),
|
|
2680
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["getAddressDecoder"])
|
|
2681
|
+
),
|
|
2682
|
+
encoder: pipe24(
|
|
2683
|
+
fragment("getAddressEncoder()"),
|
|
2684
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["getAddressEncoder"])
|
|
2685
|
+
),
|
|
2686
|
+
looseType: addressFragment,
|
|
2687
|
+
strictType: addressFragment
|
|
2688
|
+
});
|
|
2286
2689
|
},
|
|
2287
2690
|
visitPublicKeyValue(node) {
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2691
|
+
return typeManifest({
|
|
2692
|
+
value: pipe24(
|
|
2693
|
+
fragment(`address("${node.publicKey}")`),
|
|
2694
|
+
(f) => addFragmentImports(f, "solanaAddresses", ["address"])
|
|
2695
|
+
)
|
|
2696
|
+
});
|
|
2291
2697
|
},
|
|
2292
2698
|
visitRemainderOptionType(node, { self }) {
|
|
2293
2699
|
const childManifest = visit5(node.item, self);
|
|
2294
|
-
childManifest.strictType.mapRender((r) => `Option<${r}>`).addImports("solanaOptions", "type Option");
|
|
2295
|
-
childManifest.looseType.mapRender((r) => `OptionOrNullable<${r}>`).addImports("solanaOptions", "type OptionOrNullable");
|
|
2296
2700
|
const encoderOptions = ["prefix: null"];
|
|
2297
2701
|
const decoderOptions = ["prefix: null"];
|
|
2298
2702
|
const encoderOptionsAsString = encoderOptions.length > 0 ? `, { ${encoderOptions.join(", ")} }` : "";
|
|
2299
2703
|
const decoderOptionsAsString = decoderOptions.length > 0 ? `, { ${decoderOptions.join(", ")} }` : "";
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2704
|
+
return typeManifest({
|
|
2705
|
+
...childManifest,
|
|
2706
|
+
decoder: pipe24(
|
|
2707
|
+
childManifest.decoder,
|
|
2708
|
+
(f) => mapFragmentContent11(f, (c) => `getOptionDecoder(${c + decoderOptionsAsString})`),
|
|
2709
|
+
(f) => addFragmentImports(f, "solanaOptions", ["getOptionDecoder"])
|
|
2710
|
+
),
|
|
2711
|
+
encoder: pipe24(
|
|
2712
|
+
childManifest.encoder,
|
|
2713
|
+
(f) => mapFragmentContent11(f, (c) => `getOptionEncoder(${c + encoderOptionsAsString})`),
|
|
2714
|
+
(f) => addFragmentImports(f, "solanaOptions", ["getOptionEncoder"])
|
|
2715
|
+
),
|
|
2716
|
+
looseType: pipe24(
|
|
2717
|
+
childManifest.looseType,
|
|
2718
|
+
(f) => mapFragmentContent11(f, (c) => `OptionOrNullable<${c}>`),
|
|
2719
|
+
(f) => addFragmentImports(f, "solanaOptions", ["type OptionOrNullable"])
|
|
2720
|
+
),
|
|
2721
|
+
strictType: pipe24(
|
|
2722
|
+
childManifest.strictType,
|
|
2723
|
+
(f) => mapFragmentContent11(f, (c) => `Option<${c}>`),
|
|
2724
|
+
(f) => addFragmentImports(f, "solanaOptions", ["type Option"])
|
|
2725
|
+
)
|
|
2726
|
+
});
|
|
2303
2727
|
},
|
|
2304
2728
|
visitSentinelType(node, { self }) {
|
|
2305
2729
|
const manifest = visit5(node.type, self);
|
|
2306
2730
|
const sentinel = visit5(node.sentinel, self).value;
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2731
|
+
return typeManifest({
|
|
2732
|
+
...manifest,
|
|
2733
|
+
decoder: pipe24(
|
|
2734
|
+
mergeFragments(
|
|
2735
|
+
[manifest.decoder, sentinel],
|
|
2736
|
+
([child, sentinel2]) => `addDecoderSentinel(${child}, ${sentinel2})`
|
|
2737
|
+
),
|
|
2738
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["addDecoderSentinel"])
|
|
2739
|
+
),
|
|
2740
|
+
encoder: pipe24(
|
|
2741
|
+
mergeFragments(
|
|
2742
|
+
[manifest.encoder, sentinel],
|
|
2743
|
+
([child, sentinel2]) => `addEncoderSentinel(${child}, ${sentinel2})`
|
|
2744
|
+
),
|
|
2745
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["addEncoderSentinel"])
|
|
2746
|
+
)
|
|
2747
|
+
});
|
|
2310
2748
|
},
|
|
2311
2749
|
visitSetType(setType, { self }) {
|
|
2312
2750
|
const childManifest = visit5(setType.item, self);
|
|
2313
|
-
childManifest.strictType.mapRender((r) => `Set<${r}>`);
|
|
2314
|
-
childManifest.looseType.mapRender((r) => `Set<${r}>`);
|
|
2315
2751
|
const sizeManifest = getArrayLikeSizeOption(setType.count, self);
|
|
2316
|
-
const encoderOptions = sizeManifest.encoder.
|
|
2317
|
-
const decoderOptions = sizeManifest.decoder.
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2752
|
+
const encoderOptions = sizeManifest.encoder.content ? `, { ${sizeManifest.encoder.content} }` : "";
|
|
2753
|
+
const decoderOptions = sizeManifest.decoder.content ? `, { ${sizeManifest.decoder.content} }` : "";
|
|
2754
|
+
return typeManifest({
|
|
2755
|
+
...childManifest,
|
|
2756
|
+
decoder: pipe24(
|
|
2757
|
+
childManifest.decoder,
|
|
2758
|
+
(f) => mapFragmentContent11(f, (c) => `getSetDecoder(${c + decoderOptions})`),
|
|
2759
|
+
(f) => mergeFragmentImports(f, [sizeManifest.decoder.imports]),
|
|
2760
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getSetDecoder"])
|
|
2761
|
+
),
|
|
2762
|
+
encoder: pipe24(
|
|
2763
|
+
childManifest.encoder,
|
|
2764
|
+
(f) => mapFragmentContent11(f, (c) => `getSetEncoder(${c + encoderOptions})`),
|
|
2765
|
+
(f) => mergeFragmentImports(f, [sizeManifest.encoder.imports]),
|
|
2766
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getSetEncoder"])
|
|
2767
|
+
),
|
|
2768
|
+
looseType: pipe24(childManifest.looseType, (f) => mapFragmentContent11(f, (c) => `Set<${c}>`)),
|
|
2769
|
+
strictType: pipe24(childManifest.strictType, (f) => mapFragmentContent11(f, (c) => `Set<${c}>`))
|
|
2770
|
+
});
|
|
2321
2771
|
},
|
|
2322
2772
|
visitSetValue(node, { self }) {
|
|
2323
|
-
return
|
|
2773
|
+
return mergeTypeManifests(
|
|
2324
2774
|
node.items.map((v) => visit5(v, self)),
|
|
2325
2775
|
{ mergeValues: (renders) => `new Set([${renders.join(", ")}])` }
|
|
2326
2776
|
);
|
|
@@ -2328,26 +2778,54 @@ function getTypeManifestVisitor(input) {
|
|
|
2328
2778
|
visitSizePrefixType(node, { self }) {
|
|
2329
2779
|
const manifest = visit5(node.type, self);
|
|
2330
2780
|
const prefix = visit5(node.prefix, self);
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2781
|
+
return typeManifest({
|
|
2782
|
+
...manifest,
|
|
2783
|
+
decoder: pipe24(
|
|
2784
|
+
mergeFragments(
|
|
2785
|
+
[manifest.decoder, prefix.decoder],
|
|
2786
|
+
([decoder, prefix2]) => `addDecoderSizePrefix(${decoder}, ${prefix2})`
|
|
2787
|
+
),
|
|
2788
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["addDecoderSizePrefix"])
|
|
2789
|
+
),
|
|
2790
|
+
encoder: pipe24(
|
|
2791
|
+
mergeFragments(
|
|
2792
|
+
[manifest.encoder, prefix.encoder],
|
|
2793
|
+
([encoder, prefix2]) => `addEncoderSizePrefix(${encoder}, ${prefix2})`
|
|
2794
|
+
),
|
|
2795
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["addEncoderSizePrefix"])
|
|
2796
|
+
)
|
|
2797
|
+
});
|
|
2334
2798
|
},
|
|
2335
2799
|
visitSolAmountType({ number }, { self }) {
|
|
2336
2800
|
const numberManifest = visit5(number, self);
|
|
2337
|
-
const
|
|
2338
|
-
|
|
2339
|
-
|
|
2801
|
+
const lamportFragment = pipe24(
|
|
2802
|
+
fragment("Lamports"),
|
|
2803
|
+
(f) => addFragmentImports(f, "solanaRpcTypes", ["type Lamports"])
|
|
2804
|
+
);
|
|
2805
|
+
return typeManifest({
|
|
2340
2806
|
...numberManifest,
|
|
2341
|
-
decoder:
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2807
|
+
decoder: pipe24(
|
|
2808
|
+
numberManifest.decoder,
|
|
2809
|
+
(f) => mapFragmentContent11(f, (c) => `getLamportsDecoder(${c})`),
|
|
2810
|
+
(f) => addFragmentImports(f, "solanaRpcTypes", ["getLamportsDecoder"])
|
|
2811
|
+
),
|
|
2812
|
+
encoder: pipe24(
|
|
2813
|
+
numberManifest.encoder,
|
|
2814
|
+
(f) => mapFragmentContent11(f, (c) => `getLamportsEncoder(${c})`),
|
|
2815
|
+
(f) => addFragmentImports(f, "solanaRpcTypes", ["getLamportsEncoder"])
|
|
2816
|
+
),
|
|
2817
|
+
looseType: lamportFragment,
|
|
2818
|
+
strictType: lamportFragment
|
|
2819
|
+
});
|
|
2346
2820
|
},
|
|
2347
2821
|
visitSomeValue(node, { self }) {
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2822
|
+
return typeManifest({
|
|
2823
|
+
value: pipe24(
|
|
2824
|
+
visit5(node.value, self).value,
|
|
2825
|
+
(f) => mapFragmentContent11(f, (c) => `some(${c})`),
|
|
2826
|
+
(f) => addFragmentImports(f, "solanaOptions", ["some"])
|
|
2827
|
+
)
|
|
2828
|
+
});
|
|
2351
2829
|
},
|
|
2352
2830
|
visitStringType(stringType) {
|
|
2353
2831
|
const [encoder, decoder] = (() => {
|
|
@@ -2364,57 +2842,99 @@ function getTypeManifestVisitor(input) {
|
|
|
2364
2842
|
throw new Error(`Unsupported string encoding: ${stringType.encoding}`);
|
|
2365
2843
|
}
|
|
2366
2844
|
})();
|
|
2367
|
-
return {
|
|
2368
|
-
decoder:
|
|
2369
|
-
|
|
2370
|
-
|
|
2845
|
+
return typeManifest({
|
|
2846
|
+
decoder: pipe24(
|
|
2847
|
+
fragment(`${decoder}()`),
|
|
2848
|
+
(f) => addFragmentImports(f, "solanaCodecsStrings", [decoder])
|
|
2849
|
+
),
|
|
2850
|
+
encoder: pipe24(
|
|
2851
|
+
fragment(`${encoder}()`),
|
|
2852
|
+
(f) => addFragmentImports(f, "solanaCodecsStrings", [encoder])
|
|
2853
|
+
),
|
|
2371
2854
|
looseType: fragment("string"),
|
|
2372
|
-
strictType: fragment("string")
|
|
2373
|
-
|
|
2374
|
-
};
|
|
2855
|
+
strictType: fragment("string")
|
|
2856
|
+
});
|
|
2375
2857
|
},
|
|
2376
2858
|
visitStringValue(node) {
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2859
|
+
return typeManifest({
|
|
2860
|
+
value: fragment(JSON.stringify(node.string))
|
|
2861
|
+
});
|
|
2380
2862
|
},
|
|
2381
2863
|
visitStructFieldType(structFieldType, { self }) {
|
|
2382
2864
|
const name = camelCase10(structFieldType.name);
|
|
2383
|
-
const
|
|
2865
|
+
const originalChildManifest = visit5(structFieldType.type, self);
|
|
2384
2866
|
const structFieldDocs = parseDocs2(structFieldType.docs);
|
|
2385
2867
|
const docblock = structFieldDocs.length > 0 ? `
|
|
2386
2868
|
${jsDocblock(structFieldDocs)}` : "";
|
|
2387
|
-
const originalLooseType =
|
|
2388
|
-
childManifest
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2869
|
+
const originalLooseType = originalChildManifest.looseType.content;
|
|
2870
|
+
const childManifest = typeManifest({
|
|
2871
|
+
...originalChildManifest,
|
|
2872
|
+
decoder: pipe24(
|
|
2873
|
+
originalChildManifest.decoder,
|
|
2874
|
+
(f) => mapFragmentContent11(f, (c) => `['${name}', ${c}]`)
|
|
2875
|
+
),
|
|
2876
|
+
encoder: pipe24(
|
|
2877
|
+
originalChildManifest.encoder,
|
|
2878
|
+
(f) => mapFragmentContent11(f, (c) => `['${name}', ${c}]`)
|
|
2879
|
+
),
|
|
2880
|
+
looseType: pipe24(
|
|
2881
|
+
originalChildManifest.looseType,
|
|
2882
|
+
(f) => mapFragmentContent11(f, (c) => `${docblock}${name}: ${c}; `)
|
|
2883
|
+
),
|
|
2884
|
+
strictType: pipe24(
|
|
2885
|
+
originalChildManifest.strictType,
|
|
2886
|
+
(f) => mapFragmentContent11(f, (c) => `${docblock}${name}: ${c}; `)
|
|
2887
|
+
)
|
|
2888
|
+
});
|
|
2392
2889
|
if (!structFieldType.defaultValue) {
|
|
2393
2890
|
return childManifest;
|
|
2394
2891
|
}
|
|
2395
2892
|
if (structFieldType.defaultValueStrategy !== "omitted") {
|
|
2396
|
-
|
|
2397
|
-
|
|
2893
|
+
return typeManifest({
|
|
2894
|
+
...childManifest,
|
|
2895
|
+
looseType: pipe24(
|
|
2896
|
+
childManifest.looseType,
|
|
2897
|
+
(f) => setFragmentContent2(f, `${docblock}${name}?: ${originalLooseType}; `)
|
|
2898
|
+
)
|
|
2899
|
+
});
|
|
2398
2900
|
}
|
|
2399
|
-
|
|
2400
|
-
|
|
2901
|
+
return typeManifest({
|
|
2902
|
+
...childManifest,
|
|
2903
|
+
looseType: fragment("")
|
|
2904
|
+
});
|
|
2401
2905
|
},
|
|
2402
2906
|
visitStructFieldValue(node, { self }) {
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2907
|
+
return typeManifest({
|
|
2908
|
+
value: pipe24(
|
|
2909
|
+
visit5(node.value, self).value,
|
|
2910
|
+
(f) => mapFragmentContent11(f, (c) => `${node.name}: ${c}`)
|
|
2911
|
+
)
|
|
2912
|
+
});
|
|
2406
2913
|
},
|
|
2407
2914
|
visitStructType(structType, { self }) {
|
|
2408
2915
|
const optionalFields = structType.fields.filter((f) => !!f.defaultValue);
|
|
2409
|
-
const mergedManifest =
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2916
|
+
const mergedManifest = pipe24(
|
|
2917
|
+
mergeTypeManifests(
|
|
2918
|
+
structType.fields.map((field) => visit5(field, self)),
|
|
2919
|
+
{
|
|
2920
|
+
mergeCodecs: (renders) => `([${renders.join(", ")}])`,
|
|
2921
|
+
mergeTypes: (renders) => `{ ${renders.join("")} }`
|
|
2922
|
+
}
|
|
2923
|
+
),
|
|
2924
|
+
(manifest) => typeManifest({
|
|
2925
|
+
...manifest,
|
|
2926
|
+
decoder: pipe24(
|
|
2927
|
+
manifest.decoder,
|
|
2928
|
+
(f) => mapFragmentContent11(f, (c) => `getStructDecoder${c}`),
|
|
2929
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getStructDecoder"])
|
|
2930
|
+
),
|
|
2931
|
+
encoder: pipe24(
|
|
2932
|
+
manifest.encoder,
|
|
2933
|
+
(f) => mapFragmentContent11(f, (c) => `getStructEncoder${c}`),
|
|
2934
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getStructEncoder"])
|
|
2935
|
+
)
|
|
2936
|
+
})
|
|
2415
2937
|
);
|
|
2416
|
-
mergedManifest.encoder.mapRender((r) => `getStructEncoder${r}`).addImports("solanaCodecsDataStructures", "getStructEncoder");
|
|
2417
|
-
mergedManifest.decoder.mapRender((r) => `getStructDecoder${r}`).addImports("solanaCodecsDataStructures", "getStructDecoder");
|
|
2418
2938
|
if (optionalFields.length === 0) {
|
|
2419
2939
|
return mergedManifest;
|
|
2420
2940
|
}
|
|
@@ -2424,6 +2944,7 @@ ${jsDocblock(structFieldDocs)}` : "";
|
|
|
2424
2944
|
const discriminatorPrefix = instructionNode ? instructionNode.name : accountNode?.name;
|
|
2425
2945
|
const discriminators = (instructionNode ? instructionNode.discriminators : accountNode?.discriminators) ?? [];
|
|
2426
2946
|
const fieldDiscriminators = discriminators.filter(isNodeFilter6("fieldDiscriminatorNode"));
|
|
2947
|
+
const encoderImports = new ImportMap();
|
|
2427
2948
|
const defaultValues = optionalFields.map((f) => {
|
|
2428
2949
|
const key = camelCase10(f.name);
|
|
2429
2950
|
if (fieldDiscriminators.some((d) => d.name === f.name)) {
|
|
@@ -2431,56 +2952,98 @@ ${jsDocblock(structFieldDocs)}` : "";
|
|
|
2431
2952
|
return f.defaultValueStrategy === "omitted" ? `${key}: ${constantName}` : `${key}: value.${key} ?? ${constantName}`;
|
|
2432
2953
|
}
|
|
2433
2954
|
const defaultValue = f.defaultValue;
|
|
2434
|
-
const {
|
|
2435
|
-
|
|
2955
|
+
const { content: renderedValue, imports } = visit5(defaultValue, self).value;
|
|
2956
|
+
encoderImports.mergeWith(imports);
|
|
2436
2957
|
return f.defaultValueStrategy === "omitted" ? `${key}: ${renderedValue}` : `${key}: value.${key} ?? ${renderedValue}`;
|
|
2437
2958
|
}).join(", ");
|
|
2438
|
-
|
|
2439
|
-
|
|
2959
|
+
return typeManifest({
|
|
2960
|
+
...mergedManifest,
|
|
2961
|
+
encoder: pipe24(
|
|
2962
|
+
mergedManifest.encoder,
|
|
2963
|
+
(f) => mapFragmentContent11(
|
|
2964
|
+
f,
|
|
2965
|
+
(c) => `transformEncoder(${c}, (value) => ({ ...value, ${defaultValues} }))`
|
|
2966
|
+
),
|
|
2967
|
+
(f) => addFragmentImports(f, "solanaCodecsCore", ["transformEncoder"]),
|
|
2968
|
+
(f) => mergeFragmentImports(f, [encoderImports])
|
|
2969
|
+
)
|
|
2970
|
+
});
|
|
2440
2971
|
},
|
|
2441
2972
|
visitStructValue(node, { self }) {
|
|
2442
|
-
return
|
|
2973
|
+
return mergeTypeManifests(
|
|
2443
2974
|
node.fields.map((field) => visit5(field, self)),
|
|
2444
2975
|
{ mergeValues: (renders) => `{ ${renders.join(", ")} }` }
|
|
2445
2976
|
);
|
|
2446
2977
|
},
|
|
2447
2978
|
visitTupleType(tupleType, { self }) {
|
|
2448
2979
|
const items = tupleType.items.map((item) => visit5(item, self));
|
|
2449
|
-
const mergedManifest =
|
|
2980
|
+
const mergedManifest = mergeTypeManifests(items, {
|
|
2450
2981
|
mergeCodecs: (codecs) => `[${codecs.join(", ")}]`,
|
|
2451
2982
|
mergeTypes: (types) => `readonly [${types.join(", ")}]`
|
|
2452
2983
|
});
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2984
|
+
return typeManifest({
|
|
2985
|
+
...mergedManifest,
|
|
2986
|
+
decoder: pipe24(
|
|
2987
|
+
mergedManifest.decoder,
|
|
2988
|
+
(f) => mapFragmentContent11(f, (c) => `getTupleDecoder(${c})`),
|
|
2989
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getTupleDecoder"])
|
|
2990
|
+
),
|
|
2991
|
+
encoder: pipe24(
|
|
2992
|
+
mergedManifest.encoder,
|
|
2993
|
+
(f) => mapFragmentContent11(f, (c) => `getTupleEncoder(${c})`),
|
|
2994
|
+
(f) => addFragmentImports(f, "solanaCodecsDataStructures", ["getTupleEncoder"])
|
|
2995
|
+
)
|
|
2996
|
+
});
|
|
2456
2997
|
},
|
|
2457
2998
|
visitTupleValue(node, { self }) {
|
|
2458
|
-
return
|
|
2999
|
+
return mergeTypeManifests(
|
|
2459
3000
|
node.items.map((v) => visit5(v, self)),
|
|
2460
3001
|
{ mergeValues: (renders) => `[${renders.join(", ")}]` }
|
|
2461
3002
|
);
|
|
2462
3003
|
},
|
|
2463
3004
|
visitZeroableOptionType(node, { self }) {
|
|
2464
3005
|
const childManifest = visit5(node.item, self);
|
|
2465
|
-
childManifest.strictType.mapRender((r) => `Option<${r}>`).addImports("solanaOptions", "type Option");
|
|
2466
|
-
childManifest.looseType.mapRender((r) => `OptionOrNullable<${r}>`).addImports("solanaOptions", "type OptionOrNullable");
|
|
2467
3006
|
const encoderOptions = ["prefix: null"];
|
|
2468
3007
|
const decoderOptions = ["prefix: null"];
|
|
3008
|
+
const encoderImports = new ImportMap();
|
|
3009
|
+
const decoderImports = new ImportMap();
|
|
2469
3010
|
if (node.zeroValue) {
|
|
2470
3011
|
const zeroValueManifest = visit5(node.zeroValue, self);
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
encoderOptions.push(`noneValue: ${zeroValueManifest.value.
|
|
2474
|
-
decoderOptions.push(`noneValue: ${zeroValueManifest.value.
|
|
3012
|
+
encoderImports.mergeWith(zeroValueManifest.value);
|
|
3013
|
+
decoderImports.mergeWith(zeroValueManifest.value);
|
|
3014
|
+
encoderOptions.push(`noneValue: ${zeroValueManifest.value.content}`);
|
|
3015
|
+
decoderOptions.push(`noneValue: ${zeroValueManifest.value.content}`);
|
|
2475
3016
|
} else {
|
|
2476
3017
|
encoderOptions.push(`noneValue: "zeroes"`);
|
|
2477
3018
|
decoderOptions.push(`noneValue: "zeroes"`);
|
|
2478
3019
|
}
|
|
2479
3020
|
const encoderOptionsAsString = encoderOptions.length > 0 ? `, { ${encoderOptions.join(", ")} }` : "";
|
|
2480
3021
|
const decoderOptionsAsString = decoderOptions.length > 0 ? `, { ${decoderOptions.join(", ")} }` : "";
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
3022
|
+
return typeManifest({
|
|
3023
|
+
...childManifest,
|
|
3024
|
+
decoder: pipe24(
|
|
3025
|
+
childManifest.decoder,
|
|
3026
|
+
(f) => mapFragmentContent11(f, (c) => `getOptionDecoder(${c + decoderOptionsAsString})`),
|
|
3027
|
+
(f) => addFragmentImports(f, "solanaOptions", ["getOptionDecoder"]),
|
|
3028
|
+
(f) => mergeFragmentImports(f, [decoderImports])
|
|
3029
|
+
),
|
|
3030
|
+
encoder: pipe24(
|
|
3031
|
+
childManifest.encoder,
|
|
3032
|
+
(f) => mapFragmentContent11(f, (c) => `getOptionEncoder(${c + encoderOptionsAsString})`),
|
|
3033
|
+
(f) => addFragmentImports(f, "solanaOptions", ["getOptionEncoder"]),
|
|
3034
|
+
(f) => mergeFragmentImports(f, [encoderImports])
|
|
3035
|
+
),
|
|
3036
|
+
looseType: pipe24(
|
|
3037
|
+
childManifest.looseType,
|
|
3038
|
+
(f) => mapFragmentContent11(f, (c) => `OptionOrNullable<${c}>`),
|
|
3039
|
+
(f) => addFragmentImports(f, "solanaOptions", ["type OptionOrNullable"])
|
|
3040
|
+
),
|
|
3041
|
+
strictType: pipe24(
|
|
3042
|
+
childManifest.strictType,
|
|
3043
|
+
(f) => mapFragmentContent11(f, (c) => `Option<${c}>`),
|
|
3044
|
+
(f) => addFragmentImports(f, "solanaOptions", ["type Option"])
|
|
3045
|
+
)
|
|
3046
|
+
});
|
|
2484
3047
|
}
|
|
2485
3048
|
}),
|
|
2486
3049
|
(visitor) => recordNodeStackVisitor(visitor, stack)
|
|
@@ -2504,9 +3067,10 @@ function getArrayLikeSizeOption(count, visitor) {
|
|
|
2504
3067
|
return { decoder: fragment(""), encoder: fragment("") };
|
|
2505
3068
|
}
|
|
2506
3069
|
const prefixManifest = visit5(count.prefix, visitor);
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
3070
|
+
return {
|
|
3071
|
+
decoder: pipe24(prefixManifest.decoder, (f) => mapFragmentContent11(f, (c) => `size: ${c}`)),
|
|
3072
|
+
encoder: pipe24(prefixManifest.encoder, (f) => mapFragmentContent11(f, (c) => `size: ${c}`))
|
|
3073
|
+
};
|
|
2510
3074
|
}
|
|
2511
3075
|
|
|
2512
3076
|
// src/nameTransformers.ts
|
|
@@ -2614,10 +3178,10 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2614
3178
|
typeManifestVisitor
|
|
2615
3179
|
};
|
|
2616
3180
|
const render2 = (template, context, renderOptions) => {
|
|
2617
|
-
return render(
|
|
3181
|
+
return render(join2("pages", template), context, renderOptions);
|
|
2618
3182
|
};
|
|
2619
|
-
return
|
|
2620
|
-
staticVisitor2(() =>
|
|
3183
|
+
return pipe25(
|
|
3184
|
+
staticVisitor2(() => renderMap(), {
|
|
2621
3185
|
keys: ["rootNode", "programNode", "pdaNode", "accountNode", "definedTypeNode", "instructionNode"]
|
|
2622
3186
|
}),
|
|
2623
3187
|
(v) => extendVisitor2(v, {
|
|
@@ -2650,14 +3214,15 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2650
3214
|
accountSizeHelpersFragment,
|
|
2651
3215
|
accountPdaHelpersFragment
|
|
2652
3216
|
);
|
|
2653
|
-
return
|
|
3217
|
+
return addToRenderMap(
|
|
3218
|
+
renderMap(),
|
|
2654
3219
|
`accounts/${camelCase12(node.name)}.ts`,
|
|
2655
3220
|
render2("accountsPage.njk", {
|
|
2656
|
-
accountDiscriminatorConstantsFragment,
|
|
2657
|
-
accountFetchHelpersFragment,
|
|
2658
|
-
accountPdaHelpersFragment,
|
|
2659
|
-
accountSizeHelpersFragment,
|
|
2660
|
-
accountTypeFragment,
|
|
3221
|
+
accountDiscriminatorConstantsFragment: accountDiscriminatorConstantsFragment.content,
|
|
3222
|
+
accountFetchHelpersFragment: accountFetchHelpersFragment.content,
|
|
3223
|
+
accountPdaHelpersFragment: accountPdaHelpersFragment.content,
|
|
3224
|
+
accountSizeHelpersFragment: accountSizeHelpersFragment.content,
|
|
3225
|
+
accountTypeFragment: accountTypeFragment.content,
|
|
2661
3226
|
imports: imports.toString(dependencyMap, useGranularImports)
|
|
2662
3227
|
})
|
|
2663
3228
|
);
|
|
@@ -2684,15 +3249,13 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2684
3249
|
nameApi.decoderFunction(node.name),
|
|
2685
3250
|
nameApi.codecFunction(node.name)
|
|
2686
3251
|
]);
|
|
2687
|
-
return
|
|
3252
|
+
return addToRenderMap(
|
|
3253
|
+
renderMap(),
|
|
2688
3254
|
`types/${camelCase12(node.name)}.ts`,
|
|
2689
3255
|
render2("definedTypesPage.njk", {
|
|
2690
|
-
imports: imports.toString({
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
}),
|
|
2694
|
-
typeDiscriminatedUnionHelpersFragment,
|
|
2695
|
-
typeWithCodecFragment
|
|
3256
|
+
imports: imports.toString({ ...dependencyMap, generatedTypes: "." }),
|
|
3257
|
+
typeDiscriminatedUnionHelpersFragment: typeDiscriminatedUnionHelpersFragment.content,
|
|
3258
|
+
typeWithCodecFragment: typeWithCodecFragment.content
|
|
2696
3259
|
})
|
|
2697
3260
|
);
|
|
2698
3261
|
},
|
|
@@ -2744,34 +3307,36 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2744
3307
|
instructionFunctionSyncFragment,
|
|
2745
3308
|
instructionParseFunctionFragment
|
|
2746
3309
|
);
|
|
2747
|
-
return
|
|
3310
|
+
return addToRenderMap(
|
|
3311
|
+
renderMap(),
|
|
2748
3312
|
`instructions/${camelCase12(node.name)}.ts`,
|
|
2749
3313
|
render2("instructionsPage.njk", {
|
|
2750
3314
|
imports: imports.toString(dependencyMap, useGranularImports),
|
|
2751
3315
|
instruction: node,
|
|
2752
|
-
instructionDataFragment,
|
|
2753
|
-
instructionDiscriminatorConstantsFragment,
|
|
2754
|
-
instructionExtraArgsFragment,
|
|
2755
|
-
instructionFunctionAsyncFragment,
|
|
2756
|
-
instructionFunctionSyncFragment,
|
|
2757
|
-
instructionParseFunctionFragment,
|
|
2758
|
-
instructionTypeFragment
|
|
3316
|
+
instructionDataFragment: instructionDataFragment.content,
|
|
3317
|
+
instructionDiscriminatorConstantsFragment: instructionDiscriminatorConstantsFragment.content,
|
|
3318
|
+
instructionExtraArgsFragment: instructionExtraArgsFragment.content,
|
|
3319
|
+
instructionFunctionAsyncFragment: instructionFunctionAsyncFragment.content,
|
|
3320
|
+
instructionFunctionSyncFragment: instructionFunctionSyncFragment.content,
|
|
3321
|
+
instructionParseFunctionFragment: instructionParseFunctionFragment.content,
|
|
3322
|
+
instructionTypeFragment: instructionTypeFragment.content
|
|
2759
3323
|
})
|
|
2760
3324
|
);
|
|
2761
3325
|
},
|
|
2762
3326
|
visitPda(node) {
|
|
2763
3327
|
const pdaPath = stack.getPath("pdaNode");
|
|
2764
3328
|
if (!findProgramNodeFromPath7(pdaPath)) {
|
|
2765
|
-
throw new Error("
|
|
3329
|
+
throw new Error("PDA must be visited inside a program.");
|
|
2766
3330
|
}
|
|
2767
3331
|
const scope = { ...globalScope, pdaPath };
|
|
2768
3332
|
const pdaFunctionFragment = getPdaFunctionFragment(scope);
|
|
2769
3333
|
const imports = new ImportMap().mergeWith(pdaFunctionFragment);
|
|
2770
|
-
return
|
|
3334
|
+
return addToRenderMap(
|
|
3335
|
+
renderMap(),
|
|
2771
3336
|
`pdas/${camelCase12(node.name)}.ts`,
|
|
2772
3337
|
render2("pdasPage.njk", {
|
|
2773
3338
|
imports: imports.toString(dependencyMap, useGranularImports),
|
|
2774
|
-
pdaFunctionFragment
|
|
3339
|
+
pdaFunctionFragment: pdaFunctionFragment.content
|
|
2775
3340
|
})
|
|
2776
3341
|
);
|
|
2777
3342
|
},
|
|
@@ -2781,35 +3346,42 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2781
3346
|
...getDefinedTypeNodesToExtract(node.instructions, customInstructionData)
|
|
2782
3347
|
];
|
|
2783
3348
|
const scope = { ...globalScope, programNode: node };
|
|
2784
|
-
|
|
3349
|
+
let renders = mergeRenderMaps([
|
|
3350
|
+
...node.pdas.map((p) => visit6(p, self)),
|
|
3351
|
+
...node.accounts.map((a) => visit6(a, self)),
|
|
3352
|
+
...node.definedTypes.map((t) => visit6(t, self)),
|
|
3353
|
+
...customDataDefinedType.map((t) => visit6(t, self))
|
|
3354
|
+
]);
|
|
2785
3355
|
if (node.errors.length > 0) {
|
|
2786
3356
|
const programErrorsFragment = getProgramErrorsFragment(scope);
|
|
2787
|
-
|
|
3357
|
+
renders = addToRenderMap(
|
|
3358
|
+
renders,
|
|
2788
3359
|
`errors/${camelCase12(node.name)}.ts`,
|
|
2789
3360
|
render2("errorsPage.njk", {
|
|
2790
3361
|
imports: new ImportMap().mergeWith(programErrorsFragment).toString(dependencyMap, useGranularImports),
|
|
2791
|
-
programErrorsFragment
|
|
3362
|
+
programErrorsFragment: programErrorsFragment.content
|
|
2792
3363
|
})
|
|
2793
3364
|
);
|
|
2794
3365
|
}
|
|
2795
3366
|
const programFragment = getProgramFragment(scope);
|
|
2796
3367
|
const programAccountsFragment = getProgramAccountsFragment(scope);
|
|
2797
3368
|
const programInstructionsFragment = getProgramInstructionsFragment(scope);
|
|
2798
|
-
|
|
3369
|
+
renders = addToRenderMap(
|
|
3370
|
+
renders,
|
|
2799
3371
|
`programs/${camelCase12(node.name)}.ts`,
|
|
2800
3372
|
render2("programsPage.njk", {
|
|
2801
3373
|
imports: new ImportMap().mergeWith(programFragment, programAccountsFragment, programInstructionsFragment).toString(dependencyMap, useGranularImports),
|
|
2802
|
-
programAccountsFragment,
|
|
2803
|
-
programFragment,
|
|
2804
|
-
programInstructionsFragment
|
|
3374
|
+
programAccountsFragment: programAccountsFragment.content,
|
|
3375
|
+
programFragment: programFragment.content,
|
|
3376
|
+
programInstructionsFragment: programInstructionsFragment.content
|
|
2805
3377
|
})
|
|
2806
3378
|
);
|
|
2807
|
-
|
|
3379
|
+
return mergeRenderMaps([
|
|
3380
|
+
renders,
|
|
2808
3381
|
...getAllInstructionsWithSubs2(node, {
|
|
2809
3382
|
leavesOnly: !renderParentInstructions
|
|
2810
3383
|
}).map((ix) => visit6(ix, self))
|
|
2811
|
-
);
|
|
2812
|
-
return renderMap;
|
|
3384
|
+
]);
|
|
2813
3385
|
},
|
|
2814
3386
|
visitRoot(node, { self }) {
|
|
2815
3387
|
const isNotInternal = (n) => !internalNodes.includes(n.name);
|
|
@@ -2832,9 +3404,10 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2832
3404
|
programsWithErrorsToExport,
|
|
2833
3405
|
root: node
|
|
2834
3406
|
};
|
|
2835
|
-
|
|
3407
|
+
let renders = renderMap();
|
|
2836
3408
|
if (hasAnythingToExport) {
|
|
2837
|
-
|
|
3409
|
+
renders = addToRenderMap(
|
|
3410
|
+
renders,
|
|
2838
3411
|
"shared/index.ts",
|
|
2839
3412
|
render2("sharedPage.njk", {
|
|
2840
3413
|
...ctx,
|
|
@@ -2855,24 +3428,32 @@ function getRenderMapVisitor(options = {}) {
|
|
|
2855
3428
|
);
|
|
2856
3429
|
}
|
|
2857
3430
|
if (programsToExport.length > 0) {
|
|
2858
|
-
|
|
3431
|
+
renders = addToRenderMap(renders, "programs/index.ts", render2("programsIndex.njk", ctx));
|
|
2859
3432
|
}
|
|
2860
3433
|
if (programsWithErrorsToExport.length > 0) {
|
|
2861
|
-
|
|
3434
|
+
renders = addToRenderMap(renders, "errors/index.ts", render2("errorsIndex.njk", ctx));
|
|
2862
3435
|
}
|
|
2863
3436
|
if (accountsToExport.length > 0) {
|
|
2864
|
-
|
|
3437
|
+
renders = addToRenderMap(renders, "accounts/index.ts", render2("accountsIndex.njk", ctx));
|
|
2865
3438
|
}
|
|
2866
3439
|
if (pdasToExport.length > 0) {
|
|
2867
|
-
|
|
3440
|
+
renders = addToRenderMap(renders, "pdas/index.ts", render2("pdasIndex.njk", ctx));
|
|
2868
3441
|
}
|
|
2869
3442
|
if (instructionsToExport.length > 0) {
|
|
2870
|
-
|
|
3443
|
+
renders = addToRenderMap(
|
|
3444
|
+
renders,
|
|
3445
|
+
"instructions/index.ts",
|
|
3446
|
+
render2("instructionsIndex.njk", ctx)
|
|
3447
|
+
);
|
|
2871
3448
|
}
|
|
2872
3449
|
if (definedTypesToExport.length > 0) {
|
|
2873
|
-
|
|
3450
|
+
renders = addToRenderMap(renders, "types/index.ts", render2("definedTypesIndex.njk", ctx));
|
|
2874
3451
|
}
|
|
2875
|
-
return
|
|
3452
|
+
return pipe25(
|
|
3453
|
+
renders,
|
|
3454
|
+
(r) => addToRenderMap(r, "index.ts", render2("rootIndex.njk", ctx)),
|
|
3455
|
+
(r) => mergeRenderMaps([r, ...getAllPrograms(node).map((p) => visit6(p, self))])
|
|
3456
|
+
);
|
|
2876
3457
|
}
|
|
2877
3458
|
}),
|
|
2878
3459
|
(v) => recordNodeStackVisitor2(v, stack),
|
|
@@ -2898,7 +3479,7 @@ function getRenamedArgsMap(instruction) {
|
|
|
2898
3479
|
}
|
|
2899
3480
|
|
|
2900
3481
|
// src/renderVisitor.ts
|
|
2901
|
-
import { deleteDirectory } from "@codama/renderers-core";
|
|
3482
|
+
import { deleteDirectory, mapRenderMapContentAsync, writeRenderMap } from "@codama/renderers-core";
|
|
2902
3483
|
import { rootNodeVisitor, visit as visit7 } from "@codama/visitors-core";
|
|
2903
3484
|
import * as estreePlugin from "prettier/plugins/estree";
|
|
2904
3485
|
import * as typeScriptPlugin from "prettier/plugins/typescript";
|
|
@@ -2919,12 +3500,12 @@ function renderVisitor(path, options = {}) {
|
|
|
2919
3500
|
if (options.deleteFolderBeforeRendering ?? true) {
|
|
2920
3501
|
deleteDirectory(path);
|
|
2921
3502
|
}
|
|
2922
|
-
|
|
3503
|
+
let renderMap2 = visit7(root, getRenderMapVisitor(options));
|
|
2923
3504
|
if (options.formatCode ?? true) {
|
|
2924
3505
|
const prettierOptions = { ...DEFAULT_PRETTIER_OPTIONS, ...options.prettierOptions };
|
|
2925
|
-
await
|
|
3506
|
+
renderMap2 = await mapRenderMapContentAsync(renderMap2, (code) => format(code, prettierOptions));
|
|
2926
3507
|
}
|
|
2927
|
-
|
|
3508
|
+
writeRenderMap(renderMap2, path);
|
|
2928
3509
|
});
|
|
2929
3510
|
}
|
|
2930
3511
|
export {
|
|
@@ -2934,7 +3515,7 @@ export {
|
|
|
2934
3515
|
getNameApi,
|
|
2935
3516
|
getRenderMapVisitor,
|
|
2936
3517
|
getTypeManifestVisitor,
|
|
2937
|
-
|
|
3518
|
+
mergeTypeManifests,
|
|
2938
3519
|
renderVisitor,
|
|
2939
3520
|
typeManifest
|
|
2940
3521
|
};
|