Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Permit DataTree token decimals #27328

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions shared/lib/transactions-controller-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export function toPrecisionWithoutTrailingZeros(n, precision) {

/**
* @param {number|string|BigNumber} value
* @param {number} decimals
* @param {number=} decimals
pedronfigueiredo marked this conversation as resolved.
Show resolved Hide resolved
* @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);
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/confirmations/signatures/permit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/confirm/info/row/text-token-units.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ConfirmInfoRowText } from './text';

type ConfirmInfoRowTextTokenUnitsProps = {
value: number | string | BigNumber;
decimals: number;
decimals?: number;
};

export const ConfirmInfoRowTextTokenUnits: React.FC<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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]);

Expand Down
16 changes: 11 additions & 5 deletions ui/pages/confirmations/components/confirm/row/dataTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ const FIELD_DATE_PRIMARY_TYPES: Record<string, string[]> = {
*/
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<string, TreeData> | TreeData[],
): Promise<void | number> => {
Expand All @@ -91,7 +97,7 @@ const getTokenDecimalsOfDataTree = async (
export const DataTree = ({
data,
primaryType,
tokenDecimals = 0,
tokenDecimals: tokenDecimalsProp,
}: {
data: Record<string, TreeData> | TreeData[];
primaryType?: PrimaryType;
Expand All @@ -102,8 +108,8 @@ export const DataTree = ({
[data],
);

const tokenContractDecimals =
typeof decimalsResponse === 'number' ? decimalsResponse : undefined;
const tokenDecimals =
typeof decimalsResponse === 'number' ? decimalsResponse : tokenDecimalsProp;

return (
<Box width={BlockSize.Full}>
Expand All @@ -122,7 +128,7 @@ export const DataTree = ({
primaryType={primaryType}
value={value}
type={type}
tokenDecimals={tokenContractDecimals ?? tokenDecimals}
tokenDecimals={tokenDecimals}
/>
}
</ConfirmInfoRow>
Expand Down Expand Up @@ -153,7 +159,7 @@ const DataField = memo(
primaryType?: PrimaryType;
type: string;
value: ValueType;
tokenDecimals: number;
tokenDecimals?: number;
}) => {
const t = useI18nContext();

Expand Down
16 changes: 6 additions & 10 deletions ui/pages/confirmations/confirm/confirm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,10 @@ describe('Confirm', () => {
});

await act(async () => {
const { container, findByText } = await renderWithConfirmContextProvider(
<Confirm />,
mockStore,
);
const { container, findAllByText } =
await renderWithConfirmContextProvider(<Confirm />, mockStore);

expect(await findByText('1,461,501,637,3...')).toBeInTheDocument();
expect(await findAllByText('14,615,016,373,...')).toHaveLength(2);
expect(container).toMatchSnapshot();
});
});
Expand All @@ -172,12 +170,10 @@ describe('Confirm', () => {
});

await act(async () => {
const { container, findByText } = await renderWithConfirmContextProvider(
<Confirm />,
mockStore,
);
const { container, findAllByText } =
await renderWithConfirmContextProvider(<Confirm />, mockStore);

expect(await findByText('1,461,501,637,3...')).toBeInTheDocument();
expect(await findAllByText('14,615,016,373,...')).toHaveLength(2);
expect(container).toMatchSnapshot();
});
});
Expand Down