Skip to content

Commit baeaf14

Browse files
authored
fix: render PDA helper with programAddress = node.programId if defined (#52)
1 parent 0771b9d commit baeaf14

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

.changeset/polite-cats-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@kinobi-so/renderers-js': patch
3+
---
4+
5+
Fix rendering PDA helper functions with a default programId.

packages/renderers-js/public/templates/fragments/pdaFunction.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function {{ findPdaFunction }}(
1717
{% endif %}
1818
config: { programAddress?: Address | undefined } = {},
1919
): Promise<ProgramDerivedAddress> {
20-
const { programAddress = '{{ program.publicKey }}' as Address<'{{ program.publicKey }}'> } = config;
20+
const { programAddress = '{{ programAddress }}' as Address<'{{ programAddress }}'> } = config;
2121
return await getProgramDerivedAddress({ programAddress, seeds: [
2222
{% for seed in seeds %}
2323
{% if seed.kind === 'constantPdaSeedNode' and seed.value.kind === 'programIdValueNode' %}

packages/renderers-js/src/fragments/pdaFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function getPdaFunctionFragment(
3737
findPdaFunction: nameApi.pdaFindFunction(pdaNode.name),
3838
hasVariableSeeds,
3939
pdaSeedsType: nameApi.pdaSeedsType(pdaNode.name),
40-
program: programNode,
40+
programAddress: pdaNode.programId ?? programNode.publicKey,
4141
seeds,
4242
})
4343
.mergeImportsWith(imports)

packages/renderers-js/test/pdasPage.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ test('it renders a PDA helper function and its input type', async () => {
4141
]);
4242
});
4343

44+
test('it renders a PDA helper function with a default program address', async () => {
45+
// Given the following PDA node with a default program address.
46+
const node = programNode({
47+
name: 'myProgram',
48+
pdas: [
49+
pdaNode({
50+
name: 'foo',
51+
programId: 'myProgramId',
52+
seeds: [constantPdaSeedNodeFromString('utf8', 'myPrefix')],
53+
}),
54+
],
55+
publicKey: '1111',
56+
});
57+
58+
// When we render it.
59+
const renderMap = visit(node, getRenderMapVisitor());
60+
61+
// Then we expect the following PDA function using the programId as the default program address.
62+
await renderMapContains(renderMap, 'pdas/foo.ts', [
63+
'export async function findFooPda',
64+
"const { programAddress = 'myProgramId' as Address<'myProgramId'> } = config;",
65+
]);
66+
});
67+
4468
test('it renders an empty array of seeds for seedless PDAs', async () => {
4569
// Given the following program with 1 account and 1 pda with empty seeds.
4670
const node = programNode({

0 commit comments

Comments
 (0)