import type { NetworkConfig } from "../../../types/config.js"; import type { ChainType, NetworkConnection } from "../../../types/network.js"; import type { EthereumProvider } from "../../../types/providers.js"; export type CloseConnectionFunction = ( networkConnection: NetworkConnectionImplementation, ) => Promise; export class NetworkConnectionImplementation< ChainTypeT extends ChainType | string, > implements NetworkConnection { public readonly id: number; public readonly networkName: string; public readonly networkConfig: Readonly; public readonly chainType: ChainTypeT; #provider!: EthereumProvider; readonly #closeConnection: CloseConnectionFunction; public static async create( id: number, networkName: string, chainType: ChainTypeT, networkConfig: NetworkConfig, closeConnection: CloseConnectionFunction, createProvider: ( networkConnection: NetworkConnectionImplementation, ) => Promise, ): Promise> { const networkConnection = new NetworkConnectionImplementation( id, networkName, chainType, networkConfig, closeConnection, ); const provider = await createProvider(networkConnection); networkConnection.#setProvider(provider); return networkConnection; } private constructor( id: number, networkName: string, chainType: ChainTypeT, networkConfig: NetworkConfig, closeConnection: CloseConnectionFunction, ) { this.id = id; this.networkName = networkName; this.chainType = chainType; this.networkConfig = networkConfig; this.#closeConnection = closeConnection; this.close = this.close.bind(this); } public get provider(): EthereumProvider { return this.#provider; } #setProvider(provider: EthereumProvider) { this.#provider = provider; } public async close(): Promise { await this.#closeConnection(this); } }