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: cherry-pick: Prevent coercing small spending caps to zero (#28179) #28183

Merged
merged 2 commits into from
Oct 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('useApproveTokenSimulation', () => {

expect(result.current).toMatchInlineSnapshot(`
{
"formattedSpendingCap": 7,
"formattedSpendingCap": "7",
"pending": undefined,
"spendingCap": "#7",
"value": {
Expand Down Expand Up @@ -155,4 +155,70 @@ describe('useApproveTokenSimulation', () => {
}
`);
});

it('returns correct small decimal number token amount for fungible tokens', async () => {
const useIsNFTMock = jest.fn().mockImplementation(() => ({ isNFT: false }));

const useDecodedTransactionDataMock = jest.fn().mockImplementation(() => ({
pending: false,
value: {
data: [
{
name: 'approve',
params: [
{
type: 'address',
value: '0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4',
},
{
type: 'uint256',
value: 10 ** 5,
},
],
},
],
source: 'FourByte',
},
}));

(useIsNFT as jest.Mock).mockImplementation(useIsNFTMock);
(useDecodedTransactionData as jest.Mock).mockImplementation(
useDecodedTransactionDataMock,
);

const transactionMeta = genUnapprovedContractInteractionConfirmation({
address: CONTRACT_INTERACTION_SENDER_ADDRESS,
}) as TransactionMeta;

const { result } = renderHookWithProvider(
() => useApproveTokenSimulation(transactionMeta, '18'),
mockState,
);

expect(result.current).toMatchInlineSnapshot(`
{
"formattedSpendingCap": "0.0000000000001",
"pending": undefined,
"spendingCap": "0.0000000000001",
"value": {
"data": [
{
"name": "approve",
"params": [
{
"type": "address",
"value": "0x9bc5baF874d2DA8D216aE9f137804184EE5AfEF4",
},
{
"type": "uint256",
"value": 100000,
},
],
},
],
"source": "FourByte",
},
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ function isSpendingCapUnlimited(decodedSpendingCap: number) {
return decodedSpendingCap >= UNLIMITED_THRESHOLD;
}

function toNonScientificString(num: number): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for other reviewers, this function was ported here from this other recent PR: #27992

This will create a conflict when we merge this back into develop; we'll need to update this file to import this function instead if using it inlined here (i.e. we'll need to accept the develop version when it conflicts, as we typically would).

if (num >= 10e-18) {
return num.toFixed(18).replace(/\.?0+$/u, '');
}

// keep in scientific notation
return num.toString();
}

export const useApproveTokenSimulation = (
transactionMeta: TransactionMeta,
decimals: string,
Expand Down Expand Up @@ -46,8 +55,9 @@ export const useApproveTokenSimulation = (
}, [value, decimals]);

const formattedSpendingCap = useMemo(() => {
return isNFT
? decodedSpendingCap
// formatting coerces small numbers to 0
return isNFT || decodedSpendingCap < 1
? toNonScientificString(decodedSpendingCap)
: new Intl.NumberFormat(locale).format(decodedSpendingCap);
}, [decodedSpendingCap, isNFT, locale]);

Expand Down