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

feat(23012): convert 2 level 19 files to ts #23276

Closed
wants to merge 3 commits into from
Closed

Conversation

DDDDDanica
Copy link
Contributor

@DDDDDanica DDDDDanica commented Mar 1, 2024

Description

In order to contribute to ts conversion of metamask-controller.js, we started the conversion of related files.
shared/modules/transaction.utils.ts and shared/notifications/index.ts will be 2 lower level files we included here.

Open in GitHub Codespaces

Related issues

Fixes:

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained what problem this PR is solving and how it is solved.
  • I've linked related issues
  • I've included manual testing steps
  • I've included screenshots/recordings if applicable
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.
  • I’ve properly set the pull request status:
    • In case it's not yet "ready for review", I've set it to "draft".
    • In case it's "ready for review", I've changed it from "draft" to "non-draft".

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.

@DDDDDanica DDDDDanica added the team-extension-platform Extension Platform team label Mar 1, 2024
@DDDDDanica DDDDDanica self-assigned this Mar 1, 2024
@DDDDDanica DDDDDanica requested a review from a team as a code owner March 1, 2024 19:29
Copy link
Contributor

github-actions bot commented Mar 1, 2024

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.

@DDDDDanica DDDDDanica force-pushed the feature/23012 branch 13 times, most recently from e75f733 to 5ffd231 Compare March 5, 2024 02:34
@metamaskbot
Copy link
Collaborator

Builds ready [2d08eed]
Page Load Metrics (968 ± 428 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint742091213718
domContentLoaded127229168
load602072968892428
domInteractive127229168
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -5 Bytes (-0.00%)
  • ui: 0 Bytes (0.00%)
  • common: -296 Bytes (-0.01%)

Copy link

codecov bot commented Mar 5, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 68.74%. Comparing base (318a8ce) to head (00694ce).
Report is 7 commits behind head on develop.

❗ Current head 00694ce differs from pull request most recent head 5d8c134. Consider uploading reports for the commit 5d8c134 to get more accurate results

Files Patch % Lines
shared/notifications/index.ts 44.44% 10 Missing ⚠️
shared/modules/transaction.utils.ts 86.11% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #23276      +/-   ##
===========================================
+ Coverage    68.67%   68.74%   +0.07%     
===========================================
  Files         1106     1106              
  Lines        43356    43307      -49     
  Branches     11591    11573      -18     
===========================================
- Hits         29773    29769       -4     
+ Misses       13583    13538      -45     

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

@metamaskbot
Copy link
Collaborator

Builds ready [4245085]
Page Load Metrics (1188 ± 397 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint801781223015
domContentLoaded146630147
load6721241188826397
domInteractive146630147

@metamaskbot metamaskbot added the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Mar 6, 2024
@legobeat legobeat requested a review from a team March 7, 2024 22:35
zone-live
zone-live previously approved these changes Mar 8, 2024
Copy link
Contributor

@zone-live zone-live left a comment

Choose a reason for hiding this comment

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

LGTM

try {
({ name } = data && parseStandardTokenTransactionData(data));
const parsedData = data && parseStandardTokenTransactionData(data);
if (parsedData && parsedData.name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

a lot better for readability 👍🏼

@metamaskbot
Copy link
Collaborator

Builds ready [00694ce]
Page Load Metrics (1521 ± 396 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint832131443416
domContentLoaded1088392311
load6823081521824396
domInteractive1088392311
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -5 Bytes (-0.00%)
  • ui: 0 Bytes (0.00%)
  • common: -296 Bytes (-0.01%)

// If the transaction type is already one of the inferrable types, then we do
// not need to re-establish the type.
let inferrableType = txMeta.type;
if (INFERRABLE_TRANSACTION_TYPES.includes(txMeta.type) === false) {
if (!INFERRABLE_TRANSACTION_TYPES.includes(txMeta.type as TransactionType)) {
Copy link
Contributor

@MajorLift MajorLift Mar 11, 2024

Choose a reason for hiding this comment

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

Let's resolve this by letting the compiler narrow the type (+ adding a runtime check) instead of relying on a type assertion.

Suggested change
if (!INFERRABLE_TRANSACTION_TYPES.includes(txMeta.type as TransactionType)) {
if (txMeta.type && !INFERRABLE_TRANSACTION_TYPES.includes(txMeta.type)) {

This added null check removes the | undefined from the type with runtime guarantees.

The type assertion is dangerous because it fixes the entire type as TransactionType, rather than only removing a portion of the type signature. For example, if we defined a TransactionMetaType somewhere down the road, the as TransactionType assertion would prevent the compiler from inferring that type, while a null check or non-nullable assertion would not.

Copy link
Contributor Author

@DDDDDanica DDDDDanica Mar 12, 2024

Choose a reason for hiding this comment

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

Thanks for the advice, will add ! see 5d8c13

Comment on lines 47 to 48
isHexString(transactionMeta?.txParams?.maxFeePerGas as string) &&
isHexString(transactionMeta?.txParams?.maxPriorityFeePerGas as string)
Copy link
Contributor

@MajorLift MajorLift Mar 11, 2024

Choose a reason for hiding this comment

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

We could guarantee that undefined is never passed into isHexString by providing fallback values that won't pollute the arguments' type signatures. This provides runtime safety while the type assertion does not.

Suggested change
isHexString(transactionMeta?.txParams?.maxFeePerGas as string) &&
isHexString(transactionMeta?.txParams?.maxPriorityFeePerGas as string)
isHexString(transactionMeta?.txParams?.maxFeePerGas ?? '') &&
isHexString(transactionMeta?.txParams?.maxPriorityFeePerGas ?? '')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 154 to 155
const parsedData = data && parseStandardTokenTransactionData(data);
if (parsedData && parsedData.name) {
Copy link
Contributor

@MajorLift MajorLift Mar 11, 2024

Choose a reason for hiding this comment

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

Using && to assert non-nullability like this is not a good pattern, since it pollutes the type signature of the assignee.

In this case parsedData gets stuck with a falsy "" type which is neither useful nor actually representative of its expected behavior.

Could we narrow parsedData down to TransactionDescription | undefined with a ternary expression? That way we can just use optional chaining for the null check.

Suggested change
const parsedData = data && parseStandardTokenTransactionData(data);
if (parsedData && parsedData.name) {
const parsedData = data
? parseStandardTokenTransactionData(data)
: undefined;
if (parsedData?.name) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah of course ! it is definitely a better type check !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DDDDDanica DDDDDanica force-pushed the feature/23012 branch 2 times, most recently from f0a044d to 0a94eac Compare March 12, 2024 22:20
@DDDDDanica DDDDDanica closed this Mar 13, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 13, 2024
@DDDDDanica
Copy link
Contributor Author

Reopened here to be able to add changes
#23445

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) team-extension-platform Extension Platform team
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants