Skip to content

Commit 2702c2c

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

2 files changed

Lines changed: 24 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ 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([
79+
structFieldTypeNode({ name: 'seedEnum', type: numberTypeNode('u8') }),
80+
]),
81+
}),
82+
]),
83+
}),
84+
];
85+
86+
const shallow = pdaSeedNodeFromAnchorV01({ kind: 'arg', path: 'input.seed_enum' }, instructionArgs);
87+
const deep = pdaSeedNodeFromAnchorV01({ kind: 'arg', path: 'input.inner_struct.seed_enum' }, instructionArgs);
88+
89+
expect(shallow?.definition).toEqual(variablePdaSeedNode('seedEnum', numberTypeNode('u8')));
90+
expect(deep?.definition).toEqual(variablePdaSeedNode('innerStructSeedEnum', numberTypeNode('u8')));
91+
});
92+
7093
test('it returns undefined for unresolvable nested arg type', () => {
7194
const result = pdaSeedNodeFromAnchorV01({ kind: 'arg', path: 'args.owner' }, [
7295
instructionArgumentNode({ name: 'args', type: definedTypeLinkNode('UnknownType') }),

0 commit comments

Comments
 (0)