import { safeJsonStringify as e } from "../utils/serialize.utils.mjs";
import { COMPUTE_UNIT_PADDING_PERCENTAGE as t, MAX_COMPUTE_UNIT_LIMIT as n, MIN_COMPUTE_UNIT_LIMIT as r } from "../../constants/protocol.constants.mjs";
import { AccountActions as i } from "../../data-structures/AccountActions/AccountActions.mjs";
import { SolanaPriorityFeeMode as a } from "../../types/fee.types.mjs";
import { getSolanaPriorityFee as o } from "../web3/getSolanaPriorityFee.mjs";
import { pollForValue as s } from "../utils/process.utils.mjs";
import { shouldRetryForSolanaError as c } from "../../error-handling/retrySolanaError.mjs";
import { ComputeBudgetProgram as l, PublicKey as u, TransactionMessage as d, VersionedTransaction as f } from "@solana/web3.js";
import "@solana/spl-token";
//#region libs/shared/common/src/functions/pre-transaction/sendV0Transaction.ts
var p = 5e3, m = 6e4, h = async (e, t, n = "confirmed") => await s(() => e.getTransaction(t, {
	commitment: n,
	maxSupportedTransactionVersion: 0
}), {
	timeoutMs: m,
	intervalMs: p
}) ?? null, g = async (e, i, a, o) => {
	try {
		let { blockhash: s } = await e.getLatestBlockhash(), c = new f(new d({
			payerKey: i,
			recentBlockhash: s,
			instructions: [l.setComputeUnitLimit({ units: n }), ...a]
		}).compileToV0Message(o)), { unitsConsumed: u } = (await e.simulateTransaction(c, {
			sigVerify: !1,
			replaceRecentBlockhash: !0
		})).value;
		if (!u) return n;
		let p = Math.ceil(u * (1 + t));
		return Math.max(r, Math.min(p, n));
	} catch {
		return n;
	}
}, _ = (e, t, n, r) => {
	let i = /* @__PURE__ */ new Set();
	return t && i.add(t.toBase58()), n?.forEach((e) => i.add(e.toBase58())), e.forEach((e) => {
		i.add(e.programId.toBase58()), e.keys.forEach((e) => {
			i.add(e.pubkey.toBase58());
		});
	}), r && r.forEach((e) => {
		i.add(e.key.toBase58()), e.state.addresses.forEach((e) => {
			i.add(e.toBase58());
		});
	}), [...i].map((e) => new u(e));
}, v = (t) => {
	if (!t) throw Error("Failed to fetch Solana transaction details for signature");
	if (t.meta?.err) {
		let n = e(t.meta.err);
		throw Error(`Solana transaction failed on-chain: ${n}`);
	}
}, y = async (e, t, n, r, i) => {
	let a = n.filter((e) => !e.programId.equals(l.programId)), s = await g(e, t, a, r), c = l.setComputeUnitLimit({ units: s }), u = e.rpcEndpoint.includes("helius") ? await o(_(a, t, [l.programId], r), i) : 0;
	return [
		c,
		l.setComputeUnitPrice({ microLamports: u }),
		...a
	];
}, b = async ({ connection: e, chainId: t, subAccount: n, payerPublicKey: r, instructions: o, lookupTableAccounts: s }) => {
	let l = i.getPrivateKeyFromSubAccount(t, n);
	if (!l) throw Error("Private key is not available");
	for (let t = 0; t < 10; t += 1) {
		let n = t === 9;
		try {
			let t = await y(e, r, o, s, n ? a.HIGH : a.RECOMMENDED), { blockhash: c, lastValidBlockHeight: u } = await e.getLatestBlockhash(), p = new f(new d({
				payerKey: r,
				recentBlockhash: c,
				instructions: t
			}).compileToV0Message(s)), m = await i.sendAndSignSolanaTransactionFromPrivateKey(e, l, p, !0);
			return await e.confirmTransaction({
				signature: m,
				blockhash: c,
				lastValidBlockHeight: u
			}, "confirmed"), v(await h(e, m)), m;
		} catch (e) {
			if (c(e) && !n) continue;
			throw e;
		}
	}
	throw Error("Failed to send proxy v0 transaction due to expired blockheight after all retries");
};
//#endregion
export { g as estimateComputeUnitLimit, h as fetchSolanaTransaction, _ as getAccountsFromInstructions, b as sendProxyV0Transaction };
