Skip to content

Commit

Permalink
Support nested instruction accounts from IDL (#213)
Browse files Browse the repository at this point in the history
Fix #210
  • Loading branch information
lorisleiva authored Apr 15, 2024
1 parent 8cb70c8 commit a0b8d90
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
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

0 comments on commit a0b8d90

Please sign in to comment.