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: decimal places displayed on token value on permit pages #25410

Merged
merged 26 commits into from
Jul 2, 2024

Conversation

jpuri
Copy link
Contributor

@jpuri jpuri commented Jun 19, 2024

Description

fix decimal places displayed on token value on permit pages

Related issues

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/2648

Manual testing steps

  1. Go to test dapp
  2. Submit Permit signature request
  3. Check format of token values displayed

Screenshots/Recordings

Screenshot 2024-06-19 at 3 08 14 PM

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@jpuri jpuri added confirmation-redesign team-confirmations Push issues to confirmations team labels Jun 19, 2024
@jpuri jpuri requested a review from a team as a code owner June 19, 2024 09:51
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@jpuri jpuri requested a review from a team as a code owner June 19, 2024 10:01
@jpuri jpuri changed the base branch from develop to permit_deadline June 19, 2024 10:01
@metamaskbot
Copy link
Collaborator

Builds ready [ac15aa4]
Page Load Metrics (54 ± 4 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint721048684
domContentLoaded9201121
load42745484
domInteractive9201121
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 0 Bytes (0.00%)
  • ui: 2.51 KiB (0.04%)
  • common: 118 Bytes (0.00%)

Copy link

codecov bot commented Jun 19, 2024

Codecov Report

Attention: Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Project coverage is 69.61%. Comparing base (548b54a) to head (ed96b06).
Report is 1 commits behind head on develop.

Files Patch % Lines
ui/pages/confirmations/components/confirm/utils.ts 80.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #25410      +/-   ##
===========================================
+ Coverage    69.60%   69.61%   +0.01%     
===========================================
  Files         1364     1364              
  Lines        48173    48189      +16     
  Branches     13291    13297       +6     
===========================================
+ Hits         33527    33542      +15     
- Misses       14646    14647       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -42,9 +44,23 @@ const TypedSignInfo: React.FC = () => {

const isPermit = isPermitSignatureRequest(currentConfirmation);

useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

We have similar logic within useBalanceChanges in fetchErc20Decimals and it seems we have to account for base 10 and base 16 responses, plus checking it's a finite value.

Is it worth re-using that function and moving it to a util or even creating a hook such as useTokenDecimals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Writing a new hook could be useful, a small limitation here is that we are conditionally getting this for only permit types.

Copy link
Member

Choose a reason for hiding this comment

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

Even if we don't make a hook or util, do we not have to match the support for base 16 responses or NaN values for the sake of stability?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This value is not used for signatures that are not permit.

@@ -65,7 +68,10 @@ const PermitSimulation: React.FC = () => {
paddingInline={2}
textAlign={TextAlign.Center}
>
{value}
{formatNumber(
value / Math.pow(10, tokenDecimals),
Copy link
Member

Choose a reason for hiding this comment

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

Rather than doing this math inline, is it worth including this within the formatNumber util or a new one?

We seem to also do this within dataTree.

Maybe a combined function such as applyDecimals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it will not be very useful to do division inside format number to get actual value of tokens. It is not a lot of calculation thus I I left it inline.

Copy link
Member

@matthewwalsh0 matthewwalsh0 Jun 27, 2024

Choose a reason for hiding this comment

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

It is indeed brief, but I'd argue it still limits the readability a little, plus introduces duplication.

It would be both the division and the pow call also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks bit like over-engineering to me to add a util method for this.

Copy link
Member

Choose a reason for hiding this comment

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

It's certainly not a blocker so we can review in future if we develop additional usages.

@metamaskbot
Copy link
Collaborator

Builds ready [673f415]
Page Load Metrics (48 ± 4 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6010281105
domContentLoaded9131011
load40734894
domInteractive9131011
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 0 Bytes (0.00%)
  • ui: 2.51 KiB (0.04%)
  • common: 118 Bytes (0.00%)

@jpuri jpuri requested a review from matthewwalsh0 June 25, 2024 10:00
Base automatically changed from permit_deadline to develop June 25, 2024 13:28
@metamaskbot
Copy link
Collaborator

Builds ready [c5539fb]
Page Load Metrics (57 ± 7 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6013995178
domContentLoaded9181221
load399657147
domInteractive9181221
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 1.48 KiB (0.02%)
  • common: 0 Bytes (0.00%)

if (value === undefined) {
return value;
}
const formatter = new Intl.NumberFormat('en-US', {
Copy link
Contributor

Choose a reason for hiding this comment

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

in the future, we may want to support formats based on the user's locale settings

Copy link
Contributor

@digiwand digiwand Jul 1, 2024

Choose a reason for hiding this comment

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

I've since looked into the code and I'd like to surface 2 other formatNumber/formatAccount* methods I've found used in our repo. Each have their own nuances. We utilize Intl.NumberFormat for each of the methods, but differently.

app/scripts/controllers/push-platform-notifications/utils/get-notification-data.ts:

  • uses abbreviation / notation (K, M, B, T)
  • uses ellipsis
  • does not support locales; Uses en-US

ui/pages/confirmations/components/simulation-details/formatAmount.ts:

  • neither abbreviated nor uses ellipsis
  • displays all left numbers if value is >= 0
  • rounds decimals based on desired precision
  • supports locales which if I'm not mistaken only utilizes the benefit of commas vs decimals. Symbols are handled outside of the method.

since the related issue ticket mentions:

This is already done for simulations UI that exists on transactions confirmations in case there's logic we can re-use from there.

I'd suggest we extract the simulation details formatAmount method to be reusable across the repo. We could update the method to specify the precision number / significant decimal places. This suggestion would apply more cohesion across our app, reduce formatNumber/formatAmount* variants, and support i18n locale decimals/commas.

Copy link
Contributor

Choose a reason for hiding this comment

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

regarding the last suggestion to remove formatNumber in favor of reusing the simulation's formatAmount method, I see you are doing in in a subsequent PR here https://github.com/MetaMask/metamask-extension/pull/25438/files#diff-27e9fac2551026bbb4a89ea07db94a27210316d346fed5e3234e696601e425b5L21. 👍🏼

other comment to support locale still applies

@jpuri jpuri requested a review from digiwand June 28, 2024 16:17
Copy link

sonarcloud bot commented Jul 2, 2024

@jpuri jpuri merged commit 6557b59 into develop Jul 2, 2024
72 of 73 checks passed
@jpuri jpuri deleted the permit_token_decimal_fix branch July 2, 2024 14:39
@github-actions github-actions bot locked and limited conversation to collaborators Jul 2, 2024
@metamaskbot metamaskbot added the release-12.2.0 Issue or pull request that will be included in release 12.2.0 label Jul 2, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [ed96b06]
Page Load Metrics (141 ± 157 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint65139962010
domContentLoaded94523115
load421566141328157
domInteractive94523115
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 1.48 KiB (0.02%)
  • common: 0 Bytes (0.00%)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmation-redesign release-12.2.0 Issue or pull request that will be included in release 12.2.0 team-confirmations Push issues to confirmations team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants