Skip to content

Commit

Permalink
fix: filter unaccessible endpoints (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 authored Nov 30, 2024
1 parent db877b9 commit 910108c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
11 changes: 8 additions & 3 deletions packages/extension-polkagate/src/hooks/useEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export function useEndpoints (genesisHash: string | null | undefined): DropdownO
}

const endpoints = allEndpoints?.filter((e) => e.value &&
(String(e.info)?.toLowerCase() === chainName?.toLowerCase() ||
String(e.text)?.toLowerCase()?.includes(chainName?.toLowerCase() ?? ''))
// Check if e.value matches the pattern 'wss://<any_number>'
!/^wss:\/\/\d+$/.test(e.value) &&
!e.value.includes('onfinality') && // ignore due to its rate limits
(
String(e.info)?.toLowerCase() === chainName?.toLowerCase() ||
String(e.text)?.toLowerCase()?.includes(chainName?.toLowerCase() ?? '')
)
);

if (!endpoints) {
Expand All @@ -52,7 +57,7 @@ export function useEndpoints (genesisHash: string | null | undefined): DropdownO
}

endpointOptions.length > 1 &&
endpointOptions?.unshift(AUTO_MODE);
endpointOptions?.unshift(AUTO_MODE);

if (!endpointOptions?.length && userAddedEndpoint) {
return userAddedEndpoint;
Expand Down
16 changes: 4 additions & 12 deletions packages/extension-polkagate/src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { Theme } from '@mui/material';
import type { ApiPromise } from '@polkadot/api';
import type { DeriveBalancesAll } from '@polkadot/api-derive/types';
import type { AccountJson } from '@polkadot/extension-base/background/types';
import type { Chain } from '@polkadot/extension-chains/types';
Expand All @@ -11,11 +12,11 @@ import type { Compact, u128 } from '@polkadot/types-codec';
import type { HexString } from '@polkadot/util/types';
import type { DropdownOption, FastestConnectionType, RecentChainsType, TransactionDetail, UserAddedChains } from './types';

import { ApiPromise, WsProvider } from '@polkadot/api';
import { BN, BN_TEN, BN_ZERO, hexToBn, hexToString, hexToU8a, isHex, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';

import { EXTRA_PRICE_IDS } from './api/getPrices';
import { fastestEndpoint } from './workers/utils';
import allChains from './chains';
import { ASSET_HUBS, BLOCK_RATE, FLOATING_POINT_DIGIT, INITIAL_RECENT_CHAINS_GENESISHASH, PROFILE_COLORS, RELAY_CHAINS_GENESISHASH, SHORT_ADDRESS_CHARACTERS, WESTEND_GENESIS_HASH } from './constants';

Expand Down Expand Up @@ -499,18 +500,9 @@ export async function updateRecentChains (addressKey: string, genesisHashKey: st

export async function fastestConnection (endpoints: DropdownOption[]): Promise<FastestConnectionType> {
try {
const connections = endpoints.map(({ value }) => {
const wsProvider = new WsProvider(value as string);
const urls = endpoints.map(({ value }) => ({ value: value as string }));
const { api, connections } = await fastestEndpoint(urls);

const connection = ApiPromise.create({ provider: wsProvider });

return {
connection,
wsProvider
};
});

const api = await Promise.any(connections.map(({ connection }) => connection));
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const selectedEndpoint = api.registry.knownTypes.provider.endpoint as string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export async function fastestEndpoint (endpoints) {
let connection;

const connections = endpoints.map(({ value }) => {
// Check if e.value matches the pattern 'wss://<any_number>'
// ignore due to its rate limits
if (/^wss:\/\/\d+$/.test(value) || (value).includes('onfinality')) {
return undefined;
}

const wsProvider = new WsProvider(value);

connection = ApiPromise.create({ provider: wsProvider });
Expand All @@ -18,7 +24,7 @@ export async function fastestEndpoint (endpoints) {
connection,
wsProvider
};
});
}).filter((i) => !!i);

const api = await Promise.any(connections.map(({ connection }) => connection));

Expand Down

0 comments on commit 910108c

Please sign in to comment.