| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import {
- accountNode,
- bytesTypeNode,
- fieldDiscriminatorNode,
- fixedSizeTypeNode,
- instructionAccountNode,
- instructionArgumentNode,
- instructionNode,
- numberTypeNode,
- publicKeyTypeNode,
- structFieldTypeNode,
- structTypeNode,
- } from '@codama/nodes';
- import { expect, test } from 'vitest';
- import { GenericsV01, getAnchorDiscriminatorV01, instructionNodeFromAnchorV01 } from '../../src';
- const generics = {} as GenericsV01;
- test('it creates instruction nodes', () => {
- const node = instructionNodeFromAnchorV01(
- [
- accountNode({
- data: structTypeNode([
- structFieldTypeNode({
- name: 'groupMint',
- type: publicKeyTypeNode(),
- }),
- structFieldTypeNode({
- name: 'paymentMint',
- type: publicKeyTypeNode(),
- }),
- ]),
- name: 'distribution',
- }),
- ],
- {
- accounts: [
- {
- name: 'distribution',
- pda: {
- seeds: [
- { kind: 'const', value: [42, 31, 29] },
- { account: 'Distribution', kind: 'account', path: 'distribution.group_mint' },
- ],
- },
- signer: false,
- writable: true,
- },
- ],
- args: [{ name: 'amount', type: 'u8' }],
- discriminator: [246, 28, 6, 87, 251, 45, 50, 42],
- name: 'mintTokens',
- },
- generics,
- );
- expect(node).toEqual(
- instructionNode({
- accounts: [
- instructionAccountNode({
- // TODO: Handle seeds with nested paths.
- // defaultValue: pdaValueNode(
- // pdaNode({
- // name: 'distribution',
- // seeds: [
- // constantPdaSeedNodeFromBytes('base16', '2a1f1d'),
- // variablePdaSeedNode('distributionGroupMint', publicKeyTypeNode()),
- // ],
- // }),
- // [],
- // ),
- isSigner: false,
- isWritable: true,
- name: 'distribution',
- }),
- ],
- arguments: [
- instructionArgumentNode({
- defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
- defaultValueStrategy: 'omitted',
- name: 'discriminator',
- type: fixedSizeTypeNode(bytesTypeNode(), 8),
- }),
- instructionArgumentNode({ name: 'amount', type: numberTypeNode('u8') }),
- ],
- discriminators: [fieldDiscriminatorNode('discriminator')],
- name: 'mintTokens',
- }),
- );
- });
- test('it creates instruction nodes with anchor discriminators', () => {
- const node = instructionNodeFromAnchorV01(
- [],
- {
- accounts: [],
- args: [],
- discriminator: [246, 28, 6, 87, 251, 45, 50, 42],
- name: 'myInstruction',
- },
- generics,
- );
- expect(node).toEqual(
- instructionNode({
- arguments: [
- instructionArgumentNode({
- defaultValue: getAnchorDiscriminatorV01([246, 28, 6, 87, 251, 45, 50, 42]),
- defaultValueStrategy: 'omitted',
- name: 'discriminator',
- type: fixedSizeTypeNode(bytesTypeNode(), 8),
- }),
- ],
- discriminators: [fieldDiscriminatorNode('discriminator')],
- name: 'myInstruction',
- }),
- );
- });
|