/** * Example: Using the networks configuration with @steerprotocol/sdk integration * * This example demonstrates how to use the new TypeScript-based network configuration * that integrates with Chain and ChainId from @steerprotocol/sdk. */ import { Chain, ChainId } from '@steerprotocol/sdk'; import { networks, getNetworkByChainId, getNetworkByChain, NetworkConfig, NetworkContract } from '../src/config/networks'; import { getNetworkConfig } from '../src/helpers/NetworkConfig'; // Type guard to check if a value is a NetworkContract function isNetworkContract(value: any): value is NetworkContract { return typeof value === 'object' && value !== null && 'address' in value; } // Example 1: Direct access to networks console.log('Example 1: Direct network access'); const arbitrumConfig = networks.arbitrum; console.log('Arbitrum chainId:', arbitrumConfig.chainId); console.log('Arbitrum chain:', arbitrumConfig.chain); if (isNetworkContract(arbitrumConfig.QuoterV2)) { console.log('Arbitrum QuoterV2:', arbitrumConfig.QuoterV2.address); } // Example 2: Using the helper function (case-insensitive) console.log('\nExample 2: Using getNetworkConfig helper'); const polygonConfig = getNetworkConfig(Chain.Polygon); // Case-insensitive console.log('Polygon chainId:', polygonConfig.chainId); console.log('Polygon chain:', polygonConfig.chain); // Example 3: Get network by chainId console.log('\nExample 3: Get network by chainId'); const networkByChainId = getNetworkByChainId(42161); if (networkByChainId) { console.log('Network with chainId 42161:', networkByChainId.chain); } // Example 4: Get network by Chain enum console.log('\nExample 4: Get network by Chain enum'); const networkByChain = getNetworkByChain(Chain.Base); if (networkByChain) { console.log('Base network chainId:', networkByChain.chainId); if (isNetworkContract(networkByChain.QuoterV2)) { console.log('Base QuoterV2:', networkByChain.QuoterV2.address); } } // Example 5: Type-safe access with TypeScript console.log('\nExample 5: Type-safe configuration'); function getQuoterAddress(networkName: Chain): string | undefined { const config: NetworkConfig = getNetworkConfig(networkName); if (isNetworkContract(config.QuoterV2)) { return config.QuoterV2.address; } return undefined; } const quoterAddress = getQuoterAddress(Chain.Optimism); console.log('Optimism QuoterV2 address:', quoterAddress); // Example 6: Using SDK ChainId constants console.log('\nExample 6: Using SDK ChainId constants'); console.log('ChainId.Arbitrum:', ChainId.Arbitrum); console.log('ChainId.Polygon:', ChainId.Polygon); console.log('ChainId.Base:', ChainId.Base); // Example 7: Iterate over all networks console.log('\nExample 7: List all networks with SDK Chain mapping'); Object.entries(networks).forEach(([name, config]) => { if (config.chain) { console.log(`${name}: ${config.chain} (${config.chainId})`); } });