import { al as m, x as C, a as T, V as v, D as E, z as F } from "./unsignedTransaction-DCDxTFq8.mjs";
import { n as y, c as I, k as M } from "./index-hyk_rzOj.mjs";
import { d as x, a as b } from "./signer-0hWrZ5BZ.mjs";
import { d as A } from "./platform-Cil_UtAQ.mjs";
function O(e) {
  return e.includes("rpcpool.com") ? "triton" : e.includes("helius-rpc.com") ? "helius" : e.includes("rpc.ankr.com") ? "ankr" : "unknown";
}
async function j(e, t, l, c) {
  const s = c.transaction.transaction, u = (a) => a.programId.toString() !== "ComputeBudget111111111111111111111111111111";
  if (m(s)) {
    const a = (await Promise.all(
      s.message.addressTableLookups.map(
        (r) => e.getAddressLookupTable(r.accountKey)
      )
    )).map((r) => r.value).filter((r) => r !== null), o = C.decompile(s.message, {
      addressLookupTableAccounts: a
    });
    o.recentBlockhash = t, s.message.recentBlockhash = t, o.instructions = o.instructions.filter(u), s.message = o.compileToV0Message(a), o.instructions.push(
      ...await w(e, s)
    ), s.message = o.compileToV0Message(a), s.sign(c.transaction.signers ?? []);
  } else
    s.recentBlockhash = t, s.lastValidBlockHeight = l, s.instructions = s.instructions.filter(
      u
    ), s.add(
      ...await w(e, s)
    ), c.transaction.signers && s.partialSign(...c.transaction.signers);
  return s;
}
async function w(e, t, l = "confirmed") {
  let c = 2e5, s = 0;
  const u = T.setComputeUnitLimit({
    units: 3e5
  });
  let a;
  if (m(t)) {
    const n = (await Promise.all(
      t.message.addressTableLookups.map(
        (d) => e.getAddressLookupTable(d.accountKey)
      )
    )).map((d) => d.value).filter((d) => d !== null), i = C.decompile(t.message, {
      addressLookupTableAccounts: n
    });
    i.instructions = [...i.instructions, u], a = new v(i.compileToV0Message(n)), a.signatures = [...t.signatures];
  } else {
    const n = new E();
    n.recentBlockhash = t.recentBlockhash, n.feePayer = t.feePayer, n.instructions = [...t.instructions, u], n.signatures = [...t.signatures], a = n;
  }
  e: for (; ; ) {
    if (m(a) && !a.message.recentBlockhash) {
      const { blockhash: i } = await e.getLatestBlockhash(l);
      a.message.recentBlockhash = i;
    }
    const n = await (m(a) ? e.simulateTransaction(a, {
      commitment: l,
      replaceRecentBlockhash: !0
    }) : e.simulateTransaction(a));
    if (n.value.err) {
      if (V(n.value)) {
        if (s < 5) {
          s++, await y(1e3);
          continue e;
        }
      } else if (s < 3) {
        s++, await y(1e3);
        continue e;
      }
      throw new Error(
        `Simulation failed: ${JSON.stringify(n.value.err)}
Logs:
${(n.value.logs || []).join(`
  `)}`
      );
    } else {
      n.value.unitsConsumed && (c = n.value.unitsConsumed);
      break;
    }
  }
  const o = Math.floor(c * 1.2), r = [];
  r.push(
    T.setComputeUnitLimit({
      // Set compute budget to 120% of the units used in the simulated transaction
      units: o
    })
  );
  const P = I.transactionSettings?.Solana?.priorityFee || {}, {
    percentile: g = 0.9,
    percentileMultiple: f = 1,
    min: p = 1e5,
    max: S = 1e8
  } = P, L = async (n) => {
    if (n === "triton")
      try {
        return {
          fee: await x(
            e,
            t,
            g,
            f,
            p,
            S
          ),
          methodUsed: "triton"
        };
      } catch (i) {
        console.warn("Failed to determine priority fee using Triton RPC:", i);
      }
    try {
      return {
        fee: await b(
          e,
          t,
          g,
          f,
          p,
          S
        ),
        methodUsed: "default"
      };
    } catch (i) {
      return console.warn("Failed to determine priority fee using Triton RPC:", i), {
        fee: p,
        methodUsed: "minimum"
      };
    }
  }, k = O(e.rpcEndpoint), { fee: h, methodUsed: B } = await L(k), N = h / // convert microlamports to lamports
  1e6 / // convert lamports to SOL
  F * // multiply by maximum compute units used
  o;
  return console.table({
    "RPC Provider": k,
    "Method used": B,
    "Percentile used": g,
    "Multiple used": f,
    "Compute budget": o,
    "Priority fee": h,
    "Max fee in SOL": N
  }), r.push(
    T.setComputeUnitPrice({ microLamports: h })
  ), r;
}
function V(e) {
  const t = {};
  if (e.err === "BlockhashNotFound" && (t.BlockhashNotFound = "Blockhash not found during simulation. Trying again."), e.logs)
    for (const l of e.logs)
      l.includes("SlippageToleranceExceeded") && (t.SlippageToleranceExceeded = "Slippage failure during simulation. Trying again."), l.includes("RequireGteViolated") && (t.RequireGteViolated = "Swap instruction failure during simulation. Trying again.");
  return M(t) ? !1 : (console.table(t), !0);
}
function D(e) {
  return A.has(e) && A.get(e) === "Solana";
}
const K = "solana:signAndSendTransaction", G = "solana:signIn", $ = "solana:signMessage", z = "solana:signTransaction", J = "standard:connect", Q = "standard:disconnect", W = "standard:events", X = "solana:mainnet", Y = "solana:devnet", Z = "solana:testnet", q = "solana:localnet";
export {
  K as S,
  z as a,
  $ as b,
  G as c,
  W as d,
  Q as e,
  J as f,
  X as g,
  Y as h,
  D as i,
  Z as j,
  q as k,
  j as s
};
