Skip to content
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/tasty-weeks-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codama/nodes-from-anchor": patch
---

Recognize program addresses from seeds of kind set to `"account"` if the address of the account it points to is statically known. (See #607)
12 changes: 11 additions & 1 deletion packages/nodes-from-anchor/src/v01/InstructionAccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,15 @@ export function instructionAccountNodeFromAnchorV01(
programId = getBase58Codec().decode(new Uint8Array(idl.pda.program.value));
break;
}
case 'account': {
const programPath = idl.pda.program.path;
const programNode = parentIdl.find(acc => acc.name == programPath);
if (!(programNode && 'address' in programNode)) {
throw new CodamaError(CODAMA_ERROR__ANCHOR__PROGRAM_ID_KIND_UNIMPLEMENTED, { kind });
}
programId = programNode.address;
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