import BigNumber from "bignumber.js";
import { BN } from "@coral-xyz/anchor";
import { ParsedTransactionWithMeta } from "@solana/web3.js";

export class BigNumberUtil {
  public static fromBigInt(input: bigint, shift = 0): BigNumber {
    return new BigNumber(input.toString()).div(new BigNumber(10).pow(shift));
  }

  public static fromBN(input: BN, shift = 0): BigNumber {
    return new BigNumber(input.toString()).div(new BigNumber(10).pow(shift));
  }
}

const JUPITER_PROGRAM_IDS = [
  "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4",
  "JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB",
  "JUP2jxvXaqu7NQY1GmNF4m1vodw12LVXYxbFL2uJvfo",
  "JUP3c2Uh3WA4Ng34tw6kPd2G4C5BB21Xo36Je1s32Ph",
];

export const isJupiterTransaction = (
  tx: ParsedTransactionWithMeta,
): boolean => {
  if (!tx || !tx.transaction || !tx.transaction.message) return false;

  for (const innerInstructionSet of tx.meta?.innerInstructions || []) {
    for (const instruction of innerInstructionSet.instructions) {
      const programId = instruction.programId?.toString();
      if (programId && JUPITER_PROGRAM_IDS.includes(programId)) {
        return true;
      }
    }
  }

  return false;
};
