Browse Source

Allow updating more fields in `updateInstructionsVisitor` (#747)

Callum McIntyre 3 months ago
parent
commit
99c299d65e

+ 5 - 0
.changeset/purple-lies-search.md

@@ -0,0 +1,5 @@
+---
+'@codama/visitors': patch
+---
+
+Allow updating more fields in `updateInstructionsVisitor`

+ 7 - 18
packages/visitors/src/updateInstructionsVisitor.ts

@@ -27,24 +27,13 @@ import {
 import { fillDefaultPdaSeedValuesVisitor } from './fillDefaultPdaSeedValuesVisitor';
 import { fillDefaultPdaSeedValuesVisitor } from './fillDefaultPdaSeedValuesVisitor';
 
 
 export type InstructionUpdates =
 export type InstructionUpdates =
-    | { delete: true }
-    | (InstructionMetadataUpdates & {
-          accounts?: InstructionAccountUpdates;
-          arguments?: InstructionArgumentUpdates;
-      });
-
-export type InstructionMetadataUpdates = Partial<
-    Omit<
-        InstructionNodeInput,
-        | 'accounts'
-        | 'arguments'
-        | 'byteDeltas'
-        | 'discriminators'
-        | 'extraArguments'
-        | 'remainingAccounts'
-        | 'subInstructions'
-    >
->;
+    | Partial<
+          Omit<InstructionNodeInput, 'accounts' | 'arguments' | 'extraArguments'> & {
+              accounts?: InstructionAccountUpdates;
+              arguments?: InstructionArgumentUpdates;
+          }
+      >
+    | { delete: true };
 
 
 export type InstructionAccountUpdates = Record<
 export type InstructionAccountUpdates = Record<
     string,
     string,

+ 77 - 0
packages/visitors/test/updateInstructionsVisitor.test.ts

@@ -1,10 +1,18 @@
 import {
 import {
+    ArgumentValueNode,
+    argumentValueNode,
     assertIsNode,
     assertIsNode,
     CamelCaseString,
     CamelCaseString,
+    ConstantDiscriminatorNode,
+    constantDiscriminatorNode,
+    constantValueNode,
     instructionAccountNode,
     instructionAccountNode,
     instructionArgumentNode,
     instructionArgumentNode,
+    instructionByteDeltaNode,
     instructionNode,
     instructionNode,
+    instructionRemainingAccountsNode,
     numberTypeNode,
     numberTypeNode,
+    NumberValueNode,
     numberValueNode,
     numberValueNode,
     programNode,
     programNode,
     rootNode,
     rootNode,
@@ -199,3 +207,72 @@ test('it updates the default value strategy of an instruction argument', () => {
     expect(result.arguments[1].defaultValue).toEqual(numberValueNode(1));
     expect(result.arguments[1].defaultValue).toEqual(numberValueNode(1));
     expect(result.arguments[1].defaultValueStrategy).toBe('optional');
     expect(result.arguments[1].defaultValueStrategy).toBe('optional');
 });
 });
+
+test('it updates the byteDeltas of an instruction', () => {
+    // Given the following instruction node with no byteDeltas.
+    const node = instructionNode({
+        name: 'myInstruction',
+    });
+
+    // When we update the byteDeltas of that instruction.
+    const result = visit(
+        node,
+        updateInstructionsVisitor({
+            myInstruction: {
+                byteDeltas: [instructionByteDeltaNode(numberValueNode(100))],
+            },
+        }),
+    );
+
+    // Then we expect the following tree changes.
+    assertIsNode(result, 'instructionNode');
+    expect(result.byteDeltas).toEqual([instructionByteDeltaNode(numberValueNode(100))]);
+});
+
+test('it updates the discriminators of an instruction', () => {
+    // Given the following instruction node with no discriminators.
+    const node = instructionNode({
+        name: 'myInstruction',
+    });
+
+    // When we update the discriminators of that instruction.
+    const result = visit(
+        node,
+        updateInstructionsVisitor({
+            myInstruction: {
+                discriminators: [
+                    constantDiscriminatorNode(constantValueNode(numberTypeNode('u64'), numberValueNode(42))),
+                ],
+            },
+        }),
+    );
+
+    // Then we expect the following tree changes.
+    assertIsNode(result, 'instructionNode');
+    expect(result.discriminators![0].kind).toBe('constantDiscriminatorNode');
+    expect(((result.discriminators![0] as ConstantDiscriminatorNode).constant.value as NumberValueNode).number).toEqual(
+        42,
+    );
+});
+
+test('it updates the remainingAccounts of an instruction', () => {
+    // Given the following instruction node with no remaining accounts.
+    const node = instructionNode({
+        name: 'myInstruction',
+    });
+
+    // When we update the remaining accounts of that instruction.
+    const result = visit(
+        node,
+        updateInstructionsVisitor({
+            myInstruction: {
+                remainingAccounts: [instructionRemainingAccountsNode(argumentValueNode('abc'))],
+            },
+        }),
+    );
+
+    // Then we expect the following tree changes.
+    assertIsNode(result, 'instructionNode');
+    expect(result.remainingAccounts![0].kind).toBe('instructionRemainingAccountsNode');
+    expect((result.remainingAccounts![0].value as ArgumentValueNode).name).toBe('abc');
+});