import { UserFriendlyErrorCodes as e, transactionErrorCodes as t } from "../../error-handling/error-codes.constants.mjs";
import { getErrorMessage as n } from "../../error-handling/get-error.message.mjs";
import "../../error-handling/index.mjs";
//#region libs/shared/common/src/functions/web3/runContractFunction.ts
var r = 5e4, i = 1, a = 11, o = 15, s = /* @__PURE__ */ function(e) {
	return e[e.Initial = 0] = "Initial", e[e.Secondary = 1] = "Secondary", e;
}(s || {}), c = async (e, t) => {
	try {
		let n = await e.getFeeData(), r = t === s.Initial ? a : o;
		return n.gasPrice ? n.gasPrice * BigInt(r) / BigInt(10) : void 0;
	} catch (e) {
		console.log("getGasPrice error: ", { err: e });
		return;
	}
}, l = (e, n, r, i) => new Promise((a, o) => {
	let s = setTimeout(() => {
		o(Error(t.TRANSACTION_TIMEOUT));
	}, i);
	e.waitForTransaction(n.hash, r).then((e) => {
		clearTimeout(s), e ? a(e) : o(/* @__PURE__ */ Error("failed to confirm tx"));
	}).catch((e) => {
		clearTimeout(s), o(e);
	});
}), u = async ({ provider: e, contractFunction: t, args: n, gasPrice: a, gasLimit: o, nonce: s, confirmations: c, waitTime: u }) => await l(e, await t(...n, {
	type: 0,
	gasLimit: o,
	gasPrice: a,
	nonce: s
}), c ?? i, u ?? r), d = async ({ provider: r, contractFunction: i, args: a, gasLimit: o, nonce: l, confirmations: d }) => {
	try {
		return await u({
			provider: r,
			contractFunction: i,
			args: a,
			gasPrice: await c(r, s.Initial),
			gasLimit: o,
			nonce: l,
			confirmations: d
		});
	} catch (f) {
		let p = n(f);
		if (p === e.MAX_FEE_GAS_ERROR || p === t.TRANSACTION_TIMEOUT) return console.log("Timeout Hit: Increase gas"), await u({
			provider: r,
			contractFunction: i,
			args: a,
			gasPrice: await c(r, s.Secondary),
			gasLimit: o,
			nonce: l,
			confirmations: d
		});
		throw f;
	}
};
//#endregion
export { d as runContractFunction };
