InstructionNode.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. accountNode,
  3. bytesTypeNode,
  4. fieldDiscriminatorNode,
  5. fixedSizeTypeNode,
  6. instructionAccountNode,
  7. instructionArgumentNode,
  8. instructionNode,
  9. numberTypeNode,
  10. publicKeyTypeNode,
  11. structFieldTypeNode,
  12. structTypeNode,
  13. } from '@codama/nodes';
  14. import { expect, test } from 'vitest';
  15. import { GenericsV01, getAnchorDiscriminatorV01, instructionNodeFromAnchorV01 } from '../../src';
  16. const generics = {} as GenericsV01;
  17. test('it creates instruction nodes', () => {
  18. const node = instructionNodeFromAnchorV01(
  19. [
  20. accountNode({
  21. data: structTypeNode([
  22. structFieldTypeNode({
  23. name: 'groupMint',
  24. type: publicKeyTypeNode(),
  25. }),
  26. structFieldTypeNode({
  27. name: 'paymentMint',
  28. type: publicKeyTypeNode(),
  29. }),
  30. ]),
  31. name: 'distribution',
  32. }),
  33. ],
  34. {
  35. accounts: [
  36. {
  37. name: 'distribution',
  38. pda: {
  39. seeds: [
  40. { kind: 'const', value: [42, 31, 29] },
  41. { account: 'Distribution', kind: 'account', path: 'distribution.group_mint' },
  42. ],
  43. },
  44. signer: false,
  45. writable: true,
  46. },
  47. ],
  48. args: [{ name: 'amount', type: 'u8' }],
  49. discriminator: [246, 28, 6, 87, 251, 45, 50, 42],
  50. name: 'mintTokens',
  51. },
  52. generics,
  53. );
  54. expect(node).toEqual(
  55. instructionNode({
  56. accounts: [
  57. instructionAccountNode({
  58. // TODO: Handle seeds with nested paths.
  59. // defaultValue: pdaValueNode(
  60. // pdaNode({
  61. // name: 'distribution',
  62. // seeds: [
  63. // constantPdaSeedNodeFromBytes('base16', '2a1f1d'),
  64. // variablePdaSeedNode('distributionGroupMint', publicKeyTypeNode()),
  65. // ],
  66. // }),
  67. // [],
  68. // ),
  69. isSigner: false,
  70. isWritable: true,
  71. name: 'distribution',
  72. }),
  73. ],
  74. arguments: [
  75. instructionArgumentNode({
  76. defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
  77. defaultValueStrategy: 'omitted',
  78. name: 'discriminator',
  79. type: fixedSizeTypeNode(bytesTypeNode(), 8),
  80. }),
  81. instructionArgumentNode({ name: 'amount', type: numberTypeNode('u8') }),
  82. ],
  83. discriminators: [fieldDiscriminatorNode('discriminator')],
  84. name: 'mintTokens',
  85. }),
  86. );
  87. });
  88. test('it creates instruction nodes with anchor discriminators', () => {
  89. const node = instructionNodeFromAnchorV01(
  90. [],
  91. {
  92. accounts: [],
  93. args: [],
  94. discriminator: [246, 28, 6, 87, 251, 45, 50, 42],
  95. name: 'myInstruction',
  96. },
  97. generics,
  98. );
  99. expect(node).toEqual(
  100. instructionNode({
  101. arguments: [
  102. instructionArgumentNode({
  103. defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
  104. defaultValueStrategy: 'omitted',
  105. name: 'discriminator',
  106. type: fixedSizeTypeNode(bytesTypeNode(), 8),
  107. }),
  108. ],
  109. discriminators: [fieldDiscriminatorNode('discriminator')],
  110. name: 'myInstruction',
  111. }),
  112. );
  113. });