From d0ff4f2f93b9659aa93cd7395de393a29105a1de Mon Sep 17 00:00:00 2001 From: Mikhala <122326421+imx-mikhala@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:11:52 +0800 Subject: [PATCH] WT-2034 Lower case addresses when comparing balances to allowlist (#1357) --- .../widgets-lib/src/lib/balance.test.ts | 45 +++++++++++++++++++ .../checkout/widgets-lib/src/lib/balance.ts | 4 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/checkout/widgets-lib/src/lib/balance.test.ts b/packages/checkout/widgets-lib/src/lib/balance.test.ts index 88194c4ff1..946fe22096 100644 --- a/packages/checkout/widgets-lib/src/lib/balance.test.ts +++ b/packages/checkout/widgets-lib/src/lib/balance.test.ts @@ -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 () => { @@ -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 }, diff --git a/packages/checkout/widgets-lib/src/lib/balance.ts b/packages/checkout/widgets-lib/src/lib/balance.ts index 6051992ea0..3532f6bdd8 100644 --- a/packages/checkout/widgets-lib/src/lib/balance.ts +++ b/packages/checkout/widgets-lib/src/lib/balance.ts @@ -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 };