Skip to content

Commit 14aeb2d

Browse files
daog1lorisleiva
andauthored
Recognize program address from Anchor IDL with seed of kind "account" (#621)
* fix 607 * fix 607 test * Throw if program address is not found * Create tasty-weeks-protect.md --------- Co-authored-by: Loris Leiva <[email protected]>
1 parent 879bf6b commit 14aeb2d

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

.changeset/tasty-weeks-protect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codama/nodes-from-anchor": patch
3+
---
4+
5+
Recognize program addresses from seeds of kind set to `"account"` if the address of the account it points to is statically known. (See #607)

packages/nodes-from-anchor/src/v01/InstructionAccountNode.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ export function instructionAccountNodesFromAnchorV01(
4040
return idl.flatMap(account =>
4141
'accounts' in account
4242
? instructionAccountNodesFromAnchorV01(allAccounts, instructionArguments, account.accounts)
43-
: [instructionAccountNodeFromAnchorV01(allAccounts, instructionArguments, account)],
43+
: [instructionAccountNodeFromAnchorV01(allAccounts, instructionArguments, account, idl)],
4444
);
4545
}
4646

4747
export function instructionAccountNodeFromAnchorV01(
4848
allAccounts: AccountNode[],
4949
instructionArguments: InstructionArgumentNode[],
5050
idl: IdlV01InstructionAccount,
51+
parentIdl: IdlV01InstructionAccountItem[],
5152
): InstructionAccountNode {
5253
const isOptional = idl.optional ?? false;
5354
const docs = idl.docs ?? [];
@@ -133,6 +134,15 @@ export function instructionAccountNodeFromAnchorV01(
133134
programId = getBase58Codec().decode(new Uint8Array(idl.pda.program.value));
134135
break;
135136
}
137+
case 'account': {
138+
const programPath = idl.pda.program.path;
139+
const programNode = parentIdl.find(acc => acc.name == programPath);
140+
if (!(programNode && 'address' in programNode)) {
141+
throw new CodamaError(CODAMA_ERROR__ANCHOR__PROGRAM_ID_KIND_UNIMPLEMENTED, { kind });
142+
}
143+
programId = programNode.address;
144+
break;
145+
}
136146
default: {
137147
throw new CodamaError(CODAMA_ERROR__ANCHOR__PROGRAM_ID_KIND_UNIMPLEMENTED, { kind });
138148
}

packages/nodes-from-anchor/test/v01/InstructionAccountNode.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ import { expect, test } from 'vitest';
2121
import { instructionAccountNodeFromAnchorV01, instructionAccountNodesFromAnchorV01 } from '../../src';
2222

2323
test('it creates instruction account nodes', () => {
24-
const node = instructionAccountNodeFromAnchorV01([], [], {
25-
docs: ['my docs'],
26-
name: 'MyInstructionAccount',
27-
optional: true,
28-
signer: false,
29-
writable: true,
30-
});
24+
const node = instructionAccountNodeFromAnchorV01(
25+
[],
26+
[],
27+
{
28+
docs: ['my docs'],
29+
name: 'MyInstructionAccount',
30+
optional: true,
31+
signer: false,
32+
writable: true,
33+
},
34+
[],
35+
);
3136

3237
expect(node).toEqual(
3338
instructionAccountNode({

0 commit comments

Comments
 (0)