Skip to content

Commit 910108c

Browse files
authored
fix: filter unaccessible endpoints (#1678)
1 parent db877b9 commit 910108c

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

packages/extension-polkagate/src/hooks/useEndpoints.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ export function useEndpoints (genesisHash: string | null | undefined): DropdownO
3636
}
3737

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

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

5459
endpointOptions.length > 1 &&
55-
endpointOptions?.unshift(AUTO_MODE);
60+
endpointOptions?.unshift(AUTO_MODE);
5661

5762
if (!endpointOptions?.length && userAddedEndpoint) {
5863
return userAddedEndpoint;

packages/extension-polkagate/src/util/utils.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

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

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

1818
import { EXTRA_PRICE_IDS } from './api/getPrices';
19+
import { fastestEndpoint } from './workers/utils';
1920
import allChains from './chains';
2021
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';
2122

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

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

505-
const connection = ApiPromise.create({ provider: wsProvider });
506-
507-
return {
508-
connection,
509-
wsProvider
510-
};
511-
});
512-
513-
const api = await Promise.any(connections.map(({ connection }) => connection));
514506
// @ts-ignore
515507
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
516508
const selectedEndpoint = api.registry.knownTypes.provider.endpoint as string;

packages/extension-polkagate/src/util/workers/utils/fastestEndpoint.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export async function fastestEndpoint (endpoints) {
1010
let connection;
1111

1212
const connections = endpoints.map(({ value }) => {
13+
// Check if e.value matches the pattern 'wss://<any_number>'
14+
// ignore due to its rate limits
15+
if (/^wss:\/\/\d+$/.test(value) || (value).includes('onfinality')) {
16+
return undefined;
17+
}
18+
1319
const wsProvider = new WsProvider(value);
1420

1521
connection = ApiPromise.create({ provider: wsProvider });
@@ -18,7 +24,7 @@ export async function fastestEndpoint (endpoints) {
1824
connection,
1925
wsProvider
2026
};
21-
});
27+
}).filter((i) => !!i);
2228

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

0 commit comments

Comments
 (0)