Skip to content

Commit

Permalink
WT-2034 Lower case addresses when comparing balances to allowlist (#1357
Browse files Browse the repository at this point in the history
)
  • Loading branch information
imx-mikhala authored Jan 16, 2024
1 parent 09c4202 commit d0ff4f2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions packages/checkout/widgets-lib/src/lib/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('getAllowedBalances', () => {
token: { symbol: 'CCC', name: 'CCC', address: '0xC' } as TokenInfo,
formattedBalance: '36.34',
},
{
balance: BigNumber.from(1),
token: { symbol: 'DDD', name: 'DDD', address: '0xd' } as TokenInfo,
formattedBalance: '36.34',
},
];

it('should return allowList and allowedBalances', async () => {
Expand Down Expand Up @@ -271,6 +276,46 @@ describe('getAllowedBalances', () => {
});
});

it('should return allowedBalances when casing on address is different', async () => {
const checkout = new Checkout({
baseConfig: { environment: Environment.PRODUCTION },
});

const mockProvider = {
getSigner: jest.fn().mockReturnValue({
getAddress: jest.fn().mockResolvedValue('0xaddress'),
}),
};
jest.spyOn(checkout, 'getNetworkInfo').mockResolvedValue(
{ chainId: ChainId.IMTBL_ZKEVM_MAINNET } as unknown as NetworkInfo,
);
jest.spyOn(checkout, 'getAllBalances').mockResolvedValue({ balances });
jest.spyOn(checkout, 'getTokenAllowList').mockResolvedValue({
tokens: [
{
address: '0xD',
} as unknown as TokenInfo,
],
});

const resp = await getAllowedBalances({
checkout,
provider: mockProvider as unknown as Web3Provider,
allowTokenListType: TokenFilterTypes.BRIDGE,
});

expect(resp).toEqual({
allowList: {
tokens: [{ address: '0xD' }],
},
allowedBalances: [{
balance: BigNumber.from(1),
token: { symbol: 'DDD', name: 'DDD', address: '0xd' } as TokenInfo,
formattedBalance: '36.34',
}],
});
});

it('should not return native address or empty address', async () => {
const checkout = new Checkout({
baseConfig: { environment: Environment.PRODUCTION },
Expand Down
4 changes: 2 additions & 2 deletions packages/checkout/widgets-lib/src/lib/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ export const getAllowedBalances = async ({
});

const tokensAddresses = new Map();
allowList.tokens.forEach((token) => tokensAddresses.set(token.address || NATIVE, true));
allowList.tokens.forEach((token) => tokensAddresses.set(token.address?.toLowerCase() || NATIVE, true));

const allowedBalances = tokenBalances.balances.filter((balance) => {
// Balance is <= 0 and it is not allow to have zeros
if (balance.balance.lte(0) && !allowZero) return false;

return tokensAddresses.get(balance.token.address || NATIVE);
return tokensAddresses.get(balance.token.address?.toLowerCase() || NATIVE);
}) ?? [];

return { allowList, allowedBalances };
Expand Down

0 comments on commit d0ff4f2

Please sign in to comment.