InstructionNode.test.ts 1.6 KB

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