import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import { zeroAddress as t } from "../../constants/protocol.constants.mjs";
import { ContractType as n } from "../../types/ethereum-network.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as r } from "../../error-handling/error-codes.constants.mjs";
import { getAmountInWei as i } from "./etherFunctions.mjs";
import { logError as a } from "../../error-handling/logError.mjs";
import "../../error-handling/index.mjs";
import "../../constants/index.mjs";
import { ethers as o } from "ethers";
//#region libs/shared/common/src/functions/web3/uniswapAPI.ts
var s = async (e, n, i, a) => {
	let o = (await Promise.all([
		100,
		500,
		3e3,
		1e4
	].map(async (e) => ({
		poolAddress: await n.getPool(i, a, e),
		fee: e
	})))).filter(({ poolAddress: e }) => e !== t), s = await Promise.all(o.map(async (t) => ({
		poolAddress: t.poolAddress,
		fee: t.fee,
		token1Balance: await e.balanceOf(t.poolAddress)
	}))), c = s.map((e) => e.token1Balance).reduce((e, t) => t >= e ? t : e, 0n);
	if (c === 0n) throw Error(r.UNISWAP_NOT_ENOUGH_LIQUIDITY);
	return s.find((e) => e.token1Balance === c);
}, c = async (t, r, i, o, s, c) => {
	try {
		if (!e[r].quoterV2Address) throw Error("No Quoter Contract Provided");
		return (await t.getContractWithFetcherByChainId(r, n.UniswapV3QuoterContract, e[r].quoterV2Address).quoteExactInputSingle.staticCall({
			tokenIn: i.wrappedErc20TokenAddress ?? i.erc20TokenAddress,
			tokenOut: o.wrappedErc20TokenAddress ?? o.erc20TokenAddress,
			fee: s,
			amountIn: c,
			sqrtPriceLimitX96: 0
		})).amountOut;
	} catch (e) {
		throw a(e), e;
	}
}, l = async (t, r, i, a) => {
	try {
		let o = t.getContractWithFetcherByChainId(r, n.UniswapV3FactoryContract, e[r].uniswapV3FactoryAddress), { fee: c } = await s(t.getContractWithFetcherByChainId(r, n.ERC20Contract, a.wrappedErc20TokenAddress ?? a.erc20TokenAddress), o, i.wrappedErc20TokenAddress ?? i.erc20TokenAddress, a.wrappedErc20TokenAddress ?? a.erc20TokenAddress);
		return c;
	} catch (e) {
		throw console.log("Error in getUniswapFee", e), e;
	}
}, u = async (e, t, n, a, s) => {
	try {
		let r = await l(e, t, a, s), u = o.AbiCoder.defaultAbiCoder().encode(["uint24"], [r]);
		return {
			tokenPrice: await c(e, t, a, s, r, i(a, n)),
			poolFee: u
		};
	} catch {
		throw Error(r.NO_UNISWAP_PRICE);
	}
};
//#endregion
export { l as getUniswapFee, u as getUniswapPrice, c as getUniswapPriceHelper, s as searchPoolAndFee };
