{"version":3,"file":"logic-error.mjs","names":[],"sources":["../../src/types/logic-error.ts"],"sourcesContent":["const LOGIC_ERROR = /transaction ([A-Z0-9]+): logic eval error: (.*). Details: .*pc=([0-9]+).*/\n// inner tx 0 failed: logic eval error: err opcode executed. Details: app=6248, pc=29, opcodes=tx\nconst INNER_LOGIC_ERROR = /inner tx (\\d+) failed:.*?pc=([0-9]+)/\n\n/**\n * Details about a smart contract logic error\n */\nexport interface LogicErrorDetails {\n  /** The ID of the transaction with the logic error */\n  txId: string\n  /** The program counter where the error was */\n  pc: number\n  /** The error message */\n  msg: string\n  /** The full error description */\n  desc: string\n  /** Any trace information included in the error */\n  traces: Record<string, unknown>[]\n}\n\n/** Wraps key functionality around processing logic errors */\nexport class LogicError extends Error {\n  /** Takes an error message and parses out the details of any logic errors in there.\n   * @param error The error message to parse\n   * @returns The logic error details if any, or undefined\n   */\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  static parseLogicError(error: any): LogicErrorDetails | undefined {\n    const errorMessage = error.message\n    const res = LOGIC_ERROR.exec(errorMessage)\n    const innerRes = INNER_LOGIC_ERROR.exec(errorMessage)\n\n    if (res === null || res.length <= 3) return undefined\n\n    return {\n      txId: res[1],\n      msg: res[2],\n      desc: errorMessage,\n      pc: parseInt(innerRes?.[2] ?? res[3] ?? '0'),\n      traces: error.traces,\n    } as LogicErrorDetails\n  }\n\n  public led: LogicErrorDetails\n  public program: string[]\n  public lines = 5\n  public teal_line = 0\n  override stack?: string\n\n  /**\n   * Create a new logic error object.\n   * @param errorDetails The details of the logic error\n   * @param program The TEAL source code, split by line\n   * @param getLineForPc The source map of the TEAL source code\n   */\n  constructor(errorDetails: LogicErrorDetails, program: string[], getLineForPc: (pc: number) => number | undefined) {\n    super()\n    this.led = errorDetails\n    this.program = program\n\n    const line = getLineForPc(errorDetails.pc)\n    this.teal_line = line === undefined ? 0 : line\n\n    this.message = `${this.led.msg}. at:${line}. ${this.led.desc}`\n\n    if (this.teal_line > 0) {\n      const start = this.teal_line > this.lines ? this.teal_line - this.lines : 0\n      const stop = program.length > this.teal_line + this.lines ? this.teal_line + this.lines : program.length\n\n      const stack_lines = program.slice(start, stop)\n\n      stack_lines[stack_lines.length / 2] += ' <--- Error'\n\n      this.stack = stack_lines.join('\\n')\n    }\n  }\n}\n"],"mappings":";AAAA,MAAM,cAAc;AAEpB,MAAM,oBAAoB;;AAmB1B,IAAa,aAAb,cAAgC,MAAM;;;;;CAMpC,OAAO,gBAAgB,OAA2C;EAChE,MAAM,eAAe,MAAM;EAC3B,MAAM,MAAM,YAAY,KAAK,YAAY;EACzC,MAAM,WAAW,kBAAkB,KAAK,YAAY;EAEpD,IAAI,QAAQ,QAAQ,IAAI,UAAU,GAAG,OAAO,KAAA;EAE5C,OAAO;GACL,MAAM,IAAI;GACV,KAAK,IAAI;GACT,MAAM;GACN,IAAI,SAAS,WAAW,MAAM,IAAI,MAAM,GAAG;GAC3C,QAAQ,MAAM;EAChB;CACF;CAEA;CACA;CACA,QAAe;CACf,YAAmB;CACnB;;;;;;;CAQA,YAAY,cAAiC,SAAmB,cAAkD;EAChH,MAAM;EACN,KAAK,MAAM;EACX,KAAK,UAAU;EAEf,MAAM,OAAO,aAAa,aAAa,EAAE;EACzC,KAAK,YAAY,SAAS,KAAA,IAAY,IAAI;EAE1C,KAAK,UAAU,GAAG,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,KAAK,IAAI;EAExD,IAAI,KAAK,YAAY,GAAG;GACtB,MAAM,QAAQ,KAAK,YAAY,KAAK,QAAQ,KAAK,YAAY,KAAK,QAAQ;GAC1E,MAAM,OAAO,QAAQ,SAAS,KAAK,YAAY,KAAK,QAAQ,KAAK,YAAY,KAAK,QAAQ,QAAQ;GAElG,MAAM,cAAc,QAAQ,MAAM,OAAO,IAAI;GAE7C,YAAY,YAAY,SAAS,MAAM;GAEvC,KAAK,QAAQ,YAAY,KAAK,IAAI;EACpC;CACF;AACF"}