(
network: ProjectNetworkConfig<EndpointConfig> & {chainId: string},
createConnection: (endpoint: string, config: EndpointConfig) => Promise<Connection>,
/* Used to monitor the state of the connection */
postConnectedHook?: (connection: Connection, endpoint: string, index: number) => void
)
| 86 | } |
| 87 | |
| 88 | async createConnections( |
| 89 | network: ProjectNetworkConfig<EndpointConfig> & {chainId: string}, |
| 90 | createConnection: (endpoint: string, config: EndpointConfig) => Promise<Connection>, |
| 91 | /* Used to monitor the state of the connection */ |
| 92 | postConnectedHook?: (connection: Connection, endpoint: string, index: number) => void |
| 93 | ): Promise<void> { |
| 94 | const endpointToApiIndex: Record<string, Connection> = {}; |
| 95 | |
| 96 | const failedConnections: Map<number, [string, EndpointConfig]> = new Map(); |
| 97 | |
| 98 | const endpoints = normalizeNetworkEndpoints<EndpointConfig>(network.endpoint); |
| 99 | |
| 100 | const connectionPromises = Object.entries(endpoints).map(async ([endpoint, config], i) => { |
| 101 | try { |
| 102 | const connection = await this.performConnection( |
| 103 | createConnection, |
| 104 | network, |
| 105 | i, |
| 106 | endpoint, |
| 107 | config, |
| 108 | postConnectedHook |
| 109 | ); |
| 110 | |
| 111 | void this.connectionPoolService.addToConnections(connection, endpoint); |
| 112 | } catch (e) { |
| 113 | logger.error(`Failed to init ${endpoint}: ${e}`); |
| 114 | endpointToApiIndex[endpoint] = null as unknown as Connection; |
| 115 | |
| 116 | // Don't reconnect if connection is for wrong network |
| 117 | if (!(e instanceof MetadataMismatchError)) { |
| 118 | failedConnections.set(i, [endpoint, config]); |
| 119 | } |
| 120 | |
| 121 | throw e; |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | try { |
| 126 | await Promise.any(connectionPromises); |
| 127 | } catch (e) { |
| 128 | throw new Error('All endpoints failed to initialize. Please add healthier endpoints', {cause: e}); |
| 129 | } |
| 130 | |
| 131 | void Promise.allSettled(connectionPromises).then((res) => { |
| 132 | // Retry failed connections in the background |
| 133 | for (const [index, [endpoint, config]] of failedConnections) { |
| 134 | this.retryConnection(createConnection, network, index, endpoint, config, postConnectedHook); |
| 135 | } |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | private async performConnection( |
| 140 | createConnection: (endpoint: string, config: EndpointConfig) => Promise<Connection>, |
nothing calls this directly
no test coverage detected