Skip to content

Commit

Permalink
Add SupportedTokenDetectionNetworks enum and util for token detection…
Browse files Browse the repository at this point in the history
… support. (#811)
  • Loading branch information
Cal-L authored and MajorLift committed Oct 11, 2023
1 parent 36457b1 commit 06f1aeb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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,
);
}

0 comments on commit 06f1aeb

Please sign in to comment.