Skip to content

Commit

Permalink
feat: Try to pick healthy indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-rib committed Aug 31, 2021
1 parent 5925a0b commit 48f1448
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
27 changes: 18 additions & 9 deletions src/modules/context/buildContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,15 +21,23 @@ export const buildContext = async <M extends SkybridgeMode>({
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 }) => {
Expand Down Expand Up @@ -72,8 +81,8 @@ export const buildContext = async <M extends SkybridgeMode>({
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' })),
},
},
};
Expand Down
14 changes: 12 additions & 2 deletions src/modules/context/getNetworkDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] },
Expand Down

0 comments on commit 48f1448

Please sign in to comment.