@codama/dynamic-address-resolution 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +155 -0
  3. package/dist/index.browser.cjs +1288 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.mjs +1278 -0
  6. package/dist/index.browser.mjs.map +1 -0
  7. package/dist/index.node.cjs +1288 -0
  8. package/dist/index.node.cjs.map +1 -0
  9. package/dist/index.node.mjs +1278 -0
  10. package/dist/index.node.mjs.map +1 -0
  11. package/dist/index.react-native.mjs +1278 -0
  12. package/dist/index.react-native.mjs.map +1 -0
  13. package/dist/types/index.d.ts +7 -0
  14. package/dist/types/index.d.ts.map +1 -0
  15. package/dist/types/resolvers/index.d.ts +9 -0
  16. package/dist/types/resolvers/index.d.ts.map +1 -0
  17. package/dist/types/resolvers/resolve-account-address.d.ts +13 -0
  18. package/dist/types/resolvers/resolve-account-address.d.ts.map +1 -0
  19. package/dist/types/resolvers/resolve-account-value-node-address.d.ts +15 -0
  20. package/dist/types/resolvers/resolve-account-value-node-address.d.ts.map +1 -0
  21. package/dist/types/resolvers/resolve-conditional.d.ts +13 -0
  22. package/dist/types/resolvers/resolve-conditional.d.ts.map +1 -0
  23. package/dist/types/resolvers/resolve-instruction-account-address.d.ts +13 -0
  24. package/dist/types/resolvers/resolve-instruction-account-address.d.ts.map +1 -0
  25. package/dist/types/resolvers/resolve-pda-address.d.ts +13 -0
  26. package/dist/types/resolvers/resolve-pda-address.d.ts.map +1 -0
  27. package/dist/types/resolvers/resolve-standalone-pda.d.ts +8 -0
  28. package/dist/types/resolvers/resolve-standalone-pda.d.ts.map +1 -0
  29. package/dist/types/resolvers/types.d.ts +16 -0
  30. package/dist/types/resolvers/types.d.ts.map +1 -0
  31. package/dist/types/shared/address.d.ts +13 -0
  32. package/dist/types/shared/address.d.ts.map +1 -0
  33. package/dist/types/shared/bytes-encoding.d.ts +15 -0
  34. package/dist/types/shared/bytes-encoding.d.ts.map +1 -0
  35. package/dist/types/shared/codecs.d.ts +9 -0
  36. package/dist/types/shared/codecs.d.ts.map +1 -0
  37. package/dist/types/shared/nodes.d.ts +2 -0
  38. package/dist/types/shared/nodes.d.ts.map +1 -0
  39. package/dist/types/shared/types.d.ts +13 -0
  40. package/dist/types/shared/types.d.ts.map +1 -0
  41. package/dist/types/shared/util.d.ts +15 -0
  42. package/dist/types/shared/util.d.ts.map +1 -0
  43. package/dist/types/visitors/account-default-value.d.ts +16 -0
  44. package/dist/types/visitors/account-default-value.d.ts.map +1 -0
  45. package/dist/types/visitors/codec-input-transformer.d.ts +37 -0
  46. package/dist/types/visitors/codec-input-transformer.d.ts.map +1 -0
  47. package/dist/types/visitors/condition-node-value.d.ts +12 -0
  48. package/dist/types/visitors/condition-node-value.d.ts.map +1 -0
  49. package/dist/types/visitors/default-value-encoder.d.ts +16 -0
  50. package/dist/types/visitors/default-value-encoder.d.ts.map +1 -0
  51. package/dist/types/visitors/index.d.ts +7 -0
  52. package/dist/types/visitors/index.d.ts.map +1 -0
  53. package/dist/types/visitors/pda-seed-value.d.ts +20 -0
  54. package/dist/types/visitors/pda-seed-value.d.ts.map +1 -0
  55. package/dist/types/visitors/value-node-value.d.ts +14 -0
  56. package/dist/types/visitors/value-node-value.d.ts.map +1 -0
  57. package/package.json +71 -0
@@ -0,0 +1,1288 @@
1
+ 'use strict';
2
+
3
+ var errors = require('@codama/errors');
4
+ var codama = require('codama');
5
+ var addresses = require('@solana/addresses');
6
+ var dynamicCodecs = require('@codama/dynamic-codecs');
7
+ var codecs = require('@solana/codecs');
8
+
9
+ // src/resolvers/resolve-account-address.ts
10
+
11
+ // src/shared/util.ts
12
+ function isObjectRecord(value) {
13
+ return typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
14
+ }
15
+ function getMaybeNodeKind(node) {
16
+ if (!isObjectRecord(node)) return null;
17
+ return node.kind ?? null;
18
+ }
19
+ function formatValueType(value) {
20
+ if (value === null) return "null";
21
+ if (Array.isArray(value)) return `array (length ${value.length})`;
22
+ if (value instanceof Uint8Array) return `Uint8Array (length ${value.length})`;
23
+ if (typeof value === "object") return "object";
24
+ return typeof value;
25
+ }
26
+ function safeStringify(value) {
27
+ try {
28
+ return JSON.stringify(value, (_key, v) => typeof v === "bigint" ? String(v) : v);
29
+ } catch {
30
+ return `non-serializable ${formatValueType(value)}`;
31
+ }
32
+ }
33
+
34
+ // src/shared/address.ts
35
+ function isPublicKeyLike(value) {
36
+ const obj = value;
37
+ return typeof value === "object" && value !== null && "toBase58" in obj && typeof obj.toBase58 === "function";
38
+ }
39
+ function toAddress(input) {
40
+ if (isPublicKeyLike(input)) return addresses.address(input.toBase58());
41
+ if (typeof input === "string" && addresses.isAddress(input)) return addresses.address(input);
42
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__CANNOT_CONVERT_TO_ADDRESS, {
43
+ value: safeStringify(input)
44
+ });
45
+ }
46
+ function isAddressConvertible(value) {
47
+ if (value == null) return false;
48
+ return isPublicKeyLike(value) || typeof value === "string" && addresses.isAddress(value);
49
+ }
50
+ async function resolveAccountValueNodeAddress(node, ctx) {
51
+ const { accountsInput, ixNode, resolutionPath } = ctx;
52
+ const providedAddress = accountsInput?.[node.name];
53
+ if (providedAddress !== void 0 && providedAddress !== null) {
54
+ return toAddress(providedAddress);
55
+ }
56
+ const referencedIxAccountNode = ixNode.accounts.find((acc) => acc.name === node.name);
57
+ if (!referencedIxAccountNode) {
58
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__NODE_REFERENCE_NOT_FOUND, {
59
+ instructionName: ixNode.name,
60
+ referencedName: node.name
61
+ });
62
+ }
63
+ detectCircularDependency(node.name, resolutionPath);
64
+ return await resolveAccountAddress({
65
+ accountsInput: ctx.accountsInput,
66
+ argumentsInput: ctx.argumentsInput,
67
+ ixAccountNode: referencedIxAccountNode,
68
+ ixNode,
69
+ resolutionPath: [...resolutionPath, node.name],
70
+ resolversInput: ctx.resolversInput,
71
+ root: ctx.root
72
+ });
73
+ }
74
+ function detectCircularDependency(nodeName, resolutionPath) {
75
+ if (resolutionPath.includes(nodeName)) {
76
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__CIRCULAR_ACCOUNT_DEPENDENCY, {
77
+ chain: [...resolutionPath, nodeName].join(" -> ")
78
+ });
79
+ }
80
+ }
81
+ var CONDITION_NODE_SUPPORTED_NODE_KINDS = [
82
+ "accountValueNode",
83
+ "argumentValueNode",
84
+ "resolverValueNode"
85
+ ];
86
+ function createConditionNodeValueVisitor(ctx) {
87
+ const { root, ixNode, argumentsInput, accountsInput, resolutionPath, resolversInput } = ctx;
88
+ return {
89
+ visitAccountValue: async (node) => {
90
+ const accountAddressInput = accountsInput?.[node.name];
91
+ if (accountAddressInput === null) {
92
+ return null;
93
+ }
94
+ return await resolveAccountValueNodeAddress(node, {
95
+ accountsInput,
96
+ argumentsInput,
97
+ ixNode,
98
+ resolutionPath,
99
+ resolversInput,
100
+ root
101
+ });
102
+ },
103
+ visitArgumentValue: async (node) => {
104
+ const argInput = argumentsInput?.[node.name];
105
+ return await Promise.resolve(argInput);
106
+ },
107
+ visitResolverValue: async (node) => {
108
+ const resolverFn = resolversInput?.[node.name];
109
+ if (!resolverFn) {
110
+ return void 0;
111
+ }
112
+ try {
113
+ return await resolverFn(argumentsInput ?? {}, accountsInput ?? {});
114
+ } catch (error) {
115
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__FAILED_TO_EXECUTE_RESOLVER, {
116
+ cause: error,
117
+ resolverName: node.name,
118
+ targetKind: "conditionalValueNode",
119
+ targetName: node.name
120
+ });
121
+ }
122
+ }
123
+ };
124
+ }
125
+ var VALUE_NODE_SUPPORTED_NODE_KINDS = [
126
+ "arrayValueNode",
127
+ "booleanValueNode",
128
+ "bytesValueNode",
129
+ "constantValueNode",
130
+ "enumValueNode",
131
+ "mapValueNode",
132
+ "noneValueNode",
133
+ "numberValueNode",
134
+ "publicKeyValueNode",
135
+ "setValueNode",
136
+ "someValueNode",
137
+ "stringValueNode",
138
+ "structValueNode",
139
+ "tupleValueNode"
140
+ ];
141
+ function createValueNodeVisitor() {
142
+ const visitor = {
143
+ visitArrayValue: (node) => ({
144
+ kind: node.kind,
145
+ value: node.items.map(
146
+ (item) => codama.visitOrElse(item, visitor, (n) => {
147
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
148
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
149
+ kind: n.kind,
150
+ node: n
151
+ });
152
+ })
153
+ )
154
+ }),
155
+ visitBooleanValue: (node) => ({
156
+ kind: node.kind,
157
+ value: node.boolean
158
+ }),
159
+ visitBytesValue: (node) => ({
160
+ encoding: node.encoding,
161
+ kind: node.kind,
162
+ value: node.data
163
+ }),
164
+ visitConstantValue: (node) => {
165
+ return codama.visitOrElse(node.value, visitor, (innerNode) => {
166
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
167
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
168
+ kind: innerNode.kind,
169
+ node: innerNode
170
+ });
171
+ });
172
+ },
173
+ visitEnumValue: (node) => ({
174
+ kind: node.kind,
175
+ value: node.variant
176
+ }),
177
+ visitMapValue: (node) => ({
178
+ kind: node.kind,
179
+ value: node.entries.map((entry) => ({
180
+ key: codama.visitOrElse(entry.key, visitor, (n) => {
181
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
182
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
183
+ kind: n.kind,
184
+ node: n
185
+ });
186
+ }),
187
+ value: codama.visitOrElse(entry.value, visitor, (n) => {
188
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
189
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
190
+ kind: n.kind,
191
+ node: n
192
+ });
193
+ })
194
+ }))
195
+ }),
196
+ visitNoneValue: (node) => ({
197
+ kind: node.kind,
198
+ value: null
199
+ }),
200
+ visitNumberValue: (node) => ({
201
+ kind: node.kind,
202
+ value: node.number
203
+ }),
204
+ visitPublicKeyValue: (node) => ({
205
+ kind: node.kind,
206
+ value: addresses.address(node.publicKey)
207
+ }),
208
+ visitSetValue: (node) => ({
209
+ kind: node.kind,
210
+ value: node.items.map(
211
+ (item) => codama.visitOrElse(item, visitor, (n) => {
212
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
213
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
214
+ kind: n.kind,
215
+ node: n
216
+ });
217
+ })
218
+ )
219
+ }),
220
+ visitSomeValue: (node) => {
221
+ return codama.visitOrElse(node.value, visitor, (innerNode) => {
222
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
223
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
224
+ kind: innerNode.kind,
225
+ node: innerNode
226
+ });
227
+ });
228
+ },
229
+ visitStringValue: (node) => ({
230
+ kind: node.kind,
231
+ value: node.string
232
+ }),
233
+ visitStructValue: (node) => ({
234
+ kind: node.kind,
235
+ value: Object.fromEntries(
236
+ node.fields.map((field) => [
237
+ field.name,
238
+ codama.visitOrElse(field.value, visitor, (n) => {
239
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
240
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
241
+ kind: n.kind,
242
+ node: n
243
+ });
244
+ })
245
+ ])
246
+ )
247
+ }),
248
+ visitTupleValue: (node) => ({
249
+ kind: node.kind,
250
+ value: node.items.map(
251
+ (item) => codama.visitOrElse(item, visitor, (n) => {
252
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
253
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
254
+ kind: n.kind,
255
+ node: n
256
+ });
257
+ })
258
+ )
259
+ })
260
+ };
261
+ return visitor;
262
+ }
263
+
264
+ // src/resolvers/resolve-conditional.ts
265
+ async function resolveConditionalValueNodeCondition({
266
+ root,
267
+ ixNode,
268
+ ixAccountNode,
269
+ conditionalValueNode,
270
+ argumentsInput,
271
+ accountsInput,
272
+ resolutionPath,
273
+ resolversInput
274
+ }) {
275
+ if (!codama.isNode(conditionalValueNode, "conditionalValueNode")) {
276
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
277
+ expectedKinds: ["conditionalValueNode"],
278
+ kind: getMaybeNodeKind(conditionalValueNode),
279
+ node: conditionalValueNode
280
+ });
281
+ }
282
+ const { condition, value: expectedValueNode, ifTrue, ifFalse } = conditionalValueNode;
283
+ if (!expectedValueNode && !ifTrue && !ifFalse) {
284
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__INVARIANT_VIOLATION, {
285
+ message: `Invalid conditionalValueNode: missing value and branches for account ${ixAccountNode.name} in ${ixNode.name}`
286
+ });
287
+ }
288
+ const conditionVisitor = createConditionNodeValueVisitor({
289
+ accountsInput,
290
+ argumentsInput,
291
+ ixNode,
292
+ resolutionPath,
293
+ resolversInput,
294
+ root
295
+ });
296
+ const actualProvidedValue = await codama.visitOrElse(condition, conditionVisitor, (condNode) => {
297
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
298
+ expectedKinds: [...CONDITION_NODE_SUPPORTED_NODE_KINDS],
299
+ kind: condNode.kind,
300
+ node: condNode
301
+ });
302
+ });
303
+ if (!expectedValueNode) {
304
+ return actualProvidedValue ? ifTrue : ifFalse;
305
+ }
306
+ const valueVisitor = createValueNodeVisitor();
307
+ const expectedValue = codama.visitOrElse(expectedValueNode, valueVisitor, (valueNode) => {
308
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
309
+ expectedKinds: [...VALUE_NODE_SUPPORTED_NODE_KINDS],
310
+ kind: valueNode.kind,
311
+ node: valueNode
312
+ });
313
+ });
314
+ if (typeof expectedValue.value === "object" || typeof actualProvidedValue === "object") {
315
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__INVARIANT_VIOLATION, {
316
+ message: "Deep equality comparison not yet supported for conditional value"
317
+ });
318
+ }
319
+ return actualProvidedValue === expectedValue.value ? ifTrue : ifFalse;
320
+ }
321
+ var addressEncoder;
322
+ function getMemoizedAddressEncoder() {
323
+ if (!addressEncoder) addressEncoder = addresses.getAddressEncoder();
324
+ return addressEncoder;
325
+ }
326
+ var utf8Encoder;
327
+ function getMemoizedUtf8Encoder() {
328
+ if (!utf8Encoder) utf8Encoder = codecs.getUtf8Encoder();
329
+ return utf8Encoder;
330
+ }
331
+ var booleanEncoder;
332
+ function getMemoizedBooleanEncoder() {
333
+ if (!booleanEncoder) booleanEncoder = codecs.getBooleanEncoder();
334
+ return booleanEncoder;
335
+ }
336
+ var utf8Codec;
337
+ function getMemoizedUtf8Codec() {
338
+ if (!utf8Codec) utf8Codec = codecs.getUtf8Codec();
339
+ return utf8Codec;
340
+ }
341
+ var base16Codec;
342
+ function getMemoizedBase16Codec() {
343
+ if (!base16Codec) base16Codec = codecs.getBase16Codec();
344
+ return base16Codec;
345
+ }
346
+ var base58Codec;
347
+ function getMemoizedBase58Codec() {
348
+ if (!base58Codec) base58Codec = codecs.getBase58Codec();
349
+ return base58Codec;
350
+ }
351
+ var base64Codec;
352
+ function getMemoizedBase64Codec() {
353
+ if (!base64Codec) base64Codec = codecs.getBase64Codec();
354
+ return base64Codec;
355
+ }
356
+
357
+ // src/shared/bytes-encoding.ts
358
+ function uint8ArrayToEncodedString(bytes, encoding) {
359
+ const codec = getCodecFromBytesEncoding(encoding);
360
+ return codec.decode(bytes);
361
+ }
362
+ function getCodecFromBytesEncoding(encoding) {
363
+ switch (encoding) {
364
+ case "base16":
365
+ return getMemoizedBase16Codec();
366
+ case "base58":
367
+ return getMemoizedBase58Codec();
368
+ case "base64":
369
+ return getMemoizedBase64Codec();
370
+ case "utf8":
371
+ return getMemoizedUtf8Codec();
372
+ default:
373
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNRECOGNIZED_BYTES_ENCODING, {
374
+ encoding: safeStringify(encoding)
375
+ });
376
+ }
377
+ }
378
+ function isUint8Array(value) {
379
+ return value instanceof Uint8Array;
380
+ }
381
+ var CODEC_INPUT_TRANSFORMER_SUPPORTED_NODE_KINDS = [
382
+ "amountTypeNode",
383
+ "arrayTypeNode",
384
+ "booleanTypeNode",
385
+ "bytesTypeNode",
386
+ "dateTimeTypeNode",
387
+ "definedTypeLinkNode",
388
+ "enumTypeNode",
389
+ "fixedSizeTypeNode",
390
+ "hiddenPrefixTypeNode",
391
+ "hiddenSuffixTypeNode",
392
+ "mapTypeNode",
393
+ "numberTypeNode",
394
+ "optionTypeNode",
395
+ "postOffsetTypeNode",
396
+ "preOffsetTypeNode",
397
+ "publicKeyTypeNode",
398
+ "remainderOptionTypeNode",
399
+ "sentinelTypeNode",
400
+ "setTypeNode",
401
+ "sizePrefixTypeNode",
402
+ "solAmountTypeNode",
403
+ "stringTypeNode",
404
+ "structFieldTypeNode",
405
+ "structTypeNode",
406
+ "tupleTypeNode",
407
+ "zeroableOptionTypeNode"
408
+ ];
409
+ function unexpectedNodeFallback(node) {
410
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
411
+ expectedKinds: [...CODEC_INPUT_TRANSFORMER_SUPPORTED_NODE_KINDS],
412
+ kind: node.kind,
413
+ node
414
+ });
415
+ }
416
+ function createCodecInputTransformerVisitor(root, options = {}) {
417
+ const bytesEncoding = options.bytesEncoding ?? "base16";
418
+ const visitor = {
419
+ visitAmountType(node) {
420
+ return codama.visitOrElse(node.number, visitor, unexpectedNodeFallback);
421
+ },
422
+ visitArrayType(node) {
423
+ const itemTransform = codama.visitOrElse(node.item, visitor, unexpectedNodeFallback);
424
+ return (input) => {
425
+ if (!Array.isArray(input)) {
426
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
427
+ actualType: formatValueType(input),
428
+ expectedType: "array",
429
+ nodeKind: "arrayTypeNode"
430
+ });
431
+ }
432
+ return input.map(itemTransform);
433
+ };
434
+ },
435
+ visitBooleanType() {
436
+ return (input) => input;
437
+ },
438
+ visitBytesType() {
439
+ return (input) => {
440
+ if (isUint8Array(input)) {
441
+ return [bytesEncoding, uint8ArrayToEncodedString(input, bytesEncoding)];
442
+ }
443
+ if (Array.isArray(input) && input.every((item) => typeof item === "number")) {
444
+ return [bytesEncoding, uint8ArrayToEncodedString(new Uint8Array(input), bytesEncoding)];
445
+ }
446
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
447
+ actualType: formatValueType(input),
448
+ expectedType: "Uint8Array | number[]",
449
+ nodeKind: "bytesTypeNode"
450
+ });
451
+ };
452
+ },
453
+ visitDateTimeType(node) {
454
+ return codama.visitOrElse(node.number, visitor, unexpectedNodeFallback);
455
+ },
456
+ visitDefinedTypeLink(node) {
457
+ const definedType = root.program.definedTypes.find((dt) => dt.name === node.name);
458
+ if (!definedType) {
459
+ throw new errors.CodamaError(errors.CODAMA_ERROR__LINKED_NODE_NOT_FOUND, {
460
+ kind: "definedTypeLinkNode",
461
+ linkNode: node,
462
+ name: node.name,
463
+ path: []
464
+ });
465
+ }
466
+ return codama.visitOrElse(definedType.type, visitor, unexpectedNodeFallback);
467
+ },
468
+ visitEnumType(node) {
469
+ return (input) => {
470
+ if (typeof input === "number" || typeof input === "string") {
471
+ return input;
472
+ }
473
+ if (!isObjectRecord(input)) {
474
+ return input;
475
+ }
476
+ if (!("__kind" in input)) {
477
+ return input;
478
+ }
479
+ const { __kind, ...rest } = input;
480
+ const kindObj = { __kind: codama.pascalCase(String(__kind)) };
481
+ const variantNode = node.variants.find((v) => v.name === __kind);
482
+ if (!variantNode) {
483
+ const availableVariants = node.variants.map((v) => v.name).join(", ");
484
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
485
+ actualType: `variant '${String(__kind)}'`,
486
+ expectedType: `one of [${availableVariants}]`,
487
+ nodeKind: "enumTypeNode"
488
+ });
489
+ }
490
+ if (codama.isNode(variantNode, "enumEmptyVariantTypeNode")) {
491
+ return { ...input, ...kindObj };
492
+ }
493
+ if (codama.isNode(variantNode, "enumStructVariantTypeNode")) {
494
+ const structTransform = codama.visitOrElse(variantNode.struct, visitor, unexpectedNodeFallback);
495
+ const transformedFields = structTransform(rest);
496
+ if (!isObjectRecord(transformedFields)) {
497
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
498
+ actualType: formatValueType(transformedFields),
499
+ expectedType: "object",
500
+ nodeKind: "enumStructVariantTypeNode"
501
+ });
502
+ }
503
+ return { ...kindObj, ...transformedFields };
504
+ }
505
+ if (codama.isNode(variantNode, "enumTupleVariantTypeNode")) {
506
+ const tupleTransform = codama.visitOrElse(variantNode.tuple, visitor, unexpectedNodeFallback);
507
+ if (!("fields" in rest) || !Array.isArray(rest.fields)) {
508
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
509
+ actualType: formatValueType(rest.fields ?? rest),
510
+ expectedType: "array (fields)",
511
+ nodeKind: "enumTupleVariantTypeNode"
512
+ });
513
+ }
514
+ return { ...kindObj, fields: tupleTransform(rest.fields) };
515
+ }
516
+ return input;
517
+ };
518
+ },
519
+ visitFixedSizeType(node) {
520
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
521
+ },
522
+ visitHiddenPrefixType(node) {
523
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
524
+ },
525
+ visitHiddenSuffixType(node) {
526
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
527
+ },
528
+ visitMapType(node) {
529
+ const valueTransform = codama.visitOrElse(node.value, visitor, unexpectedNodeFallback);
530
+ return (input) => {
531
+ if (!isObjectRecord(input)) {
532
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
533
+ actualType: formatValueType(input),
534
+ expectedType: "object",
535
+ nodeKind: "mapTypeNode"
536
+ });
537
+ }
538
+ const result = {};
539
+ for (const [key, value] of Object.entries(input)) {
540
+ result[key] = valueTransform(value);
541
+ }
542
+ return result;
543
+ };
544
+ },
545
+ // Primitive types (pass through)
546
+ visitNumberType() {
547
+ return (input) => input;
548
+ },
549
+ visitOptionType(node) {
550
+ const innerTransform = codama.visitOrElse(node.item, visitor, unexpectedNodeFallback);
551
+ return (input) => {
552
+ if (input === null || input === void 0) return input;
553
+ return innerTransform(input);
554
+ };
555
+ },
556
+ visitPostOffsetType(node) {
557
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
558
+ },
559
+ visitPreOffsetType(node) {
560
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
561
+ },
562
+ visitPublicKeyType() {
563
+ return (input) => input;
564
+ },
565
+ visitRemainderOptionType(node) {
566
+ const innerTransform = codama.visitOrElse(node.item, visitor, unexpectedNodeFallback);
567
+ return (input) => {
568
+ if (input === null || input === void 0) return input;
569
+ return innerTransform(input);
570
+ };
571
+ },
572
+ visitSentinelType(node) {
573
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
574
+ },
575
+ visitSetType(node) {
576
+ const itemTransform = codama.visitOrElse(node.item, visitor, unexpectedNodeFallback);
577
+ return (input) => {
578
+ if (!Array.isArray(input)) {
579
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
580
+ actualType: formatValueType(input),
581
+ expectedType: "array",
582
+ nodeKind: "setTypeNode"
583
+ });
584
+ }
585
+ return input.map(itemTransform);
586
+ };
587
+ },
588
+ visitSizePrefixType(node) {
589
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
590
+ },
591
+ visitSolAmountType(node) {
592
+ return codama.visitOrElse(node.number, visitor, unexpectedNodeFallback);
593
+ },
594
+ visitStringType() {
595
+ return (input) => input;
596
+ },
597
+ visitStructFieldType(node) {
598
+ return codama.visitOrElse(node.type, visitor, unexpectedNodeFallback);
599
+ },
600
+ visitStructType(node) {
601
+ const fieldTransformers = node.fields.map((field) => {
602
+ const transform = codama.visitOrElse(field, visitor, unexpectedNodeFallback);
603
+ return { name: field.name, transform };
604
+ });
605
+ return (input) => {
606
+ if (!isObjectRecord(input)) {
607
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
608
+ actualType: formatValueType(input),
609
+ expectedType: "object",
610
+ nodeKind: "structTypeNode"
611
+ });
612
+ }
613
+ const result = { ...input };
614
+ for (const { name, transform } of fieldTransformers) {
615
+ if (name in result) {
616
+ result[name] = transform(result[name]);
617
+ }
618
+ }
619
+ return result;
620
+ };
621
+ },
622
+ visitTupleType(node) {
623
+ const itemTransforms = node.items.map((item) => codama.visitOrElse(item, visitor, unexpectedNodeFallback));
624
+ return (input) => {
625
+ if (!Array.isArray(input)) {
626
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
627
+ actualType: formatValueType(input),
628
+ expectedType: "array",
629
+ nodeKind: "tupleTypeNode"
630
+ });
631
+ }
632
+ if (input.length !== itemTransforms.length) {
633
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
634
+ actualType: `array(length:${input.length})`,
635
+ expectedType: `array(length:${itemTransforms.length})`,
636
+ nodeKind: "tupleTypeNode"
637
+ });
638
+ }
639
+ return input.map((value, index) => itemTransforms[index](value));
640
+ };
641
+ },
642
+ visitZeroableOptionType(node) {
643
+ const innerTransform = codama.visitOrElse(node.item, visitor, unexpectedNodeFallback);
644
+ return (input) => {
645
+ if (input === null || input === void 0) return input;
646
+ return innerTransform(input);
647
+ };
648
+ }
649
+ };
650
+ return visitor;
651
+ }
652
+ function createCodecInputTransformer(typeNode, root, options) {
653
+ const visitor = createCodecInputTransformerVisitor(root, options);
654
+ return codama.visitOrElse(typeNode, visitor, unexpectedNodeFallback);
655
+ }
656
+
657
+ // src/visitors/pda-seed-value.ts
658
+ var PDA_SEED_VALUE_SUPPORTED_NODE_KINDS = [
659
+ "accountValueNode",
660
+ "argumentValueNode",
661
+ "booleanValueNode",
662
+ "bytesValueNode",
663
+ "constantValueNode",
664
+ "noneValueNode",
665
+ "numberValueNode",
666
+ "programIdValueNode",
667
+ "publicKeyValueNode",
668
+ "someValueNode",
669
+ "stringValueNode"
670
+ ];
671
+ function createPdaSeedValueVisitor(ctx) {
672
+ const { root, ixNode, programId, seedTypeNode, resolversInput, resolutionPath } = ctx;
673
+ const accountsInput = ctx.accountsInput ?? {};
674
+ const argumentsInput = ctx.argumentsInput ?? {};
675
+ return {
676
+ visitAccountValue: async (node) => {
677
+ const resolvedAddress = await resolveAccountValueNodeAddress(node, {
678
+ accountsInput,
679
+ argumentsInput,
680
+ ixNode,
681
+ resolutionPath,
682
+ resolversInput,
683
+ root
684
+ });
685
+ if (resolvedAddress === null) {
686
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__FAILED_TO_DERIVE_PDA, {
687
+ accountName: node.name
688
+ });
689
+ }
690
+ return getMemoizedAddressEncoder().encode(resolvedAddress);
691
+ },
692
+ visitArgumentValue: async (node) => {
693
+ const ixArgumentNode = ixNode.arguments.find((arg) => arg.name === node.name);
694
+ if (!ixArgumentNode) {
695
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__NODE_REFERENCE_NOT_FOUND, {
696
+ instructionName: ixNode.name,
697
+ referencedName: node.name
698
+ });
699
+ }
700
+ const argInput = argumentsInput[node.name];
701
+ const typeNode = seedTypeNode ?? ixArgumentNode.type;
702
+ if (argInput === void 0 || argInput === null) {
703
+ if (codama.isNode(typeNode, "remainderOptionTypeNode")) {
704
+ return new Uint8Array(0);
705
+ }
706
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ARGUMENT_MISSING, {
707
+ argumentName: node.name,
708
+ instructionName: ixNode.name
709
+ });
710
+ }
711
+ const codec = dynamicCodecs.getNodeCodec([root, root.program, ixNode, { ...ixArgumentNode, type: typeNode }]);
712
+ const transformer = createCodecInputTransformer(typeNode, root, {
713
+ bytesEncoding: "base16"
714
+ });
715
+ const transformedInput = transformer(argInput);
716
+ return await Promise.resolve(codec.encode(transformedInput));
717
+ },
718
+ visitBooleanValue: async (node) => await Promise.resolve(getMemoizedBooleanEncoder().encode(node.boolean)),
719
+ visitBytesValue: async (node) => {
720
+ const encodedValue = getCodecFromBytesEncoding(node.encoding).encode(node.data);
721
+ return await Promise.resolve(encodedValue);
722
+ },
723
+ visitConstantValue: async (node) => {
724
+ const innerVisitor = createPdaSeedValueVisitor(ctx);
725
+ return await codama.visitOrElse(node.value, innerVisitor, (innerNode) => {
726
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
727
+ expectedKinds: [...PDA_SEED_VALUE_SUPPORTED_NODE_KINDS],
728
+ kind: innerNode.kind,
729
+ node: innerNode
730
+ });
731
+ });
732
+ },
733
+ visitNoneValue: async (_node) => await Promise.resolve(new Uint8Array(0)),
734
+ visitNumberValue: async (node) => {
735
+ if (!Number.isInteger(node.number) || node.number < 0 || node.number > 255) {
736
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__INVARIANT_VIOLATION, {
737
+ message: `NumberValueNode PDA seed is out of range: must be a valid u8 (0\u2013255), got ${node.number}`
738
+ });
739
+ }
740
+ return await Promise.resolve(new Uint8Array([node.number]));
741
+ },
742
+ visitProgramIdValue: async (_node) => {
743
+ return await Promise.resolve(getMemoizedAddressEncoder().encode(toAddress(programId)));
744
+ },
745
+ visitPublicKeyValue: async (node) => {
746
+ return await Promise.resolve(getMemoizedAddressEncoder().encode(toAddress(node.publicKey)));
747
+ },
748
+ visitSomeValue: async (node) => {
749
+ const innerVisitor = createPdaSeedValueVisitor(ctx);
750
+ return await codama.visitOrElse(node.value, innerVisitor, (innerNode) => {
751
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
752
+ expectedKinds: [...PDA_SEED_VALUE_SUPPORTED_NODE_KINDS],
753
+ kind: innerNode.kind,
754
+ node: innerNode
755
+ });
756
+ });
757
+ },
758
+ visitStringValue: async (node) => await Promise.resolve(getMemoizedUtf8Codec().encode(node.string))
759
+ };
760
+ }
761
+
762
+ // src/resolvers/resolve-pda-address.ts
763
+ async function resolvePDAAddress({
764
+ root,
765
+ ixNode,
766
+ argumentsInput,
767
+ accountsInput,
768
+ pdaValueNode,
769
+ resolutionPath,
770
+ resolversInput
771
+ }) {
772
+ if (!codama.isNode(pdaValueNode, "pdaValueNode")) {
773
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
774
+ expectedKinds: ["pdaValueNode"],
775
+ kind: getMaybeNodeKind(pdaValueNode),
776
+ node: pdaValueNode
777
+ });
778
+ }
779
+ const pdaNode = resolvePdaNode(pdaValueNode, root.program.pdas);
780
+ const programId = addresses.address(pdaNode.programId || root.program.publicKey);
781
+ const seedValues = await Promise.all(
782
+ pdaNode.seeds.map(async (seedNode) => {
783
+ if (seedNode.kind === "constantPdaSeedNode") {
784
+ return await resolveConstantPdaSeed({
785
+ accountsInput,
786
+ argumentsInput,
787
+ ixNode,
788
+ programId,
789
+ resolutionPath,
790
+ resolversInput,
791
+ root,
792
+ seedNode
793
+ });
794
+ }
795
+ if (seedNode.kind === "variablePdaSeedNode") {
796
+ const variableSeedValueNodes = pdaValueNode.seeds;
797
+ const seedName = seedNode.name;
798
+ const variableSeedValueNode = variableSeedValueNodes.find((node) => node.name === seedName);
799
+ if (!variableSeedValueNode) {
800
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__NODE_REFERENCE_NOT_FOUND, {
801
+ instructionName: ixNode.name,
802
+ referencedName: seedName
803
+ });
804
+ }
805
+ return await resolveVariablePdaSeed({
806
+ accountsInput,
807
+ argumentsInput,
808
+ ixNode,
809
+ programId,
810
+ resolutionPath,
811
+ resolversInput,
812
+ root,
813
+ seedNode,
814
+ variableSeedValueNode
815
+ });
816
+ }
817
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNRECOGNIZED_NODE_KIND, {
818
+ kind: getMaybeNodeKind(seedNode) ?? "unknown"
819
+ });
820
+ })
821
+ );
822
+ return await addresses.getProgramDerivedAddress({
823
+ programAddress: programId,
824
+ seeds: seedValues
825
+ });
826
+ }
827
+ function resolvePdaNode(pdaDefaultValue, pdas) {
828
+ if (codama.isNode(pdaDefaultValue.pda, "pdaLinkNode")) {
829
+ const linkedPda = pdas.find((p) => p.name === pdaDefaultValue.pda.name);
830
+ if (!linkedPda) {
831
+ throw new errors.CodamaError(errors.CODAMA_ERROR__LINKED_NODE_NOT_FOUND, {
832
+ kind: "pdaLinkNode",
833
+ linkNode: pdaDefaultValue.pda,
834
+ name: pdaDefaultValue.pda.name,
835
+ path: []
836
+ });
837
+ }
838
+ return linkedPda;
839
+ }
840
+ if (codama.isNode(pdaDefaultValue.pda, "pdaNode")) {
841
+ return pdaDefaultValue.pda;
842
+ }
843
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
844
+ expectedKinds: ["pdaLinkNode", "pdaNode"],
845
+ kind: getMaybeNodeKind(pdaDefaultValue.pda),
846
+ node: pdaDefaultValue.pda
847
+ });
848
+ }
849
+ function resolveVariablePdaSeed({
850
+ accountsInput,
851
+ argumentsInput,
852
+ ixNode,
853
+ programId,
854
+ resolutionPath,
855
+ resolversInput,
856
+ root,
857
+ seedNode,
858
+ variableSeedValueNode
859
+ }) {
860
+ if (!codama.isNode(variableSeedValueNode, "pdaSeedValueNode")) {
861
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
862
+ expectedKinds: ["pdaSeedValueNode"],
863
+ kind: getMaybeNodeKind(variableSeedValueNode),
864
+ node: variableSeedValueNode
865
+ });
866
+ }
867
+ if (seedNode.name !== variableSeedValueNode.name) {
868
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__INVARIANT_VIOLATION, {
869
+ message: `Mismatched PDA seed names: expected [${seedNode.name}], got [${variableSeedValueNode.name}]`
870
+ });
871
+ }
872
+ const visitor = createPdaSeedValueVisitor({
873
+ accountsInput,
874
+ argumentsInput,
875
+ ixNode,
876
+ programId,
877
+ resolutionPath,
878
+ resolversInput,
879
+ root,
880
+ seedTypeNode: seedNode.type
881
+ });
882
+ return codama.visitOrElse(variableSeedValueNode.value, visitor, (node) => {
883
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
884
+ expectedKinds: [...PDA_SEED_VALUE_SUPPORTED_NODE_KINDS],
885
+ kind: node.kind,
886
+ node
887
+ });
888
+ });
889
+ }
890
+ function resolveConstantPdaSeed({
891
+ accountsInput,
892
+ argumentsInput,
893
+ ixNode,
894
+ programId,
895
+ resolutionPath,
896
+ resolversInput,
897
+ root,
898
+ seedNode
899
+ }) {
900
+ if (!codama.isNode(seedNode, "constantPdaSeedNode")) {
901
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
902
+ expectedKinds: ["constantPdaSeedNode"],
903
+ kind: seedNode.kind,
904
+ node: seedNode
905
+ });
906
+ }
907
+ const visitor = createPdaSeedValueVisitor({
908
+ accountsInput,
909
+ argumentsInput,
910
+ ixNode,
911
+ programId,
912
+ resolutionPath,
913
+ resolversInput,
914
+ root,
915
+ seedTypeNode: seedNode.type
916
+ });
917
+ return codama.visitOrElse(seedNode.value, visitor, (node) => {
918
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
919
+ expectedKinds: [...PDA_SEED_VALUE_SUPPORTED_NODE_KINDS],
920
+ kind: node.kind,
921
+ node
922
+ });
923
+ });
924
+ }
925
+
926
+ // src/visitors/account-default-value.ts
927
+ var ACCOUNT_DEFAULT_VALUE_SUPPORTED_NODE_KINDS = [
928
+ "accountBumpValueNode",
929
+ "accountValueNode",
930
+ "argumentValueNode",
931
+ "conditionalValueNode",
932
+ "identityValueNode",
933
+ "payerValueNode",
934
+ "pdaValueNode",
935
+ "programIdValueNode",
936
+ "publicKeyValueNode",
937
+ "resolverValueNode"
938
+ ];
939
+ function createAccountDefaultValueVisitor(ctx) {
940
+ const { root, ixNode, ixAccountNode, argumentsInput, accountsInput, resolversInput, resolutionPath } = ctx;
941
+ const accountAddressInput = accountsInput?.[ixAccountNode.name];
942
+ return {
943
+ visitAccountBumpValue: async (_node) => {
944
+ return await Promise.reject(
945
+ new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNSUPPORTED_NODE, {
946
+ nodeKind: "accountBumpValueNode"
947
+ })
948
+ );
949
+ },
950
+ visitAccountValue: async (node) => {
951
+ return await resolveAccountValueNodeAddress(node, {
952
+ accountsInput,
953
+ argumentsInput,
954
+ ixNode,
955
+ resolutionPath,
956
+ resolversInput,
957
+ root
958
+ });
959
+ },
960
+ visitArgumentValue: async (node) => {
961
+ const argValue = argumentsInput?.[node.name];
962
+ if (argValue === void 0 || argValue === null) {
963
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ARGUMENT_MISSING, {
964
+ argumentName: node.name,
965
+ instructionName: ixNode.name
966
+ });
967
+ }
968
+ if (!isAddressConvertible(argValue)) {
969
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ADDRESS_TYPE, {
970
+ accountName: ixAccountNode.name,
971
+ actualType: formatValueType(argValue),
972
+ expectedType: "Address | PublicKey"
973
+ });
974
+ }
975
+ return await Promise.resolve(toAddress(argValue));
976
+ },
977
+ visitConditionalValue: async (conditionalValueNode) => {
978
+ const resolvedInputValueNode = await resolveConditionalValueNodeCondition({
979
+ accountsInput,
980
+ argumentsInput,
981
+ conditionalValueNode,
982
+ ixAccountNode,
983
+ ixNode,
984
+ resolutionPath,
985
+ resolversInput,
986
+ root
987
+ });
988
+ if (resolvedInputValueNode === void 0) {
989
+ if (ixAccountNode.isOptional) {
990
+ return null;
991
+ }
992
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ACCOUNT_MISSING, {
993
+ accountName: ixAccountNode.name,
994
+ instructionName: ixNode.name
995
+ });
996
+ }
997
+ const visitor = createAccountDefaultValueVisitor(ctx);
998
+ const addressValue = await codama.visitOrElse(resolvedInputValueNode, visitor, (innerNode) => {
999
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
1000
+ expectedKinds: [...ACCOUNT_DEFAULT_VALUE_SUPPORTED_NODE_KINDS],
1001
+ kind: innerNode.kind,
1002
+ node: innerNode
1003
+ });
1004
+ });
1005
+ return addressValue;
1006
+ },
1007
+ visitIdentityValue: async (_node) => {
1008
+ if (accountAddressInput === void 0 || accountAddressInput === null) {
1009
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ACCOUNT_MISSING, {
1010
+ accountName: ixAccountNode.name,
1011
+ instructionName: ixNode.name
1012
+ });
1013
+ }
1014
+ return await Promise.resolve(toAddress(accountAddressInput));
1015
+ },
1016
+ visitPayerValue: async (_node) => {
1017
+ if (accountAddressInput === void 0 || accountAddressInput === null) {
1018
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ACCOUNT_MISSING, {
1019
+ accountName: ixAccountNode.name,
1020
+ instructionName: ixNode.name
1021
+ });
1022
+ }
1023
+ return await Promise.resolve(toAddress(accountAddressInput));
1024
+ },
1025
+ visitPdaValue: async (node) => {
1026
+ const pda = await resolvePDAAddress({
1027
+ accountsInput,
1028
+ argumentsInput,
1029
+ ixNode,
1030
+ pdaValueNode: node,
1031
+ resolutionPath,
1032
+ resolversInput,
1033
+ root
1034
+ });
1035
+ if (pda === null) {
1036
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__FAILED_TO_DERIVE_PDA, {
1037
+ accountName: ixAccountNode.name
1038
+ });
1039
+ }
1040
+ return pda[0];
1041
+ },
1042
+ visitProgramIdValue: async (_node) => {
1043
+ return await Promise.resolve(addresses.address(root.program.publicKey));
1044
+ },
1045
+ visitPublicKeyValue: async (node) => {
1046
+ return await Promise.resolve(addresses.address(node.publicKey));
1047
+ },
1048
+ visitResolverValue: async (node) => {
1049
+ const resolverFn = resolversInput?.[node.name];
1050
+ if (!resolverFn) {
1051
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ACCOUNT_RESOLVER_MISSING, {
1052
+ accountName: ixAccountNode.name,
1053
+ resolverName: node.name
1054
+ });
1055
+ }
1056
+ let result;
1057
+ try {
1058
+ result = await resolverFn(argumentsInput ?? {}, accountsInput ?? {});
1059
+ } catch (error) {
1060
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__FAILED_TO_EXECUTE_RESOLVER, {
1061
+ cause: error,
1062
+ resolverName: node.name,
1063
+ targetKind: "instructionAccountNode",
1064
+ targetName: ixAccountNode.name
1065
+ });
1066
+ }
1067
+ if (!isAddressConvertible(result)) {
1068
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__INVALID_ACCOUNT_ADDRESS, {
1069
+ accountName: ixAccountNode.name,
1070
+ value: safeStringify(result)
1071
+ });
1072
+ }
1073
+ return toAddress(result);
1074
+ }
1075
+ };
1076
+ }
1077
+
1078
+ // src/resolvers/resolve-account-address.ts
1079
+ async function resolveAccountAddress({
1080
+ root,
1081
+ ixNode,
1082
+ ixAccountNode,
1083
+ argumentsInput,
1084
+ accountsInput,
1085
+ resolutionPath,
1086
+ resolversInput
1087
+ }) {
1088
+ const accountAddressInput = accountsInput?.[ixAccountNode.name];
1089
+ if (accountAddressInput === null && ixAccountNode.isOptional) {
1090
+ return resolveOptionalAccountWithStrategy(root, ixNode, ixAccountNode);
1091
+ }
1092
+ if (ixAccountNode.defaultValue) {
1093
+ const visitor = createAccountDefaultValueVisitor({
1094
+ accountsInput,
1095
+ argumentsInput,
1096
+ ixAccountNode,
1097
+ ixNode,
1098
+ resolutionPath,
1099
+ resolversInput,
1100
+ root
1101
+ });
1102
+ const addressValue = await codama.visitOrElse(ixAccountNode.defaultValue, visitor, (node) => {
1103
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
1104
+ expectedKinds: [...ACCOUNT_DEFAULT_VALUE_SUPPORTED_NODE_KINDS],
1105
+ kind: node.kind,
1106
+ node
1107
+ });
1108
+ });
1109
+ if (addressValue === null && ixAccountNode.isOptional) {
1110
+ return resolveOptionalAccountWithStrategy(root, ixNode, ixAccountNode);
1111
+ }
1112
+ return addressValue;
1113
+ }
1114
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ACCOUNT_MISSING, {
1115
+ accountName: ixAccountNode.name,
1116
+ instructionName: ixNode.name
1117
+ });
1118
+ }
1119
+ function resolveOptionalAccountWithStrategy(root, ixNode, ixAccountNode) {
1120
+ if (!ixAccountNode.isOptional) {
1121
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__INVARIANT_VIOLATION, {
1122
+ message: `resolveOptionalAccountWithStrategy called for non-optional account: ${ixAccountNode.name}`
1123
+ });
1124
+ }
1125
+ switch (ixNode.optionalAccountStrategy) {
1126
+ case "omitted":
1127
+ return null;
1128
+ case "programId":
1129
+ return toAddress(root.program.publicKey);
1130
+ default:
1131
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNSUPPORTED_OPTIONAL_ACCOUNT_STRATEGY, {
1132
+ accountName: ixAccountNode.name,
1133
+ instructionName: ixNode.name,
1134
+ strategy: safeStringify(ixNode.optionalAccountStrategy)
1135
+ });
1136
+ }
1137
+ }
1138
+ async function resolveInstructionAccountAddress({
1139
+ accountsInput,
1140
+ argumentsInput,
1141
+ ixAccountNode,
1142
+ ixNode,
1143
+ resolversInput,
1144
+ root
1145
+ }) {
1146
+ const accountAddressInput = accountsInput?.[ixAccountNode.name];
1147
+ const isAccountProvided = accountAddressInput !== void 0 && accountAddressInput !== null;
1148
+ const canAutoResolve = !!ixAccountNode.defaultValue || ixAccountNode.isOptional && accountAddressInput === null;
1149
+ if (!isAccountProvided && !canAutoResolve) {
1150
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ACCOUNT_MISSING, {
1151
+ accountName: ixAccountNode.name,
1152
+ instructionName: ixNode.name
1153
+ });
1154
+ }
1155
+ if (isAccountProvided) {
1156
+ return toAddress(accountAddressInput);
1157
+ }
1158
+ return await resolveAccountAddress({
1159
+ accountsInput,
1160
+ argumentsInput,
1161
+ ixAccountNode,
1162
+ ixNode,
1163
+ resolutionPath: [],
1164
+ resolversInput,
1165
+ root
1166
+ });
1167
+ }
1168
+ var STANDALONE_IX_NODE = {
1169
+ accounts: [],
1170
+ arguments: [],
1171
+ kind: "instructionNode",
1172
+ name: "__standalone__"
1173
+ };
1174
+ async function resolveStandalonePda(root, pdaNode, seedInputs = {}) {
1175
+ const programAddress = toAddress(pdaNode.programId || root.program.publicKey);
1176
+ const seedValues = await Promise.all(
1177
+ pdaNode.seeds.map(async (seedNode) => {
1178
+ if (seedNode.kind === "constantPdaSeedNode") {
1179
+ return await resolveStandaloneConstantSeed(root, programAddress, seedNode);
1180
+ }
1181
+ if (seedNode.kind === "variablePdaSeedNode") {
1182
+ return await resolveStandaloneVariableSeed(root, seedNode, seedInputs);
1183
+ }
1184
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNRECOGNIZED_NODE_KIND, {
1185
+ kind: getMaybeNodeKind(seedNode) ?? "unknown"
1186
+ });
1187
+ })
1188
+ );
1189
+ return await addresses.getProgramDerivedAddress({ programAddress, seeds: seedValues });
1190
+ }
1191
+ function resolveStandaloneConstantSeed(root, programAddress, seedNode) {
1192
+ if (!codama.isNode(seedNode, "constantPdaSeedNode")) {
1193
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
1194
+ expectedKinds: ["constantPdaSeedNode"],
1195
+ kind: seedNode.kind,
1196
+ node: seedNode
1197
+ });
1198
+ }
1199
+ const visitor = createPdaSeedValueVisitor({
1200
+ accountsInput: void 0,
1201
+ argumentsInput: void 0,
1202
+ ixNode: STANDALONE_IX_NODE,
1203
+ programId: programAddress,
1204
+ resolutionPath: [],
1205
+ resolversInput: void 0,
1206
+ root
1207
+ });
1208
+ return codama.visitOrElse(seedNode.value, visitor, (node) => {
1209
+ throw new errors.CodamaError(errors.CODAMA_ERROR__UNEXPECTED_NODE_KIND, {
1210
+ expectedKinds: Array.from(PDA_SEED_VALUE_SUPPORTED_NODE_KINDS),
1211
+ kind: node.kind,
1212
+ node
1213
+ });
1214
+ });
1215
+ }
1216
+ function resolveStandaloneVariableSeed(root, seedNode, seedInputs) {
1217
+ const input = seedInputs[seedNode.name];
1218
+ const typeNode = seedNode.type;
1219
+ if (input === void 0 || input === null) {
1220
+ if (codama.isNode(typeNode, "remainderOptionTypeNode")) {
1221
+ return Promise.resolve(new Uint8Array(0));
1222
+ }
1223
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__ARGUMENT_MISSING, {
1224
+ argumentName: seedNode.name,
1225
+ instructionName: codama.camelCase("standaloneSeedNode")
1226
+ });
1227
+ }
1228
+ if (codama.isNode(typeNode, "stringTypeNode")) {
1229
+ if (typeof input !== "string") {
1230
+ throw new errors.CodamaError(errors.CODAMA_ERROR__DYNAMIC_CLIENT__UNEXPECTED_ARGUMENT_TYPE, {
1231
+ actualType: formatValueType(input),
1232
+ expectedType: "string",
1233
+ nodeKind: "stringTypeNode"
1234
+ });
1235
+ }
1236
+ return Promise.resolve(getMemoizedUtf8Encoder().encode(input));
1237
+ }
1238
+ const syntheticArgNode = createSyntheticArgNode(seedNode);
1239
+ const codec = dynamicCodecs.getNodeCodec([root, root.program, syntheticArgNode]);
1240
+ const transformer = createCodecInputTransformer(typeNode, root, { bytesEncoding: "base16" });
1241
+ const transformedInput = transformer(input);
1242
+ return Promise.resolve(codec.encode(transformedInput));
1243
+ }
1244
+ function createSyntheticArgNode(seedNode) {
1245
+ return {
1246
+ docs: [],
1247
+ kind: "instructionArgumentNode",
1248
+ name: seedNode.name,
1249
+ type: seedNode.type
1250
+ };
1251
+ }
1252
+
1253
+ // src/visitors/default-value-encoder.ts
1254
+ var DEFAULT_VALUE_ENCODER_SUPPORTED_NODE_KINDS = [
1255
+ "booleanValueNode",
1256
+ "bytesValueNode",
1257
+ "enumValueNode",
1258
+ "noneValueNode",
1259
+ "numberValueNode",
1260
+ "publicKeyValueNode",
1261
+ "stringValueNode"
1262
+ ];
1263
+ function createDefaultValueEncoderVisitor(codec) {
1264
+ return {
1265
+ visitBooleanValue: (node) => codec.encode(node.boolean),
1266
+ visitBytesValue: (node) => codec.encode([node.encoding, node.data]),
1267
+ visitEnumValue: (node) => codec.encode(node.variant),
1268
+ visitNoneValue: () => codec.encode(null),
1269
+ visitNumberValue: (node) => codec.encode(node.number),
1270
+ visitPublicKeyValue: (node) => codec.encode(node.publicKey),
1271
+ visitStringValue: (node) => codec.encode(node.string)
1272
+ };
1273
+ }
1274
+
1275
+ // src/shared/nodes.ts
1276
+ var OPTIONAL_NODE_KINDS = ["optionTypeNode", "zeroableOptionTypeNode", "remainderOptionTypeNode"];
1277
+
1278
+ exports.DEFAULT_VALUE_ENCODER_SUPPORTED_NODE_KINDS = DEFAULT_VALUE_ENCODER_SUPPORTED_NODE_KINDS;
1279
+ exports.OPTIONAL_NODE_KINDS = OPTIONAL_NODE_KINDS;
1280
+ exports.createCodecInputTransformer = createCodecInputTransformer;
1281
+ exports.createDefaultValueEncoderVisitor = createDefaultValueEncoderVisitor;
1282
+ exports.isAddressConvertible = isAddressConvertible;
1283
+ exports.isPublicKeyLike = isPublicKeyLike;
1284
+ exports.resolveInstructionAccountAddress = resolveInstructionAccountAddress;
1285
+ exports.resolveStandalonePda = resolveStandalonePda;
1286
+ exports.toAddress = toAddress;
1287
+ //# sourceMappingURL=index.node.cjs.map
1288
+ //# sourceMappingURL=index.node.cjs.map