| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- import {
- accountValueNode,
- instructionAccountNode,
- instructionArgumentNode,
- instructionNode,
- numberTypeNode,
- pdaSeedValueNode,
- pdaValueNode,
- publicKeyTypeNode,
- } from '@codama/nodes';
- import { expect, test } from 'vitest';
- import { getResolvedInstructionInputsVisitor, visit } from '../src';
- test('it returns all instruction accounts in order of resolution', () => {
- // Given the following instruction node with an account that defaults to another account.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- defaultValue: accountValueNode('authority'),
- isSigner: true,
- isWritable: false,
- name: 'owner',
- }),
- instructionAccountNode({
- isSigner: true,
- isWritable: false,
- name: 'authority',
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the accounts to be in order of resolution.
- expect(result).toEqual([
- {
- ...node.accounts[1],
- dependsOn: [],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: true,
- },
- {
- ...node.accounts[0],
- dependsOn: [accountValueNode('authority')],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: true,
- },
- ]);
- });
- test('it sets the resolved signer to either when a non signer defaults to a signer account', () => {
- // Given the following instruction node such that a non signer account defaults to a signer account.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- defaultValue: accountValueNode('authority'),
- isSigner: false,
- isWritable: false,
- name: 'owner',
- }),
- instructionAccountNode({
- isSigner: true,
- isWritable: false,
- name: 'authority',
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the resolved signer to be either for the non signer account.
- expect(result[1]).toEqual({
- ...node.accounts[0],
- dependsOn: [accountValueNode('authority')],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: 'either',
- });
- });
- test('it sets the resolved signer to either when a signer defaults to a non signer account', () => {
- // Given the following instruction node such that a signer account defaults to a non signer account.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- defaultValue: accountValueNode('authority'),
- isSigner: true,
- isWritable: false,
- name: 'owner',
- }),
- instructionAccountNode({
- isSigner: false,
- isWritable: false,
- name: 'authority',
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the resolved signer to be either for the signer account.
- expect(result[1]).toEqual({
- ...node.accounts[0],
- dependsOn: [accountValueNode('authority')],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: 'either',
- });
- });
- test('it includes instruction data arguments with default values', () => {
- // Given the following instruction node with two arguments such that:
- // - The first argument defaults to an account.
- // - The second argument has no default value.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- isSigner: true,
- isWritable: false,
- name: 'owner',
- }),
- ],
- arguments: [
- instructionArgumentNode({
- defaultValue: accountValueNode('owner'),
- name: 'ownerArg',
- type: publicKeyTypeNode(),
- }),
- instructionArgumentNode({
- name: 'argWithoutDefaults',
- type: numberTypeNode('u8'),
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the following inputs.
- expect(result).toEqual([
- {
- ...node.accounts[0],
- dependsOn: [],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: true,
- },
- {
- ...node.arguments[0],
- dependsOn: [accountValueNode('owner')],
- },
- ]);
- // And the argument without default value is not included.
- expect(result.some(input => input.name === 'argWithoutDefaults')).toBe(false);
- });
- test('it includes instruction extra arguments with default values', () => {
- // Given the following instruction node with two extra arguments such that:
- // - The first argument defaults to an account.
- // - The second argument has no default value.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- isSigner: true,
- isWritable: false,
- name: 'owner',
- }),
- ],
- extraArguments: [
- instructionArgumentNode({
- defaultValue: accountValueNode('owner'),
- name: 'ownerArg',
- type: publicKeyTypeNode(),
- }),
- instructionArgumentNode({
- name: 'argWithoutDefaults',
- type: numberTypeNode('u8'),
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the following inputs.
- expect(result).toEqual([
- {
- ...node.accounts[0],
- dependsOn: [],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: true,
- },
- {
- ...node.extraArguments![0],
- dependsOn: [accountValueNode('owner')],
- },
- ]);
- // And the argument without default value is not included.
- expect(result.some(input => input.name === 'argWithoutDefaults')).toBe(false);
- });
- test('it returns an empty array for empty instructions', () => {
- // Given the following empty instruction node.
- const node = instructionNode({ name: 'myInstruction' });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect an empty array.
- expect(result).toEqual([]);
- });
- test('it resolves the seeds of a PdaValueNode first', () => {
- // Given the following instruction node with an account that defaults to another account.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- defaultValue: pdaValueNode('counter', [pdaSeedValueNode('authority', accountValueNode('payer'))]),
- isSigner: false,
- isWritable: false,
- name: 'counter',
- }),
- instructionAccountNode({
- isSigner: true,
- isWritable: false,
- name: 'payer',
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the accounts to be in order of resolution.
- expect(result).toEqual([
- {
- ...node.accounts[1],
- dependsOn: [],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: true,
- },
- {
- ...node.accounts[0],
- dependsOn: [accountValueNode('payer')],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: false,
- },
- ]);
- });
- test('it resolves the program id of a PdaValueNode first', () => {
- // Given the following instruction node with an account that defaults to another account.
- const node = instructionNode({
- accounts: [
- instructionAccountNode({
- defaultValue: pdaValueNode('counter', [], accountValueNode('counterProgram')),
- isSigner: false,
- isWritable: false,
- name: 'counter',
- }),
- instructionAccountNode({
- isSigner: false,
- isWritable: false,
- name: 'counterProgram',
- }),
- ],
- name: 'myInstruction',
- });
- // When we get its resolved inputs.
- const result = visit(node, getResolvedInstructionInputsVisitor());
- // Then we expect the accounts to be in order of resolution.
- expect(result).toEqual([
- {
- ...node.accounts[1],
- dependsOn: [],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: false,
- },
- {
- ...node.accounts[0],
- dependsOn: [accountValueNode('counterProgram')],
- isPda: false,
- resolvedIsOptional: false,
- resolvedIsSigner: false,
- },
- ]);
- });
|