Skip to content

Commit 638856e

Browse files
authored
Add SupportedTokenDetectionNetworks enum and util for token detection support. (#811)
1 parent d4030cf commit 638856e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/util.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
GasPriceValue,
88
FeeMarketEIP1559Values,
99
} from './transaction/TransactionController';
10+
import { NetworksChainId } from './network/NetworkController';
1011

1112
const VALID = '4e1fF7229BDdAf0A73DF183a88d9c3a04cc975e0';
1213
const SOME_API = 'https://someapi.com';
@@ -1267,3 +1268,27 @@ describe('isValidJson', () => {
12671268
expect(util.isValidJson({ foo: 'bar', test: { num: 5 } })).toBe(true);
12681269
});
12691270
});
1271+
1272+
describe('isTokenDetectionEnabledForNetwork', () => {
1273+
it('returns true for Mainnet', () => {
1274+
expect(
1275+
util.isTokenDetectionEnabledForNetwork(
1276+
util.SupportedTokenDetectionNetworks.mainnet,
1277+
),
1278+
).toBe(true);
1279+
});
1280+
1281+
it('returns true for custom network such as BSC', () => {
1282+
expect(
1283+
util.isTokenDetectionEnabledForNetwork(
1284+
util.SupportedTokenDetectionNetworks.bsc,
1285+
),
1286+
).toBe(true);
1287+
});
1288+
1289+
it('returns false for testnets such as Ropsten', () => {
1290+
expect(
1291+
util.isTokenDetectionEnabledForNetwork(NetworksChainId.ropsten),
1292+
).toBe(false);
1293+
});
1294+
});

src/util.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export async function safelyExecute(
324324
) {
325325
try {
326326
return await operation();
327-
} catch (error) {
327+
} catch (error: any) {
328328
/* istanbul ignore next */
329329
if (logError) {
330330
console.error(error);
@@ -931,3 +931,25 @@ export function isValidJson(value: unknown): value is Json {
931931
return false;
932932
}
933933
}
934+
935+
/**
936+
* Networks where token detection is supported - Values are in decimal format
937+
*/
938+
export enum SupportedTokenDetectionNetworks {
939+
mainnet = '1',
940+
bsc = '56',
941+
polygon = '137',
942+
avax = '43114',
943+
}
944+
945+
/**
946+
* Check if token detection is enabled for certain networks.
947+
*
948+
* @param chainId - ChainID of network
949+
* @returns Whether the current network supports token detection
950+
*/
951+
export function isTokenDetectionEnabledForNetwork(chainId: string): boolean {
952+
return Object.values<string>(SupportedTokenDetectionNetworks).includes(
953+
chainId,
954+
);
955+
}

0 commit comments

Comments
 (0)