Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Token Detection V2] 2 of 7 - Add enum and util to check for supported token detection networks #811

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GasPriceValue,
FeeMarketEIP1559Values,
} from './transaction/TransactionController';
import { NetworksChainId } from './network/NetworkController';

const VALID = '4e1fF7229BDdAf0A73DF183a88d9c3a04cc975e0';
const SOME_API = 'https://someapi.com';
Expand Down Expand Up @@ -1267,3 +1268,27 @@ describe('isValidJson', () => {
expect(util.isValidJson({ foo: 'bar', test: { num: 5 } })).toBe(true);
});
});

describe('isTokenDetectionEnabledForNetwork', () => {
it('returns true for Mainnet', () => {
expect(
util.isTokenDetectionEnabledForNetwork(
util.SupportedTokenDetectionNetworks.mainnet,
),
).toBe(true);
});

it('returns true for custom network such as BSC', () => {
expect(
util.isTokenDetectionEnabledForNetwork(
util.SupportedTokenDetectionNetworks.bsc,
),
).toBe(true);
});

it('returns false for testnets such as Ropsten', () => {
expect(
util.isTokenDetectionEnabledForNetwork(NetworksChainId.ropsten),
).toBe(false);
});
});
24 changes: 23 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export async function safelyExecute(
) {
try {
return await operation();
} catch (error) {
} catch (error: any) {
/* istanbul ignore next */
if (logError) {
console.error(error);
Expand Down Expand Up @@ -931,3 +931,25 @@ export function isValidJson(value: unknown): value is Json {
return false;
}
}

/**
* Networks where token detection is supported - Values are in decimal format
*/
export enum SupportedTokenDetectionNetworks {
mainnet = '1',
bsc = '56',
polygon = '137',
avax = '43114',
}

/**
* Check if token detection is enabled for certain networks.
*
* @param chainId - ChainID of network
* @returns Whether the current network supports token detection
*/
export function isTokenDetectionEnabledForNetwork(chainId: string): boolean {
return Object.values<string>(SupportedTokenDetectionNetworks).includes(
chainId,
);
}