/** * QuoterFactory creates quoter V2 contracts with dynamic address resolution * * Ports logic from serverless-strategy-tx-processor to enable protocol-specific * quoter contract creation with addresses resolved from network configuration. */ import { Contract, ContractRunner } from 'ethers'; import { ProtocolDetector } from './ProtocolDetector'; import { AbiManager } from './AbiManager'; import { getQuoterV2Address } from './ProtocolConfig'; import { NetworkConfig } from './NetworkConfig'; /** * Factory for creating QuoterV2 contract instances with dynamic address resolution * * Combines protocol detection, network configuration, and ABI management to create * quoter contracts without requiring explicit addresses. * * @example * ```typescript * const factory = new QuoterFactory(networkConfig); * const quoter = factory.createQuoterV2('uniswapV3VaultBeacon', signer); * ``` */ export class QuoterFactory { private protocolDetector: ProtocolDetector; private abiManager: AbiManager; private networkConfig: NetworkConfig; /** * Create a QuoterFactory instance * * @param networkConfig - Network configuration with contract addresses * @param protocolDetector - Optional ProtocolDetector instance * @param abiManager - Optional AbiManager instance */ constructor( networkConfig: NetworkConfig, protocolDetector?: ProtocolDetector, abiManager?: AbiManager, ) { this.networkConfig = networkConfig; this.protocolDetector = protocolDetector || new ProtocolDetector(); this.abiManager = abiManager || new AbiManager(); } /** * Create a QuoterV2 contract instance with dynamic address resolution * * This method: * 1. Detects the protocol from the beacon name * 2. Resolves the quoter address from network config * 3. Selects the protocol-specific ABI * 4. Creates and returns the contract instance * * @param beaconName - The vault beacon name to determine protocol * @param signer - Contract runner for the contract * @returns ethers.Contract - QuoterV2 contract instance * @throws Error if quoter address not found in network config * * @example * ```typescript * const quoter = factory.createQuoterV2('quickSwapV3VaultBeacon', signer); * // Returns contract at QuickSwapQuoterV2 address with correct ABI * ``` */ createQuoterV2(beaconName: string, signer: ContractRunner): Contract { // Get protocol from beacon name const protocol = this.protocolDetector.getProtocol(beaconName); // Resolve quoter address from network config const quoterV2Address = getQuoterV2Address(protocol, this.networkConfig); if (!quoterV2Address) { throw new Error(`QuoterV2 address not found for protocol: ${protocol}`); } // Get protocol-specific ABI const abi = this.abiManager.getQuoterV2Abi(beaconName); // Create and return contract return new Contract(quoterV2Address, abi, signer); } }