Skip to content

Commit a37264c

Browse files
committed
Fix duplicate seed names for nested arg PDA seeds
* found while rebasing onto main
1 parent 09b1602 commit a37264c

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function pdaSeedNodeFromAnchorV01(
6666
}
6767
case 'arg': {
6868
const pathParts = seed.path.split('.');
69-
const argumentName = camelCase(pathParts.length > 1 ? pathParts[pathParts.length - 1] : pathParts[0]);
69+
const argumentName = camelCase(pathParts.length > 1 ? pathParts.slice(1).join('_') : pathParts[0]);
7070

7171
let argumentType: TypeNode;
7272
if (pathParts.length > 1) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ test('it resolves nested arg path from defined type link', () => {
6767
expect(nodes!.value).toEqual(pdaSeedValueNode('amount', argumentValueNode('amount')));
6868
});
6969

70+
test('it uses full nested path to avoid name collisions for deeply nested arg seeds', () => {
71+
const instructionArgs = [
72+
instructionArgumentNode({
73+
name: 'input',
74+
type: structTypeNode([
75+
structFieldTypeNode({ name: 'seedEnum', type: numberTypeNode('u8') }),
76+
structFieldTypeNode({
77+
name: 'innerStruct',
78+
type: structTypeNode([structFieldTypeNode({ name: 'seedEnum', type: numberTypeNode('u8') })]),
79+
}),
80+
]),
81+
}),
82+
];
83+
84+
const shallow = pdaSeedNodeFromAnchorV01({ kind: 'arg', path: 'input.seed_enum' }, instructionArgs);
85+
const deep = pdaSeedNodeFromAnchorV01({ kind: 'arg', path: 'input.inner_struct.seed_enum' }, instructionArgs);
86+
87+
expect(shallow?.definition).toEqual(variablePdaSeedNode('seedEnum', numberTypeNode('u8')));
88+
expect(deep?.definition).toEqual(variablePdaSeedNode('innerStructSeedEnum', numberTypeNode('u8')));
89+
});
90+
7091
test('it returns undefined for unresolvable nested arg type', () => {
7192
const result = pdaSeedNodeFromAnchorV01({ kind: 'arg', path: 'args.owner' }, [
7293
instructionArgumentNode({ name: 'args', type: definedTypeLinkNode('UnknownType') }),

0 commit comments

Comments
 (0)