Skip to content

Commit a32c424

Browse files
authored
Allow specifying docs in addPdasVisitor (#778)
1 parent b7a4032 commit a32c424

File tree

8 files changed

+62
-3
lines changed

8 files changed

+62
-3
lines changed

.changeset/forty-oranges-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/renderers-js': patch
3+
---
4+
5+
Add docs to PDA derivation functions

.changeset/old-cats-fall.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/visitors': patch
3+
---
4+
5+
Allow specifying docs for PDA nodes in addPdasVisitor

packages/renderers-js/e2e/token/idl.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,7 @@
22602260
{
22612261
"kind": "pdaNode",
22622262
"name": "associatedToken",
2263+
"docs": ["The address of the associated token account."],
22632264
"seeds": [
22642265
{
22652266
"kind": "variablePdaSeedNode",

packages/renderers-js/e2e/token/src/generated/pdas/associatedToken.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type AssociatedTokenSeeds = {
2222
mint: Address;
2323
};
2424

25+
/** The address of the associated token account. */
2526
export async function findAssociatedTokenPda(
2627
seeds: AssociatedTokenSeeds,
2728
config: { programAddress?: Address | undefined } = {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type {{ pdaSeedsType }} = {
1111
}
1212
{% endif %}
1313

14+
{{ macros.docblock(pdaDocs) }}
1415
export async function {{ findPdaFunction }}(
1516
{% if hasVariableSeeds %}
1617
seeds: {{ pdaSeedsType }},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function getPdaFunctionFragment(
3737
return fragmentFromTemplate('pdaFunction.njk', {
3838
findPdaFunction: nameApi.pdaFindFunction(pdaNode.name),
3939
hasVariableSeeds,
40+
pdaDocs: pdaNode.docs,
4041
pdaSeedsType: nameApi.pdaSeedsType(pdaNode.name),
4142
programAddress: pdaNode.programId ?? programNode.publicKey,
4243
seeds,

packages/visitors/src/addPdasVisitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CODAMA_ERROR__VISITORS__CANNOT_ADD_DUPLICATED_PDA_NAMES, CodamaError } from '@codama/errors';
2-
import { assertIsNode, camelCase, pdaNode, PdaSeedNode, programNode } from '@codama/nodes';
2+
import { assertIsNode, camelCase, pdaNode, PdaNodeInput, programNode } from '@codama/nodes';
33
import { bottomUpTransformerVisitor } from '@codama/visitors-core';
44

5-
export function addPdasVisitor(pdas: Record<string, { name: string; seeds: PdaSeedNode[] }[]>) {
5+
export function addPdasVisitor(pdas: Record<string, Omit<PdaNodeInput, 'programId'>[]>) {
66
return bottomUpTransformerVisitor(
77
Object.entries(pdas).map(([uncasedProgramName, newPdas]) => {
88
const programName = camelCase(uncasedProgramName);
@@ -22,7 +22,7 @@ export function addPdasVisitor(pdas: Record<string, { name: string; seeds: PdaSe
2222
}
2323
return programNode({
2424
...node,
25-
pdas: [...node.pdas, ...newPdas.map(pda => pdaNode({ name: pda.name, seeds: pda.seeds }))],
25+
pdas: [...node.pdas, ...newPdas.map(({ name, seeds, docs }) => pdaNode({ docs, name, seeds }))],
2626
});
2727
},
2828
};

packages/visitors/test/addPdasVisitor.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,48 @@ test('it fails to add a PDA if its name conflicts with an existing PDA on the pr
9999
}),
100100
);
101101
});
102+
103+
test('it adds PDA nodes to a program with docs', () => {
104+
// Given a program with a single PDA.
105+
const node = programNode({
106+
name: 'myProgram',
107+
pdas: [
108+
pdaNode({
109+
name: 'associatedToken',
110+
seeds: [
111+
variablePdaSeedNode('owner', publicKeyTypeNode()),
112+
constantPdaSeedNodeFromProgramId(),
113+
variablePdaSeedNode('mint', publicKeyTypeNode()),
114+
],
115+
}),
116+
],
117+
publicKey: 'Epo9rxh99jpeeWabRZi4tpgUVxZQeVn9vbbDjUztJtu4',
118+
});
119+
120+
// When we add two more PDAs.
121+
const newPdas = [
122+
pdaNode({
123+
docs: 'Metadata for a token.',
124+
name: 'metadata',
125+
seeds: [
126+
constantPdaSeedNodeFromString('utf8', 'metadata'),
127+
constantPdaSeedNodeFromProgramId(),
128+
variablePdaSeedNode('mint', publicKeyTypeNode()),
129+
],
130+
}),
131+
pdaNode({
132+
docs: 'The master edition.',
133+
name: 'masterEdition',
134+
seeds: [
135+
constantPdaSeedNodeFromString('utf8', 'metadata'),
136+
constantPdaSeedNodeFromProgramId(),
137+
variablePdaSeedNode('mint', publicKeyTypeNode()),
138+
constantPdaSeedNodeFromString('utf8', 'edition'),
139+
],
140+
}),
141+
];
142+
const result = visit(node, addPdasVisitor({ myProgram: newPdas }));
143+
144+
// Then we expect the following program to be returned.
145+
expect(result).toEqual({ ...node, pdas: [...node.pdas, ...newPdas] });
146+
});

0 commit comments

Comments
 (0)