From 5e95e8140fc68d4541d8fc26aeb0e1188142c97b Mon Sep 17 00:00:00 2001 From: Danica Shen Date: Wed, 19 Apr 2023 19:20:18 +0200 Subject: [PATCH] fix(18574): fix new BigNumber() not a number: undefined for setApprovalForAll method (#18660) --- ui/helpers/utils/token-util.js | 14 ++++----- ui/hooks/useAssetDetails.test.js | 50 ++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/ui/helpers/utils/token-util.js b/ui/helpers/utils/token-util.js index 882966472..e34bd9888 100644 --- a/ui/helpers/utils/token-util.js +++ b/ui/helpers/utils/token-util.js @@ -261,17 +261,15 @@ export async function getAssetDetails( // if we can't determine any token standard or details return the data we can extract purely from the parsed transaction data return { toAddress, tokenId }; } - + const tokenValue = getTokenValueParam(tokenData); + const tokenDecimals = tokenDetails?.decimals; const tokenAmount = tokenData && - tokenDetails?.decimals && - calcTokenAmount( - getTokenValueParam(tokenData), - tokenDetails?.decimals, - ).toString(10); + tokenValue && + tokenDecimals && + calcTokenAmount(tokenValue, tokenDecimals).toString(10); - const decimals = - tokenDetails?.decimals && Number(tokenDetails.decimals?.toString(10)); + const decimals = tokenDecimals && Number(tokenDecimals?.toString(10)); if (tokenDetails?.standard === TokenStandard.ERC20) { tokenId = undefined; diff --git a/ui/hooks/useAssetDetails.test.js b/ui/hooks/useAssetDetails.test.js index 6060c2dc2..f057a3f3e 100644 --- a/ui/hooks/useAssetDetails.test.js +++ b/ui/hooks/useAssetDetails.test.js @@ -37,12 +37,16 @@ describe('useAssetDetails', () => { let getTokenStandardAndDetailsStub; beforeEach(() => { - getTokenStandardAndDetailsStub = jest - .spyOn(Actions, 'getTokenStandardAndDetails') - .mockImplementation(() => Promise.resolve({})); + getTokenStandardAndDetailsStub = jest.spyOn( + Actions, + 'getTokenStandardAndDetails', + ); }); it('should return object with tokenSymbol set to an empty string, when getAssetDetails returns and empty object', async () => { + getTokenStandardAndDetailsStub.mockImplementation(() => + Promise.resolve({}), + ); const toAddress = '000000000000000000000000000000000000dead'; const tokenAddress = '0x1'; @@ -106,6 +110,46 @@ describe('useAssetDetails', () => { }); }); + it('should return object with correct tokenValues for an ERC20 token with no decimals', async () => { + const userAddress = '0xf04a5cc80b1e94c69b48f5ee68a08cd2f09a7c3e'; + const tokenAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; + const toAddress = '000000000000000000000000000000000000dead'; + const transactionData = `0xa9059cbb000000000000000000000000${toAddress}00000000000000000000000000000000000000000000000000000000000001f4`; + + const standard = TokenStandard.ERC20; + const symbol = 'WETH'; + const balance = '1'; + + getTokenStandardAndDetailsStub.mockImplementation(() => + Promise.resolve({ + standard, + balance, + symbol, + }), + ); + + const { result, waitForNextUpdate } = renderUseAssetDetails({ + tokenAddress, + userAddress, + transactionData, + }); + + await waitForNextUpdate(); + + expect(result.current).toStrictEqual({ + assetAddress: tokenAddress, + assetName: undefined, + assetStandard: standard, + toAddress: `0x${toAddress}`, + tokenAmount: undefined, + tokenId: undefined, + tokenImage: undefined, + tokenSymbol: symbol, + userBalance: balance, + decimals: undefined, + }); + }); + it('should return object with correct tokenValues for an ERC721 token', async () => { const tokenAddress = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'; const toAddress = '000000000000000000000000000000000000dead';