Skip to content

Commit 99c299d

Browse files
authored
Allow updating more fields in updateInstructionsVisitor (#747)
1 parent 9312533 commit 99c299d

File tree

3 files changed

+89
-18
lines changed

3 files changed

+89
-18
lines changed

.changeset/purple-lies-search.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 updating more fields in `updateInstructionsVisitor`

packages/visitors/src/updateInstructionsVisitor.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,13 @@ import {
2727
import { fillDefaultPdaSeedValuesVisitor } from './fillDefaultPdaSeedValuesVisitor';
2828

2929
export type InstructionUpdates =
30-
| { delete: true }
31-
| (InstructionMetadataUpdates & {
32-
accounts?: InstructionAccountUpdates;
33-
arguments?: InstructionArgumentUpdates;
34-
});
35-
36-
export type InstructionMetadataUpdates = Partial<
37-
Omit<
38-
InstructionNodeInput,
39-
| 'accounts'
40-
| 'arguments'
41-
| 'byteDeltas'
42-
| 'discriminators'
43-
| 'extraArguments'
44-
| 'remainingAccounts'
45-
| 'subInstructions'
46-
>
47-
>;
30+
| Partial<
31+
Omit<InstructionNodeInput, 'accounts' | 'arguments' | 'extraArguments'> & {
32+
accounts?: InstructionAccountUpdates;
33+
arguments?: InstructionArgumentUpdates;
34+
}
35+
>
36+
| { delete: true };
4837

4938
export type InstructionAccountUpdates = Record<
5039
string,

packages/visitors/test/updateInstructionsVisitor.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import {
2+
ArgumentValueNode,
3+
argumentValueNode,
24
assertIsNode,
35
CamelCaseString,
6+
ConstantDiscriminatorNode,
7+
constantDiscriminatorNode,
8+
constantValueNode,
49
instructionAccountNode,
510
instructionArgumentNode,
11+
instructionByteDeltaNode,
612
instructionNode,
13+
instructionRemainingAccountsNode,
714
numberTypeNode,
15+
NumberValueNode,
816
numberValueNode,
917
programNode,
1018
rootNode,
@@ -199,3 +207,72 @@ test('it updates the default value strategy of an instruction argument', () => {
199207
expect(result.arguments[1].defaultValue).toEqual(numberValueNode(1));
200208
expect(result.arguments[1].defaultValueStrategy).toBe('optional');
201209
});
210+
211+
test('it updates the byteDeltas of an instruction', () => {
212+
// Given the following instruction node with no byteDeltas.
213+
const node = instructionNode({
214+
name: 'myInstruction',
215+
});
216+
217+
// When we update the byteDeltas of that instruction.
218+
const result = visit(
219+
node,
220+
updateInstructionsVisitor({
221+
myInstruction: {
222+
byteDeltas: [instructionByteDeltaNode(numberValueNode(100))],
223+
},
224+
}),
225+
);
226+
227+
// Then we expect the following tree changes.
228+
assertIsNode(result, 'instructionNode');
229+
expect(result.byteDeltas).toEqual([instructionByteDeltaNode(numberValueNode(100))]);
230+
});
231+
232+
test('it updates the discriminators of an instruction', () => {
233+
// Given the following instruction node with no discriminators.
234+
const node = instructionNode({
235+
name: 'myInstruction',
236+
});
237+
238+
// When we update the discriminators of that instruction.
239+
const result = visit(
240+
node,
241+
updateInstructionsVisitor({
242+
myInstruction: {
243+
discriminators: [
244+
constantDiscriminatorNode(constantValueNode(numberTypeNode('u64'), numberValueNode(42))),
245+
],
246+
},
247+
}),
248+
);
249+
250+
// Then we expect the following tree changes.
251+
assertIsNode(result, 'instructionNode');
252+
expect(result.discriminators![0].kind).toBe('constantDiscriminatorNode');
253+
expect(((result.discriminators![0] as ConstantDiscriminatorNode).constant.value as NumberValueNode).number).toEqual(
254+
42,
255+
);
256+
});
257+
258+
test('it updates the remainingAccounts of an instruction', () => {
259+
// Given the following instruction node with no remaining accounts.
260+
const node = instructionNode({
261+
name: 'myInstruction',
262+
});
263+
264+
// When we update the remaining accounts of that instruction.
265+
const result = visit(
266+
node,
267+
updateInstructionsVisitor({
268+
myInstruction: {
269+
remainingAccounts: [instructionRemainingAccountsNode(argumentValueNode('abc'))],
270+
},
271+
}),
272+
);
273+
274+
// Then we expect the following tree changes.
275+
assertIsNode(result, 'instructionNode');
276+
expect(result.remainingAccounts![0].kind).toBe('instructionRemainingAccountsNode');
277+
expect((result.remainingAccounts![0].value as ArgumentValueNode).name).toBe('abc');
278+
});

0 commit comments

Comments
 (0)