11import { Injectable , Logger } from '@nestjs/common' ;
22import { NetworkMetrics , GasPriceSnapshot } from '../interfaces/gas-price.interface' ;
33import { Cron , CronExpression } from '@nestjs/schedule' ;
4+ import { NetworkConfigService } from '../config/network-config.service' ;
45
56/**
67 * NetworkMonitorService
@@ -15,6 +16,8 @@ export class NetworkMonitorService {
1516 private metricsCache : Map < string , NetworkMetrics > = new Map ( ) ;
1617 private priceSnapshotCache : Map < string , GasPriceSnapshot > = new Map ( ) ;
1718
19+ constructor ( private readonly networkConfigService : NetworkConfigService ) { }
20+
1821 /**
1922 * Get current network metrics for a chain
2023 */
@@ -51,8 +54,7 @@ export class NetworkMonitorService {
5154 @Cron ( CronExpression . EVERY_10_SECONDS )
5255 async updateNetworkMetrics ( ) : Promise < void > {
5356 try {
54- // Update for all monitored chains
55- const chainIds = [ 'soroban-testnet' , 'soroban-mainnet' ] ;
57+ const chainIds = this . networkConfigService . getSupportedChainIds ( ) ;
5658
5759 for ( const chainId of chainIds ) {
5860 const metrics = await this . fetchNetworkMetricsFromRpc ( chainId ) ;
@@ -75,19 +77,22 @@ export class NetworkMonitorService {
7577 private async fetchNetworkMetricsFromRpc ( chainId : string ) : Promise < NetworkMetrics > {
7678 // This would connect to actual Soroban RPC in production
7779 // For now, return mock data with some randomization to simulate network conditions
80+ const network = this . networkConfigService . getNetworkConfig ( chainId ) ;
7881
79- const baseLoad = 35 ; // baseline network load
82+ const baseLoad = network . baselineLoad ;
8083 const randomFluctuation = Math . random ( ) * 30 - 15 ; // -15 to +15
8184 const congestionLevel = Math . max ( 0 , Math . min ( 100 , baseLoad + randomFluctuation ) ) ;
8285
8386 return {
8487 congestionLevel,
8588 gasPoolUtilization : congestionLevel * 0.8 ,
86- averageTransactionTime : 4000 + Math . random ( ) * 2000 , // 4-6 seconds
89+ averageTransactionTime : network . averageBlockTimeMs + Math . random ( ) * 2000 ,
8790 pendingTransactionCount : Math . floor ( congestionLevel * 10 ) ,
88- lastBlockGasUsed : 75000000 + Math . floor ( Math . random ( ) * 25000000 ) ,
89- lastBlockGasLimit : 100000000 ,
90- historicalAverageGasPrice : 1000 , // stroops per instruction
91+ lastBlockGasUsed :
92+ network . defaultBlockGasLimit * 0.75 +
93+ Math . floor ( Math . random ( ) * ( network . defaultBlockGasLimit * 0.25 ) ) ,
94+ lastBlockGasLimit : network . defaultBlockGasLimit ,
95+ historicalAverageGasPrice : network . historicalAverageGasPrice ,
9196 priceVolatility : congestionLevel * 0.5 , // volatility increases with congestion
9297 } ;
9398 }
@@ -96,13 +101,14 @@ export class NetworkMonitorService {
96101 * Create a gas price snapshot based on current metrics
97102 */
98103 private async createGasPriceSnapshot ( chainId : string ) : Promise < GasPriceSnapshot > {
104+ const network = this . networkConfigService . getNetworkConfig ( chainId ) ;
99105 const metrics = await this . getNetworkMetrics ( chainId ) ;
100106
101107 // Calculate surge multiplier based on congestion
102108 const surgeMultiplier = this . calculateSurgeMultiplier ( metrics . congestionLevel ) ;
103109
104110 // Base price (stroops per instruction) - Soroban default
105- const basePrice = 1000 ;
111+ const basePrice = network . baseFeePerInstruction ;
106112 const recommendedFeeRate = basePrice * surgeMultiplier ;
107113
108114 // Estimate price confidence (higher during stable, lower during volatile periods)
@@ -111,7 +117,7 @@ export class NetworkMonitorService {
111117 return {
112118 id : `snapshot-${ chainId } -${ Date . now ( ) } ` ,
113119 chainId,
114- chainName : chainId === 'soroban-mainnet' ? 'Soroban Mainnet' : 'Soroban Testnet' ,
120+ chainName : network . chainName ,
115121 timestamp : new Date ( ) ,
116122 baseFeePerInstruction : basePrice ,
117123 surgePriceMultiplier : surgeMultiplier ,
@@ -153,6 +159,7 @@ export class NetworkMonitorService {
153159 chainId : string ,
154160 hoursBack : number = 24 ,
155161 ) : Promise < GasPriceSnapshot [ ] > {
162+ const network = this . networkConfigService . getNetworkConfig ( chainId ) ;
156163 // In production, query database for historical snapshots
157164 const snapshots : GasPriceSnapshot [ ] = [ ] ;
158165 const now = Date . now ( ) ;
@@ -164,15 +171,17 @@ export class NetworkMonitorService {
164171 snapshots . push ( {
165172 id : `hist-${ chainId } -${ i } ` ,
166173 chainId,
167- chainName : chainId === 'soroban-mainnet' ? 'Soroban Mainnet' : 'Soroban Testnet' ,
174+ chainName : network . chainName ,
168175 timestamp,
169- baseFeePerInstruction : 1000 ,
176+ baseFeePerInstruction : network . baseFeePerInstruction ,
170177 surgePriceMultiplier : this . calculateSurgeMultiplier ( congestionAtTime ) ,
171- recommendedFeeRate : 1000 * this . calculateSurgeMultiplier ( congestionAtTime ) ,
178+ recommendedFeeRate :
179+ network . baseFeePerInstruction *
180+ this . calculateSurgeMultiplier ( congestionAtTime ) ,
172181 networkLoad : congestionAtTime ,
173182 memoryPoolSize : 0 ,
174183 transactionCount : Math . floor ( congestionAtTime * 10 ) ,
175- averageBlockTime : 4000 + Math . random ( ) * 2000 ,
184+ averageBlockTime : network . averageBlockTimeMs + Math . random ( ) * 2000 ,
176185 volatilityIndex : congestionAtTime * 0.4 ,
177186 priceConfidence : Math . max ( 40 , 100 - congestionAtTime ) ,
178187 } ) ;
0 commit comments