All files / src instruction-classifier.ts

95.45% Statements 21/22
66.66% Branches 4/6
100% Functions 9/9
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x       1x 1x   1x 1x         1x 6x 6x               1x 1x 1x 2x 8x 8x                       14x   14x 14x 14x       1x       8x      
import { SYSTEM_PROGRAMS } from './constants';
import { TransactionAdapter } from './transaction-adapter';
import { ClassifiedInstruction } from './types/common';
 
export class InstructionClassifier {
  private instructionMap: Map<string, ClassifiedInstruction[]> = new Map();
 
  constructor(private adapter: TransactionAdapter) {
    this.classifyInstructions();
  }
 
  private classifyInstructions() {
    // outer instructions
    this.adapter.instructions.forEach((instruction: any, outerIndex: any) => {
      const programId = this.adapter.getInstructionProgramId(instruction);
      this.addInstruction({
        instruction,
        programId,
        outerIndex,
      });
    });
 
    // innerInstructions
    const innerInstructions = this.adapter.innerInstructions;
    if (innerInstructions) {
      innerInstructions.forEach((set) => {
        set.instructions.forEach((instruction, innerIndex) => {
          const programId = this.adapter.getInstructionProgramId(instruction);
          this.addInstruction({
            instruction,
            programId,
            outerIndex: set.index,
            innerIndex,
          });
        });
      });
    }
  }
 
  private addInstruction(classified: ClassifiedInstruction) {
    Iif (!classified.programId) return;
 
    const instructions = this.instructionMap.get(classified.programId) || [];
    instructions.push(classified);
    this.instructionMap.set(classified.programId, instructions);
  }
 
  public getInstructions(programId: string): ClassifiedInstruction[] {
    return this.instructionMap.get(programId) || [];
  }
 
  public getAllProgramIds(): string[] {
    return Array.from(this.instructionMap.keys()).filter((it) => !SYSTEM_PROGRAMS.includes(it));
  }
}