"use strict"; /** * Polygon (EVM) blockchain integration via JSON-RPC. * USDC Transfer event scanning and outbound ERC-20 transfers. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getCurrentBlock = getCurrentBlock; exports.getUsdcTransfersToAddress = getUsdcTransfersToAddress; exports.sendUsdc = sendUsdc; exports.getNativeBalanceWei = getNativeBalanceWei; exports.getUsdcBalanceRaw = getUsdcBalanceRaw; exports.sendMatic = sendMatic; const ethers_1 = require("ethers"); const config_1 = require("../../config"); const ERC20_ABI = [ 'function transfer(address to, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 value)', ]; const RPC_TIMEOUT_MS = 60000; // 60s for slow public RPCs const MIN_GETLOGS_BLOCK_RANGE = 10; /** * Returns true when an RPC error indicates the eth_getLogs block window is too large. * @param err Caught provider/ethers error */ function isBlockRangeLimitError(err) { const parts = [ err?.message, err?.shortMessage, err?.error?.message, ]; return parts.some((m) => typeof m === 'string' && /block range exceeds/i.test(m)); } let provider = null; /** * Returns a cached JsonRpcProvider with configured timeout for Polygon RPC. * @returns Ethers provider for config.blockchain.polygon.rpcUrl */ function getProvider() { if (!provider) { const req = new ethers_1.ethers.FetchRequest(config_1.config.blockchain.polygon.rpcUrl); req.timeout = RPC_TIMEOUT_MS; provider = new ethers_1.ethers.JsonRpcProvider(req); } return provider; } /** * Returns the current Polygon block number. * @returns Latest block height */ async function getCurrentBlock() { const p = getProvider(); return p.getBlockNumber(); } /** * Queries USDC Transfer events to an address within a block range (chunked for RPC limits). * @param toAddress Recipient address (0x-prefixed or raw hex) * @param fromBlock Inclusive start block * @param maxEndBlock Optional inclusive end block cap (defaults to current block) * @returns Matching transfers with tx hash, parties, value, and block number */ async function getUsdcTransfersToAddress(toAddress, fromBlock, maxEndBlock) { const p = getProvider(); const contract = new ethers_1.ethers.Contract(config_1.config.blockchain.polygon.usdcContract, ERC20_ABI, p); const toNormalized = ethers_1.ethers.getAddress(toAddress.startsWith('0x') ? toAddress : '0x' + toAddress); const filter = contract.filters.Transfer(null, toNormalized); const currentBlock = await p.getBlockNumber(); const endCap = maxEndBlock != null ? Math.min(maxEndBlock, currentBlock) : currentBlock; const out = []; let chunkSize = config_1.config.blockchain.polygon.getLogsBlockRange; for (let start = fromBlock; start <= endCap;) { let chunk = chunkSize; while (true) { const end = Math.min(start + chunk - 1, endCap); try { const events = await contract.queryFilter(filter, start, end); for (const e of events) { const args = 'args' in e ? e.args : undefined; if (args && e.blockNumber !== null) { out.push({ txHash: e.transactionHash, from: String(args[0]), to: String(args[1]), value: BigInt(args[2]), blockNumber: e.blockNumber, }); } } start = end + 1; break; } catch (err) { if (isBlockRangeLimitError(err) && chunk > MIN_GETLOGS_BLOCK_RANGE) { chunk = Math.max(MIN_GETLOGS_BLOCK_RANGE, Math.floor(chunk / 2)); continue; } throw err; } } } return out; } /** * Sends USDC on Polygon from a wallet private key. * @param fromPrivateKeyHex Sender private key (with or without 0x prefix) * @param toAddress Recipient checksummed or hex address * @param amountRaw USDC amount in smallest unit (6 decimals) * @returns Confirmed transaction hash from receipt */ async function sendUsdc(fromPrivateKeyHex, toAddress, amountRaw) { const p = getProvider(); const wallet = new ethers_1.ethers.Wallet(fromPrivateKeyHex.startsWith('0x') ? fromPrivateKeyHex : '0x' + fromPrivateKeyHex, p); const contract = new ethers_1.ethers.Contract(config_1.config.blockchain.polygon.usdcContract, ERC20_ABI, wallet); const tx = await contract.transfer(toAddress, amountRaw); const receipt = await tx.wait(); return receipt.hash; } /** Returns native MATIC balance in wei. */ async function getNativeBalanceWei(address) { const p = getProvider(); return p.getBalance(address); } /** Returns USDC balance in smallest units for an address. */ async function getUsdcBalanceRaw(address) { const p = getProvider(); const contract = new ethers_1.ethers.Contract(config_1.config.blockchain.polygon.usdcContract, ['function balanceOf(address) view returns (uint256)'], p); const balance = await contract.balanceOf(address); return balance; } /** Sends native MATIC for gas sponsorship. */ async function sendMatic(fromPrivateKeyHex, toAddress, amountWei) { const p = getProvider(); const wallet = new ethers_1.ethers.Wallet(fromPrivateKeyHex.startsWith('0x') ? fromPrivateKeyHex : '0x' + fromPrivateKeyHex, p); const tx = await wallet.sendTransaction({ to: toAddress, value: amountWei }); const receipt = await tx.wait(); return receipt.hash; } //# sourceMappingURL=polygonService.js.map