|
1 | 1 | import type { |
2 | | - DiscriminatorNode, |
3 | | - InstructionAccountNode, |
4 | 2 | InstructionArgumentNode, |
5 | | - InstructionByteDeltaNode, |
6 | 3 | InstructionNode, |
7 | | - InstructionRemainingAccountsNode, |
8 | 4 | OptionalAccountStrategy, |
9 | 5 | ProgramNode, |
10 | 6 | RootNode, |
11 | 7 | } from '@codama/node-types'; |
12 | 8 |
|
13 | 9 | import { isNode } from './Node'; |
14 | 10 | import { getAllInstructions } from './ProgramNode'; |
15 | | -import { camelCase, DocsInput, parseDocs } from './shared'; |
16 | | - |
17 | | -type SubInstructionNode = InstructionNode; |
18 | | - |
19 | | -export type InstructionNodeInput< |
20 | | - TAccounts extends InstructionAccountNode[] = InstructionAccountNode[], |
21 | | - TArguments extends InstructionArgumentNode[] = InstructionArgumentNode[], |
22 | | - TExtraArguments extends InstructionArgumentNode[] | undefined = InstructionArgumentNode[] | undefined, |
23 | | - TRemainingAccounts extends InstructionRemainingAccountsNode[] | undefined = |
24 | | - | InstructionRemainingAccountsNode[] |
25 | | - | undefined, |
26 | | - TByteDeltas extends InstructionByteDeltaNode[] | undefined = InstructionByteDeltaNode[] | undefined, |
27 | | - TDiscriminators extends DiscriminatorNode[] | undefined = DiscriminatorNode[] | undefined, |
28 | | - TSubInstructions extends SubInstructionNode[] | undefined = SubInstructionNode[] | undefined, |
29 | | -> = Omit< |
30 | | - Partial< |
31 | | - InstructionNode< |
32 | | - TAccounts, |
33 | | - TArguments, |
34 | | - TExtraArguments, |
35 | | - TRemainingAccounts, |
36 | | - TByteDeltas, |
37 | | - TDiscriminators, |
38 | | - TSubInstructions |
39 | | - > |
40 | | - >, |
41 | | - 'docs' | 'kind' | 'name' |
42 | | -> & { |
43 | | - readonly docs?: DocsInput; |
44 | | - readonly name: string; |
45 | | -}; |
46 | | - |
47 | | -export function instructionNode< |
48 | | - const TAccounts extends InstructionAccountNode[] = [], |
49 | | - const TArguments extends InstructionArgumentNode[] = [], |
50 | | - const TExtraArguments extends InstructionArgumentNode[] | undefined = undefined, |
51 | | - const TRemainingAccounts extends InstructionRemainingAccountsNode[] | undefined = undefined, |
52 | | - const TByteDeltas extends InstructionByteDeltaNode[] | undefined = undefined, |
53 | | - const TDiscriminators extends DiscriminatorNode[] | undefined = undefined, |
54 | | - const TSubInstructions extends SubInstructionNode[] | undefined = undefined, |
55 | | ->( |
56 | | - input: InstructionNodeInput< |
57 | | - TAccounts, |
58 | | - TArguments, |
59 | | - TExtraArguments, |
60 | | - TRemainingAccounts, |
61 | | - TByteDeltas, |
62 | | - TDiscriminators, |
63 | | - TSubInstructions |
64 | | - >, |
65 | | -): InstructionNode< |
66 | | - TAccounts, |
67 | | - TArguments, |
68 | | - TExtraArguments, |
69 | | - TRemainingAccounts, |
70 | | - TByteDeltas, |
71 | | - TDiscriminators, |
72 | | - TSubInstructions |
73 | | -> { |
74 | | - return Object.freeze({ |
75 | | - kind: 'instructionNode', |
76 | | - |
77 | | - // Data. |
78 | | - name: camelCase(input.name), |
79 | | - docs: parseDocs(input.docs), |
80 | | - optionalAccountStrategy: parseOptionalAccountStrategy(input.optionalAccountStrategy), |
81 | | - ...(input.status !== undefined && { status: input.status }), |
82 | | - |
83 | | - // Children. |
84 | | - accounts: (input.accounts ?? []) as TAccounts, |
85 | | - arguments: (input.arguments ?? []) as TArguments, |
86 | | - extraArguments: input.extraArguments, |
87 | | - remainingAccounts: input.remainingAccounts, |
88 | | - byteDeltas: input.byteDeltas, |
89 | | - discriminators: input.discriminators, |
90 | | - subInstructions: input.subInstructions, |
91 | | - }); |
92 | | -} |
93 | 11 |
|
94 | 12 | export function parseOptionalAccountStrategy( |
95 | 13 | optionalAccountStrategy: OptionalAccountStrategy | undefined, |
|
0 commit comments