//#region scripts/alchemy/alchemyHelpers.ts
/**
* Retry mechanism with exponential backoff (following gnosis pattern)
*/
async function withRetry(fn, maxRetries = 3, initialDelay = 1e3, operationName = "Alchemy API call") {
	for (let attempt = 0; attempt < maxRetries; attempt++) try {
		return await fn();
	} catch (error) {
		if ((error?.message?.includes("Too Many Requests") || error?.message?.includes("429") || error?.status === 429) && attempt < maxRetries - 1) {
			const waitTime = initialDelay * Math.pow(2, attempt);
			console.log(`[${operationName}] Rate limited, retrying in ${waitTime}ms...`);
			await new Promise((resolve) => setTimeout(resolve, waitTime));
			continue;
		}
		if (attempt === maxRetries - 1) console.error(`[${operationName}] Max retries exceeded:`, error?.message);
		throw error;
	}
	throw new Error(`${operationName}: Max retries exceeded`);
}
/**
* Validate Ethereum address format
*/
function isValidEthereumAddress(address) {
	return /^0x[a-fA-F0-9]{40}$/.test(address);
}
/**
* Convert Alchemy transaction to document model format
*/
function convertToTransactionEntry(transfer, userAddress, blockTimestamp) {
	let value;
	if (transfer.category === "erc20" && transfer.rawContract.decimal) {
		const decimals = parseInt(transfer.rawContract.decimal, 16);
		value = (parseInt(transfer.rawContract.value, 16) / Math.pow(10, decimals)).toString();
	} else value = transfer.value?.toString() || transfer.rawContract.value || "0";
	const token = transfer.asset || "ETH";
	const userAddressLower = userAddress.toLowerCase();
	const fromAddressLower = transfer.from.toLowerCase();
	const toAddressLower = transfer.to.toLowerCase();
	let direction;
	let counterParty;
	if (toAddressLower === userAddressLower) {
		direction = "INFLOW";
		counterParty = transfer.from;
	} else if (fromAddressLower === userAddressLower) {
		direction = "OUTFLOW";
		counterParty = transfer.to;
	} else {
		direction = "OUTFLOW";
		counterParty = transfer.from;
	}
	const datetime = blockTimestamp ? new Date(blockTimestamp).toISOString() : (/* @__PURE__ */ new Date()).toISOString();
	const transactionDate = new Date(blockTimestamp || Date.now());
	if (!direction) throw new Error(`Direction is undefined for transaction ${transfer.hash}. From: ${transfer.from}, To: ${transfer.to}, User: ${userAddress}`);
	if (!transfer.from) throw new Error(`From address is undefined for transaction ${transfer.hash}`);
	if (!transfer.to) throw new Error(`To address is undefined for transaction ${transfer.hash}`);
	return {
		uniqueId: transfer.uniqueId,
		counterParty,
		amount: {
			unit: token,
			value
		},
		txHash: transfer.hash,
		token,
		blockNumber: parseInt(transfer.blockNum, 16),
		datetime,
		accountingPeriod: transactionDate.getFullYear().toString(),
		from: transfer.from,
		to: transfer.to,
		direction
	};
}
/**
* Validate environment variables
*/
function validateEnvironment() {
	const apiKey = process.env.ALCHEMY_API_KEY;
	if (!apiKey) throw new Error("ALCHEMY_API_KEY environment variable is not set");
	return {
		apiKey,
		network: process.env.ALCHEMY_NETWORK || "mainnet"
	};
}
//#endregion
export { withRetry as i, isValidEthereumAddress as n, validateEnvironment as r, convertToTransactionEntry as t };

//# sourceMappingURL=alchemyHelpers-CvNseqvu.js.map