From 9f910f193fff9bbc26cf96f1711447271ffcaf09 Mon Sep 17 00:00:00 2001 From: digiwand <20778143+digiwand@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:19:21 +0800 Subject: [PATCH 1/4] fix: calcTokenAmount handle null decimals --- shared/lib/transactions-controller-utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/lib/transactions-controller-utils.js b/shared/lib/transactions-controller-utils.js index 88b7015b2090..cf66a37fc3b6 100644 --- a/shared/lib/transactions-controller-utils.js +++ b/shared/lib/transactions-controller-utils.js @@ -39,8 +39,8 @@ export function toPrecisionWithoutTrailingZeros(n, precision) { * @param {number} decimals * @returns {BigNumber} */ -export function calcTokenAmount(value, decimals = 0) { - const divisor = new BigNumber(10).pow(decimals); +export function calcTokenAmount(value, decimals) { + const divisor = new BigNumber(10).pow(decimals ?? 0); return new BigNumber(String(value)).div(divisor); } From 5cd40b38cf7348adf55628f43f91981d443e46d8 Mon Sep 17 00:00:00 2001 From: digiwand <20778143+digiwand@users.noreply.github.com> Date: Mon, 23 Sep 2024 18:20:55 +0800 Subject: [PATCH 2/4] fix: TypedSignInfo uses fetchErc20Decimals instead of getTokenStandardAndDetails and pass to DataTree --- shared/lib/transactions-controller-utils.js | 2 +- .../app/confirm/info/row/text-token-units.tsx | 2 +- .../confirm/info/typed-sign/typed-sign.tsx | 8 +++----- .../components/confirm/row/dataTree.tsx | 16 +++++++++++----- .../confirm/__snapshots__/confirm.test.tsx.snap | 14 +++++++------- ui/pages/confirmations/confirm/confirm.test.tsx | 16 ++++++---------- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/shared/lib/transactions-controller-utils.js b/shared/lib/transactions-controller-utils.js index cf66a37fc3b6..437fbc1063b9 100644 --- a/shared/lib/transactions-controller-utils.js +++ b/shared/lib/transactions-controller-utils.js @@ -36,7 +36,7 @@ export function toPrecisionWithoutTrailingZeros(n, precision) { /** * @param {number|string|BigNumber} value - * @param {number} decimals + * @param {number=} decimals * @returns {BigNumber} */ export function calcTokenAmount(value, decimals) { diff --git a/ui/components/app/confirm/info/row/text-token-units.tsx b/ui/components/app/confirm/info/row/text-token-units.tsx index cb0a620c4b5e..27ca63c852c8 100644 --- a/ui/components/app/confirm/info/row/text-token-units.tsx +++ b/ui/components/app/confirm/info/row/text-token-units.tsx @@ -11,7 +11,7 @@ import { ConfirmInfoRowText } from './text'; type ConfirmInfoRowTextTokenUnitsProps = { value: number | string | BigNumber; - decimals: number; + decimals?: number; }; export const ConfirmInfoRowTextTokenUnits: React.FC< diff --git a/ui/pages/confirmations/components/confirm/info/typed-sign/typed-sign.tsx b/ui/pages/confirmations/components/confirm/info/typed-sign/typed-sign.tsx index 86ea63470c37..7a608fc68b8a 100644 --- a/ui/pages/confirmations/components/confirm/info/typed-sign/typed-sign.tsx +++ b/ui/pages/confirmations/components/confirm/info/typed-sign/typed-sign.tsx @@ -12,12 +12,12 @@ import { ConfirmInfoRowUrl, } from '../../../../../../components/app/confirm/info/row'; import { useI18nContext } from '../../../../../../hooks/useI18nContext'; -import { getTokenStandardAndDetails } from '../../../../../../store/actions'; import { SignatureRequestType } from '../../../../types/confirm'; import { isOrderSignatureRequest, isPermitSignatureRequest, } from '../../../../utils'; +import { fetchErc20Decimals } from '../../../../utils/token'; import { useConfirmContext } from '../../../../context/confirm'; import { selectUseTransactionSimulations } from '../../../../selectors/preferences'; import { ConfirmInfoRowTypedSignData } from '../../row/typed-sign-data/typedSignData'; @@ -49,10 +49,8 @@ const TypedSignInfo: React.FC = () => { if (!isPermit && !isOrder) { return; } - const tokenDetails = await getTokenStandardAndDetails(verifyingContract); - const tokenDecimals = tokenDetails?.decimals; - - setDecimals(parseInt(tokenDecimals ?? '0', 10)); + const tokenDecimals = await fetchErc20Decimals(verifyingContract); + setDecimals(tokenDecimals); })(); }, [verifyingContract]); diff --git a/ui/pages/confirmations/components/confirm/row/dataTree.tsx b/ui/pages/confirmations/components/confirm/row/dataTree.tsx index a98ccdd84826..26c91baed3a6 100644 --- a/ui/pages/confirmations/components/confirm/row/dataTree.tsx +++ b/ui/pages/confirmations/components/confirm/row/dataTree.tsx @@ -72,6 +72,12 @@ const FIELD_DATE_PRIMARY_TYPES: Record = { */ const NONE_DATE_VALUE = -1; +/** + * If a token contract is found within the dataTree, fetch the token decimal of this contract + * to be utilized for displaying token amounts of the dataTree. + * + * @param dataTreeData + */ const getTokenDecimalsOfDataTree = async ( dataTreeData: Record | TreeData[], ): Promise => { @@ -91,7 +97,7 @@ const getTokenDecimalsOfDataTree = async ( export const DataTree = ({ data, primaryType, - tokenDecimals = 0, + tokenDecimals: tokenDecimalsProp, }: { data: Record | TreeData[]; primaryType?: PrimaryType; @@ -102,8 +108,8 @@ export const DataTree = ({ [data], ); - const tokenContractDecimals = - typeof decimalsResponse === 'number' ? decimalsResponse : undefined; + const tokenDecimals = + typeof decimalsResponse === 'number' ? decimalsResponse : tokenDecimalsProp; return ( @@ -122,7 +128,7 @@ export const DataTree = ({ primaryType={primaryType} value={value} type={type} - tokenDecimals={tokenContractDecimals ?? tokenDecimals} + tokenDecimals={tokenDecimals} /> } @@ -153,7 +159,7 @@ const DataField = memo( primaryType?: PrimaryType; type: string; value: ValueType; - tokenDecimals: number; + tokenDecimals?: number; }) => { const t = useI18nContext(); diff --git a/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap b/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap index 1030178befca..a164bed036e2 100644 --- a/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap +++ b/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap @@ -289,7 +289,7 @@ exports[`Confirm should match snapshot for signature - typed sign - V4 - PermitB >
{ }); await act(async () => { - const { container, findByText } = await renderWithConfirmContextProvider( - , - mockStore, - ); + const { container, findAllByText } = + await renderWithConfirmContextProvider(, mockStore); - expect(await findByText('1,461,501,637,3...')).toBeInTheDocument(); + expect(await findAllByText('14,615,016,373,...')).toHaveLength(2); expect(container).toMatchSnapshot(); }); }); @@ -172,12 +170,10 @@ describe('Confirm', () => { }); await act(async () => { - const { container, findByText } = await renderWithConfirmContextProvider( - , - mockStore, - ); + const { container, findAllByText } = + await renderWithConfirmContextProvider(, mockStore); - expect(await findByText('1,461,501,637,3...')).toBeInTheDocument(); + expect(await findAllByText('14,615,016,373,...')).toHaveLength(2); expect(container).toMatchSnapshot(); }); }); From 0fbc0c826daaa88d81f233f1a26928f674a16061 Mon Sep 17 00:00:00 2001 From: digiwand <20778143+digiwand@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:40:58 +0800 Subject: [PATCH 3/4] fix: confirm snapshot --- .../confirm/__snapshots__/confirm.test.tsx.snap | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap b/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap index a164bed036e2..1030178befca 100644 --- a/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap +++ b/ui/pages/confirmations/confirm/__snapshots__/confirm.test.tsx.snap @@ -289,7 +289,7 @@ exports[`Confirm should match snapshot for signature - typed sign - V4 - PermitB >
Date: Mon, 23 Sep 2024 20:36:54 +0800 Subject: [PATCH 4/4] fix: expect Permit value w/ default 18 decimals --- test/e2e/tests/confirmations/signatures/permit.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/tests/confirmations/signatures/permit.spec.ts b/test/e2e/tests/confirmations/signatures/permit.spec.ts index 8455a9fa8254..4fa2d8dcb128 100644 --- a/test/e2e/tests/confirmations/signatures/permit.spec.ts +++ b/test/e2e/tests/confirmations/signatures/permit.spec.ts @@ -123,7 +123,7 @@ async function assertInfoValues(driver: Driver) { css: '.name__value', text: '0x5B38D...eddC4', }); - const value = driver.findElement({ text: '3,000' }); + const value = driver.findElement({ text: '<0.000001' }); const nonce = driver.findElement({ text: '0' }); const deadline = driver.findElement({ text: '09 June 3554, 16:53' });