Skip to content

Commit

Permalink
WT-1971 - error if chain provided is not supported by environment (#1300
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andrearampin authored Dec 19, 2023
1 parent 59825c2 commit 3441b77
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
49 changes: 33 additions & 16 deletions packages/checkout/sdk/src/balances/balances.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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: {
Expand All @@ -440,7 +438,7 @@ describe('balances', () => {
} as unknown as CheckoutConfiguration,
jest.fn() as unknown as Web3Provider,
'abc123',
chainId,
ChainId.ETHEREUM,
);

expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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: {
Expand All @@ -564,7 +560,7 @@ describe('balances', () => {
} as unknown as CheckoutConfiguration,
jest.fn() as unknown as Web3Provider,
'abc123',
chainId,
ChainId.ETHEREUM,
);

expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -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: {
Expand All @@ -624,7 +618,7 @@ describe('balances', () => {
} as unknown as CheckoutConfiguration,
jest.fn() as unknown as Web3Provider,
'abc123',
chainId,
ChainId.ETHEREUM,
);

expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -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: {
Expand All @@ -672,7 +664,7 @@ describe('balances', () => {
} as unknown as CheckoutConfiguration,
jest.fn() as unknown as Web3Provider,
'abc123',
chainId,
ChainId.ETHEREUM,
);

expect(getNativeTokenByWalletAddressMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/checkout/sdk/src/balances/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down

0 comments on commit 3441b77

Please sign in to comment.