InstructionNode.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {
  2. bytesTypeNode,
  3. bytesValueNode,
  4. fieldDiscriminatorNode,
  5. fixedSizeTypeNode,
  6. instructionAccountNode,
  7. instructionArgumentNode,
  8. instructionNode,
  9. numberTypeNode,
  10. } from '@codama/nodes';
  11. import { expect, test } from 'vitest';
  12. import { instructionNodeFromAnchorV00 } from '../../src';
  13. test('it creates instruction nodes', () => {
  14. const node = instructionNodeFromAnchorV00({
  15. accounts: [{ isMut: true, isSigner: false, name: 'mint' }],
  16. args: [{ name: 'amount', type: 'u8' }],
  17. name: 'mintTokens',
  18. });
  19. expect(node).toEqual(
  20. instructionNode({
  21. accounts: [instructionAccountNode({ isSigner: false, isWritable: true, name: 'mint' })],
  22. arguments: [instructionArgumentNode({ name: 'amount', type: numberTypeNode('u8') })],
  23. name: 'mintTokens',
  24. }),
  25. );
  26. });
  27. test('it creates instruction nodes with anchor discriminators', () => {
  28. const node = instructionNodeFromAnchorV00(
  29. {
  30. accounts: [],
  31. args: [],
  32. name: 'myInstruction',
  33. },
  34. 'anchor',
  35. );
  36. expect(node).toEqual(
  37. instructionNode({
  38. arguments: [
  39. instructionArgumentNode({
  40. defaultValue: bytesValueNode('base16', 'c3f1b80e7f9b4435'),
  41. defaultValueStrategy: 'omitted',
  42. name: 'discriminator',
  43. type: fixedSizeTypeNode(bytesTypeNode(), 8),
  44. }),
  45. ],
  46. discriminators: [fieldDiscriminatorNode('discriminator')],
  47. name: 'myInstruction',
  48. }),
  49. );
  50. });