/** * Example usage of CustomRouterFactory with protocol-specific parameters * * This file demonstrates various ways to use the factory with different protocols */ import { ethers } from 'ethers'; import { CustomRouterFactory, Logger, NoopLogger, CreateCustomRouterParams, ProtocolSpecificParams } from '../src/main'; // ============================================================================ // Example 1: Basic Usage with Algebra Integral v1.9 and Blackhole Deployer // ============================================================================ async function example1_AlgebraIntegral19WithBlackholeDeployer() { // Initialize your logger (or use NoopLogger for testing) const logger: Logger = NoopLogger; // Initialize the factory const factory = new CustomRouterFactory(logger); // Your contracts (these would be actual contract instances in real usage) const quoterV2 = {} as ethers.Contract; // Replace with actual QuoterV2 contract const poolObj = {} as ethers.Contract; // Replace with actual Pool contract // Create router with Blackhole deployer address const router = factory.createCustomRouter({ beaconName: "AlgebraIntegral_v1_9_Blackhole", quoterV2: quoterV2, poolObj: poolObj, protocolParams: { algebraIntegral19Deployer: "0x1234567890123456789012345678901234567890" } }); console.log("Router created successfully:", router); } // ============================================================================ // Example 2: Using Environment Variables for Configuration // ============================================================================ async function example2_EnvironmentBasedConfiguration() { const logger: Logger = NoopLogger; const factory = new CustomRouterFactory(logger); // Get configuration from environment const BLACKHOLE_DEPLOYER = process.env.BLACKHOLE_DEPLOYER_ADDRESS; const beaconName = process.env.BEACON_NAME || "AlgebraIntegral_v1_9"; const quoterV2 = {} as ethers.Contract; const poolObj = {} as ethers.Contract; // Conditionally include protocol params const router = factory.createCustomRouter({ beaconName, quoterV2, poolObj, protocolParams: BLACKHOLE_DEPLOYER ? { algebraIntegral19Deployer: BLACKHOLE_DEPLOYER } : undefined }); console.log("Router created with environment config:", router); } // ============================================================================ // Example 3: Multiple Vaults with Different Configurations // ============================================================================ interface VaultConfiguration { beaconName: string; quoterV2Address: string; poolAddress: string; deployerAddress?: string; } async function example3_MultipleVaults( provider: ethers.providers.Provider, vaultConfigs: VaultConfiguration[] ) { const logger: Logger = NoopLogger; const factory = new CustomRouterFactory(logger); const routers = await Promise.all( vaultConfigs.map(async (config) => { // Initialize contracts const quoterV2 = new ethers.Contract( config.quoterV2Address, [], // ABI would go here provider ); const poolObj = new ethers.Contract( config.poolAddress, [], // ABI would go here provider ); // Build protocol params const protocolParams: ProtocolSpecificParams | undefined = config.deployerAddress ? { algebraIntegral19Deployer: config.deployerAddress } : undefined; // Create router return factory.createCustomRouter({ beaconName: config.beaconName, quoterV2, poolObj, protocolParams }); }) ); console.log(`Created ${routers.length} routers`); return routers; } // ============================================================================ // Example 4: Factory Pattern with Custom Logger // ============================================================================ class VaultRouterManager { private factory: CustomRouterFactory; private provider: ethers.providers.Provider; constructor(logger: Logger, provider: ethers.providers.Provider) { this.factory = new CustomRouterFactory(logger); this.provider = provider; } async createRouterForVault( beaconName: string, quoterV2Address: string, poolAddress: string, options?: { deployerAddress?: string; } ) { // Initialize contracts const quoterV2 = new ethers.Contract(quoterV2Address, [], this.provider); const poolObj = new ethers.Contract(poolAddress, [], this.provider); // Prepare params const params: CreateCustomRouterParams = { beaconName, quoterV2, poolObj, protocolParams: options?.deployerAddress ? { algebraIntegral19Deployer: options.deployerAddress } : undefined }; // Create and return router return this.factory.createCustomRouter(params); } async createBlackholeRouter( quoterV2Address: string, poolAddress: string, blackholeDeployerAddress: string ) { return this.createRouterForVault( "AlgebraIntegral_v1_9_Blackhole", quoterV2Address, poolAddress, { deployerAddress: blackholeDeployerAddress } ); } } async function example4_FactoryPattern() { const provider = new ethers.providers.JsonRpcProvider("https://..."); const logger: Logger = NoopLogger; const manager = new VaultRouterManager(logger, provider); // Create Blackhole router const blackholeRouter = await manager.createBlackholeRouter( "0xQuoterAddress...", "0xPoolAddress...", "0xBlackholeDeployer..." ); // Create generic vault router const genericRouter = await manager.createRouterForVault( "UniswapV3", "0xQuoterAddress...", "0xPoolAddress..." ); console.log("Routers created:", { blackholeRouter, genericRouter }); } // ============================================================================ // Example 5: Legacy Compatibility - Old API Still Works // ============================================================================ async function example5_LegacyCompatibility() { const logger: Logger = NoopLogger; const factory = new CustomRouterFactory(logger); const quoterV2 = {} as ethers.Contract; const poolObj = {} as ethers.Contract; const beaconName = "UniswapV3"; // Old way - still works! const router1 = factory.createCustomRouter(beaconName, quoterV2, poolObj); // Old way with protocol params as 4th argument const router2 = factory.createCustomRouter( beaconName, quoterV2, poolObj, { algebraIntegral19Deployer: "0x..." } ); console.log("Legacy API still works:", { router1, router2 }); } // ============================================================================ // Example 6: Type-Safe Configuration Object // ============================================================================ async function example6_TypeSafeConfiguration() { const logger: Logger = NoopLogger; const factory = new CustomRouterFactory(logger); // Define configuration with full type safety const config: CreateCustomRouterParams = { beaconName: "AlgebraIntegral_v1_9_Blackhole", quoterV2: {} as ethers.Contract, poolObj: {} as ethers.Contract, protocolParams: { algebraIntegral19Deployer: "0x1234567890123456789012345678901234567890" } }; // TypeScript will ensure all required fields are present const router = factory.createCustomRouter(config); console.log("Type-safe router created:", router); } // ============================================================================ // Example 7: Conditional Protocol Parameters // ============================================================================ function buildProtocolParams( vaultType: string, deployerAddress?: string ): ProtocolSpecificParams | undefined { // Only include params if they're relevant for this vault type if (vaultType.includes("AlgebraIntegral_v1_9") && deployerAddress) { return { algebraIntegral19Deployer: deployerAddress }; } // Add more conditions for other protocols as needed // if (vaultType.includes("NewProtocol")) { // return { newProtocolParam: "value" }; // } return undefined; } async function example7_ConditionalParams() { const logger: Logger = NoopLogger; const factory = new CustomRouterFactory(logger); const vaultConfigs = [ { type: "AlgebraIntegral_v1_9_Blackhole", deployer: "0x123..." }, { type: "UniswapV3", deployer: undefined } ]; for (const config of vaultConfigs) { const router = factory.createCustomRouter({ beaconName: config.type, quoterV2: {} as ethers.Contract, poolObj: {} as ethers.Contract, protocolParams: buildProtocolParams(config.type, config.deployer) }); console.log(`Router created for ${config.type}:`, router); } } // ============================================================================ // Export examples for testing/usage // ============================================================================ export { example1_AlgebraIntegral19WithBlackholeDeployer, example2_EnvironmentBasedConfiguration, example3_MultipleVaults, example4_FactoryPattern, example5_LegacyCompatibility, example6_TypeSafeConfiguration, example7_ConditionalParams, VaultRouterManager };