InstructionNode.test.ts 3.4 KB

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