Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 13 additions & 2 deletions packages/nodes-from-anchor/src/v01/InstructionAccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import { getBase58Codec } from '@solana/codecs';

import { hex } from '../utils';
import { IdlV01InstructionAccount, IdlV01InstructionAccountItem, IdlV01Seed } from './idl';
import { IdlV01InstructionAccount, IdlV01InstructionAccountItem, IdlV01Seed, IdlV01SeedAccount } from './idl';

export function instructionAccountNodesFromAnchorV01(
allAccounts: AccountNode[],
Expand All @@ -40,14 +40,15 @@ export function instructionAccountNodesFromAnchorV01(
return idl.flatMap(account =>
'accounts' in account
? instructionAccountNodesFromAnchorV01(allAccounts, instructionArguments, account.accounts)
: [instructionAccountNodeFromAnchorV01(allAccounts, instructionArguments, account)],
: [instructionAccountNodeFromAnchorV01(allAccounts, instructionArguments, account, idl)],
);
}

export function instructionAccountNodeFromAnchorV01(
allAccounts: AccountNode[],
instructionArguments: InstructionArgumentNode[],
idl: IdlV01InstructionAccount,
parentIdl: IdlV01InstructionAccountItem[],
): InstructionAccountNode {
const isOptional = idl.optional ?? false;
const docs = idl.docs ?? [];
Expand Down Expand Up @@ -133,6 +134,16 @@ export function instructionAccountNodeFromAnchorV01(
programId = getBase58Codec().decode(new Uint8Array(idl.pda.program.value));
break;
}
case 'account': {
const accNode = parentIdl.find(acc => acc.name == (idl.pda?.program as IdlV01SeedAccount).path);
if (accNode) {
programId = (accNode as IdlV01InstructionAccount).address;
} else {
programId = '';
}

break;
}
default: {
throw new CodamaError(CODAMA_ERROR__ANCHOR__PROGRAM_ID_KIND_UNIMPLEMENTED, { kind });
}
Expand Down
19 changes: 12 additions & 7 deletions packages/nodes-from-anchor/test/v01/InstructionAccountNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ import { expect, test } from 'vitest';
import { instructionAccountNodeFromAnchorV01, instructionAccountNodesFromAnchorV01 } from '../../src';

test('it creates instruction account nodes', () => {
const node = instructionAccountNodeFromAnchorV01([], [], {
docs: ['my docs'],
name: 'MyInstructionAccount',
optional: true,
signer: false,
writable: true,
});
const node = instructionAccountNodeFromAnchorV01(
[],
[],
{
docs: ['my docs'],
name: 'MyInstructionAccount',
optional: true,
signer: false,
writable: true,
},
[],
);

expect(node).toEqual(
instructionAccountNode({
Expand Down
Loading