Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested instruction accounts from IDL #213

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-rockets-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": patch
---

Support nested instruction accounts from IDL
7 changes: 6 additions & 1 deletion src/idl/IdlInstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IdlType } from './IdlType';

export type IdlInstruction = {
name: string;
accounts: IdlInstructionAccount[];
accounts: (IdlInstructionAccount | IdlInstructionNestedAccounts)[];
args: IdlInstructionArg[];
defaultOptionalAccounts?: boolean;
legacyOptionalAccountsStrategy?: boolean;
Expand All @@ -21,6 +21,11 @@ export type IdlInstructionAccount = {
desc?: string;
};

export type IdlInstructionNestedAccounts = {
name: string;
accounts: (IdlInstructionAccount | IdlInstructionNestedAccounts)[];
};

export type IdlInstructionArg = {
name: string;
type: IdlType;
Expand Down
12 changes: 11 additions & 1 deletion src/nodes/InstructionAccountNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IdlInstructionAccount } from '../idl';
import { IdlInstructionAccount, IdlInstructionNestedAccounts } from '../idl';
import { MainCaseString, PartialExcept, mainCase } from '../shared';
import { InstructionInputValueNode } from './contextualValueNodes';

Expand Down Expand Up @@ -50,6 +50,16 @@ export function instructionAccountNode<
};
}

export function instructionAccountNodesFromIdl(
idl: (IdlInstructionAccount | IdlInstructionNestedAccounts)[]
): InstructionAccountNode[] {
return idl.flatMap((account) =>
'accounts' in account
? instructionAccountNodesFromIdl(account.accounts)
: [instructionAccountNodeFromIdl(account)]
);
}

export function instructionAccountNodeFromIdl(
idl: IdlInstructionAccount
): InstructionAccountNode {
Expand Down
6 changes: 2 additions & 4 deletions src/nodes/InstructionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IdlInstruction } from '../idl';
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../shared';
import {
InstructionAccountNode,
instructionAccountNodeFromIdl,
instructionAccountNodesFromIdl,
} from './InstructionAccountNode';
import {
InstructionArgumentNode,
Expand Down Expand Up @@ -171,9 +171,7 @@ export function instructionNodeFromIdl(
name,
idlName,
docs: idl.docs ?? [],
accounts: (idl.accounts ?? []).map((account) =>
instructionAccountNodeFromIdl(account)
),
accounts: instructionAccountNodesFromIdl(idl.accounts ?? []),
arguments: dataArguments,
discriminators,
optionalAccountStrategy: idl.legacyOptionalAccountsStrategy
Expand Down
Loading