Skip to content

Commit 2b6a037

Browse files
committed
Use NodePaths in fillDefaultPdaSeedValuesVisitor
1 parent c31eea8 commit 2b6a037

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

.changeset/curly-berries-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/visitors': minor
3+
---
4+
5+
Use `NodePaths` in `fillDefaultPdaSeedValuesVisitor`

packages/visitors/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ codama.update(deduplicateIdenticalDefinedTypesVisitor());
9696
9797
### `fillDefaultPdaSeedValuesVisitor`
9898
99-
This visitor fills any missing `PdaSeedValueNodes` from `PdaValueNodes` using the provided `InstructionNode` such that:
99+
This visitor fills any missing `PdaSeedValueNodes` from `PdaValueNodes` using the provided `NodePath<InstructionNode>` such that:
100100
101101
- If a `VariablePdaSeedNode` is of type `PublicKeyTypeNode` and the name of the seed matches the name of an account in the `InstructionNode`, then a new `PdaSeedValueNode` will be added with the matching account.
102102
- Otherwise, if a `VariablePdaSeedNode` is of any other type and the name of the seed matches the name of an argument in the `InstructionNode`, then a new `PdaSeedValueNode` will be added with the matching argument.
@@ -107,7 +107,7 @@ It also requires a [`LinkableDictionary`](../visitors-core/README.md#linkable-di
107107
Note that this visitor is mainly used for internal purposes.
108108
109109
```ts
110-
codama.update(fillDefaultPdaSeedValuesVisitor(instructionNode, linkables, strictMode));
110+
codama.update(fillDefaultPdaSeedValuesVisitor(instructionPath, linkables, strictMode));
111111
```
112112
113113
### `flattenInstructionDataArgumentsVisitor`

packages/visitors/src/fillDefaultPdaSeedValuesVisitor.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ import {
1414
pdaSeedValueNode,
1515
pdaValueNode,
1616
} from '@codama/nodes';
17-
import { extendVisitor, identityVisitor, LinkableDictionary, NodeStack, pipe, Visitor } from '@codama/visitors-core';
17+
import {
18+
extendVisitor,
19+
getLastNodeFromPath,
20+
identityVisitor,
21+
LinkableDictionary,
22+
NodePath,
23+
pipe,
24+
Visitor,
25+
} from '@codama/visitors-core';
1826

1927
/**
2028
* Fills in default values for variable PDA seeds that are not explicitly provided.
@@ -29,19 +37,19 @@ import { extendVisitor, identityVisitor, LinkableDictionary, NodeStack, pipe, Vi
2937
* pdaSeedValueNodes contains invalid seeds or if there aren't enough variable seeds.
3038
*/
3139
export function fillDefaultPdaSeedValuesVisitor(
32-
instruction: InstructionNode,
33-
stack: NodeStack,
40+
instructionPath: NodePath<InstructionNode>,
3441
linkables: LinkableDictionary,
3542
strictMode: boolean = false,
3643
) {
44+
const instruction = getLastNodeFromPath(instructionPath);
3745
return pipe(identityVisitor(INSTRUCTION_INPUT_VALUE_NODES), v =>
3846
extendVisitor(v, {
3947
visitPdaValue(node, { next }) {
4048
const visitedNode = next(node);
4149
assertIsNode(visitedNode, 'pdaValueNode');
4250
const foundPda = isNode(visitedNode.pda, 'pdaNode')
4351
? visitedNode.pda
44-
: linkables.get([...stack.getPath(), visitedNode.pda]);
52+
: linkables.get([...instructionPath, visitedNode.pda]);
4553
if (!foundPda) return visitedNode;
4654
const seeds = addDefaultSeedValuesFromPdaWhenMissing(instruction, foundPda, visitedNode.seeds);
4755
if (strictMode && !allSeedsAreValid(instruction, foundPda, seeds)) {

packages/visitors/src/setInstructionAccountDefaultValuesVisitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export function setInstructionAccountDefaultValuesVisitor(rules: InstructionAcco
167167
v =>
168168
extendVisitor(v, {
169169
visitInstruction(node) {
170+
const instructionPath = stack.getPath('instructionNode');
170171
const instructionAccounts = node.accounts.map((account): InstructionAccountNode => {
171172
const rule = matchRule(node, account);
172173
if (!rule) return account;
@@ -180,7 +181,7 @@ export function setInstructionAccountDefaultValuesVisitor(rules: InstructionAcco
180181
...account,
181182
defaultValue: visit(
182183
rule.defaultValue,
183-
fillDefaultPdaSeedValuesVisitor(node, stack, linkables, true),
184+
fillDefaultPdaSeedValuesVisitor(instructionPath, linkables, true),
184185
),
185186
};
186187
} catch (error) {

packages/visitors/src/updateInstructionsVisitor.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
BottomUpNodeTransformerWithSelector,
1717
bottomUpTransformerVisitor,
1818
LinkableDictionary,
19+
NodePath,
1920
NodeStack,
2021
pipe,
2122
recordLinkablesOnFirstVisitVisitor,
@@ -72,10 +73,11 @@ export function updateInstructionsVisitor(map: Record<string, InstructionUpdates
7273
return null;
7374
}
7475

76+
const instructionPath = stack.getPath('instructionNode');
7577
const { accounts: accountUpdates, arguments: argumentUpdates, ...metadataUpdates } = updates;
7678
const { newArguments, newExtraArguments } = handleInstructionArguments(node, argumentUpdates ?? {});
7779
const newAccounts = node.accounts.map(account =>
78-
handleInstructionAccount(node, stack, account, accountUpdates ?? {}, linkables),
80+
handleInstructionAccount(instructionPath, account, accountUpdates ?? {}, linkables),
7981
);
8082
return instructionNode({
8183
...node,
@@ -96,8 +98,7 @@ export function updateInstructionsVisitor(map: Record<string, InstructionUpdates
9698
}
9799

98100
function handleInstructionAccount(
99-
instruction: InstructionNode,
100-
stack: NodeStack,
101+
instructionPath: NodePath<InstructionNode>,
101102
account: InstructionAccountNode,
102103
accountUpdates: InstructionAccountUpdates,
103104
linkables: LinkableDictionary,
@@ -115,7 +116,7 @@ function handleInstructionAccount(
115116

116117
return instructionAccountNode({
117118
...acountWithoutDefault,
118-
defaultValue: visit(defaultValue, fillDefaultPdaSeedValuesVisitor(instruction, stack, linkables)),
119+
defaultValue: visit(defaultValue, fillDefaultPdaSeedValuesVisitor(instructionPath, linkables)),
119120
});
120121
}
121122

packages/visitors/test/fillDefaultPdaSeedValuesVisitor.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
publicKeyTypeNode,
1515
variablePdaSeedNode,
1616
} from '@codama/nodes';
17-
import { LinkableDictionary, NodeStack, visit } from '@codama/visitors-core';
17+
import { LinkableDictionary, visit } from '@codama/visitors-core';
1818
import { expect, test } from 'vitest';
1919

2020
import { fillDefaultPdaSeedValuesVisitor } from '../src';
@@ -56,10 +56,9 @@ test('it fills missing pda seed values with default values', () => {
5656
arguments: [instructionArgumentNode({ name: 'seed2', type: numberTypeNode('u64') })],
5757
name: 'myInstruction',
5858
});
59-
const instructionStack = new NodeStack([program, instruction]);
6059

6160
// When we fill the PDA seeds with default values.
62-
const result = visit(node, fillDefaultPdaSeedValuesVisitor(instruction, instructionStack, linkables));
61+
const result = visit(node, fillDefaultPdaSeedValuesVisitor([program, instruction], linkables));
6362

6463
// Then we expect the following pdaValueNode to be returned.
6564
expect(result).toEqual(
@@ -111,10 +110,9 @@ test('it fills nested pda value nodes', () => {
111110
arguments: [instructionArgumentNode({ name: 'seed2', type: numberTypeNode('u64') })],
112111
name: 'myInstruction',
113112
});
114-
const instructionStack = new NodeStack([program, instruction]);
115113

116114
// When we fill the PDA seeds with default values.
117-
const result = visit(node, fillDefaultPdaSeedValuesVisitor(instruction, instructionStack, linkables));
115+
const result = visit(node, fillDefaultPdaSeedValuesVisitor([program, instruction], linkables));
118116

119117
// Then we expect the following conditionalValueNode to be returned.
120118
expect(result).toEqual(
@@ -159,10 +157,9 @@ test('it ignores default seeds missing from the instruction', () => {
159157
arguments: [instructionArgumentNode({ name: 'seed2', type: numberTypeNode('u64') })],
160158
name: 'myInstruction',
161159
});
162-
const instructionStack = new NodeStack([program, instruction]);
163160

164161
// When we fill the PDA seeds with default values.
165-
const result = visit(node, fillDefaultPdaSeedValuesVisitor(instruction, instructionStack, linkables));
162+
const result = visit(node, fillDefaultPdaSeedValuesVisitor([program, instruction], linkables));
166163

167164
// Then we expect the following pdaValueNode to be returned.
168165
expect(result).toEqual(

0 commit comments

Comments
 (0)