diff --git a/src/modules/context/buildContext.ts b/src/modules/context/buildContext.ts index 0f34cfe..c91c708 100644 --- a/src/modules/context/buildContext.ts +++ b/src/modules/context/buildContext.ts @@ -2,6 +2,7 @@ import { DateTime } from 'luxon'; import type { PartialDeep } from 'type-fest'; import { SkybridgeBridge, SKYBRIDGE_BRIDGES } from '../bridges'; +import { fetcher } from '../fetch'; import type { SkybridgeMode } from '../modes'; import { getNetworkDetails } from './getNetworkDetails'; @@ -20,15 +21,23 @@ export const buildContext = async ({ SKYBRIDGE_BRIDGES.map((bridge) => getNetworkDetails({ mode, bridge })), ); - const getRandomIndexer = ({ bridge }: { bridge: SkybridgeBridge }) => { + const getRandomIndexer = async ({ bridge }: { bridge: SkybridgeBridge }) => { const index = SKYBRIDGE_BRIDGES.findIndex((it) => it === bridge); - try { - return ( - results[index].indexerNodes[randomInt(0, results[index].indexerNodes.length - 1)] || null - ); - } catch (e) { - return null; + + if (results[index].indexerNodes.length <= 1) { + return results[index].indexerNodes[0] ?? null; } + + for (const indexer of results[index].indexerNodes) { + try { + const result = await fetcher<{ blockbook: { inSync: boolean } }>(`${indexer}/api/v2`); + if (result.blockbook.inSync) { + return indexer; + } + } catch (e) {} + } + + return null; }; const getRandomSwapNode = ({ bridge }: { bridge: SkybridgeBridge }) => { @@ -72,8 +81,8 @@ export const buildContext = async ({ btc_bep20: servers?.swapNode?.btc_bep20 ?? getRandomSwapNode({ bridge: 'btc_bep20' }), }, indexer: { - btc_erc: servers?.indexer?.btc_erc ?? getRandomIndexer({ bridge: 'btc_erc' }), - btc_bep20: servers?.indexer?.btc_bep20 ?? getRandomIndexer({ bridge: 'btc_bep20' }), + btc_erc: servers?.indexer?.btc_erc ?? (await getRandomIndexer({ bridge: 'btc_erc' })), + btc_bep20: servers?.indexer?.btc_bep20 ?? (await getRandomIndexer({ bridge: 'btc_bep20' })), }, }, }; diff --git a/src/modules/context/getNetworkDetails.ts b/src/modules/context/getNetworkDetails.ts index 3dd6bb6..8e91017 100644 --- a/src/modules/context/getNetworkDetails.ts +++ b/src/modules/context/getNetworkDetails.ts @@ -18,8 +18,18 @@ const NETWORK_INFO: { [k in SkybridgeMode]: { [k in SkybridgeBridge]: { indexerNodes: string[] } }; } = { production: { - btc_erc: { indexerNodes: ['https://btc-eth-indexer.swingby.network/bb-eth'] }, - btc_bep20: { indexerNodes: ['https://btc-bsc-indexer.swingby.network/bb-bsc'] }, + btc_erc: { + indexerNodes: [ + 'https://btc-eth-indexer.swingby.network/bb-eth', + 'https://btc-eth-indexer-2.swingby.network/bb-eth', + ], + }, + btc_bep20: { + indexerNodes: [ + 'https://btc-bsc-indexer.swingby.network/bb-bsc', + 'https://btc-bsc-indexer-2.swingby.network/bb-bsc', + ], + }, }, test: { btc_erc: { indexerNodes: ['https://tbtc-goerli-node-1.swingby.network/bb-eth'] },