diff --git a/packages/checkout/sdk/src/balances/balances.test.ts b/packages/checkout/sdk/src/balances/balances.test.ts index bb68471127..99aabf4dda 100644 --- a/packages/checkout/sdk/src/balances/balances.test.ts +++ b/packages/checkout/sdk/src/balances/balances.test.ts @@ -26,7 +26,7 @@ import { BlockscoutTokens, BlockscoutTokenType, } from '../client'; -import { BLOCKSCOUT_CHAIN_URL_MAP, ERC20ABI, NATIVE } from '../env'; +import { ERC20ABI, NATIVE } from '../env'; jest.mock('../tokens'); jest.mock('../client'); @@ -427,8 +427,6 @@ describe('balances', () => { getNativeTokenByWalletAddress: getNativeTokenByWalletAddressMock, }); - const chainId = Object.keys(BLOCKSCOUT_CHAIN_URL_MAP)[0] as unknown as ChainId; - const getAllBalancesResult = await getAllBalances( { remote: { @@ -440,7 +438,7 @@ describe('balances', () => { } as unknown as CheckoutConfiguration, jest.fn() as unknown as Web3Provider, 'abc123', - chainId, + ChainId.ETHEREUM, ); expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1); @@ -518,7 +516,7 @@ describe('balances', () => { blockscout: true, }), }, - networkMap: testCheckoutConfig.networkMap, + networkMap: new CheckoutConfiguration({ baseConfig: { environment: Environment.SANDBOX } }).networkMap, } as unknown as CheckoutConfiguration, jest.fn() as unknown as Web3Provider, 'abc123', @@ -551,8 +549,6 @@ describe('balances', () => { getNativeTokenByWalletAddress: getNativeTokenByWalletAddressMock, }); - const chainId = Object.keys(BLOCKSCOUT_CHAIN_URL_MAP)[0] as unknown as ChainId; - const getAllBalancesResult = await getAllBalances( { remote: { @@ -564,7 +560,7 @@ describe('balances', () => { } as unknown as CheckoutConfiguration, jest.fn() as unknown as Web3Provider, 'abc123', - chainId, + ChainId.ETHEREUM, ); expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1); @@ -611,8 +607,6 @@ describe('balances', () => { getNativeTokenByWalletAddress: getNativeTokenByWalletAddressMock, }); - const chainId = Object.keys(BLOCKSCOUT_CHAIN_URL_MAP)[0] as unknown as ChainId; - const getAllBalancesResult = await getAllBalances( { remote: { @@ -624,7 +618,7 @@ describe('balances', () => { } as unknown as CheckoutConfiguration, jest.fn() as unknown as Web3Provider, 'abc123', - chainId, + ChainId.ETHEREUM, ); expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1); @@ -659,8 +653,6 @@ describe('balances', () => { getNativeTokenByWalletAddress: getNativeTokenByWalletAddressMock, }); - const chainId = Object.keys(BLOCKSCOUT_CHAIN_URL_MAP)[0] as unknown as ChainId; - const getAllBalancesResult = await getAllBalances( { remote: { @@ -672,7 +664,7 @@ describe('balances', () => { } as unknown as CheckoutConfiguration, jest.fn() as unknown as Web3Provider, 'abc123', - chainId, + ChainId.ETHEREUM, ); expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1); @@ -705,7 +697,6 @@ describe('balances', () => { getNativeTokenByWalletAddress: getNativeTokenByWalletAddressMock, }); - const chainId = Object.keys(BLOCKSCOUT_CHAIN_URL_MAP)[0] as unknown as ChainId; let message; let type; let data; @@ -721,7 +712,7 @@ describe('balances', () => { } as unknown as CheckoutConfiguration, jest.fn() as unknown as Web3Provider, '0xabc123', // use unique wallet address to prevent cached data - chainId, + ChainId.ETHEREUM, ); } catch (err: any) { message = err.message; @@ -739,6 +730,32 @@ describe('balances', () => { }); }); }); + + it('should fail if unsupported chain is provided', async () => { + let message; + let type; + try { + await getAllBalances( + { + remote: { + getTokensConfig: () => ({ + blockscout: true, + }), + }, + networkMap: testCheckoutConfig.networkMap, + } as unknown as CheckoutConfiguration, + jest.fn() as unknown as Web3Provider, + '0xabc123', // use unique wallet address to prevent cached data + ChainId.SEPOLIA, + ); + } catch (err: any) { + message = err.message; + type = err.type; + } + + expect(message).toEqual(`chain ID ${ChainId.SEPOLIA} not supported by the environment`); + expect(type).toEqual(CheckoutErrorType.CHAIN_NOT_SUPPORTED_ERROR); + }); }); describe('getBalances()', () => { diff --git a/packages/checkout/sdk/src/balances/balances.ts b/packages/checkout/sdk/src/balances/balances.ts index 4cdc4f57d9..991a51f484 100644 --- a/packages/checkout/sdk/src/balances/balances.ts +++ b/packages/checkout/sdk/src/balances/balances.ts @@ -258,6 +258,13 @@ export const getAllBalances = async ( ); } + if (!config.networkMap.get(chainId)) { + throw new CheckoutError( + `chain ID ${chainId} not supported by the environment`, + CheckoutErrorType.CHAIN_NOT_SUPPORTED_ERROR, + ); + } + const { tokens } = await getTokenAllowList( config, {