| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import {
- accountNode,
- constantDiscriminatorNode,
- constantValueNodeFromBytes,
- instructionNode,
- programNode,
- rootNode,
- sizeDiscriminatorNode,
- } from '@codama/nodes';
- import { describe, expect, test } from 'vitest';
- import { identifyAccountData, identifyInstructionData } from '../src';
- import { hex } from './_setup';
- describe('identifyAccountData', () => {
- test('it identifies an account using its discriminator nodes', () => {
- const root = rootNode(
- programNode({
- accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myAccount' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyAccountData(root, hex('01020304'));
- expect(result).toStrictEqual([root, root.program, root.program.accounts[0]]);
- });
- test('it fails to identify accounts whose discriminator nodes do not match the given data', () => {
- const root = rootNode(
- programNode({
- accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(999)], name: 'myAccount' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyAccountData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- test('it fails to identify accounts with no discriminator nodes', () => {
- const root = rootNode(
- programNode({ accounts: [accountNode({ name: 'myAccount' })], name: 'myProgram', publicKey: '1111' }),
- );
- const result = identifyAccountData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- test('it identifies the first matching account if multiple accounts match', () => {
- const root = rootNode(
- programNode({
- accounts: [
- accountNode({
- discriminators: [sizeDiscriminatorNode(4)],
- name: 'accountA',
- }),
- accountNode({
- discriminators: [constantDiscriminatorNode(constantValueNodeFromBytes('base16', 'ff'))],
- name: 'accountB',
- }),
- ],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyAccountData(root, hex('ff010203'));
- expect(result).toStrictEqual([root, root.program, root.program.accounts[0]]);
- });
- test('it does not identify accounts in additional programs', () => {
- const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
- programNode({
- accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myAccount' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- ]);
- const result = identifyAccountData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- test('it does not identify accounts using instruction discriminators', () => {
- const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
- programNode({
- instructions: [instructionNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myInstruction' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- ]);
- const result = identifyAccountData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- });
- describe('identifyInstructionData', () => {
- test('it identifies an instruction using its discriminator nodes', () => {
- const root = rootNode(
- programNode({
- instructions: [instructionNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myInstruction' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyInstructionData(root, hex('01020304'));
- expect(result).toStrictEqual([root, root.program, root.program.instructions[0]]);
- });
- test('it fails to identify instructions whose discriminator nodes do not match the given data', () => {
- const root = rootNode(
- programNode({
- instructions: [
- instructionNode({ discriminators: [sizeDiscriminatorNode(999)], name: 'myInstruction' }),
- ],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyInstructionData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- test('it fails to identify instructions with no discriminator nodes', () => {
- const root = rootNode(
- programNode({
- instructions: [instructionNode({ name: 'myInstruction' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyInstructionData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- test('it identifies the first matching instruction if multiple instructions match', () => {
- const root = rootNode(
- programNode({
- instructions: [
- instructionNode({
- discriminators: [sizeDiscriminatorNode(4)],
- name: 'instructionA',
- }),
- instructionNode({
- discriminators: [constantDiscriminatorNode(constantValueNodeFromBytes('base16', 'ff'))],
- name: 'instructionB',
- }),
- ],
- name: 'myProgram',
- publicKey: '1111',
- }),
- );
- const result = identifyInstructionData(root, hex('ff010203'));
- expect(result).toStrictEqual([root, root.program, root.program.instructions[0]]);
- });
- test('it does not identify instructions in additional programs', () => {
- const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
- programNode({
- instructions: [instructionNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myInstruction' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- ]);
- const result = identifyInstructionData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- test('it does not identify instructions using account discriminators', () => {
- const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
- programNode({
- accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myAccount' })],
- name: 'myProgram',
- publicKey: '1111',
- }),
- ]);
- const result = identifyInstructionData(root, hex('01020304'));
- expect(result).toBeUndefined();
- });
- });
|